classification
Title: pdb bug with "with" statement
Type: behavior Stage:
Components: Interpreter Core, Library (Lib) Versions: Python 3.0
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: amaury.forgeotdarc, christian.heimes, gvanrossum
Priority: high Keywords:

Created on 2007-10-11 16:00 by christian.heimes, last changed 2008-01-06 22:29 by admin. This issue is now closed.

Messages (9)
msg56346 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2007-10-11 16:00
found a pretty annoying bug caused by with blocks. A with block
terminates the debugging session and the program keeps running. It's not
possible to go to the next line with 'n'. 's' steps into the open() call.

# pdbtest.py
import pdb
pdb.set_trace()
print("before with")
with open("/etc/passwd") as fd:
    data = fd.read()
print("after with")
print("end of program")

$ ./python pdbtest.py
> /home/heimes/dev/python/py3k/pdbtest.py(3)<module>()
-> print("before with")
(Pdb) n
before with
> /home/heimes/dev/python/py3k/pdbtest.py(4)<module>()
-> with open("/etc/passwd") as fd:
(Pdb) n
after with
end of program
msg57049 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2007-11-02 13:27
The bug makes debugging a crux :(
msg57433 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2007-11-13 01:06
Corrected by revision 58958.
"with" is innocent. The problem is this function in abc.py which uses a
generator expression:

    def __instancecheck__(cls, instance):
        """Override for isinstance(instance, cls)."""
        return any(cls.__subclasscheck__(c)
                   for c in {instance.__class__, type(instance)})

It is called both by pdb (deep inside 'print') and the debugged code (in
PyObject_MethodCall), and this reveals the bug.
msg57435 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2007-11-13 01:19
__instancecheck__ should be simpler: all classes are new-style, how is
it possible to have different results for
   instance.__class__
and
   type(instance)
?
msg57436 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2007-11-13 01:43
Good work Amaury! :)

I also wonder how type(cls) != cls.__class__ is possible with new style
classes. So far I found only one way and it ain't beautiful:

>>> class Meta(type):
...     def __getattribute__(self, key):
...         if key == "__class__": return object
...         return type.__getattribute__(self, key)
...
>>> class Example(metaclass=Meta): pass
...
>>> Example.__class__
<type 'object'>
>>> type(Example)
<class '__main__.Meta'>
msg57438 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2007-11-13 05:14
Good work indeed!

Here's another way:

>>> class C:
...   @property
...   def __class__(self): return D
... 
>>> class D: pass
... 
>>> C().__class__
<class '__main__.D'>
>>>
msg57439 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2007-11-13 05:19
BTW: if you can show (e.g. with your unittest) that this indeed breaks
in 2.6 and 2.5, please do backport it.

BTW**2: I've noticed that abc.py's __instancecheck__ gets called a lot
at times when I don't expect it.  Can you research this a bit?
msg57440 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2007-11-13 05:20
PPSS. When backporting, a note in Misc/NEWS is appreciated.
msg57443 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2007-11-13 09:27
> BTW**2: I've noticed that abc.py's __instancecheck__ gets called a lot
> at times when I don't expect it.  Can you research this a bit?

In classobject.c, method_call() calls PyObject_IsInstance() on the first
arg when the method is unbound.
This happens a lot in io.py, each time the code calls a base class method.
History
Date User Action Args
2008-01-06 22:29:45adminsetkeywords: - py3k
versions: Python 3.0
2007-11-17 22:27:59gvanrossumlinkissue1454 superseder
2007-11-13 09:27:34amaury.forgeotdarcsetmessages: + msg57443
2007-11-13 05:20:20gvanrossumsetmessages: + msg57440
2007-11-13 05:19:34gvanrossumsetmessages: + msg57439
2007-11-13 05:14:01gvanrossumsetmessages: + msg57438
2007-11-13 01:43:57christian.heimessetstatus: open -> closed
resolution: accepted -> fixed
messages: + msg57436
keywords: + py3k
2007-11-13 01:19:07amaury.forgeotdarcsetmessages: + msg57435
2007-11-13 01:06:09amaury.forgeotdarcsetnosy: + amaury.forgeotdarc
messages: + msg57433
2007-11-02 13:27:43christian.heimessetpriority: high
type: behavior
resolution: accepted
messages: + msg57049
2007-10-11 17:40:46gvanrossumsetnosy: + gvanrossum
2007-10-11 16:00:52christian.heimescreate