4040 */
4141package com .oracle .graal .python .nodes .statement ;
4242
43+ import static com .oracle .graal .python .builtins .PythonBuiltinClassType .NotImplementedError ;
44+ import static com .oracle .graal .python .builtins .PythonBuiltinClassType .RuntimeError ;
4345import static com .oracle .graal .python .builtins .objects .module .ModuleBuiltins .T__INITIALIZING ;
46+ import static com .oracle .graal .python .nodes .BuiltinNames .T_MODULES ;
47+ import static com .oracle .graal .python .nodes .BuiltinNames .T_SYS ;
4448import static com .oracle .graal .python .nodes .BuiltinNames .T___IMPORT__ ;
4549import static com .oracle .graal .python .nodes .ErrorMessages .ATTEMPTED_RELATIVE_IMPORT_BEYOND_TOPLEVEL ;
4650import static com .oracle .graal .python .nodes .ErrorMessages .IMPORT_NOT_FOUND ;
4751import static com .oracle .graal .python .nodes .StringLiterals .T_DOT ;
4852import static com .oracle .graal .python .util .PythonUtils .TS_ENCODING ;
4953import static com .oracle .graal .python .util .PythonUtils .tsLiteral ;
5054import static com .oracle .graal .python .util .PythonUtils .tsbCapacity ;
51- import static com .oracle .truffle .api .CompilerDirectives .shouldNotReachHere ;
52- import static com .oracle .truffle .api .CompilerDirectives .transferToInterpreterAndInvalidate ;
5355
5456import com .oracle .graal .python .PythonLanguage ;
5557import com .oracle .graal .python .builtins .PythonBuiltinClassType ;
5658import com .oracle .graal .python .builtins .objects .PNone ;
5759import com .oracle .graal .python .builtins .objects .dict .PDict ;
58- import com .oracle .graal .python .builtins .objects .function .PFunction ;
59- import com .oracle .graal .python .builtins .objects .method .PMethod ;
6060import com .oracle .graal .python .builtins .objects .module .PythonModule ;
6161import com .oracle .graal .python .builtins .objects .str .StringUtils ;
6262import com .oracle .graal .python .lib .PyDictGetItem ;
63- import com .oracle .graal .python .lib .PyFrameGetBuiltins ;
6463import com .oracle .graal .python .lib .PyObjectCallMethodObjArgs ;
6564import com .oracle .graal .python .lib .PyObjectGetAttr ;
65+ import com .oracle .graal .python .lib .PyObjectGetItem ;
6666import com .oracle .graal .python .lib .PyObjectIsTrueNode ;
6767import com .oracle .graal .python .lib .PyObjectLookupAttr ;
6868import com .oracle .graal .python .nodes .ErrorMessages ;
8282import com .oracle .graal .python .runtime .object .PythonObjectFactory ;
8383import com .oracle .graal .python .util .PythonUtils ;
8484import com .oracle .truffle .api .CompilerDirectives ;
85- import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
8685import com .oracle .truffle .api .CompilerDirectives .ValueType ;
8786import com .oracle .truffle .api .dsl .Bind ;
8887import com .oracle .truffle .api .dsl .Cached ;
@@ -101,37 +100,32 @@ public abstract class AbstractImportNode extends PNodeWithContext {
101100
102101 public static final TruffleString T__FIND_AND_LOAD = tsLiteral ("_find_and_load" );
103102
103+ /**
104+ * Equivalent of {@code PyImport_Import} and {@code PyImport_ImportModule}.
105+ */
104106 public static PythonModule importModule (TruffleString name ) {
105- return importModule (name , PythonUtils .EMPTY_TRUFFLESTRING_ARRAY );
106- }
107-
108- public static PythonModule importModule (TruffleString name , TruffleString [] fromList ) {
109- return importModule (name , PythonObjectFactory .getUncached ().createTuple (fromList ), 0 );
110- }
111-
112- @ TruffleBoundary
113- public static PythonModule importModule (TruffleString name , Object [] fromList , Object level ) {
114- return importModule (name , PythonObjectFactory .getUncached ().createTuple (fromList ), level );
115- }
116-
117- @ TruffleBoundary
118- public static PythonModule importModule (TruffleString name , Object fromList , Object level ) {
119- Object builtinImport = PyFrameGetBuiltins .executeUncached ().getAttribute (T___IMPORT__ );
107+ /*
108+ * TODO we should rather use {@link com.oracle.graal.python.lib.PyImportImport}, but it
109+ * currently can't be made uncached because it properly reads builtins from frame globals.
110+ */
111+ PythonContext context = PythonContext .get (null );
112+ Object builtinImport = context .getBuiltins ().getAttribute (T___IMPORT__ );
120113 if (builtinImport == PNone .NO_VALUE ) {
121114 throw PConstructAndRaiseNode .getUncached ().raiseImportError (null , IMPORT_NOT_FOUND );
122115 }
123- assert builtinImport instanceof PMethod || builtinImport instanceof PFunction ;
124- Object module = CallNode .getUncached ().execute (builtinImport , name , PNone .NONE , PNone .NONE , fromList , level );
116+ Object fromList = context .factory ().createTuple (PythonUtils .EMPTY_TRUFFLESTRING_ARRAY );
117+ CallNode .getUncached ().execute (builtinImport , name , PNone .NONE , PNone .NONE , fromList , 0 );
118+ PythonModule sysModule = context .lookupBuiltinModule (T_SYS );
119+ Object modules = sysModule .getAttribute (T_MODULES );
120+ if (modules == PNone .NO_VALUE ) {
121+ throw PRaiseNode .getUncached ().raise (RuntimeError , ErrorMessages .UNABLE_TO_GET_S , "sys.modules" );
122+ }
123+ Object module = PyObjectGetItem .executeUncached (modules , name );
125124 if (module instanceof PythonModule pythonModule ) {
126125 return pythonModule ;
127126 }
128- transferToInterpreterAndInvalidate ();
129- throw shouldNotReachHere ("__import__ returned " + module .getClass () + " instead of PythonModule" );
130- }
131-
132- @ TruffleBoundary
133- public static Object importModule (PythonContext context , TruffleString name , TruffleString [] fromList , int level ) {
134- return ImportNameNodeGen .getUncached ().execute (null , context , PyFrameGetBuiltins .execute (context ), name , PNone .NONE , fromList , level );
127+ // FIXME CPython allows putting any object in sys.modules
128+ throw PRaiseNode .getUncached ().raise (NotImplementedError , ErrorMessages .PUTTING_NON_MODULE_OBJECTS_IN_SYS_MODULES_IS_NOT_SUPPORTED );
135129 }
136130
137131 protected final Object importModule (VirtualFrame frame , TruffleString name , Object globals , TruffleString [] fromList , int level , ImportName importNameNode ) {
0 commit comments