Skip to content

Commit

Permalink
Compatibility with jupyter notebooks
Browse files Browse the repository at this point in the history
  • Loading branch information
sizhky authored and cool-RR committed Sep 9, 2024
1 parent ac74c8f commit 05f1359
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion pysnooper/tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

ipython_filename_pattern = re.compile('^<ipython-input-([0-9]+)-.*>$')
ansible_filename_pattern = re.compile(r'^(.+\.zip)[/|\\](ansible[/|\\]modules[/|\\].+\.py)$')
ipykernel_filename_pattern = re.compile(r'^/var/folders/.*/ipykernel_[0-9]+/[0-9]+.py$')
RETURN_OPCODES = {
'RETURN_GENERATOR', 'RETURN_VALUE', 'RETURN_CONST',
'INSTRUMENTED_RETURN_GENERATOR', 'INSTRUMENTED_RETURN_VALUE',
Expand Down Expand Up @@ -74,7 +75,15 @@ def get_path_and_source_from_frame(frame):
if source is None:
ipython_filename_match = ipython_filename_pattern.match(file_name)
ansible_filename_match = ansible_filename_pattern.match(file_name)
if ipython_filename_match:
ipykernel_filename_match = ipykernel_filename_pattern.match(file_name)
if ipykernel_filename_match:
try:
import linecache
_, _, source, _ = linecache.cache.get(file_name)
source = [line.rstrip() for line in source] # remove '\n' at the end
except Exception:
pass
elif ipython_filename_match:
entry_number = int(ipython_filename_match.group(1))
try:
import IPython
Expand Down

0 comments on commit 05f1359

Please sign in to comment.