Skip to content

Commit

Permalink
Python Import Tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
kuzmich321 committed Nov 24, 2023
1 parent c528447 commit 850c48c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
9 changes: 7 additions & 2 deletions library/python/runtime/importer.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,13 @@ class ResourceImporter(object):
old_mod = sys.modules.get(mod_name, None)
sys.modules[mod_name] = mod

# __name__ and __file__ could be overwritten after execution
# So these two things are needed if wee want to be consistent at some point
initial_modname = mod.__name__
initial_filename = mod.__file__

if self._before_import_callback:
self._before_import_callback(mod)
self._before_import_callback(initial_modname, initial_filename)

try:
exec code in mod.__dict__
Expand All @@ -252,7 +257,7 @@ class ResourceImporter(object):

# "Zero-cost". Just in case import error occurs
if self._after_import_callback:
self._after_import_callback(mod)
self._after_import_callback(initial_modname, initial_filename)

# Some hacky modules (e.g. pygments.lexers) replace themselves in
# `sys.modules` with proxies.
Expand Down
9 changes: 7 additions & 2 deletions library/python/runtime_py3/importer.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -240,16 +240,21 @@ class ResourceImporter(object):
module.__path__= [executable + path_sep + module.__name__.replace('.', path_sep)]
# exec(code, module.__dict__)

# __name__ and __file__ could be overwritten after execution
# So these two things are needed if wee want to be consistent at some point
initial_modname = module.__name__
initial_filename = module.__file__

if self._before_import_callback:
self._before_import_callback(module)
self._before_import_callback(initial_modname, initial_filename)

# “Zero-cost” exceptions are implemented.
# The cost of try statements is almost eliminated when no exception is raised
try:
_call_with_frames_removed(exec, code, module.__dict__)
finally:
if self._after_import_callback:
self._after_import_callback(module)
self._after_import_callback(initial_modname, initial_filename)

# PEP-302 extension 1 of 3: data loader.
def get_data(self, path):
Expand Down

0 comments on commit 850c48c

Please sign in to comment.