|
3 | 3 | import builtins |
4 | 4 | import contextlib |
5 | 5 | import itertools |
| 6 | +import logging |
6 | 7 | import os |
7 | 8 | import pickle |
8 | 9 | import platform |
@@ -209,11 +210,19 @@ def update_sources(self) -> None: |
209 | 210 | ): |
210 | 211 | self.source_by_modname[new_modname] = "" |
211 | 212 | 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: |
214 | 219 | 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] = "" |
217 | 226 |
|
218 | 227 | constexpr_detector = ConstexprDetector() |
219 | 228 |
|
@@ -542,8 +551,15 @@ def maybe_reload_module(self, module: ModuleType) -> bool: |
542 | 551 | return False |
543 | 552 | if (fname := get_module_file_name(module)) is None: |
544 | 553 | 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 |
547 | 563 | patched_flag = False |
548 | 564 | if old_source_code := self.source_by_modname.get(modname): |
549 | 565 | # get old/new module ast |
|
0 commit comments