This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author dewin
Recipients dewin
Date 2014-01-06.17:42:23
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1389030144.11.0.294634334929.issue20149@psf.upfronthosting.co.za>
In-reply-to
Content
I was writing code for a class that uses one threading.RLock per object.  For convenience, I wanted to be able to use that lock's context manager and acquire/release methods without referencing the lock directly, so I opted to take a shortcut by assigning self.__enter__ = self.lock.__enter__ and so forth.

This fails in Python 3.3.1 (self-compiled on Debian) and both "3.3.2+" and "2.7.5+" (currently available Ubuntu packages).  Judging by the error messages, it looks 'with' is examining the __enter__ and __exit__ attributes defined on the instance's class, rather than those defined on the instance itself.

The workaround here is simple enough, but I'm under the impression that it shouldn't be needed anyways.

Test case follows:

import threading

class Foo(object):
#       Uncommenting these yields "NoneType is not callable" rather than an AttributeError
#       __enter__ = None
#       __exit__ = None
#       acquire = None
#       release = None
#       lock = None

        def __init__(self):
                self.lock = threading.RLock()
                print(repr(self.lock))
                self.__enter__ = self.lock.__enter__
                self.__exit__ = self.lock.__exit__
                self.acquire = self.lock.acquire
                self.release = self.lock.release

foo = Foo()
# These all function as expected.  The fourth line fails (correctly) on 2.7.5.
print(repr(foo.__enter__))
print(repr(foo.__exit__))
print(foo.__enter__())
print(foo.__exit__())

# This does not
with foo:
        pass
History
Date User Action Args
2014-01-06 17:42:24dewinsetrecipients: + dewin
2014-01-06 17:42:24dewinsetmessageid: <1389030144.11.0.294634334929.issue20149@psf.upfronthosting.co.za>
2014-01-06 17:42:24dewinlinkissue20149 messages
2014-01-06 17:42:23dewincreate