Skip to content
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

Show Python Script source code in tracebacks #65

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

perrinjerome
Copy link
Contributor

@perrinjerome perrinjerome commented May 24, 2024

Expose a __loader__ in globals so that linecache module is able to use it to display the source code.

This requires changing the "filename" used when compiling function, because linecache uses code.co_filename as a cache key, so it's necessary that each python script use a different filename.

Fixes #64

@perrinjerome
Copy link
Contributor Author

tests depend on zopefoundation/zope.exceptions#33 because zope.testrunner monkey patches traceback module to use zope.exceptions, see https://github.com/zopefoundation/zope.testrunner/blob/f6c62d331487538b5035d0e4b33d0bc864a291ea/src/zope/testrunner/tb_format.py#L90-L94

@@ -261,6 +273,7 @@ def _compile(self):
fc.co_argcount)
self.Python_magic = Python_magic
self.Script_magic = Script_magic
linecache.clearcache()
Copy link
Contributor Author

@perrinjerome perrinjerome May 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this empties all the cache, which will cause the next use of linecache to take a few milliseconds. In practice it's also possible to only remove the corresponding entry from the cache, like this:

        linecache.cache.pop(
            getattr(self, '_filepath', None) or self.get_filepath(), None)

but it's not a documented API and this is only called when python scripts are edited, so I thought it was better to just clear all the cache.

@perrinjerome perrinjerome force-pushed the perrinjerome/code-in-traceback branch from 90db8df to ec0214d Compare June 7, 2024 15:41
Expose a __loader__ in globals so that linecache module is able to use
it to display the source code.

This requires changing the "filename" used when compiling function,
because linecache uses code.co_filename as a cache key, so it's
necessary that each python script use a different filename.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

No source code in tracebacks
1 participant