diff options
author | Marc Hartmayer <mhartmay@linux.vnet.ibm.com> | 2018-05-16 10:24:36 +0200 |
---|---|---|
committer | Michal Privoznik <mprivozn@redhat.com> | 2018-05-16 10:40:46 +0200 |
commit | d23f8bc3a3530bd3df6c21ff288d5b3b28d1f219 (patch) | |
tree | ef4dca9844abacec4d3db483541220106ef8a843 /libvirt-override-virConnect.py | |
parent | 6f0456cdbca79d4cdc45d9b2c3bb548db7fdffbe (diff) | |
download | libvirt-python-d23f8bc3a3530bd3df6c21ff288d5b3b28d1f219.tar.gz |
Allow virConnect to be used as a context manager
The libvirt python bindings are now more 'pythonic' as virConnect can
now be used as a context manager.
For example, it's possible to write the following code:
with libvirt.open() as conn:
# do something with the connection...
print(conn.listAllDomains())
At the end of this with-block the connection will be closed
automatically.
Signed-off-by: Marc Hartmayer <mhartmay@linux.vnet.ibm.com>
Reviewed-by: Bjoern Walk <bwalk@linux.vnet.ibm.com>
Reviewed-by: Boris Fiuczynski <fiuczy@linux.vnet.ibm.com>
Diffstat (limited to 'libvirt-override-virConnect.py')
-rw-r--r-- | libvirt-override-virConnect.py | 6 |
1 files changed, 6 insertions, 0 deletions
diff --git a/libvirt-override-virConnect.py b/libvirt-override-virConnect.py index f26e65a..a24eae7 100644 --- a/libvirt-override-virConnect.py +++ b/libvirt-override-virConnect.py @@ -11,6 +11,12 @@ libvirtmod.virConnectClose(self._o) self._o = None + def __enter__(self): + return self + + def __exit__(self, exc_type_, exc_value_, traceback_): + self.close() + def domainEventDeregister(self, cb): """Removes a Domain Event Callback. De-registering for a domain callback will disable delivery of this event type """ |