5858import com .oracle .graal .python .runtime .object .PythonObjectFactory ;
5959import com .oracle .truffle .api .CompilerDirectives ;
6060import com .oracle .truffle .api .CompilerDirectives .CompilationFinal ;
61- import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
6261import com .oracle .truffle .api .TruffleLanguage .ContextReference ;
63- import com .oracle .truffle .api .TruffleLanguage .Env ;
6462import com .oracle .truffle .api .frame .VirtualFrame ;
6563import com .oracle .truffle .api .instrumentation .StandardTags ;
6664import com .oracle .truffle .api .instrumentation .Tag ;
67- import com .oracle .truffle .api .profiles .BranchProfile ;
6865
6966public abstract class AbstractImportNode extends StatementNode {
7067 @ CompilationFinal private ContextReference <PythonContext > contextRef ;
7168 @ Child PythonObjectFactory objectFactory ;
7269
73- private final BranchProfile emulatesJython = BranchProfile .create ();
7470 @ Child private CallNode callNode ;
7571 @ Child private GetDictNode getDictNode ;
7672 @ Child private PassCaughtExceptionNode passExceptionNode ;
73+ @ CompilationFinal private Boolean emulateJython ;
7774
7875 public AbstractImportNode () {
7976 super ();
@@ -127,37 +124,19 @@ protected Object importModule(VirtualFrame frame, String name, Object globals, S
127124 return builtinModule ;
128125 }
129126 }
130- Env env = getContext ().getEnv ();
131- if (env .isHostLookupAllowed () && PythonOptions .getOption (env , PythonOptions .EmulateJython )) {
132- emulatesJython .enter ();
133- forceClassloadingOnImport (name , fromList );
127+ if (emulateJython ()) {
128+ if (fromList .length > 0 ) {
129+ getContext ().pushCurrentImport (name + "." + fromList [0 ]);
130+ } else {
131+ getContext ().pushCurrentImport (name );
132+ }
134133 }
135- return __import__ (frame , name , globals , fromList , level );
136- }
137-
138- /**
139- * Because of how Jython allows you to import classes that have not been
140- * loaded, yet, we need attempt to load types here, while we have the full
141- * string available. this will ensure that if there is such a Java class,
142- * its package will have been initialized and the importer code in java.py
143- * can find it.
144- */
145- @ TruffleBoundary
146- private void forceClassloadingOnImport (String name , String [] fromList ) {
147- Env env = getContext ().getEnv ();
148134 try {
149- env .lookupHostSymbol (name );
150- } catch (RuntimeException e ) {
151- // ignore this, that's fine
152- }
153- for (String potentialClass : fromList ) {
154- try {
155- env .lookupHostSymbol (name + "." + potentialClass );
156- } catch (RuntimeException e ) {
157- continue ;
135+ return __import__ (frame , name , globals , fromList , level );
136+ } finally {
137+ if (emulateJython ()) {
138+ getContext ().popCurrentImport ();
158139 }
159- // if we got here, we can stop. the package was loaded
160- break ;
161140 }
162141 }
163142
@@ -174,6 +153,14 @@ Object __import__(VirtualFrame frame, String name, Object globals, String[] from
174153 });
175154 }
176155
156+ private boolean emulateJython () {
157+ if (emulateJython == null ) {
158+ CompilerDirectives .transferToInterpreterAndInvalidate ();
159+ emulateJython = PythonOptions .getFlag (getContext (), PythonOptions .EmulateJython );
160+ }
161+ return emulateJython ;
162+ }
163+
177164 @ Override
178165 public boolean hasTag (Class <? extends Tag > tag ) {
179166 return tag == StandardTags .CallTag .class || super .hasTag (tag );
0 commit comments