New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pdb throws AttributeError at end of debugging session #57253
Comments
Using Python 2.7.1 on OpenBSD-current and 2.7.2 on Arch Linux, pdb throws a (harmless, it appears) exception at the end of a debugging session. This does not happen for me using Python 2.5 or 2.6 or 3.2. Console Session (on linux): $ python2 --version
Python 2.7.2
$ python2 debug.py
> /tmp/debug.py(4)<module>()
-> a = 1
(Pdb) n
> /tmp/debug.py(5)<module>()
-> b = 2
(Pdb) n
> /tmp/debug.py(6)<module>()
-> sum = a + b
(Pdb) n
--Return--
> /tmp/debug.py(6)<module>()->None
-> sum = a + b
(Pdb) n
Exception AttributeError: "'NoneType' object has no attribute 'path'" in <function _remove at 0xb73b6df4> ignore |
By modifying a bit the Python intepreter, I got this traceback: Traceback (most recent call last):
File "/home/amauryfa/python/cpython2.7/Lib/_weakrefset.py", line 38, in _remove
def _remove(item, selfref=ref(self)):
File "/home/amauryfa/python/cpython2.7/Lib/bdb.py", line 50, in trace_dispatch
return self.dispatch_call(frame, arg)
File "/home/amauryfa/python/cpython2.7/Lib/bdb.py", line 76, in dispatch_call
if not (self.stop_here(frame) or self.break_anywhere(frame)):
File "/home/amauryfa/python/cpython2.7/Lib/bdb.py", line 147, in break_anywhere
return self.canonic(frame.f_code.co_filename) in self.breaks
File "/home/amauryfa/python/cpython2.7/Lib/bdb.py", line 33, in canonic
canonic = os.path.abspath(filename)
AttributeError: 'NoneType' object has no attribute 'path' Here is the gdb stack when the error is printed: #0 PyErr_Print () at Python/pythonrun.c:1040 So, when the interpreter shuts down, the debugger is still active... A possible fix is to reset the trace function to None in Py_Finalize. |
The run, runeval and runcall methods run the debugging session in a |
Do we need to fix it at all? Xavier, your proposition noticeably changes current behavior as I understand it. |
Agreed that debugging on finalization stage is useful. Debugging on =============================== class C:
def __del__(self):
print('deleted')
c = C()
import pdb; pdb.set_trace()
x = 1 =============================== $ python3 bar.py
> /tmp/bar.py(7)<module>()
-> x = 1
(Pdb) step
--Return--
> /tmp/bar.py(7)<module>()->None
-> x = 1
(Pdb) step
deleted
$ =============================== Maybe this is another issue.
This proposition only sets the global trace function to None on |
I can reproduce this bug on Ubuntu 14.04.2 LTS with Python 2.7.6 using akl's debug.py. |
One further observation: the exception is thrown only if a breakpoint (apart from the call to pdb.set_trace) is set. If no breakpoint is set, the exception is not raised. |
I can reproduce this bug with python 3.6.5 using akl's debug.py. The exception is now on 3.6.5: (Pdb) next
--Call--
Exception ignored in: <async_generator object _ag at 0x7fbca6e62320>
Traceback (most recent call last):
File "/usr/lib/python3.6/types.py", line 27, in _ag
File "/usr/lib/python3.6/bdb.py", line 53, in trace_dispatch
File "/usr/lib/python3.6/bdb.py", line 85, in dispatch_call
File "/usr/lib/python3.6/pdb.py", line 251, in user_call
File "/usr/lib/python3.6/pdb.py", line 351, in interaction
File "/usr/lib/python3.6/pdb.py", line 1453, in print_stack_entry
File "/usr/lib/python3.6/bdb.py", line 394, in format_stack_entry
TypeError: 'NoneType' object is not callable bpo-33328 is a duplicate of this issue. |
Nosying Serhiy as this post attempts to answer one of the questions raised in msg315588 in bpo-33328:
This post has four different sections showing that the successive and incremental attempts at fixing the current code to have a destructor traced in the shutdown stage may lead to a segfault. Section 1 - Current status: 1 class C: $ python test_destructor.py
> test_destructor.py(7)<module>()
-> x = 1
(Pdb) step
--Return--
> test_destructor.py(7)<module>()->None
-> x = 1
(Pdb) step
--Call--
Exception ignored in: <async_generator object _ag at 0x7f3386d18320>
Traceback (most recent call last):
File "/usr/lib/python3.6/types.py", line 27, in _ag
File "/usr/lib/python3.6/bdb.py", line 53, in trace_dispatch
File "/usr/lib/python3.6/bdb.py", line 85, in dispatch_call
File "/usr/lib/python3.6/pdb.py", line 251, in user_call
File "/usr/lib/python3.6/pdb.py", line 351, in interaction
File "/usr/lib/python3.6/pdb.py", line 1453, in print_stack_entry
File "/usr/lib/python3.6/bdb.py", line 394, in format_stack_entry
TypeError: 'NoneType' object is not callable Section 2 - Fix using PR 6730: Section 3 - Fix a bdb leak: $ /home/xavier/src/python/master/python test_destructor.py
> ./test_destructor.py(7)<module>()
-> x = 1
(Pdb) step
--Return--
> ./test_destructor.py(7)<module>()->None
-> x = 1
(Pdb) step
Exception ignored in: <function C.__del__ at 0x7ff8ffdafd48>
Traceback (most recent call last):
File "test_destructor.py", line 3, in __del__
File "test_destructor.py", line 3, in __del__
File "/home/xavier/src/python/master/Lib/bdb.py", line 88, in trace_dispatch
File "/home/xavier/src/python/master/Lib/bdb.py", line 115, in dispatch_line
File "/home/xavier/src/python/master/Lib/pdb.py", line 262, in user_line
File "/home/xavier/src/python/master/Lib/pdb.py", line 352, in interaction
File "/home/xavier/src/python/master/Lib/pdb.py", line 1454, in print_stack_entry
File "/home/xavier/src/python/master/Lib/bdb.py", line 544, in format_stack_entry
ImportError: sys.meta_path is None, Python is likely shutting down Section 4 - Remove the lazy import: $ /home/xavier/src/python/master/python test_destructor.py
> ./test_destructor.py(7)<module>()
-> x = 1
(Pdb) step
--Return--
> ./test_destructor.py(7)<module>()->None
-> x = 1
(Pdb) step
> ./test_destructor.py(3)__del__()
-> print('deleted')
Segmentation fault (core dumped) The gdb back trace is attached at gdb_backtrace.txt. Conclusion: |
Actually the segfault can be avoided by running the garbage collection before _PyState_ClearModules() in PyImport_Cleanup() and one can trace the C destructor with the attached patch global_destructors.diff applied on top of PR 6730. _PyState_ClearModules() clears the state->modules_by_index list and causes the readline module to be non-functional. The same problem occurs in test_create_at_shutdown_without_encoding() in test_io.py when it is run with the io module: the C destructor fails before hitting the "LookupError: unknown encoding: ascii" error message because the garbage collection is made after _PyState_ClearModules() and the _io module is non-functional (PyState_FindModule() returns NULL). So the destructors can be traced during finalization provided the tracer does not access any of the sys attributes that are set to None at the start of PyImport_Cleanup(). This is true for the globals of the modules that are only owned by sys.modules at the time 'weaklist' is about to be built in PyImport_Cleanup(). But it is not the case of '_ag' and that explains at last why the destructor was called and failed: The global_destructors.diff patch ensures that no access is made by the pdb and bdb modules (but imported modules have not been checked) to the the sys attributes that are set to None at the start of PyImport_Cleanup():
|
See also bpo-33458 that deals with the same problem when pdb is run as 'python -m pdb some_main.py' instead of by inserting a breakpoint with 'pdb.set_trace()'. |
Unable to reproduce with |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: