PythonFunctionGenerator: make compatible with linecache#295
PythonFunctionGenerator: make compatible with linecache#295inducer merged 5 commits intoinducer:mainfrom
Conversation
pytools/py_codegen.py
Outdated
|
|
||
| class PythonCodeGenerator(CodeGeneratorBase): | ||
| def get_module(self, name=None): | ||
| def get_module(self, name=None, write_file=True): |
There was a problem hiding this comment.
Could maybe set this to False by default.
There was a problem hiding this comment.
I think we can do this without ever writing a file, by simply poking data into the linecache directly:
https://github.com/python/cpython/blob/3.13/Lib/linecache.py#L11-L13
This seems to be at least vaguely supported; cache is not underscored.
There should of course at least be a warning if the "file name" is not unique/has been seen before.
There was a problem hiding this comment.
That won't work for the line_profiler unfortunately, it requires the code in a file, even if it is in the linecache: https://github.com/pyutils/line_profiler/blob/1630e7c9a295ace2feb1d2b188e68f4d2833fb20/line_profiler/line_profiler.py#L194-L210
There was a problem hiding this comment.
51ada01 implements a workaround that uses linecache directly (like you suggested), but also maintains compatibility with line_profiler.
There was a problem hiding this comment.
( I added a warning, it is somewhat noisy though)
pytools/py_codegen.py
Outdated
| return f"generated_code_for_{self.name}" | ||
| # Note that the '<ipython-input-' prefix is for compatibility with | ||
| # line_profiler: https://github.com/pyutils/line_profiler/blob/1630e7c9a295ace2feb1d2b188e68f4d2833fb20/line_profiler/line_profiler.py#L194-L210 | ||
| return f"<ipython-input- generated code for '{self.name}'>" |
There was a problem hiding this comment.
Please only use the work-around if line_profiler is in sys.modules.
pytools/py_codegen.py
Outdated
| return f"generated_code_for_{self.name}" | ||
| # Note that the '<ipython-input-' prefix is for compatibility with | ||
| # line_profiler: https://github.com/pyutils/line_profiler/blob/1630e7c9a295ace2feb1d2b188e68f4d2833fb20/line_profiler/line_profiler.py#L194-L210 | ||
| return f"<ipython-input- generated code for '{self.name}'>" |
There was a problem hiding this comment.
While we're at it, maybe shorten the normal prefix to just generated: .
pytools/py_codegen.py
Outdated
| if name in linecache.cache: | ||
| from warnings import warn | ||
| warn(f"Overwriting existing generated code in linecache: '{name}'.", | ||
| stacklevel=2) |
There was a problem hiding this comment.
Use stacklevel=3 if this is called through get_function. (Maybe use an underscored parameter to tell the two apart?)
0ecb4b7 to
68ed96a
Compare
|
Just merged #294 to help you out. It'd be nice to add types for the decorators parameter. It could also default to |
3068df1 to
4cb51b4
Compare
|
This is ready for review @inducer |
e567f10 to
43127bf
Compare
inducer
left a comment
There was a problem hiding this comment.
Thanks! A few more tweaks, then this should be good to go.
8874bc0 to
04837f7
Compare
6430ede to
a8b198a
Compare
23cd926 to
743be46
Compare
|
@matthiasdiener I've reworked this quite a bit. Please take a look and let me know what you think. |
de65183 to
1425f75
Compare
|
LGTM, thanks. I tested this with |
use linecache directly only adjust filename if line_profiler loaded improve args fix maintain compatibility with pudb broaden line_profiler usage check more type annotations pyright ignores rename generated functions to avoid linecache warnings remove special pudb handling make sure names are unique fix bpr expand to support PythonCodeGenerator, refactor fixes remove special line_profiler handling Relevant PR (pyutils/line_profiler#341) has been merged Rework for linecache compatibility, improve tests Co-authored-by: Andreas Kloeckner <inform@tiker.net>
add NanKeyWarning
1425f75 to
9722f81
Compare
Needs:
BEFORE MERGE: SQUASH AS APPROPRIATE.
Somewhat clumsy, but can be very useful for debugging/profiling. E.g., for the following
line_profilertest:Before:
After:
Note: Since the generated source code is not saved in a file on disk (only in the
linecache), re-running the profile viapython -m line_profiler -rtmz profile_output.lprofwill not show the line contents. Theprofile_output.txttext files will work correctly.