Skip to content

Commit 854078c

Browse files
New: use patched showast module
1 parent da19b9c commit 854078c

File tree

1 file changed

+3
-30
lines changed

1 file changed

+3
-30
lines changed

notebooks/shared/bookutils/__init__.py

Lines changed: 3 additions & 30 deletions
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:

0 commit comments

Comments
 (0)