Commit 1e453d2 1 parent c4d2345 commit 1e453d2 Copy full SHA for 1e453d2
File tree 2 files changed +25
-32
lines changed
2 files changed +25
-32
lines changed Original file line number Diff line number Diff line change @@ -164,37 +164,10 @@ def show_ast(tree: AST) -> Optional[Any]:
164
164
print (ast .dump (tree ))
165
165
return None
166
166
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
173
169
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 )
198
171
199
172
# Escaping unicode characters into ASCII for user-facing strings
200
173
def unicode_escape (s : str , error : str = 'backslashreplace' ) -> str :
Original file line number Diff line number Diff line change 1
1
import glob
2
- import imp
3
2
import inspect
4
3
import logging
5
4
import os
6
5
import uuid
7
6
import warnings
8
7
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
+
9
29
# py 2/3 compatibility
10
30
try :
11
31
import pathlib
@@ -22,7 +42,7 @@ def load_source(modname, fname):
22
42
loader .exec_module (mod )
23
43
return mod
24
44
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 )
26
46
27
47
from ipypublish import export_plugins
28
48
You can’t perform that action at this time.
0 commit comments