Skip to content
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

you can do:

    with links['eth0'] as eth0:
        eth0.mtu = 5000
  • Loading branch information...
1 parent ce72837 commit 72c2cb9e299b0510ae76e90b0b4fcc040ab485c7 @celebdor celebdor committed Oct 4, 2012
Showing with 9 additions and 0 deletions.
  1. +9 −0 python/netlink/route/
9 python/netlink/route/
@@ -152,6 +152,15 @@ def __init__(self, obj=None):
self.inet = inet.InetLink(self) = {'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
def from_capi(cls, obj):
return cls(capi.link2obj(obj))

0 comments on commit 72c2cb9

Please sign in to comment.
Something went wrong with that request. Please try again.