I want to write a decorator for some functions that take file as the first argument. The decorator has to implement the context manager protocol (i.e. turn the wrapped function into a context manager), so I figured I needed to wrap the function with a class.
I'm not really experienced with the decorator pattern and have never implemented a context manager before, but what I wrote works in Python 2.7 and it also works in Python 3.3 if I comment out the wraps
line.
from functools import wraps
def _file_reader(func):
"""A decorator implementing the context manager protocol for functions
that read files."""
# @wraps(func)
class CManager:
def __init__(self, source, *args, **kwargs):
self.source = source
self.args = args
self.kwargs = kwargs
self.close = kwargs.get('close', True)
def __enter__(self):
# _file_obj is a little helper that opens the file for reading
self.fsource = _file_obj(self.source, 'r')
return func(self.fsource, *self.args, **self.kwargs)
def __exit__(self, exc_type, exc_value, traceback):
if self.close:
self.fsource.close()
return False
return CManager
The error I get when uncommenting the wraps
line occurs inside update_wrapper
:
/usr/lib/python3.3/functools.py in update_wrapper(wrapper, wrapped, assigned, updated)
54 setattr(wrapper, attr, value)
55 for attr in updated:
---> 56 getattr(wrapper, attr).update(getattr(wrapped, attr, {}))
57 # Return the wrapper so this can be used as a decorator via partial()
58 return wrapper
AttributeError: 'mappingproxy' object has no attribute 'update'
I know the docs don't say that I even can use functools.wraps
to wrap a function with a class like this, but then again, it just works in Python 2. Can someone please explain what exactly this traceback is telling me and what I should do to achieve the effects of wraps
on both versions of Python?
EDIT: I was mistaken. The code above does not do what I want it to. I want to be able to use the function both with and without with
, like the builtin open
.
The code above turns the decorated function into a context manager. I want to be able to do:
reader = func('source.txt', arg)
for item in reader:
pass
as well as
with func('source.txt', arg) as reader:
for item in reader:
pass
So my version of the code should probably look approximately as follows:
def _file_reader(func):
"""A decorator implementing the context manager protocol for functions
that read files."""
@wraps(func)
class CManager:
def __init__(self, source, *args, **kwargs):
self.close = kwargs.get('close', True)
self.fsource = _file_obj(source, 'r')
self.reader = func(self.fsource, *args, **kwargs)
def __enter__(self):
return self.reader
def __iter__(self):
return self.reader
def __next__(self):
return next(self.reader)
def __exit__(self, exc_type, exc_value, traceback):
if self.close and not self.fsource.closed:
self.fsource.close()
return False
return CManager
Feel free to comment about anything I have overlooked.
Note: the class version by J.F. Sebastian seems to work then:
I basically removed the wraps
from the class and changed return CManager
to:
@wraps(func)
def helper(*args, **kwargs):
return CManager(*args, **kwargs)
return helper