Skip to content

Commit

Permalink
Fix #14230 (missing param for autoreload)
Browse files Browse the repository at this point in the history
This extend the tests to actually trigger the same event as what the
shell is doing, and updating the auto reload callback to accept more
parameters.

It also improve both the logging and test  mock to log more informations
on failure.
  • Loading branch information
Carreau committed Oct 31, 2023
1 parent 4977b5d commit e98a2f4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
6 changes: 5 additions & 1 deletion IPython/core/events.py
Expand Up @@ -82,7 +82,11 @@ def trigger(self, event, *args, **kwargs):
func(*args, **kwargs)
except (Exception, KeyboardInterrupt):
if self.print_on_error:
print("Error in callback {} (for {}):".format(func, event))
print(
"Error in callback {} (for {}), with arguments args {},kwargs {}:".format(
func, event, args, kwargs
)
)
self.shell.showtraceback()

# event_name -> prototype mapping
Expand Down
2 changes: 1 addition & 1 deletion IPython/extensions/autoreload.py
Expand Up @@ -701,7 +701,7 @@ def aimport(self, parameter_s="", stream=None):
# Inject module to user namespace
self.shell.push({top_name: top_module})

def pre_run_cell(self):
def pre_run_cell(self, info):
if self._reloader.enabled:
try:
self._reloader.check()
Expand Down
24 changes: 23 additions & 1 deletion IPython/extensions/tests/test_autoreload.py
Expand Up @@ -21,6 +21,7 @@
import shutil
import random
import time
import traceback
from io import StringIO
from dataclasses import dataclass

Expand All @@ -31,6 +32,7 @@
from IPython.extensions.autoreload import AutoreloadMagics
from IPython.core.events import EventManager, pre_run_cell
from IPython.testing.decorators import skipif_not_numpy
from IPython.core.interactiveshell import ExecutionInfo

if platform.python_implementation() == "PyPy":
pytest.skip(
Expand All @@ -56,8 +58,27 @@ def __init__(self):

register_magics = set_hook = noop

def showtraceback(
self,
exc_tuple=None,
filename=None,
tb_offset=None,
exception_only=False,
running_compiled_code=False,
):
traceback.print_exc()

def run_code(self, code):
self.events.trigger("pre_run_cell")
self.events.trigger(
"pre_run_cell",
ExecutionInfo(
raw_cell="",
store_history=False,
silent=False,
shell_futures=False,
cell_id=None,
),
)
exec(code, self.user_ns)
self.auto_magics.post_execute_hook()

Expand Down Expand Up @@ -279,6 +300,7 @@ def power(self, p):
@skipif_not_numpy
def test_comparing_numpy_structures(self):
self.shell.magic_autoreload("2")
self.shell.run_code("1+1")
mod_name, mod_fn = self.new_module(
textwrap.dedent(
"""
Expand Down

0 comments on commit e98a2f4

Please sign in to comment.