|
43 | 43 | import static com.oracle.graal.python.builtins.PythonBuiltinClassType.NotImplementedError;
|
44 | 44 | import static com.oracle.graal.python.builtins.objects.bytes.BytesUtils.HEXDIGITS;
|
45 | 45 | import static com.oracle.graal.python.builtins.objects.bytes.BytesUtils.digitValue;
|
| 46 | +import static com.oracle.graal.python.nodes.BuiltinNames.ENCODE; |
46 | 47 | import static com.oracle.graal.python.nodes.BuiltinNames._CODECS;
|
47 | 48 | import static com.oracle.graal.python.nodes.ErrorMessages.ARG_MUST_BE_CALLABLE;
|
48 | 49 | import static com.oracle.graal.python.nodes.ErrorMessages.BYTESLIKE_OBJ_REQUIRED;
|
|
53 | 54 | import static com.oracle.graal.python.nodes.ErrorMessages.S_MUST_RETURN_TUPLE;
|
54 | 55 | import static com.oracle.graal.python.nodes.ErrorMessages.UNKNOWN_ENCODING;
|
55 | 56 | import static com.oracle.graal.python.nodes.ErrorMessages.UNKNOWN_ERROR_HANDLER;
|
| 57 | +import static com.oracle.graal.python.nodes.SpecialMethodNames.DECODE; |
56 | 58 | import static com.oracle.graal.python.runtime.exception.PythonErrorType.LookupError;
|
57 | 59 | import static com.oracle.graal.python.runtime.exception.PythonErrorType.MemoryError;
|
58 | 60 | import static com.oracle.graal.python.runtime.exception.PythonErrorType.TypeError;
|
|
76 | 78 | import com.oracle.graal.python.builtins.PythonBuiltins;
|
77 | 79 | import com.oracle.graal.python.builtins.objects.PNone;
|
78 | 80 | import com.oracle.graal.python.builtins.objects.buffer.PythonBufferAccessLibrary;
|
| 81 | +import com.oracle.graal.python.builtins.objects.buffer.PythonBufferAcquireLibrary; |
79 | 82 | import com.oracle.graal.python.builtins.objects.bytes.ByteArrayBuffer;
|
80 | 83 | import com.oracle.graal.python.builtins.objects.bytes.BytesUtils;
|
81 | 84 | import com.oracle.graal.python.builtins.objects.bytes.PBytes;
|
|
91 | 94 | import com.oracle.graal.python.lib.PyCallableCheckNode;
|
92 | 95 | import com.oracle.graal.python.lib.PyObjectSizeNode;
|
93 | 96 | import com.oracle.graal.python.lib.PyObjectTypeCheck;
|
94 |
| -import static com.oracle.graal.python.nodes.BuiltinNames.ENCODE; |
95 | 97 | import com.oracle.graal.python.nodes.ErrorMessages;
|
96 | 98 | import com.oracle.graal.python.nodes.PNodeWithContext;
|
97 | 99 | import com.oracle.graal.python.nodes.PNodeWithRaise;
|
| 100 | +import com.oracle.graal.python.nodes.PNodeWithRaiseAndIndirectCall; |
98 | 101 | import com.oracle.graal.python.nodes.PRaiseNode;
|
99 |
| -import static com.oracle.graal.python.nodes.SpecialMethodNames.DECODE; |
100 | 102 | import com.oracle.graal.python.nodes.call.CallNode;
|
101 | 103 | import com.oracle.graal.python.nodes.call.special.CallBinaryMethodNode;
|
102 | 104 | import com.oracle.graal.python.nodes.call.special.CallUnaryMethodNode;
|
|
108 | 110 | import com.oracle.graal.python.nodes.function.builtins.PythonQuaternaryClinicBuiltinNode;
|
109 | 111 | import com.oracle.graal.python.nodes.function.builtins.PythonTernaryBuiltinNode;
|
110 | 112 | import com.oracle.graal.python.nodes.function.builtins.PythonTernaryClinicBuiltinNode;
|
111 |
| -import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode; |
112 | 113 | import com.oracle.graal.python.nodes.function.builtins.PythonUnaryClinicBuiltinNode;
|
113 | 114 | import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentClinicProvider;
|
114 | 115 | import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
|
@@ -557,49 +558,50 @@ protected ArgumentClinicProvider getArgumentClinic() {
|
557 | 558 | }
|
558 | 559 |
|
559 | 560 | @Specialization
|
560 |
| - Object decode(PBytesLike input, Object encoding, Object errors, Object finalData, |
| 561 | + Object decode(VirtualFrame frame, Object input, String encoding, String errors, boolean finalData, |
561 | 562 | @Cached InternalCodecsDecodeNode internalNode) {
|
562 |
| - return internalNode.execute(input, encoding, errors, finalData); |
| 563 | + return internalNode.execute(frame, this, input, encoding, errors, finalData); |
563 | 564 | }
|
564 | 565 | }
|
565 | 566 |
|
566 | 567 | @GenerateUncached
|
567 | 568 | public abstract static class InternalCodecsDecodeNode extends PNodeWithContext {
|
568 |
| - abstract Object execute(Object input, Object encoding, Object errors, Object finalData); |
| 569 | + abstract Object execute(Frame frame, PNodeWithRaiseAndIndirectCall node, Object input, String encoding, String errors, boolean finalData); |
569 | 570 |
|
570 |
| - public final Object call(Object input, Object encoding, Object errors, Object finalData) { |
571 |
| - return execute(input, encoding, errors, finalData); |
| 571 | + public final Object call(VirtualFrame frame, PNodeWithRaiseAndIndirectCall node, Object input, String encoding, String errors, boolean finalData) { |
| 572 | + return execute(frame, node, input, encoding, errors, finalData); |
572 | 573 | }
|
573 | 574 |
|
574 |
| - @Specialization |
575 |
| - Object decode(PBytesLike input, String encoding, String errors, boolean finalData, |
576 |
| - @Cached GetInternalByteArrayNode getBytes, |
| 575 | + @Specialization(limit = "3") |
| 576 | + Object decode(VirtualFrame frame, PNodeWithRaiseAndIndirectCall node, Object input, String encoding, String errors, boolean finalData, |
| 577 | + @CachedLibrary("input") PythonBufferAcquireLibrary acquireLib, |
| 578 | + @CachedLibrary(limit = "1") PythonBufferAccessLibrary bufferLib, |
577 | 579 | @Cached HandleDecodingErrorNode errorHandler,
|
578 | 580 | @Cached PRaiseNode raiseNode,
|
579 | 581 | @Cached PythonObjectFactory factory) {
|
580 |
| - byte[] bytes = getBytes.execute(input.getSequenceStorage()); |
581 |
| - CodingErrorAction errorAction = convertCodingErrorAction(errors); |
582 |
| - Charset charset = CharsetMapping.getCharset(encoding); |
583 |
| - if (charset == null) { |
584 |
| - throw raiseNode.raise(LookupError, ErrorMessages.UNKNOWN_ENCODING, encoding); |
585 |
| - } |
586 |
| - TruffleDecoder decoder; |
| 582 | + Object buffer = acquireLib.acquireReadonly(input, frame, node); |
587 | 583 | try {
|
588 |
| - decoder = new TruffleDecoder(CharsetMapping.normalize(encoding), charset, bytes, bytes.length, errorAction); |
589 |
| - while (!decoder.decodingStep(finalData)) { |
590 |
| - errorHandler.execute(decoder, errors, input); |
| 584 | + int len = bufferLib.getBufferLength(buffer); |
| 585 | + byte[] bytes = bufferLib.getInternalOrCopiedByteArray(buffer); |
| 586 | + CodingErrorAction errorAction = convertCodingErrorAction(errors); |
| 587 | + Charset charset = CharsetMapping.getCharset(encoding); |
| 588 | + if (charset == null) { |
| 589 | + throw raiseNode.raise(LookupError, ErrorMessages.UNKNOWN_ENCODING, encoding); |
591 | 590 | }
|
592 |
| - } catch (OutOfMemoryError e) { |
593 |
| - CompilerDirectives.transferToInterpreterAndInvalidate(); |
594 |
| - throw raiseNode.raise(MemoryError); |
| 591 | + TruffleDecoder decoder; |
| 592 | + try { |
| 593 | + decoder = new TruffleDecoder(CharsetMapping.normalize(encoding), charset, bytes, len, errorAction); |
| 594 | + while (!decoder.decodingStep(finalData)) { |
| 595 | + errorHandler.execute(decoder, errors, input); |
| 596 | + } |
| 597 | + } catch (OutOfMemoryError e) { |
| 598 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 599 | + throw raiseNode.raise(MemoryError); |
| 600 | + } |
| 601 | + return factory.createTuple(new Object[]{decoder.getString(), decoder.getInputPosition()}); |
| 602 | + } finally { |
| 603 | + bufferLib.release(buffer); |
595 | 604 | }
|
596 |
| - return factory.createTuple(new Object[]{decoder.getString(), decoder.getInputPosition()}); |
597 |
| - } |
598 |
| - |
599 |
| - @Fallback |
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); |
603 | 605 | }
|
604 | 606 | }
|
605 | 607 |
|
@@ -842,33 +844,25 @@ private static boolean hasTruffleEncoding(String encoding) {
|
842 | 844 | return CharsetMapping.getCharset(encoding) != null;
|
843 | 845 | }
|
844 | 846 |
|
845 |
| - @Builtin(name = "lookup", minNumOfPositionalArgs = 1) |
| 847 | + @Builtin(name = "lookup", minNumOfPositionalArgs = 1, parameterNames = {"encoding"}) |
| 848 | + @ArgumentClinic(name = "encoding", conversion = ArgumentClinic.ClinicConversion.String) |
846 | 849 | @GenerateNodeFactory
|
847 |
| - abstract static class LookupNode extends PythonUnaryBuiltinNode { |
| 850 | + abstract static class LookupNode extends PythonUnaryClinicBuiltinNode { |
848 | 851 | @Specialization
|
849 |
| - PTuple lookup(VirtualFrame frame, Object encoding, |
| 852 | + PTuple lookup(VirtualFrame frame, String encoding, |
850 | 853 | @Cached InternalLookupNode internalNode) {
|
851 | 854 | return internalNode.execute(frame, encoding);
|
852 | 855 | }
|
| 856 | + |
| 857 | + @Override |
| 858 | + protected ArgumentClinicProvider getArgumentClinic() { |
| 859 | + return CodecsModuleBuiltinsClinicProviders.LookupNodeClinicProviderGen.INSTANCE; |
| 860 | + } |
853 | 861 | }
|
854 | 862 |
|
855 | 863 | @GenerateUncached
|
856 | 864 | abstract static class InternalLookupNode extends PNodeWithContext {
|
857 |
| - abstract PTuple execute(Frame frame, Object encoding); |
858 |
| - |
859 |
| - @Specialization |
860 |
| - PTuple lookup(VirtualFrame frame, PBytesLike encoding, |
861 |
| - @Cached InternalCodecsDecodeNode decodeNode, |
862 |
| - @Cached PyObjectTypeCheck typeCheck, |
863 |
| - @Cached CallUnaryMethodNode callNode, |
864 |
| - @Cached PyObjectSizeNode sizeNode, |
865 |
| - @Cached ConditionProfile hasSearchPathProfile, |
866 |
| - @Cached ConditionProfile hasTruffleEncodingProfile, |
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); |
871 |
| - } |
| 865 | + abstract PTuple execute(Frame frame, String encoding); |
872 | 866 |
|
873 | 867 | @Specialization
|
874 | 868 | PTuple lookup(VirtualFrame frame, String encoding,
|
|
0 commit comments