77
77
import com .oracle .graal .python .builtins .objects .object .PythonObject ;
78
78
import com .oracle .graal .python .builtins .objects .str .PString ;
79
79
import com .oracle .graal .python .lib .PyObjectLookupAttr ;
80
+ import com .oracle .graal .python .lib .PyObjectStrAsJavaStringNode ;
80
81
import com .oracle .graal .python .nodes .attributes .ReadAttributeFromDynamicObjectNode ;
81
82
import com .oracle .graal .python .nodes .attributes .SetAttributeNode ;
82
83
import com .oracle .graal .python .nodes .function .PythonBuiltinBaseNode ;
83
84
import com .oracle .graal .python .nodes .function .PythonBuiltinNode ;
84
85
import com .oracle .graal .python .nodes .function .builtins .PythonBinaryBuiltinNode ;
85
86
import com .oracle .graal .python .nodes .function .builtins .PythonBinaryClinicBuiltinNode ;
86
87
import com .oracle .graal .python .nodes .function .builtins .clinic .ArgumentClinicProvider ;
88
+ import com .oracle .graal .python .nodes .function .builtins .PythonTernaryBuiltinNode ;
87
89
import com .oracle .graal .python .nodes .util .CastToJavaStringNode ;
88
90
import com .oracle .graal .python .parser .sst .SerializationUtils ;
89
91
import com .oracle .graal .python .runtime .ExecutionContext .IndirectCallContext ;
@@ -105,6 +107,9 @@ public class ImpModuleBuiltins extends PythonBuiltins {
105
107
106
108
static final String HPY_SUFFIX = ".hpy.so" ;
107
109
110
+ // the full module name for package imports
111
+ public String pyPackageContext ;
112
+
108
113
@ Override
109
114
protected List <? extends NodeFactory <? extends PythonBuiltinBaseNode >> getNodeFactories () {
110
115
return ImpModuleBuiltinsFactory .getFactories ();
@@ -192,7 +197,7 @@ protected byte[] getMagicNumberBytes(VirtualFrame frame) {
192
197
193
198
@ Builtin (name = "__create_dynamic__" , minNumOfPositionalArgs = 2 )
194
199
@ GenerateNodeFactory
195
- public abstract static class CreateDynamic extends PythonBuiltinNode {
200
+ public abstract static class CreateDynamic extends PythonBinaryBuiltinNode {
196
201
197
202
@ Child private CheckFunctionResultNode checkResultNode ;
198
203
@ Child private HPyCheckFunctionResultNode checkHPyResultNode ;
@@ -436,4 +441,28 @@ Object run() {
436
441
}
437
442
}
438
443
444
+ @ Builtin (name = "create_dynamic" , minNumOfPositionalArgs = 2 , declaresExplicitSelf = true , parameterNames = {"$self" , "moduleSpec" , "fileName" })
445
+ @ GenerateNodeFactory
446
+ public abstract static class CreateDynamicNode extends PythonTernaryBuiltinNode {
447
+ @ Specialization (guards = "isNoValue(fileName)" )
448
+ Object runNoFileName (VirtualFrame frame , PythonModule self , PythonObject moduleSpec , @ SuppressWarnings ("unused" ) PNone fileName ,
449
+ @ Cached CreateDynamic createDynamicNode ) {
450
+ return run (frame , self , moduleSpec , PNone .NONE , createDynamicNode );
451
+ }
452
+
453
+ @ Specialization (guards = "!isNoValue(fileName)" )
454
+ Object run (VirtualFrame frame , PythonModule self , PythonObject moduleSpec , Object fileName ,
455
+ @ Cached CreateDynamic createDynamicNode ) {
456
+ ImpModuleBuiltins impBuiltins = (ImpModuleBuiltins ) self .getBuiltins ();
457
+ String oldPackageContext = impBuiltins .pyPackageContext ;
458
+ impBuiltins .pyPackageContext = PyObjectStrAsJavaStringNode .getUncached ().execute (frame ,
459
+ PyObjectLookupAttr .getUncached ().execute (frame , moduleSpec , "name" ));
460
+ try {
461
+ return createDynamicNode .execute (frame , moduleSpec , fileName );
462
+ } finally {
463
+ impBuiltins .pyPackageContext = oldPackageContext ;
464
+ }
465
+ }
466
+ }
467
+
439
468
}
0 commit comments