[issue16068] with statement executes type(obj).__exit__ rather than getattr(obj, "__exit__")

Paul Wiseman report at bugs.python.org
Fri Sep 28 05:02:34 CEST 2012


New submission from Paul Wiseman:

I found this behaviour today and thought it was weird so asked the question on SO here http://stackoverflow.com/questions/12632894/why-doesnt-getattr-work-with-exit/12632972#12632972

basically if I attributes are returned dynamically, they seem to get overlooked by the code that runs the with statements.

>>> class FileHolder(object):
...    def __init__(self,*args,**kwargs):
...        self.f= file(*args,**kwargs)
...    def __getattr__(self,item):
...        return getattr(self.f,item)
...
>>> a= FileHolder("a","w")
>>> a.write
<built-in method write of file object at 0x018D75F8>
>>> with a as f:
...   print f
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: __exit__
>>> a.__exit__
<built-in method __exit__ of file object at 0x018D75F8>

----------
components: Library (Lib)
messages: 171399
nosy: Paul.Wiseman
priority: normal
severity: normal
status: open
title: with statement executes type(obj).__exit__ rather than getattr(obj,"__exit__")
type: behavior
versions: Python 2.7

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue16068>
_______________________________________


More information about the Python-bugs-list mailing list