I cannot say for sure, but after reading the PEP describing the with statement:
http://www.python.org/dev/peps/pep-0343/
It jumped on me:
A new statement is proposed with the syntax: with EXPR as VAR: BLOCK .... The translation of the above statement is: mgr = (EXPR) exit = type(mgr).__exit__
Right there. The with statement does not call __getattr__(__exit__) , but calls type(a).__exit__ , which does not exist, indicating an error.
Therefore, you just need to define them:
class FileHolder(object): def __init__(self,*args,**kwargs): self.f= file(*args,**kwargs) def __enter__(self,*args,**kwargs): return self.f.__enter__(*args,**kwargs) def __exit__(self,*args,**kwargs): self.f.__exit__(*args,**kwargs) def __getattr__(self,item): return getattr(self.f,item)
source share