Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9060593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:16:11+00:00 2026-06-16T15:16:11+00:00

I want to write a decorator for some functions that take file as the

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T15:16:13+00:00Added an answer on June 16, 2026 at 3:16 pm

    functools.wraps() is for wrapper functions:

    import contextlib
    import functools
    
    def file_reader(func):
        @functools.wraps(func)
        @contextlib.contextmanager
        def wrapper(file, *args, **kwargs):
            close = kwargs.pop('close', True) # remove `close` argument if present
            f = open(file)
            try:
                yield func(f, *args, **kwargs)
            finally:
                if close:
                   f.close()
        return wrapper
    

    Example

    @file_reader
    def f(file):
        print(repr(file.read(10)))
        return file
    
    with f('prog.py') as file:
        print(repr(file.read(10)))
    

    If you want to use a class-based context manager then a workaround is:

    def file_reader(func):
        @functools.wraps(func)
        def helper(*args, **kwds):
            return File(func, *args, **kwds)
        return helper
    

    To make it behave identically whether the decorated function is used directly or as a context manager you should return self in __enter__():

    import sys
    
    class File(object):
    
        def __init__(self, file, func, *args, **kwargs):
            self.close_file = kwargs.pop('close', True)
            # accept either filename or file-like object
            self.file = file if hasattr(file, 'read') else open(file)
    
            try:
                # func is responsible for self.file if it doesn't return it
                self.file = func(self.file, *args, **kwargs)
            except:  # clean up on any error
                self.__exit__(*sys.exc_info())
                raise
    
        # context manager support
        def __enter__(self):
            return self
    
        def __exit__(self, *args, **kwargs):
            if not self.close_file:
                return  # do nothing
            # clean up
            exit = getattr(self.file, '__exit__', None)
            if exit is not None:
                return exit(*args, **kwargs)
            else:
                exit = getattr(self.file, 'close', None)
                if exit is not None:
                    exit()
    
        # iterator support
        def __iter__(self):
            return self
    
        def __next__(self):
            return next(self.file)
    
        next = __next__  # Python 2 support
    
        # delegate everything else to file object
        def __getattr__(self, attr):
            return getattr(self.file, attr)
    

    Example

    file = f('prog.py')  # use as ordinary function
    print(repr(file.read(20)))
    file.seek(0)
    for line in file:
        print(repr(line))
        break
    file.close()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a decorator that acts differently depending on whether it is
I want to write a python function decorator that tests that certain arguments to
i want to write a decorator that enables methods of classes to become visible
I want to write a batch script statement where: FINDSTR has to check for
I want to write a program in Prolog that confirms if a b-tree of
How would I write a decorator like this. I want to be able to
I want to implement the decorator pattern in Python, and I wondered if there
Hi this is newbie in python, I want write a prioritized decorator which will
I've got the following problem: I need to write a decorator that would be
First of all I want to say that I know its bad to serve

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.