|
82 | 82 | import com.oracle.graal.python.builtins.modules.BuiltinFunctionsFactory.GetAttrNodeFactory; |
83 | 83 | import com.oracle.graal.python.builtins.modules.BuiltinFunctionsFactory.GlobalsNodeFactory; |
84 | 84 | import com.oracle.graal.python.builtins.objects.PNone; |
85 | | -import com.oracle.graal.python.builtins.objects.bytes.BytesNodes; |
86 | 85 | import com.oracle.graal.python.builtins.objects.bytes.BytesUtils; |
87 | 86 | import com.oracle.graal.python.builtins.objects.bytes.PBytes; |
88 | 87 | import com.oracle.graal.python.builtins.objects.bytes.PIBytesLike; |
|
183 | 182 | import com.oracle.truffle.api.dsl.TypeSystemReference; |
184 | 183 | import com.oracle.truffle.api.frame.Frame; |
185 | 184 | import com.oracle.truffle.api.frame.VirtualFrame; |
| 185 | +import com.oracle.truffle.api.interop.InteropLibrary; |
186 | 186 | import com.oracle.truffle.api.interop.UnsupportedMessageException; |
187 | 187 | import com.oracle.truffle.api.library.CachedLibrary; |
188 | 188 | import com.oracle.truffle.api.nodes.ExplodeLoop; |
@@ -747,26 +747,6 @@ public CompileNode() { |
747 | 747 |
|
748 | 748 | public abstract PCode execute(VirtualFrame frame, Object source, String filename, String mode, Object kwFlags, Object kwDontInherit, Object kwOptimize); |
749 | 749 |
|
750 | | - @Specialization |
751 | | - PCode compile(VirtualFrame frame, PBytes pBytes, String filename, String mode, Object kwFlags, Object kwDontInherit, Object kwOptimize, |
752 | | - @Cached("create()") BytesNodes.ToBytesNode toBytesNode) { |
753 | | - try { |
754 | | - byte[] bytes = toBytesNode.execute(frame, pBytes); |
755 | | - Charset charset = PythonFileDetector.findEncodingStrict(bytes); |
756 | | - return compile(createString(bytes, charset), filename, mode, kwFlags, kwDontInherit, kwOptimize); |
757 | | - } catch (PythonFileDetector.InvalidEncodingException e) { |
758 | | - throw handleInvalidEncoding(filename, e); |
759 | | - } |
760 | | - } |
761 | | - |
762 | | - @TruffleBoundary |
763 | | - private RuntimeException handleInvalidEncoding(String filename, PythonFileDetector.InvalidEncodingException e) { |
764 | | - PythonContext context = getContext(); |
765 | | - // Create non-empty source to avoid overwriting the message with "unexpected EOF" |
766 | | - Source source = PythonLanguage.newSource(context, " ", filename, mayBeFromFile); |
767 | | - throw getCore().raiseInvalidSyntax(source, source.createUnavailableSection(), "encoding problem: %s", e.getEncodingName()); |
768 | | - } |
769 | | - |
770 | 750 | @SuppressWarnings("unused") |
771 | 751 | @Specialization |
772 | 752 | @TruffleBoundary |
@@ -805,10 +785,59 @@ PCode compile(String expression, String filename, String mode, Object kwFlags, O |
805 | 785 | return factory().createCode(ct); |
806 | 786 | } |
807 | 787 |
|
808 | | - @SuppressWarnings("unused") |
809 | | - @Specialization |
810 | | - PCode compile(PCode code, String filename, String mode, Object flags, Object dontInherit, Object optimize) { |
811 | | - return code; |
| 788 | + @Specialization(limit = "3") |
| 789 | + PCode generic(VirtualFrame frame, Object wSource, Object wFilename, Object wMode, Object kwFlags, Object kwDontInherit, Object kwOptimize, |
| 790 | + @Cached CastToJavaStringNode castStr, |
| 791 | + @CachedLibrary("wSource") InteropLibrary interopLib, |
| 792 | + @CachedLibrary(limit = "3") PythonObjectLibrary lib) { |
| 793 | + if (wSource instanceof PCode) { |
| 794 | + return (PCode) wSource; |
| 795 | + } |
| 796 | + String filename = lib.asPathWithState(wFilename, PArguments.getThreadState(frame)); |
| 797 | + String mode; |
| 798 | + try { |
| 799 | + mode = castStr.execute(wMode); |
| 800 | + } catch (CannotCastException e) { |
| 801 | + throw raise(TypeError, ErrorMessages.ARG_S_MUST_BE_S_NOT_P, "compile()", "mode", "str", wMode); |
| 802 | + } |
| 803 | + String source = sourceAsString(wSource, filename, interopLib, lib); |
| 804 | + return compile(source, filename, mode, kwFlags, kwDontInherit, kwOptimize); |
| 805 | + } |
| 806 | + |
| 807 | + // modeled after _Py_SourceAsString |
| 808 | + String sourceAsString(Object source, String filename, InteropLibrary interopLib, PythonObjectLibrary pyLib) { |
| 809 | + if (interopLib.isString(source)) { |
| 810 | + try { |
| 811 | + return interopLib.asString(source); |
| 812 | + } catch (UnsupportedMessageException e) { |
| 813 | + throw CompilerDirectives.shouldNotReachHere(e); |
| 814 | + } |
| 815 | + } else if (pyLib.isBuffer(source)) { |
| 816 | + // cpython checks for bytes and bytearray separately, but we deal with it as |
| 817 | + // buffers, since that's fast for us anyway |
| 818 | + try { |
| 819 | + byte[] bytes; |
| 820 | + try { |
| 821 | + bytes = pyLib.getBufferBytes(source); |
| 822 | + } catch (UnsupportedMessageException e) { |
| 823 | + throw CompilerDirectives.shouldNotReachHere(e); |
| 824 | + } |
| 825 | + Charset charset = PythonFileDetector.findEncodingStrict(bytes); |
| 826 | + return createString(bytes, charset); |
| 827 | + } catch (PythonFileDetector.InvalidEncodingException e) { |
| 828 | + throw handleInvalidEncoding(filename, e); |
| 829 | + } |
| 830 | + } else { |
| 831 | + throw raise(TypeError, ErrorMessages.ARG_D_MUST_BE_S, "compile()", 1, "string, bytes or AST object"); |
| 832 | + } |
| 833 | + } |
| 834 | + |
| 835 | + @TruffleBoundary |
| 836 | + private RuntimeException handleInvalidEncoding(String filename, PythonFileDetector.InvalidEncodingException e) { |
| 837 | + PythonContext context = getContext(); |
| 838 | + // Create non-empty source to avoid overwriting the message with "unexpected EOF" |
| 839 | + Source source = PythonLanguage.newSource(context, " ", filename, mayBeFromFile); |
| 840 | + throw getCore().raiseInvalidSyntax(source, source.createUnavailableSection(), "encoding problem: %s", e.getEncodingName()); |
812 | 841 | } |
813 | 842 |
|
814 | 843 | @TruffleBoundary |
|
0 commit comments