@@ -788,16 +788,27 @@ abstract static class CreateEvalExecArgumentsNode extends Node {
788788 @ Specialization
789789 static Object [] inheritGlobals (VirtualFrame frame , Node inliningTarget , @ SuppressWarnings ("unused" ) PNone globals , Object locals , TruffleString mode ,
790790 @ Exclusive @ Cached ReadCallerFrameNode readCallerFrameNode ,
791+ @ Exclusive @ Cached GetOrCreateDictNode getOrCreateDictNode ,
792+ @ Exclusive @ Cached InlinedConditionProfile haveCallerFrameProfile ,
791793 @ Exclusive @ Cached InlinedConditionProfile haveLocals ,
792794 @ Exclusive @ Cached PyMappingCheckNode mappingCheckNode ,
793795 @ Exclusive @ Cached GetFrameLocalsNode getFrameLocalsNode ,
794796 @ Exclusive @ Cached PRaiseNode raiseNode ) {
795797 PFrame callerFrame = readCallerFrameNode .executeWith (frame , 0 );
796798 Object [] args = PArguments .create ();
797- PArguments .setGlobals (args , callerFrame .getGlobals ());
799+ boolean haveCallerFrame = haveCallerFrameProfile .profile (inliningTarget , callerFrame != null );
800+ if (haveCallerFrame ) {
801+ PArguments .setGlobals (args , callerFrame .getGlobals ());
802+ } else {
803+ PArguments .setGlobals (args , getOrCreateDictNode .execute (inliningTarget , PythonContext .get (inliningTarget ).getMainModule ()));
804+ }
798805 if (haveLocals .profile (inliningTarget , locals instanceof PNone )) {
799- Object callerLocals = getFrameLocalsNode .execute (inliningTarget , callerFrame );
800- setCustomLocals (args , callerLocals );
806+ if (haveCallerFrame ) {
807+ Object callerLocals = getFrameLocalsNode .execute (inliningTarget , callerFrame );
808+ setCustomLocals (args , callerLocals );
809+ } else {
810+ setCustomLocals (args , PArguments .getGlobals (args ));
811+ }
801812 } else {
802813 if (!mappingCheckNode .execute (inliningTarget , locals )) {
803814 throw raiseNode .raise (inliningTarget , TypeError , ErrorMessages .LOCALS_MUST_BE_MAPPING , mode , locals );
@@ -951,15 +962,21 @@ public final PCode compile(VirtualFrame frame, Object source, TruffleString file
951962 protected abstract Object executeInternal (VirtualFrame frame , Object source , TruffleString filename , TruffleString mode , int flags , boolean dontInherit , int optimize ,
952963 int featureVersion );
953964
965+ private int inheritFlags (VirtualFrame frame , int flags , ReadCallerFrameNode readCallerFrame ) {
966+ PFrame fr = readCallerFrame .executeWith (frame , 0 );
967+ if (fr != null ) {
968+ PCode code = PFactory .createCode (PythonLanguage .get (this ), fr .getTarget ());
969+ flags |= code .getFlags () & PyCF_MASK ;
970+ }
971+ return flags ;
972+ }
973+
954974 @ Specialization
955975 Object doCompile (VirtualFrame frame , TruffleString expression , TruffleString filename , TruffleString mode , int flags , boolean dontInherit , int optimize ,
956976 int featureVersion ,
957- @ Shared @ Cached ReadCallerFrameNode readCallerFrame ,
958- @ Bind PythonLanguage language ) {
977+ @ Shared @ Cached ReadCallerFrameNode readCallerFrame ) {
959978 if (!dontInherit ) {
960- PFrame fr = readCallerFrame .executeWith (frame , 0 );
961- PCode code = PFactory .createCode (language , fr .getTarget ());
962- flags |= code .getFlags () & PyCF_MASK ;
979+ flags = inheritFlags (frame , flags , readCallerFrame );
963980 }
964981 EncapsulatingNodeReference encapsulating = EncapsulatingNodeReference .getCurrent ();
965982 Node encapsulatingNode = encapsulating .set (this );
@@ -1037,7 +1054,7 @@ Object compile(TruffleString expression, TruffleString filename, TruffleString m
10371054
10381055 @ Specialization (limit = "3" )
10391056 @ SuppressWarnings ("truffle-static-method" )
1040- Object generic (VirtualFrame frame , Object wSource , Object wFilename , TruffleString mode , int flags , @ SuppressWarnings ( "unused" ) boolean dontInherit , int optimize , int featureVersion ,
1057+ Object generic (VirtualFrame frame , Object wSource , Object wFilename , TruffleString mode , int flags , boolean dontInherit , int optimize , int featureVersion ,
10411058 @ CachedLibrary (limit = "3" ) PythonBufferAcquireLibrary acquireLib ,
10421059 @ CachedLibrary (limit = "3" ) PythonBufferAccessLibrary bufferLib ,
10431060 @ Bind PythonContext context ,
@@ -1056,9 +1073,7 @@ Object generic(VirtualFrame frame, Object wSource, Object wFilename, TruffleStri
10561073 TruffleString filename = asPath .execute (frame , wFilename );
10571074
10581075 if (!dontInherit ) {
1059- PFrame fr = readCallerFrame .executeWith (frame , 0 );
1060- PCode code = PFactory .createCode (PythonLanguage .get (inliningTarget ), fr .getTarget ());
1061- flags |= code .getFlags () & PyCF_MASK ;
1076+ flags = inheritFlags (frame , flags , readCallerFrame );
10621077 }
10631078
10641079 EncapsulatingNodeReference encapsulating = EncapsulatingNodeReference .getCurrent ();
0 commit comments