Skip to content

Commit 66c2d7b

Browse files
committed
[GR-31276] More lib nodes for import intrinsification
PullRequest: graalpython/1823
2 parents 9eb3f7e + 5fa851a commit 66c2d7b

File tree

81 files changed

+891
-335
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+891
-335
lines changed

ci.jsonnet

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{ "overlay": "66031a14f4fa7802efbd25dc758e08a4c94f2579" }
1+
{ "overlay": "678366cf6c0b054e751248aec4d939526f029a21" }

graalpython/com.oracle.graal.python.benchmarks/java/com/oracle/graal/python/benchmarks/parser/ParserBenchRunner.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
import com.oracle.graal.python.parser.PythonParserImpl;
4747
import com.oracle.graal.python.parser.PythonSSTNodeFactory;
4848
import com.oracle.graal.python.runtime.PythonContext;
49-
import com.oracle.graal.python.runtime.PythonCore;
49+
import com.oracle.graal.python.builtins.Python3Core;
5050
import com.oracle.graal.python.runtime.PythonParser;
5151
import com.oracle.truffle.api.TruffleFile;
5252
import com.oracle.truffle.api.source.Source;
@@ -98,7 +98,7 @@ public class ParserBenchRunner {
9898

9999
protected final PythonContext pyContext;
100100
protected final PythonParserImpl parser;
101-
protected final PythonCore core;
101+
protected final Python3Core core;
102102
private List<Source> sources;
103103

104104
public ParserBenchRunner() {

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/advance/CustomModule.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
import com.oracle.graal.python.builtins.CoreFunctions;
4747
import com.oracle.graal.python.builtins.PythonBuiltins;
4848
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
49-
import com.oracle.graal.python.runtime.PythonCore;
49+
import com.oracle.graal.python.builtins.Python3Core;
5050
import com.oracle.truffle.api.dsl.NodeFactory;
5151

5252
@CoreFunctions(defineModule = "CustomModule")
@@ -58,7 +58,7 @@ protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFa
5858
}
5959

6060
@Override
61-
public void initialize(PythonCore core) {
61+
public void initialize(Python3Core core) {
6262
super.initialize(core);
6363
this.builtinConstants.put("success", "success");
6464
}

graalpython/com.oracle.graal.python.test/src/tests/test_signal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,4 @@ def handler(signal, frame):
7575
time.sleep(0.5)
7676

7777
assert triggered[0] == _signal.SIGALRM
78-
assert triggered[1].f_code.co_name == "test_alarm2", triggered[1].f_code
78+
assert triggered[1].f_code.co_name # just check that we have access to the frame

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/PythonLanguage.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959
import com.oracle.graal.python.runtime.GilNode;
6060
import com.oracle.graal.python.runtime.PythonContext;
6161
import com.oracle.graal.python.runtime.PythonContext.PythonThreadState;
62-
import com.oracle.graal.python.runtime.PythonCore;
6362
import com.oracle.graal.python.runtime.PythonOptions;
6463
import com.oracle.graal.python.runtime.PythonParser.ParserMode;
6564
import com.oracle.graal.python.runtime.exception.PException;
@@ -240,11 +239,11 @@ protected boolean areOptionsCompatible(OptionValues firstOptions, OptionValues n
240239
@Override
241240
protected boolean patchContext(PythonContext context, Env newEnv) {
242241
if (!areOptionsCompatible(context.getEnv().getOptions(), newEnv.getOptions())) {
243-
PythonCore.writeInfo("Cannot use preinitialized context.");
242+
Python3Core.writeInfo("Cannot use preinitialized context.");
244243
return false;
245244
}
246245
context.initializeHomeAndPrefixPaths(newEnv, getLanguageHome());
247-
PythonCore.writeInfo("Using preinitialized context.");
246+
Python3Core.writeInfo("Using preinitialized context.");
248247
context.patch(newEnv);
249248
return true;
250249
}
@@ -313,7 +312,7 @@ public static String getEvalMimeType(int optimize) {
313312
@Override
314313
protected CallTarget parse(ParsingRequest request) {
315314
PythonContext context = getCurrentContext(PythonLanguage.class);
316-
PythonCore core = context.getCore();
315+
Python3Core core = context.getCore();
317316
Source source = request.getSource();
318317
if (source.getMimeType() == null || MIME_TYPE.equals(source.getMimeType())) {
319318
if (!request.getArgumentNames().isEmpty()) {
@@ -377,7 +376,7 @@ private RootNode doParse(PythonContext context, Source source, int optimize) {
377376
// by default we assume a module
378377
mode = ParserMode.File;
379378
}
380-
PythonCore pythonCore = context.getCore();
379+
Python3Core pythonCore = context.getCore();
381380
try {
382381
return (RootNode) pythonCore.getParser().parse(mode, optimize, pythonCore, source, null, null);
383382
} catch (PException e) {
@@ -498,7 +497,7 @@ private Object parseAndEval(PythonContext context, MaterializedFrame frame) {
498497

499498
@TruffleBoundary
500499
protected static ExpressionNode parseInline(Source code, PythonContext context, MaterializedFrame lexicalContextFrame) {
501-
PythonCore pythonCore = context.getCore();
500+
Python3Core pythonCore = context.getCore();
502501
return (ExpressionNode) pythonCore.getParser().parse(ParserMode.InlineEvaluation, 0, pythonCore, code, lexicalContextFrame, null);
503502
}
504503

@@ -578,7 +577,7 @@ public static PythonContext getContext() {
578577
return getCurrentContext(PythonLanguage.class);
579578
}
580579

581-
public static PythonCore getCore() {
580+
public static Python3Core getCore() {
582581
return getCurrentContext(PythonLanguage.class).getCore();
583582
}
584583

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/Python3Core.java

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,9 @@
225225
import com.oracle.graal.python.nodes.call.GenericInvokeNode;
226226
import com.oracle.graal.python.runtime.PythonCodeSerializer;
227227
import com.oracle.graal.python.runtime.PythonContext;
228-
import com.oracle.graal.python.runtime.PythonCore;
229228
import com.oracle.graal.python.runtime.PythonOptions;
230229
import com.oracle.graal.python.runtime.PythonParser;
230+
import com.oracle.graal.python.runtime.PythonParser.ParserErrorCallback;
231231
import com.oracle.graal.python.runtime.PythonParser.ParserMode;
232232
import com.oracle.graal.python.runtime.exception.PException;
233233
import com.oracle.graal.python.runtime.formatting.ErrorMessageFormatter;
@@ -252,7 +252,7 @@
252252
* The core is intended to the immutable part of the interpreter, including most modules and most
253253
* types.
254254
*/
255-
public final class Python3Core implements PythonCore {
255+
public final class Python3Core implements ParserErrorCallback {
256256
private static final TruffleLogger LOGGER = PythonLanguage.getLogger(Python3Core.class);
257257
private final String[] coreFiles;
258258

@@ -535,6 +535,8 @@ private static PythonBuiltins[] initializeBuiltins(boolean nativeAccessAllowed)
535535

536536
private final Map<String, PythonModule> builtinModules = new HashMap<>();
537537
@CompilationFinal private PythonModule builtinsModule;
538+
@CompilationFinal private PythonModule sysModule;
539+
@CompilationFinal private PDict sysModules;
538540

539541
@CompilationFinal private PInt pyTrue;
540542
@CompilationFinal private PInt pyFalse;
@@ -563,31 +565,32 @@ public PythonLanguage getLanguage() {
563565
return singletonContext.getLanguage();
564566
}
565567

566-
@Override
567568
public PythonContext getContext() {
568569
return singletonContext;
569570
}
570571

571-
@Override
572572
public PythonParser getParser() {
573573
return parser;
574574
}
575575

576-
@Override
577576
public PythonCodeSerializer getSerializer() {
578577
return (PythonCodeSerializer) parser;
579578
}
580579

581-
@Override
580+
/**
581+
* Checks whether the core is initialized.
582+
*/
582583
public boolean isInitialized() {
583584
return initialized;
584585
}
585586

586-
@Override
587+
/**
588+
* Load the core library and prepare all builtin classes and modules.
589+
*/
587590
public void initialize(PythonContext context) {
588591
singletonContext = context;
589592
initializeJavaCore();
590-
initializePythonCore(context.getCoreHomeOrFail());
593+
initializePython3Core(context.getCoreHomeOrFail());
591594
assert SpecialMethodSlot.checkSlotOverrides(this);
592595
initialized = true;
593596
}
@@ -600,15 +603,19 @@ private void initializeJavaCore() {
600603
builtinsModule = builtinModules.get(BuiltinNames.BUILTINS);
601604
}
602605

603-
private void initializePythonCore(String coreHome) {
606+
private void initializePython3Core(String coreHome) {
604607
loadFile(BuiltinNames.BUILTINS, coreHome);
605608
for (String s : coreFiles) {
606609
loadFile(s, coreHome);
607610
}
608611
initialized = true;
609612
}
610613

611-
@Override
614+
/**
615+
* Run post-initialization code that needs a fully working Python environment. This will be run
616+
* eagerly when the context is initialized on the JVM or a new context is created on SVM, but is
617+
* omitted when the native image is generated.
618+
*/
612619
public void postInitialize() {
613620
if (!TruffleOptions.AOT || ImageInfo.inImageRuntimeCode()) {
614621
initialized = false;
@@ -622,29 +629,33 @@ public void postInitialize() {
622629
}
623630
}
624631

625-
@Override
626632
@TruffleBoundary
627633
public PythonModule lookupBuiltinModule(String name) {
628634
return builtinModules.get(name);
629635
}
630636

631-
@Override
632637
public PythonBuiltinClass lookupType(PythonBuiltinClassType type) {
633638
assert builtinTypes[type.ordinal()] != null;
634639
return builtinTypes[type.ordinal()];
635640
}
636641

637-
@Override
638642
@TruffleBoundary
639643
public String[] builtinModuleNames() {
640644
return builtinModules.keySet().toArray(PythonUtils.EMPTY_STRING_ARRAY);
641645
}
642646

643-
@Override
644647
public PythonModule getBuiltins() {
645648
return builtinsModule;
646649
}
647650

651+
public PythonModule getSysModule() {
652+
return sysModule;
653+
}
654+
655+
public PDict getSysModules() {
656+
return sysModules;
657+
}
658+
648659
@Override
649660
@TruffleBoundary
650661
public PException raise(PythonBuiltinClassType type, String format, Object... args) {
@@ -663,11 +674,12 @@ public void warn(PythonBuiltinClassType type, String format, Object... args) {
663674
WarningsModuleBuiltins.WarnNode.getUncached().warnFormat(null, null, type, 1, format, args);
664675
}
665676

666-
@Override
677+
/**
678+
* Returns the stderr object or signals error when stderr is "lost".
679+
*/
667680
public Object getStderr() {
668-
PythonModule sys = lookupBuiltinModule("sys");
669681
try {
670-
return PythonObjectLibrary.getUncached().lookupAttribute(sys, null, "stderr");
682+
return PythonObjectLibrary.getUncached().lookupAttribute(sysModule, null, "stderr");
671683
} catch (PException e) {
672684
try {
673685
getContext().getEnv().err().write("lost sys.stderr\n".getBytes());
@@ -679,8 +691,8 @@ public Object getStderr() {
679691
}
680692

681693
private void publishBuiltinModules() {
682-
PythonModule sysModule = builtinModules.get("sys");
683-
PDict sysModules = (PDict) sysModule.getAttribute("modules");
694+
sysModule = builtinModules.get("sys");
695+
sysModules = (PDict) sysModule.getAttribute("modules");
684696
for (Entry<String, PythonModule> entry : builtinModules.entrySet()) {
685697
sysModules.setItem(entry.getKey(), entry.getValue());
686698
}
@@ -823,28 +835,23 @@ private void loadFile(String s, String prefix) {
823835
GenericInvokeNode.getUncached().execute(callTarget, PArguments.withGlobals(mod));
824836
}
825837

826-
@Override
827838
public PythonObjectFactory factory() {
828839
return objectFactory;
829840
}
830841

831-
@Override
832842
public void setContext(PythonContext context) {
833843
assert singletonContext == null;
834844
singletonContext = context;
835845
}
836846

837-
@Override
838847
public PInt getTrue() {
839848
return pyTrue;
840849
}
841850

842-
@Override
843851
public PInt getFalse() {
844852
return pyFalse;
845853
}
846854

847-
@Override
848855
public PFloat getNaN() {
849856
return pyNaN;
850857
}
@@ -911,4 +918,12 @@ public RuntimeException raiseInvalidSyntax(PythonParser.ErrorType type, Node loc
911918
instance.setAttribute("msg", msg);
912919
throw PException.fromObject(instance, location, PythonOptions.isPExceptionWithJavaStacktrace(getLanguage()));
913920
}
921+
922+
public static final void writeInfo(String message) {
923+
PythonLanguage.getLogger(Python3Core.class).fine(message);
924+
}
925+
926+
public static final void writeInfo(Supplier<String> messageSupplier) {
927+
PythonLanguage.getLogger(Python3Core.class).fine(messageSupplier);
928+
}
914929
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/PythonBuiltins.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import com.oracle.graal.python.builtins.objects.type.PythonBuiltinClass;
4040
import com.oracle.graal.python.nodes.function.BuiltinFunctionRootNode;
4141
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
42-
import com.oracle.graal.python.runtime.PythonCore;
4342
import com.oracle.graal.python.util.BiConsumer;
4443
import com.oracle.truffle.api.RootCallTarget;
4544
import com.oracle.truffle.api.dsl.NodeFactory;
@@ -57,7 +56,7 @@ public abstract class PythonBuiltins {
5756
* {@link #builtinConstants} or such should be made before calling
5857
* {@code super.initialize(core)}.
5958
*/
60-
public void initialize(PythonCore core) {
59+
public void initialize(Python3Core core) {
6160
if (builtinFunctions.size() > 0) {
6261
return;
6362
}
@@ -108,7 +107,7 @@ public void initialize(PythonCore core) {
108107
* Run any actions that can only be run in the post-initialization step, that is, if we're
109108
* actually going to start running rather than just pre-initializing.
110109
*/
111-
public void postInitialize(@SuppressWarnings("unused") PythonCore core) {
110+
public void postInitialize(@SuppressWarnings("unused") Python3Core core) {
112111
// nothing to do by default
113112
}
114113

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/ArrayModuleBuiltins.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
6666
import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
6767
import com.oracle.graal.python.nodes.util.SplitArgsNode;
68-
import com.oracle.graal.python.runtime.PythonCore;
68+
import com.oracle.graal.python.builtins.Python3Core;
6969
import com.oracle.graal.python.runtime.exception.PException;
7070
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
7171
import com.oracle.graal.python.runtime.sequence.PSequence;
@@ -95,7 +95,7 @@ protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFa
9595
}
9696

9797
@Override
98-
public void postInitialize(PythonCore core) {
98+
public void postInitialize(Python3Core core) {
9999
super.postInitialize(core);
100100
PythonModule arrayModule = core.lookupBuiltinModule("array");
101101
arrayModule.setAttribute("ArrayType", core.lookupType(PythonBuiltinClassType.PArray));

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinConstructors.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@
243243
import com.oracle.graal.python.parser.PythonSSTNodeFactory;
244244
import com.oracle.graal.python.runtime.ExecutionContext.IndirectCallContext;
245245
import com.oracle.graal.python.runtime.PythonContext;
246-
import com.oracle.graal.python.runtime.PythonCore;
246+
import com.oracle.graal.python.builtins.Python3Core;
247247
import com.oracle.graal.python.runtime.exception.PException;
248248
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
249249
import com.oracle.graal.python.runtime.sequence.storage.ObjectSequenceStorage;
@@ -283,7 +283,7 @@ protected List<com.oracle.truffle.api.dsl.NodeFactory<? extends PythonBuiltinBas
283283
}
284284

285285
@Override
286-
public void initialize(PythonCore core) {
286+
public void initialize(Python3Core core) {
287287
super.initialize(core);
288288
builtinConstants.put("NotImplemented", PNotImplemented.NOT_IMPLEMENTED);
289289
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@
183183
import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
184184
import com.oracle.graal.python.runtime.ExecutionContext.IndirectCallContext;
185185
import com.oracle.graal.python.runtime.PythonContext;
186-
import com.oracle.graal.python.runtime.PythonCore;
186+
import com.oracle.graal.python.builtins.Python3Core;
187187
import com.oracle.graal.python.runtime.PythonOptions;
188188
import com.oracle.graal.python.runtime.PythonParser.ParserMode;
189189
import com.oracle.graal.python.runtime.exception.PException;
@@ -234,13 +234,13 @@ protected List<com.oracle.truffle.api.dsl.NodeFactory<? extends PythonBuiltinBas
234234
}
235235

236236
@Override
237-
public void initialize(PythonCore core) {
237+
public void initialize(Python3Core core) {
238238
builtinConstants.put(__GRAALPYTHON__, core.lookupBuiltinModule(__GRAALPYTHON__));
239239
super.initialize(core);
240240
}
241241

242242
@Override
243-
public void postInitialize(PythonCore core) {
243+
public void postInitialize(Python3Core core) {
244244
super.postInitialize(core);
245245
PythonModule builtinsModule = core.lookupBuiltinModule(BuiltinNames.BUILTINS);
246246
builtinsModule.setAttribute(__DEBUG__, !core.getContext().getOption(PythonOptions.PythonOptimizeFlag));

0 commit comments

Comments
 (0)