56
56
import java .util .List ;
57
57
import java .util .Map ;
58
58
import java .util .Map .Entry ;
59
- import java .util .ServiceLoader ;
60
59
import java .util .logging .Level ;
61
60
62
- import org .graalvm .nativeimage .ImageInfo ;
63
-
64
61
import com .oracle .graal .python .PythonLanguage ;
65
62
import com .oracle .graal .python .builtins .modules .AbcModuleBuiltins ;
66
63
import com .oracle .graal .python .builtins .modules .ArrayModuleBuiltins ;
393
390
import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
394
391
import com .oracle .truffle .api .RootCallTarget ;
395
392
import com .oracle .truffle .api .TruffleFile ;
393
+ import com .oracle .truffle .api .TruffleLanguage ;
396
394
import com .oracle .truffle .api .TruffleLanguage .Env ;
397
395
import com .oracle .truffle .api .TruffleLogger ;
396
+ import com .oracle .truffle .api .TruffleOptions ;
398
397
import com .oracle .truffle .api .nodes .Node ;
399
398
import com .oracle .truffle .api .object .Shape ;
400
399
import com .oracle .truffle .api .source .Source ;
@@ -417,28 +416,15 @@ public abstract class Python3Core {
417
416
418
417
private static TruffleString [] initializeCoreFiles () {
419
418
// Order matters!
420
- List < TruffleString > coreFiles = new ArrayList <>( Arrays . asList (
419
+ return new TruffleString []{
421
420
toTruffleStringUncached ("__graalpython__" ),
422
421
toTruffleStringUncached ("_weakref" ),
423
422
toTruffleStringUncached ("unicodedata" ),
424
423
toTruffleStringUncached ("_sre" ),
425
424
toTruffleStringUncached ("_sysconfig" ),
426
425
toTruffleStringUncached ("java" ),
427
- toTruffleStringUncached ("pip_hook" )));
428
- // add service loader defined python file extensions
429
- if (!ImageInfo .inImageRuntimeCode ()) {
430
- ServiceLoader <PythonBuiltins > providers = ServiceLoader .load (PythonBuiltins .class , Python3Core .class .getClassLoader ());
431
- PythonOS currentOs = PythonOS .getPythonOS ();
432
- for (PythonBuiltins builtin : providers ) {
433
- CoreFunctions annotation = builtin .getClass ().getAnnotation (CoreFunctions .class );
434
- if (!annotation .pythonFile ().isEmpty () &&
435
- (annotation .os () == PythonOS .PLATFORM_ANY || annotation .os () == currentOs )) {
436
- coreFiles .add (toTruffleStringUncached (annotation .pythonFile ()));
437
- }
438
- }
439
- }
440
- coreFiles .removeAll (Arrays .asList (new TruffleString []{null }));
441
- return coreFiles .toArray (new TruffleString [coreFiles .size ()]);
426
+ toTruffleStringUncached ("pip_hook" )
427
+ };
442
428
}
443
429
444
430
private final PythonBuiltins [] builtins ;
@@ -470,7 +456,7 @@ private static void filterBuiltins(List<PythonBuiltins> builtins) {
470
456
builtins .removeAll (toRemove );
471
457
}
472
458
473
- private static PythonBuiltins [] initializeBuiltins (boolean nativeAccessAllowed , boolean socketIOAllowed ) {
459
+ private static PythonBuiltins [] initializeBuiltins (TruffleLanguage . Env env ) {
474
460
List <PythonBuiltins > builtins = new ArrayList <>(Arrays .asList (new BuiltinConstructors (),
475
461
new AbcModuleBuiltins (),
476
462
new BuiltinFunctions (),
@@ -647,9 +633,9 @@ private static PythonBuiltins[] initializeBuiltins(boolean nativeAccessAllowed,
647
633
new JSONModuleBuiltins (),
648
634
new SREModuleBuiltins (),
649
635
new AstModuleBuiltins (),
650
- PythonImageBuildOptions .WITHOUT_NATIVE_POSIX && (PythonImageBuildOptions .WITHOUT_JAVA_INET || !socketIOAllowed ) ? null : new SelectModuleBuiltins (),
651
- PythonImageBuildOptions .WITHOUT_NATIVE_POSIX && (PythonImageBuildOptions .WITHOUT_JAVA_INET || !socketIOAllowed ) ? null : new SocketModuleBuiltins (),
652
- PythonImageBuildOptions .WITHOUT_NATIVE_POSIX && (PythonImageBuildOptions .WITHOUT_JAVA_INET || !socketIOAllowed ) ? null : new SocketBuiltins (),
636
+ PythonImageBuildOptions .WITHOUT_NATIVE_POSIX && (PythonImageBuildOptions .WITHOUT_JAVA_INET || !env . isSocketIOAllowed () ) ? null : new SelectModuleBuiltins (),
637
+ PythonImageBuildOptions .WITHOUT_NATIVE_POSIX && (PythonImageBuildOptions .WITHOUT_JAVA_INET || !env . isSocketIOAllowed () ) ? null : new SocketModuleBuiltins (),
638
+ PythonImageBuildOptions .WITHOUT_NATIVE_POSIX && (PythonImageBuildOptions .WITHOUT_JAVA_INET || !env . isSocketIOAllowed () ) ? null : new SocketBuiltins (),
653
639
PythonImageBuildOptions .WITHOUT_PLATFORM_ACCESS ? null : new SignalModuleBuiltins (),
654
640
new TracebackBuiltins (),
655
641
new GcModuleBuiltins (),
@@ -803,15 +789,11 @@ private static PythonBuiltins[] initializeBuiltins(boolean nativeAccessAllowed,
803
789
builtins .add (new LsprofModuleBuiltins ());
804
790
builtins .add (LsprofModuleBuiltins .newProfilerBuiltins ());
805
791
}
806
- if (!PythonImageBuildOptions .WITHOUT_COMPRESSION_LIBRARIES && (nativeAccessAllowed || ImageInfo . inImageBuildtimeCode ())) {
792
+ if (!PythonImageBuildOptions .WITHOUT_COMPRESSION_LIBRARIES && (env . isNativeAccessAllowed () || env . isPreInitialization ())) {
807
793
builtins .add (new BZ2CompressorBuiltins ());
808
794
builtins .add (new BZ2DecompressorBuiltins ());
809
795
builtins .add (new BZ2ModuleBuiltins ());
810
796
}
811
- ServiceLoader <PythonBuiltins > providers = ServiceLoader .load (PythonBuiltins .class , Python3Core .class .getClassLoader ());
812
- for (PythonBuiltins builtin : providers ) {
813
- builtins .add (builtin );
814
- }
815
797
filterBuiltins (builtins );
816
798
return builtins .toArray (new PythonBuiltins [builtins .size ()]);
817
799
}
@@ -844,15 +826,15 @@ private static PythonBuiltins[] initializeBuiltins(boolean nativeAccessAllowed,
844
826
845
827
private final PythonLanguage language ;
846
828
847
- public Python3Core (PythonLanguage language , boolean isNativeSupportAllowed , boolean socketIOAllowed ) {
829
+ public Python3Core (PythonLanguage language , TruffleLanguage . Env env ) {
848
830
this .language = language ;
849
- this .builtins = initializeBuiltins (isNativeSupportAllowed , socketIOAllowed );
831
+ this .builtins = initializeBuiltins (env );
850
832
this .coreFiles = initializeCoreFiles ();
851
833
}
852
834
853
835
@ CompilerDirectives .ValueType
854
836
public static class SysModuleState {
855
- private int recursionLimit = ImageInfo . inImageCode () ? NATIVE_REC_LIM : REC_LIM ;
837
+ private int recursionLimit = TruffleOptions . AOT ? NATIVE_REC_LIM : REC_LIM ;
856
838
private int checkInterval = 100 ;
857
839
private double switchInterval = 0.005 ;
858
840
@@ -1040,10 +1022,10 @@ private void initializePython3Core(TruffleString coreHome) {
1040
1022
/**
1041
1023
* Run post-initialization code that needs a fully working Python environment. This will be run
1042
1024
* eagerly when the context is initialized on the JVM or a new context is created on SVM, but is
1043
- * omitted when the native image is generated .
1025
+ * omitted when creating a pre-initialized context .
1044
1026
*/
1045
- public final void postInitialize () {
1046
- if (!ImageInfo . inImageBuildtimeCode () || ImageInfo . inImageRuntimeCode ()) {
1027
+ public final void postInitialize (Env env ) {
1028
+ if (!env . isPreInitialization ()) {
1047
1029
initialized = false ;
1048
1030
1049
1031
for (PythonBuiltins builtin : builtins ) {
@@ -1060,9 +1042,9 @@ public final void postInitialize() {
1060
1042
* fallback to another _bz2 implementation (e.g. LLVM or maybe some Java lib). This
1061
1043
* needs to be done here and cannot be done in 'initializeBuiltins' because then we
1062
1044
* would never include the intrinsified _bz2 module in the native image since native
1063
- * access is never allowed during native image build time .
1045
+ * access is never allowed during context pre-initialization .
1064
1046
*/
1065
- if (!PythonImageBuildOptions .WITHOUT_COMPRESSION_LIBRARIES && ImageInfo . inImageCode () && !getContext ().isNativeAccessAllowed ()) {
1047
+ if (!PythonImageBuildOptions .WITHOUT_COMPRESSION_LIBRARIES && TruffleOptions . AOT && !getContext ().isNativeAccessAllowed ()) {
1066
1048
removeBuiltinModule (BuiltinNames .T_BZ2 );
1067
1049
}
1068
1050
0 commit comments