Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Enabled the use of Links as context managers.

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
  • Loading branch information...
commit 72c2cb9e299b0510ae76e90b0b4fcc040ab485c7 1 parent ce72837
@celebdor celebdor authored
Showing with 9 additions and 0 deletions.
  1. +9 −0 python/netlink/route/link.py
View
9 python/netlink/route/link.py
@@ -152,6 +152,15 @@ def __init__(self, obj=None):
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))
Please sign in to comment.
Something went wrong with that request. Please try again.