Skip to content

Commit 1e453d2

Browse files
Update from debuggingbook
1 parent c4d2345 commit 1e453d2

File tree

2 files changed

+25
-32
lines changed

2 files changed

+25
-32
lines changed

notebooks/shared/bookutils/__init__.py

+3-30
Original file line numberDiff line numberDiff line change
@@ -164,37 +164,10 @@ def show_ast(tree: AST) -> Optional[Any]:
164164
print(ast.dump(tree))
165165
return None
166166

167-
# show_ast() no longer works in Python 3.12: the `imp` module is deprecated
168-
# import showast
169-
# return showast.show_ast(tree)
170-
171-
# Workaround, avoiding `imp`
172-
import_showast()
167+
# Note: For Python >=3.12, this needs a patched `showast` module
168+
# e.g. git+https://github.com/andreas-zeller/show_ast.git@andreas
173169
import showast
174-
from showast.rendering.graphviz import render
175-
return render(tree, showast.Settings)
176-
177-
# Allow importing the showast module
178-
def import_showast() -> None:
179-
try:
180-
import showast
181-
return
182-
except ModuleNotFoundError:
183-
pass
184-
185-
# Create a local (empty) 'imp' module while importing showast
186-
# This is an ugly hack until the `showast` module is updated to 3.12
187-
import os, sys, shutil
188-
os.mkdir('imp')
189-
imp_init = os.path.join('imp', '__init__.py')
190-
with open(imp_init, 'w') as fd:
191-
pass
192-
original_sys_path = sys.path
193-
sys.path = ['.'] + sys.path
194-
import showast
195-
sys.path = original_sys_path
196-
shutil.rmtree('imp')
197-
170+
return showast.show_ast(tree)
198171

199172
# Escaping unicode characters into ASCII for user-facing strings
200173
def unicode_escape(s: str, error: str = 'backslashreplace') -> str:

notebooks/shared/ipypublish/scripts/export_plugins.py

+22-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,31 @@
11
import glob
2-
import imp
32
import inspect
43
import logging
54
import os
65
import uuid
76
import warnings
87

8+
9+
# Replacement for imp.load_source; see https://docs.python.org/3/whatsnew/3.12.html#imp
10+
import importlib.util
11+
import importlib.machinery
12+
13+
def load_source(modname, filename):
14+
loader = importlib.machinery.SourceFileLoader(modname, filename)
15+
spec = importlib.util.spec_from_file_location(modname, filename, loader=loader)
16+
module = importlib.util.module_from_spec(spec)
17+
# The module is always executed and not cached in sys.modules.
18+
# Uncomment the following line to cache the module.
19+
# sys.modules[module.__name__] = module
20+
loader.exec_module(module)
21+
return module
22+
23+
# Alternative if the above code does not work
24+
# import imp
25+
# load_source = imp.load_source
26+
27+
28+
929
# py 2/3 compatibility
1030
try:
1131
import pathlib
@@ -22,7 +42,7 @@ def load_source(modname, fname):
2242
loader.exec_module(mod)
2343
return mod
2444
except ImportError as err:
25-
load_source = lambda modname, fname: imp.load_source(modname, fname)
45+
load_source = lambda modname, fname: load_source(modname, fname)
2646

2747
from ipypublish import export_plugins
2848

0 commit comments

Comments
 (0)