ipython / ipython/ipython

deepreload fails to reload importlib on Python 3.13

Aberta
#14,570 1 comentário 1 reação 0 responsáveis Ver no GitHub
Linguagem predominante
Python
Estrelas
16.8k
Forks
4.5k
Merge médio
1d 2h
PRs com merge (30d)
6

Descrição

`deepreload.reload` fails to reload `importlib` and leaves the environment in an unstable state. In some circumstances, this crashes the process.

Here is a minimal reproduction and stack trace.

```
$ ipython -c 'import importlib; from IPython.lib.deepreload import reload; reload(importlib)'
Reloading _frozen_importlib
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
File :866, in _exec(spec, module)

File :1022, in exec_module(self, module)

File :488, in _call_with_frames_removed(f, *args, **kwds)

File /opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/lib/python3.13/importlib/__init__.py:16
15 try:
---> 16 import _frozen_importlib as _bootstrap
17 except ImportError:

File ~/Desktop/optix/.venv/lib/python3.13/site-packages/IPython/lib/deepreload.py:230, in deep_import_hook(name, globals, locals, fromlist, level)
228 parent, buf = get_parent(globals, level)
--> 230 head, name, buf = load_next(parent, None if level < 0 else parent, name, buf)
232 tail = head

File ~/Desktop/optix/.venv/lib/python3.13/site-packages/IPython/lib/deepreload.py:149, in load_next(mod, altmod, name, buf)
147 buf += subname
--> 149 result = import_submodule(mod, subname, buf)
150 if result is None and mod != altmod:

File ~/Desktop/optix/.venv/lib/python3.13/site-packages/IPython/lib/deepreload.py:179, in import_submodule(mod, subname, fullname)
178 if oldm is not None:
--> 179 m = importlib.reload(oldm)
180 else:

File /opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/lib/python3.13/importlib/__init__.py:129, in reload(module)
128 raise ModuleNotFoundError(f"spec not found for the module {name!r}", name=name)
--> 129 _bootstrap._exec(spec, module)
130 # The module may have replaced itself in sys.modules!

File :848, in _exec(spec, module)

File :421, in __exit__(self, *args, **kwargs)

File :373, in release(self)

AttributeError: 'NoneType' object has no attribute 'get_ident'

During handling of the above exception, another exception occurred:

AttributeError Traceback (most recent call last)
Cell In[1], line 1
----> 1 import importlib; from IPython.lib.deepreload import reload; reload(importlib)

File ~/Desktop/optix/.venv/lib/python3.13/site-packages/IPython/lib/deepreload.py:308, in reload(module, exclude)
306 try:
307 with replace_import_hook(deep_import_hook):
--> 308 return deep_reload_hook(module)
309 finally:
310 found_now = {}

File ~/Desktop/optix/.venv/lib/python3.13/site-packages/IPython/lib/deepreload.py:273, in deep_reload_hook(m)
270 modules_reloading[name] = m
272 try:
--> 273 newm = importlib.reload(m)
274 except:
275 sys.modules[name] = m

File /opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/lib/python3.13/importlib/__init__.py:129, in reload(module)
127 if spec is None:
128 raise ModuleNotFoundError(f"spec not found for the module {name!r}", name=name)
--> 129 _bootstrap._exec(spec, module)
130 # The module may have replaced itself in sys.modules!
131 return sys.modules[name]

File :848, in _exec(spec, module)

File :421, in __exit__(self, *args, **kwargs)

File :373, in release(self)

AttributeError: 'NoneType' object has no attribute 'get_ident'
```

This seems to be choking while trying to reload a package called `_frozen_importlib`.

The error points to [this line](https://github.com/python/cpython/blob/9441993f272f42e4a97d90616ec629a11c06aa3a/Lib/importlib/_bootstrap.py#L373) in `importlib`.

Notably, the `deepreload.original_reload` function can reload `importlib` just fine.

After trying to deep reload `importlib`, attempting to import new packages fails.

```
$ touch ./empty.py
$ ipython
Python 3.13.0 (main, Oct 7 2024, 05:02:14) [Clang 16.0.0 (clang-1600.0.26.3)]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.29.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from IPython.lib import deepreload

In [2]: import importlib

In [3]: deepreload.reload(importlib)
# Fails. Output elided for brevity. Same error as before.

In [4]: import empty
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[4], line 1
----> 1 import empty

File :1357, in _find_and_load(name, import_)

File :417, in __enter__(self)

File :456, in _get_module_lock(name)

AttributeError: 'NoneType' object has no attribute 'ref'
```

This second error points to [this line](https://github.com/python/cpython/blob/9441993f272f42e4a97d90616ec629a11c06aa3a/Lib/importlib/_bootstrap.py#L456) in `importlib`.

In my case, my package is not doing anything with importlib directly. I assume one of my dependencies is using it. In fact, when I try to deep reload my package, IPython crashes. I am not able to create a minimal reproduction of the crash though.

Guia de contribuição

Abrir o guia de contribuição

Avaliação

Esta issue ainda não foi avaliada.

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.