40
40
*/
41
41
package com .oracle .graal .python .nodes .statement ;
42
42
43
+ import static com .oracle .graal .python .builtins .PythonBuiltinClassType .NotImplementedError ;
44
+ import static com .oracle .graal .python .builtins .PythonBuiltinClassType .RuntimeError ;
43
45
import 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 ;
44
48
import static com .oracle .graal .python .nodes .BuiltinNames .T___IMPORT__ ;
45
49
import static com .oracle .graal .python .nodes .ErrorMessages .ATTEMPTED_RELATIVE_IMPORT_BEYOND_TOPLEVEL ;
46
50
import static com .oracle .graal .python .nodes .ErrorMessages .IMPORT_NOT_FOUND ;
47
51
import static com .oracle .graal .python .nodes .StringLiterals .T_DOT ;
48
52
import static com .oracle .graal .python .util .PythonUtils .TS_ENCODING ;
49
53
import static com .oracle .graal .python .util .PythonUtils .tsLiteral ;
50
54
import 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 ;
53
55
54
56
import com .oracle .graal .python .PythonLanguage ;
55
57
import com .oracle .graal .python .builtins .PythonBuiltinClassType ;
56
58
import com .oracle .graal .python .builtins .objects .PNone ;
57
59
import 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 ;
60
60
import com .oracle .graal .python .builtins .objects .module .PythonModule ;
61
61
import com .oracle .graal .python .builtins .objects .str .StringUtils ;
62
62
import com .oracle .graal .python .lib .PyDictGetItem ;
63
- import com .oracle .graal .python .lib .PyFrameGetBuiltins ;
64
63
import com .oracle .graal .python .lib .PyObjectCallMethodObjArgs ;
65
64
import com .oracle .graal .python .lib .PyObjectGetAttr ;
65
+ import com .oracle .graal .python .lib .PyObjectGetItem ;
66
66
import com .oracle .graal .python .lib .PyObjectIsTrueNode ;
67
67
import com .oracle .graal .python .lib .PyObjectLookupAttr ;
68
68
import com .oracle .graal .python .nodes .ErrorMessages ;
82
82
import com .oracle .graal .python .runtime .object .PythonObjectFactory ;
83
83
import com .oracle .graal .python .util .PythonUtils ;
84
84
import com .oracle .truffle .api .CompilerDirectives ;
85
- import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
86
85
import com .oracle .truffle .api .CompilerDirectives .ValueType ;
87
86
import com .oracle .truffle .api .dsl .Bind ;
88
87
import com .oracle .truffle .api .dsl .Cached ;
@@ -101,37 +100,32 @@ public abstract class AbstractImportNode extends PNodeWithContext {
101
100
102
101
public static final TruffleString T__FIND_AND_LOAD = tsLiteral ("_find_and_load" );
103
102
103
+ /**
104
+ * Equivalent of {@code PyImport_Import} and {@code PyImport_ImportModule}.
105
+ */
104
106
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__ );
120
113
if (builtinImport == PNone .NO_VALUE ) {
121
114
throw PConstructAndRaiseNode .getUncached ().raiseImportError (null , IMPORT_NOT_FOUND );
122
115
}
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 );
125
124
if (module instanceof PythonModule pythonModule ) {
126
125
return pythonModule ;
127
126
}
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 );
135
129
}
136
130
137
131
protected final Object importModule (VirtualFrame frame , TruffleString name , Object globals , TruffleString [] fromList , int level , ImportName importNameNode ) {
0 commit comments