]> git.ipfire.org Git - thirdparty/libnl.git/commitdiff
Enabled the use of Links as context managers.
authorAntoni S. Puimedon <asegurap@redhat.com>
Thu, 4 Oct 2012 20:36:55 +0000 (16:36 -0400)
committerAntoni S. Puimedon <asegurap@redhat.com>
Thu, 4 Oct 2012 20:36:55 +0000 (16:36 -0400)
With this change you can still set do modifications of
Links and then to change to pass the changes to the
kernel. But it additionally enables you to interact
with this part of libnl-python in a more pythonic
way. Instead of:

    eth0 = links['eth0']
    eth0.mtu = 5000
    eth0.change()

you can do:

    with links['eth0'] as eth0:
        eth0.mtu = 5000

python/netlink/route/link.py

index ab28f37994a8541a39b2cb401d6c55c465061fba..36d0e9d8ce585eb44b63492cd49a17e567bd238f 100644 (file)
@@ -152,6 +152,15 @@ class Link(netlink.Object):
         self.inet = inet.InetLink(self)
         self.af = {'inet' : self.inet }
 
+    def __enter__(self):
+        return self
+
+    def __exit__(self, exc_type, exc_value, tb):
+        if exc_type is None:
+            self.change()
+        else:
+            return false
+
     @classmethod
     def from_capi(cls, obj):
         return cls(capi.link2obj(obj))