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: Feature request: allow mechanism for creator of exec-generated code to provide source to pdb
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.11
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Spencer Brown, posita
Priority: normal Keywords:

Created on 2021-12-19 16:17 by posita, last changed 2022-04-11 14:59 by admin.

Files
File name Uploaded Description Edit
test_case.py posita, 2021-12-19 16:17
Messages (4)
msg408910 - (view) Author: Matt B (posita) * Date: 2021-12-19 16:17
Unless I missed it, looking at https://github.com/python/cpython/blob/main/Lib/pdb.py, https://github.com/python/cpython/blob/main/Lib/inspect.py, and https://docs.python.org/3/library/pdb.html doesn't give much of a clue how to provide sources to exec-generated code.

I may have misread, but pdb *seems* to lean on inspect.findsource which eventually excludes sources that match `^<.*>$` (like `<string>`).

Running attached test_case.py:

% python test_case.py
> <string>(4)foo()
(Pdb) l
[EOF]
(Pdb) c
inspect.getfile(<function foo at 0x10b735160>): <string>
calling inspect.findsource(<function foo at 0x10b735160>) ...
Traceback (most recent call last):
  File "/Users/matt/Documents/dev/numerary/test_case.py", line 12, in <module>
    foo()
  File "/Library/Frameworks/MacPorts-20200907/Python.framework/Versions/3.9/lib/python3.9/inspect.py", line 835, in findsource
    raise OSError('could not get source code')
OSError: could not get source code
msg410588 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2022-01-14 18:54
The source code is read from a file. If there is no file you get OSError, as the docstring states.
msg410597 - (view) Author: Matt B (posita) * Date: 2022-01-14 20:09
Please treat this as a feature request to add the ability for pdb (and internals) to ingest sources for exec-generated code.
msg410640 - (view) Author: Spencer Brown (Spencer Brown) * Date: 2022-01-15 10:57
There is a solution to this: you can modify the linecache module's cache to add lines under a fake filename, which is the approach attrs takes here:
https://github.com/python-attrs/attrs/blob/9727008fd1e40bc55cdc6aee71e0f61553f33127/src/attr/_make.py#L347
However, there are several downsides. 
- That dict isn't documented or appears in __all__, so it's arguably private.
- The C implementation of traceback printing opens files directly, so this doesn't work there.
- You have to invent a unique filename, and manually remove lines from the cache if the function is later deleted.
This does affect both namedtuple and dataclasses, though it's probably not too important given how straightforward the generated code they produce is.
History
Date User Action Args
2022-04-11 14:59:53adminsetgithub: 90291
2022-01-15 10:57:44Spencer Brownsetnosy: + Spencer Brown
messages: + msg410640
2022-01-14 20:09:00positasetmessages: + msg410597
2022-01-14 20:08:42iritkatrielsetnosy: - iritkatriel
2022-01-14 20:08:32iritkatrielsetstage: resolved ->
versions: - Python 3.6, Python 3.7, Python 3.8, Python 3.9, Python 3.10
2022-01-14 20:07:45positasetresolution: not a bug ->
2022-01-14 20:07:28positasetstatus: closed -> open
type: behavior -> enhancement
title: Unclear whether one can (or how to) provide source to exec-generated code -> Feature request: allow mechanism for creator of exec-generated code to provide source to pdb
2022-01-14 18:54:23iritkatrielsetstatus: open -> closed

nosy: + iritkatriel
messages: + msg410588

resolution: not a bug
stage: resolved
2021-12-19 16:18:36positasettype: behavior
components: + Library (Lib)
versions: + Python 3.6, Python 3.7, Python 3.8, Python 3.9, Python 3.10, Python 3.11
2021-12-19 16:17:53positacreate