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.

classification
Title: Modules no longer usable as context managers
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: benjamin.peterson, eric.araujo, j1m, mark.dickinson
Priority: normal Keywords:

Created on 2010-07-10 19:52 by j1m, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (6)
msg109920 - (view) Author: Jim Fulton (j1m) * (Python committer) Date: 2010-07-10 19:52
In python 2.7 a module can't be used as a context manager.

For example, given the module, t.py:

    def __enter__(*args):
        print 'enter', args

    def __exit__(*args):
        print 'exit', args

In Python 2.6:

    >>> import t
    >>> with t: pass
    ... 
    enter ()
    exit (None, None, None)

In Python 2.7:

    >>> import t
    >>> with t: pass
    ... 
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: __exit__
msg109924 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-07-10 20:03
IIUC, magic methods are looked up on the object’s class, not in the object’s __dict__. This behavior in 2.6 seems like a bug to me.

I-tried-to-add-__call__-to-a-module-once-ly yours
msg109928 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-07-10 20:08
Confirmed: http://docs.python.org/reference/datamodel#specialnames
Closing. Sorry! (BTW, using import hackery and a custom module type, you could achieve your goal.)
msg109934 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2010-07-10 20:46
Just for the record, this changed with the introduction of the SETUP_WITH opcode in r72912.  Perhaps Benjamin can confirm that the behaviour change was deliberate?
msg109950 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2010-07-11 00:21
Yes, definitely.
msg109953 - (view) Author: Jim Fulton (j1m) * (Python committer) Date: 2010-07-11 01:40
On Sat, Jul 10, 2010 at 4:08 PM, Éric Araujo <report@bugs.python.org> wrote:
>
> Éric Araujo <merwok@netwok.org> added the comment:
>
> Confirmed: http://docs.python.org/reference/datamodel#specialnames
> Closing. Sorry! (BTW, using import hackery and a custom module type, you could achieve your goal.)

Yes, I'll have to use a custom module type and import hackery to get
code that worked before working again in Python 2.7.

Jim
History
Date User Action Args
2022-04-11 14:57:03adminsetgithub: 53466
2010-07-11 01:40:25j1msetmessages: + msg109953
2010-07-11 00:21:34benjamin.petersonsetmessages: + msg109950
2010-07-10 20:46:38mark.dickinsonsetnosy: + mark.dickinson, benjamin.peterson
messages: + msg109934
2010-07-10 20:08:30eric.araujosetstatus: open -> closed
resolution: not a bug
messages: + msg109928

stage: resolved
2010-07-10 20:03:13eric.araujosetnosy: + eric.araujo
messages: + msg109924
2010-07-10 19:52:28j1mcreate