3 ms·
Speaking of pdb.. maybe someone knows why pdb has some issues with scope in its REPL that are resolved in VSCode's debugger and PyCharm? Multiline statements a
by sharikous 3y ago
Speaking of pdb.. maybe someone knows why pdb has some issues with scope in its REPL that are resolved in VSCode's debugger and PyCharm?
Multiline statements are not accepted, nor things like if/for
Even list comprehensions and lambda expressions have trouble loading local variables defined via the REPL
Are there workarounds? It would reduce the need for using IDEs. People who have experience with Julia and Matlab are very used to a trial and error programming style in a console and bare python does not address this need
- BiteCode_dev 3y agoThere are not workaround, pdb is a rustic tool. Since I still enjoy a cmdline debugger more than a graphical one, I use ipdb, which doesn't suffer from the multiline limitation. However, scoping issues with lambda and comprehension are actually a Python problem, not a pdb problem.
- johnnymellor 3y agoNot supporting multi-line statements is just because pdb doesn't bother to parse the statement to work out if it is an incomplete multi-line statement. That could be easily fixed (I have a prototype patch for that using `code.compile_command`). The scope problems are more fundamental: The pdb REPL calls[1] the exec builtin as `exec(code, frame.f_globals, frame.f_locals)`, which https://docs.python.org/3/library/functions.html#exec https://docs.python.org/3/library/functions.html#exec documents as: "If exec gets two separate objects as globals and locals, the code will be executed as if it were embedded in a class definition." And https://docs.python.org/3/reference/executionmodel.html#resolution-of-names https://docs.python.org/3/reference/executionmodel.html#reso... documents that: "The scope of names defined in a class block is limited to the class block; it does not extend to the code blocks of methods - this includes comprehensions and generator expressions since they are implemented using a function scope." This is a fundamental limitation of `exec`. You can workaround it by only passing a single namespace dictionary to exec instead of passing separate globals and locals, which is what pdb's interact command does[2], but then it's ambiguous how to map changes to that dictionary back to the separate globals and locals dictionaries (pdb's interact command just discards any changes you make to the namespace). This too could be solved, but requires either brittle ast parsing or probably a PEP to add new functionality to exec. I'll file a bug against Python soon. [1]: https://github.com/python/cpython/blob/25a64fd28aaaaf2d21fae23212a0554c24fc7b20/Lib/pdb.py#L479 https://github.com/python/cpython/blob/25a64fd28aaaaf2d21fae... [2]: https://github.com/python/cpython/blob/25a64fd28aaaaf2d21fae23212a0554c24fc7b20/Lib/pdb.py#L1584 https://github.com/python/cpython/blob/25a64fd28aaaaf2d21fae...
- johnnymellor 3y agoOh, and multi-line statements will be supported from Python 3.13 (https://github.com/python/cpython/issues/103124 https://github.com/python/cpython/issues/103124)
- mejutoco 3y agoAfter triggering pdb by having "breakpoint()" in tour python code and dropping in the debugger you can type "interactive" in the console to enter multiline strings.
- BiteCode_dev 3y agoTIL again. Very good thread! It's "interact" though, not "ive". You can exit it to carry on with the regular pdb.
- mejutoco 3y agoThanks. I make that mistake every time I use it:)