Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d791458

Browse files
committed
Add debug info for autoreload.
See issue #15116
1 parent 8a5b3bf commit d791458

2 files changed

Lines changed: 26 additions & 6 deletions

File tree

IPython/extensions/autoreload.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,10 @@ def check(self, check_all=False, do_reload=True, execution_info=None):
327327
del self.failed[py_filename]
328328
except:
329329
if not self.hide_errors:
330+
logger = logging.getLogger("autoreload")
331+
logger.exception(
332+
f"Failed to reload module '{modname}' from file '{py_filename}'"
333+
)
330334
print(
331335
"[autoreload of {} failed: {}]".format(
332336
modname, traceback.format_exc(10)

IPython/extensions/deduperreload/deduperreload.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import builtins
44
import contextlib
55
import itertools
6+
import logging
67
import os
78
import pickle
89
import platform
@@ -209,11 +210,19 @@ def update_sources(self) -> None:
209210
):
210211
self.source_by_modname[new_modname] = ""
211212
continue
212-
with open(fname, "r") as f:
213-
try:
213+
# Skip binary files (e.g., .so, .pyd, .pyo)
214+
if not fname.endswith(".py"):
215+
self.source_by_modname[new_modname] = ""
216+
continue
217+
try:
218+
with open(fname, "r") as f:
214219
self.source_by_modname[new_modname] = f.read()
215-
except Exception:
216-
self.source_by_modname[new_modname] = ""
220+
except Exception as e:
221+
logger = logging.getLogger("autoreload")
222+
logger.exception(
223+
f"Failed to read module file '{fname}' for module '{new_modname}': {type(e).__name__}"
224+
)
225+
self.source_by_modname[new_modname] = ""
217226

218227
constexpr_detector = ConstexprDetector()
219228

@@ -542,8 +551,15 @@ def maybe_reload_module(self, module: ModuleType) -> bool:
542551
return False
543552
if (fname := get_module_file_name(module)) is None:
544553
return False
545-
with open(fname, "r") as f:
546-
new_source_code = f.read()
554+
try:
555+
with open(fname, "r") as f:
556+
new_source_code = f.read()
557+
except Exception as e:
558+
logger = logging.getLogger("autoreload")
559+
logger.exception(
560+
f"Failed to read module file '{fname}' for module '{modname}': {type(e).__name__}"
561+
)
562+
return False
547563
patched_flag = False
548564
if old_source_code := self.source_by_modname.get(modname):
549565
# get old/new module ast

0 commit comments

Comments
 (0)