Skip to content

Commit c39feb6

Browse files
committed
[GR-33648] Intrinsify libgraalpython/sys_post_init.
PullRequest: graalpython/1970
2 parents a8c0d42 + 0d6da13 commit c39feb6

File tree

12 files changed

+244
-195
lines changed

12 files changed

+244
-195
lines changed

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -135,17 +135,6 @@ def test_recursive_import_from():
135135
import package.recpkg
136136
assert package.recpkg.context is package.recpkg.reduction.context
137137

138-
139-
if sys.implementation.name == "graalpython":
140-
def test_imp_cached_imports():
141-
import _imp
142-
143-
finder = _imp.CachedImportFinder
144-
145-
spec = finder.find_spec("encodings", None)
146-
assert spec.submodule_search_locations
147-
148-
149138
def test_import_package_all() :
150139
import package1
151140
expected_syms = ["moduleX", "lib1_hello", "lib1_world"]

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

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -250,11 +250,9 @@
250250
import com.oracle.graal.python.builtins.objects.type.TypeBuiltins;
251251
import com.oracle.graal.python.builtins.objects.type.TypeNodes.GetNameNode;
252252
import com.oracle.graal.python.builtins.objects.zipimporter.ZipImporterBuiltins;
253-
import com.oracle.graal.python.lib.PyObjectCallMethodObjArgs;
254253
import com.oracle.graal.python.lib.PyObjectLookupAttr;
255254
import com.oracle.graal.python.nodes.BuiltinNames;
256255
import com.oracle.graal.python.nodes.call.GenericInvokeNode;
257-
import com.oracle.graal.python.nodes.statement.AbstractImportNode;
258256
import com.oracle.graal.python.runtime.PythonCodeSerializer;
259257
import com.oracle.graal.python.runtime.PythonContext;
260258
import com.oracle.graal.python.runtime.PythonOptions;
@@ -318,10 +316,6 @@ private static String[] initializeCoreFiles() {
318316
"zipimport",
319317
"mmap",
320318
"java",
321-
// TODO: see the encodings initialization before sys_post_init.py is
322-
// loaded in initializePython3Core;
323-
// once sys_post_init.py is gone, it should not be necessary
324-
"sys_post_init",
325319
"_contextvars",
326320
"pip_hook",
327321
"_struct",
@@ -670,26 +664,11 @@ private void initializeJavaCore() {
670664
private void initializePython3Core(String coreHome) {
671665
loadFile(BuiltinNames.BUILTINS, coreHome);
672666
for (String s : coreFiles) {
673-
// TODO: once sys_post_init.py is gone, this should not be necessary
674-
if (s.equals("sys_post_init")) {
675-
importEncoding();
676-
}
677667
loadFile(s, coreHome);
678668
}
679669
initialized = true;
680670
}
681671

682-
private void importEncoding() {
683-
PythonModule sys = lookupBuiltinModule("sys");
684-
Object sysPath = sys.getAttribute("path");
685-
PyObjectCallMethodObjArgs.getUncached().execute(null, sysPath, "insert", 0, getContext().getStdlibHome());
686-
try {
687-
AbstractImportNode.importModule("encodings");
688-
} finally {
689-
PyObjectCallMethodObjArgs.getUncached().execute(null, sysPath, "pop");
690-
}
691-
}
692-
693672
/**
694673
* Run post-initialization code that needs a fully working Python environment. This will be run
695674
* eagerly when the context is initialized on the JVM or a new context is created on SVM, but is

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

Lines changed: 64 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
import com.oracle.graal.python.annotations.ArgumentClinic;
7373
import com.oracle.graal.python.builtins.Builtin;
7474
import com.oracle.graal.python.builtins.CoreFunctions;
75+
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
7576
import com.oracle.graal.python.builtins.PythonBuiltins;
7677
import com.oracle.graal.python.builtins.objects.PNone;
7778
import com.oracle.graal.python.builtins.objects.buffer.PythonBufferAccessLibrary;
@@ -87,10 +88,12 @@
8788
import com.oracle.graal.python.builtins.objects.exception.PBaseException;
8889
import com.oracle.graal.python.builtins.objects.module.PythonModule;
8990
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
90-
import com.oracle.graal.python.builtins.objects.tuple.TupleBuiltins;
9191
import com.oracle.graal.python.lib.PyCallableCheckNode;
92+
import com.oracle.graal.python.lib.PyObjectSizeNode;
93+
import com.oracle.graal.python.lib.PyObjectTypeCheck;
9294
import static com.oracle.graal.python.nodes.BuiltinNames.ENCODE;
9395
import com.oracle.graal.python.nodes.ErrorMessages;
96+
import com.oracle.graal.python.nodes.PNodeWithContext;
9497
import com.oracle.graal.python.nodes.PNodeWithRaise;
9598
import com.oracle.graal.python.nodes.PRaiseNode;
9699
import static com.oracle.graal.python.nodes.SpecialMethodNames.DECODE;
@@ -111,6 +114,7 @@
111114
import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
112115
import com.oracle.graal.python.runtime.PythonContext;
113116
import com.oracle.graal.python.runtime.exception.PException;
117+
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
114118
import com.oracle.graal.python.util.CharsetMapping;
115119
import com.oracle.truffle.api.CompilerDirectives;
116120
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
@@ -120,6 +124,7 @@
120124
import com.oracle.truffle.api.dsl.GenerateUncached;
121125
import com.oracle.truffle.api.dsl.NodeFactory;
122126
import com.oracle.truffle.api.dsl.Specialization;
127+
import com.oracle.truffle.api.frame.Frame;
123128
import com.oracle.truffle.api.frame.VirtualFrame;
124129
import com.oracle.truffle.api.library.CachedLibrary;
125130
import com.oracle.truffle.api.nodes.Node;
@@ -551,15 +556,32 @@ protected ArgumentClinicProvider getArgumentClinic() {
551556
return CodecsModuleBuiltinsClinicProviders.CodecsDecodeNodeClinicProviderGen.INSTANCE;
552557
}
553558

559+
@Specialization
560+
Object decode(PBytesLike input, Object encoding, Object errors, Object finalData,
561+
@Cached InternalCodecsDecodeNode internalNode) {
562+
return internalNode.execute(input, encoding, errors, finalData);
563+
}
564+
}
565+
566+
@GenerateUncached
567+
public abstract static class InternalCodecsDecodeNode extends PNodeWithContext {
568+
abstract Object execute(Object input, Object encoding, Object errors, Object finalData);
569+
570+
public final Object call(Object input, Object encoding, Object errors, Object finalData) {
571+
return execute(input, encoding, errors, finalData);
572+
}
573+
554574
@Specialization
555575
Object decode(PBytesLike input, String encoding, String errors, boolean finalData,
556576
@Cached GetInternalByteArrayNode getBytes,
557-
@Cached HandleDecodingErrorNode errorHandler) {
577+
@Cached HandleDecodingErrorNode errorHandler,
578+
@Cached PRaiseNode raiseNode,
579+
@Cached PythonObjectFactory factory) {
558580
byte[] bytes = getBytes.execute(input.getSequenceStorage());
559581
CodingErrorAction errorAction = convertCodingErrorAction(errors);
560582
Charset charset = CharsetMapping.getCharset(encoding);
561583
if (charset == null) {
562-
throw raise(LookupError, ErrorMessages.UNKNOWN_ENCODING, encoding);
584+
throw raiseNode.raise(LookupError, ErrorMessages.UNKNOWN_ENCODING, encoding);
563585
}
564586
TruffleDecoder decoder;
565587
try {
@@ -569,14 +591,15 @@ Object decode(PBytesLike input, String encoding, String errors, boolean finalDat
569591
}
570592
} catch (OutOfMemoryError e) {
571593
CompilerDirectives.transferToInterpreterAndInvalidate();
572-
throw raise(MemoryError);
594+
throw raiseNode.raise(MemoryError);
573595
}
574-
return factory().createTuple(new Object[]{decoder.getString(), decoder.getInputPosition()});
596+
return factory.createTuple(new Object[]{decoder.getString(), decoder.getInputPosition()});
575597
}
576598

577599
@Fallback
578-
Object decode(Object bytes, @SuppressWarnings("unused") Object encoding, @SuppressWarnings("unused") Object errors, @SuppressWarnings("unused") Object finalData) {
579-
throw raise(TypeError, BYTESLIKE_OBJ_REQUIRED, bytes);
600+
Object decode(Object bytes, @SuppressWarnings("unused") Object encoding, @SuppressWarnings("unused") Object errors, @SuppressWarnings("unused") Object finalData,
601+
@Cached PRaiseNode raiseNode) {
602+
throw raiseNode.raise(TypeError, BYTESLIKE_OBJ_REQUIRED, bytes);
580603
}
581604
}
582605

@@ -822,25 +845,40 @@ private static boolean hasTruffleEncoding(String encoding) {
822845
@Builtin(name = "lookup", minNumOfPositionalArgs = 1)
823846
@GenerateNodeFactory
824847
abstract static class LookupNode extends PythonUnaryBuiltinNode {
848+
@Specialization
849+
PTuple lookup(VirtualFrame frame, Object encoding,
850+
@Cached InternalLookupNode internalNode) {
851+
return internalNode.execute(frame, encoding);
852+
}
853+
}
854+
855+
@GenerateUncached
856+
abstract static class InternalLookupNode extends PNodeWithContext {
857+
abstract PTuple execute(Frame frame, Object encoding);
858+
825859
@Specialization
826860
PTuple lookup(VirtualFrame frame, PBytesLike encoding,
827-
@Cached AsciiDecodeNode asciiDecodeNode,
861+
@Cached InternalCodecsDecodeNode decodeNode,
862+
@Cached PyObjectTypeCheck typeCheck,
828863
@Cached CallUnaryMethodNode callNode,
829-
@Cached TupleBuiltins.LenNode lenNode,
864+
@Cached PyObjectSizeNode sizeNode,
830865
@Cached ConditionProfile hasSearchPathProfile,
831866
@Cached ConditionProfile hasTruffleEncodingProfile,
832-
@Cached ConditionProfile isTupleProfile) {
833-
String decoded = (String) ((PTuple) asciiDecodeNode.execute(frame, encoding, PNone.NO_VALUE)).getSequenceStorage().getInternalArray()[0];
834-
return lookup(frame, decoded, callNode, lenNode, hasSearchPathProfile, hasTruffleEncodingProfile, isTupleProfile);
867+
@Cached ConditionProfile isTupleProfile,
868+
@Cached PRaiseNode raiseNode) {
869+
String decoded = (String) ((PTuple) decodeNode.execute(encoding, "ascii", PNone.NO_VALUE, true)).getSequenceStorage().getInternalArray()[0];
870+
return lookup(frame, decoded, callNode, typeCheck, sizeNode, hasSearchPathProfile, hasTruffleEncodingProfile, isTupleProfile, raiseNode);
835871
}
836872

837873
@Specialization
838874
PTuple lookup(VirtualFrame frame, String encoding,
839875
@Cached CallUnaryMethodNode callNode,
840-
@Cached TupleBuiltins.LenNode lenNode,
876+
@Cached PyObjectTypeCheck typeCheck,
877+
@Cached PyObjectSizeNode sizeNode,
841878
@Cached ConditionProfile hasSearchPathProfile,
842879
@Cached ConditionProfile hasTruffleEncodingProfile,
843-
@Cached ConditionProfile isTupleProfile) {
880+
@Cached ConditionProfile isTupleProfile,
881+
@Cached PRaiseNode raiseNode) {
844882
String normalized_encoding = normalizeString(encoding);
845883
PythonContext context = getContext();
846884
PTuple result = getSearchPath(context, normalized_encoding);
@@ -849,13 +887,13 @@ PTuple lookup(VirtualFrame frame, String encoding,
849887
}
850888
if (hasTruffleEncodingProfile.profile(hasTruffleEncoding(encoding))) {
851889
PythonModule codecs = context.getCore().lookupBuiltinModule("_codecs_truffle");
852-
result = CodecsTruffleModuleBuiltins.codecsInfo(codecs, encoding, context, factory());
890+
result = CodecsTruffleModuleBuiltins.codecsInfo(codecs, encoding, context, getContext().getCore().factory());
853891
} else {
854892
for (Object func : getSearchPaths(context)) {
855893
Object obj = callNode.executeObject(func, normalized_encoding);
856894
if (obj != PNone.NONE) {
857-
if (isTupleProfile.profile(!isTupleInstanceCheck(frame, obj, 4, lenNode))) {
858-
throw raise(TypeError, CODEC_SEARCH_MUST_RETURN_4);
895+
if (isTupleProfile.profile(!isTupleInstanceCheck(frame, obj, 4, typeCheck, sizeNode))) {
896+
throw raiseNode.raise(TypeError, CODEC_SEARCH_MUST_RETURN_4);
859897
}
860898
result = (PTuple) obj;
861899
break;
@@ -866,7 +904,7 @@ PTuple lookup(VirtualFrame frame, String encoding,
866904
putSearchPath(context, normalized_encoding, result);
867905
return result;
868906
}
869-
throw raise(LookupError, UNKNOWN_ENCODING, encoding);
907+
throw raiseNode.raise(LookupError, UNKNOWN_ENCODING, encoding);
870908
}
871909

872910
@TruffleBoundary
@@ -886,8 +924,8 @@ private static Object[] getSearchPaths(PythonContext ctx) {
886924
}
887925
}
888926

889-
private static boolean isTupleInstanceCheck(VirtualFrame frame, Object result, int len, TupleBuiltins.LenNode lenNode) throws PException {
890-
return (result instanceof PTuple) && ((int) lenNode.execute(frame, result) == len);
927+
private static boolean isTupleInstanceCheck(VirtualFrame frame, Object result, int len, PyObjectTypeCheck typeCheck, PyObjectSizeNode sizeNode) throws PException {
928+
return typeCheck.execute(result, PythonBuiltinClassType.PTuple) && sizeNode.execute(frame, result) == len;
891929
}
892930

893931
@TruffleBoundary
@@ -1024,11 +1062,12 @@ Object encode(VirtualFrame frame, Object obj, String encoding, String errors,
10241062
@Cached SequenceStorageNodes.GetItemNode getResultItemNode,
10251063
@Cached LookupNode lookupNode,
10261064
@Cached CallBinaryMethodNode callEncoderNode,
1027-
@Cached TupleBuiltins.LenNode lenNode,
1065+
@Cached PyObjectSizeNode sizeNode,
1066+
@Cached PyObjectTypeCheck typeCheck,
10281067
@Cached ConditionProfile isTupleProfile) {
10291068
Object encoder = CodecsModuleBuiltins.encoder(frame, encoding, lookupNode, getItemNode);
10301069
Object result = callEncoderNode.executeObject(encoder, obj, errors);
1031-
if (isTupleProfile.profile(!isTupleInstanceCheck(frame, result, 2, lenNode))) {
1070+
if (isTupleProfile.profile(!isTupleInstanceCheck(frame, result, 2, typeCheck, sizeNode))) {
10321071
throw raise(TypeError, S_MUST_RETURN_TUPLE, "encoder");
10331072
}
10341073
return getResultItemNode.execute(frame, ((PTuple) result).getSequenceStorage(), 0);
@@ -1052,11 +1091,12 @@ Object decode(VirtualFrame frame, Object obj, String encoding, String errors,
10521091
@Cached SequenceStorageNodes.GetItemNode getResultItemNode,
10531092
@Cached LookupNode lookupNode,
10541093
@Cached CallBinaryMethodNode callEncoderNode,
1055-
@Cached TupleBuiltins.LenNode lenNode,
1094+
@Cached PyObjectSizeNode sizeNode,
1095+
@Cached PyObjectTypeCheck typeCheck,
10561096
@Cached ConditionProfile isTupleProfile) {
10571097
Object decoder = CodecsModuleBuiltins.decoder(frame, encoding, lookupNode, getItemNode);
10581098
Object result = callEncoderNode.executeObject(decoder, obj, errors);
1059-
if (isTupleProfile.profile(!isTupleInstanceCheck(frame, result, 2, lenNode))) {
1099+
if (isTupleProfile.profile(!isTupleInstanceCheck(frame, result, 2, typeCheck, sizeNode))) {
10601100
throw raise(TypeError, S_MUST_RETURN_TUPLE, "decoder");
10611101
}
10621102
return getResultItemNode.execute(frame, ((PTuple) result).getSequenceStorage(), 0);

0 commit comments

Comments
 (0)