Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using a Lock as a context manager leads to AttributeError #170

Closed
Konfuzzyus opened this issue Apr 28, 2016 · 2 comments
Closed

Using a Lock as a context manager leads to AttributeError #170

Konfuzzyus opened this issue Apr 28, 2016 · 2 comments

Comments

@Konfuzzyus
Copy link

According to the documentation at http://python-etcd.readthedocs.io/en/latest/, the following code should be valid usage of the Lock class:

client = etcd.Client()
with etcd.Lock(client, 'customer1') as my_lock:
    do_stuff()
    my_lock.is_acquired()  # True
    my_lock.acquire(lock_ttl = 60)
my_lock.is_acquired() # False

This throws an AttributeError at my_lock.is_acquired(), because my_lock is None at that point.

The reason is the implementation of enter and exit on Lock, which do not comply with the context manager contract, see https://docs.python.org/2/reference/datamodel.html#context-managers.

e.g.

def __enter__(self):
        """
        You can use the lock as a contextmanager
        """
        self.acquire(blocking=True, lock_ttl=0)
        # missing return self

    def __exit__(self, type, value, traceback):
        self.release()
        # missing return False
@eyurtsev
Copy link

+1

@Oloremo Oloremo mentioned this issue Feb 8, 2017
@lavagetto
Copy link
Collaborator

This is now solved with the latest additions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants