classification
Title: Python 2.7 breaks assigned __exit__s
Type: behavior Stage: committed/rejected
Components: Interpreter Core Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: arielbyd, benjamin.peterson, dmalcolm, eric.araujo
Priority: normal Keywords:

Created on 2010-07-14 11:53 by arielbyd, last changed 2010-07-26 10:11 by dmalcolm. This issue is now closed.

Files
File name Uploaded Description Edit
tests.tar.xz arielbyd, 2010-07-14 11:53 3 tests.
Messages (9)
msg110268 - (view) Author: Ariel Ben-Yehuda (arielbyd) Date: 2010-07-14 11:53
Hello, I think there is a problem with Python 2.7: I installed it, and tried to compile GObject-Introspection 0.9.2. Here is the result:

...
  GISCAN GLib-2.0.gir
Traceback (most recent call last):
  File "../tools/g-ir-scanner", line 36, in <module>
    from giscanner.scannermain import scanner_main
  File "/sources/gobject-introspection-0.9.2/giscanner/scannermain.py", line 31, in <module>
    from giscanner.dumper import compile_introspection_binary
  File "/sources/gobject-introspection-0.9.2/giscanner/dumper.py", line 26, in <module>
    from .glibtransformer import IntrospectionBinary
  File "/sources/gobject-introspection-0.9.2/giscanner/glibtransformer.py", line 34, in <module>
    from .transformer import Names
  File "/sources/gobject-introspection-0.9.2/giscanner/transformer.py", line 31, in <module>
    from .girparser import GIRParser
  File "/sources/gobject-introspection-0.9.2/giscanner/girparser.py", line 32, in <module>
    from .girwriter import COMPATIBLE_GIR_VERSION
  File "/sources/gobject-introspection-0.9.2/giscanner/girwriter.py", line 30, in <module>
    from .xmlwriter import XMLWriter
  File "/sources/gobject-introspection-0.9.2/giscanner/xmlwriter.py", line 70, in <module>
    with LibtoolImporter:
AttributeError: __exit__

Oops, no GObject-Introspection for me. Investigating the bug, I looked in the recent changes in Python2.7 for something interesting, and found this: 
A new opcode was added to perform the initial setup for with statements, looking up the __enter__()  and __exit__()  methods. (Contributed by Benjamin Peterson.)

Finally, I made 3 test cases to the bug. All 3 produce no input in Python 2.6, but crash pretty nicely in 2.7, the first 2 not finding __exit__ and the last missing __enter__.

I will try to make a patch, however my CPython skills are not so great, so you will probably make a better one.
msg110274 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-07-14 13:07
Thank you for the report. Unfortunately, the 2.6 behavior was a bug, as I explained on #9220: Magic methods are looked up on the object’s class, not in the object’s __dict__. <http://docs.python.org/reference/datamodel#specialnames>

(Your test with class methods was a clever idea, but a class is a type instance, so it’s the same behavior: __enter__ is looked up on type, not on the class. Nice try :)

Tip for future bug reports: We prefer regular files over archives. Thanks again!
msg110276 - (view) Author: Ariel Ben-Yehuda (arielbyd) Date: 2010-07-14 13:09
Bug or not bug, Python2.7 breaks Gobject-Introspection.
msg110277 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-07-14 13:11
Please produce a test to prove that Python behaves in contradiction to the documentation. Otherwise I’m afraid it’s a GI bug, like in your tests :)
msg110279 - (view) Author: Ariel Ben-Yehuda (arielbyd) Date: 2010-07-14 13:20
My test on Class Methods was based on the GI Code that does not run on Python 2.7.
msg110280 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-07-14 13:29
I am afraid we have conflicting viewpoints. You are saying that upgrading Python broke code, and I agree it’s a bad thing. Usually such regression reports are accepted and fixed.

In this case however, the code was already broken in the previous version, it was only working because of a problem in the implementation. So from the viewpoint of GI users, it’s a regression, but from the viewpoint of Python the 2.7 behavior fixes a regression in 2.6. It is not supported to find magic methods (“__name__”) on instances.
msg110281 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-07-14 13:36
Since Benjamin Peterson, release manager for 2.7, confirmed that the change was intentional, I’m closing again.

To solve the bug in GI, you need to define a metaclass with those __enter__ and __exit__ methods, then use it as metaclass for LibtoolImporter. Please send me private email if you want more details.
msg110295 - (view) Author: Ariel Ben-Yehuda (arielbyd) Date: 2010-07-14 16:13
Done it for myself. However, I am not a GI Maintainer - you should talk with them and send them this patch.
msg110321 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-07-14 19:40
Since you know the project, the bug and the fix, I think you’re in a better position than me to talk with them.
History
Date User Action Args
2010-07-26 10:11:26dmalcolmsetnosy: + dmalcolm
2010-07-14 19:40:39eric.araujosetmessages: + msg110321
2010-07-14 16:13:46arielbydsetmessages: + msg110295
2010-07-14 13:36:56eric.araujosetstatus: open -> closed

messages: + msg110281
2010-07-14 13:29:38eric.araujosetmessages: + msg110280
2010-07-14 13:20:07arielbydsetmessages: + msg110279
2010-07-14 13:11:00eric.araujosetmessages: + msg110277
2010-07-14 13:09:29arielbydsetstatus: closed -> open

messages: + msg110276
2010-07-14 13:07:51eric.araujosetstatus: open -> closed
type: behavior
resolution: not a bug
stage: committed/rejected
2010-07-14 13:07:31eric.araujosetnosy: + eric.araujo
messages: + msg110274
2010-07-14 12:02:38pitrousetnosy: + benjamin.peterson
2010-07-14 11:53:15arielbydcreate