mirror of
https://gitlab.com/libvirt/libvirt-python.git
synced 2025-11-27 20:23:47 +03:00
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>
This commit is contained in:
committed by
Michal Privoznik
parent
6f0456cdbc
commit
d23f8bc3a3
@@ -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 """
|
||||
|
||||
Reference in New Issue
Block a user