|
40 | 40 | */
|
41 | 41 | package com.oracle.graal.python.builtins.modules.cext;
|
42 | 42 |
|
| 43 | +import static com.oracle.graal.python.builtins.modules.cext.PythonCextDictBuiltins.PYTHON_CEXT_DICT; |
| 44 | +import static com.oracle.graal.python.builtins.modules.cext.PythonCextSetBuiltins.PYTHON_CEXT_SET; |
43 | 45 | import static com.oracle.graal.python.builtins.PythonBuiltinClassType.IndexError;
|
44 | 46 | import static com.oracle.graal.python.builtins.PythonBuiltinClassType.SystemError;
|
45 | 47 | import static com.oracle.graal.python.builtins.PythonBuiltinClassType.TypeError;
|
|
49 | 51 | import static com.oracle.graal.python.nodes.ErrorMessages.BAD_ARG_TO_INTERNAL_FUNC_WAS_S_P;
|
50 | 52 | import static com.oracle.graal.python.nodes.ErrorMessages.BASE_MUST_BE;
|
51 | 53 | import static com.oracle.graal.python.nodes.ErrorMessages.BAD_ARG_TYPE_FOR_BUILTIN_OP;
|
52 |
| -import static com.oracle.graal.python.nodes.ErrorMessages.CANNOT_CONVERT_P_OBJ_TO_S; |
53 | 54 | import static com.oracle.graal.python.nodes.ErrorMessages.LIST_INDEX_OUT_OF_RANGE;
|
54 | 55 | import static com.oracle.graal.python.nodes.ErrorMessages.NATIVE_S_SUBTYPES_NOT_IMPLEMENTED;
|
55 | 56 | import static com.oracle.graal.python.nodes.ErrorMessages.P_OBJ_DOES_NOT_SUPPORT_ITEM_ASSIGMENT;
|
|
64 | 65 | import static com.oracle.graal.python.nodes.SpecialMethodNames.__GETITEM__;
|
65 | 66 | import static com.oracle.graal.python.nodes.SpecialMethodNames.__IADD__;
|
66 | 67 | import static com.oracle.graal.python.nodes.SpecialMethodNames.__IMUL__;
|
67 |
| -import static com.oracle.graal.python.nodes.SpecialMethodNames.__ITER__; |
68 | 68 | import static com.oracle.graal.python.nodes.SpecialMethodNames.__NEW__;
|
69 | 69 | import static com.oracle.graal.python.nodes.SpecialMethodNames.__SETITEM__;
|
70 | 70 | import static com.oracle.graal.python.runtime.exception.PythonErrorType.OverflowError;
|
|
96 | 96 | import com.oracle.graal.python.builtins.Python3Core;
|
97 | 97 | import com.oracle.graal.python.builtins.PythonBuiltinClassType;
|
98 | 98 | import com.oracle.graal.python.builtins.PythonBuiltins;
|
99 |
| -import com.oracle.graal.python.builtins.modules.BuiltinConstructors.BytesNode; |
100 | 99 | import com.oracle.graal.python.builtins.modules.BuiltinConstructors.ComplexNode;
|
101 | 100 | import com.oracle.graal.python.builtins.modules.BuiltinConstructors.MappingproxyNode;
|
102 | 101 | import com.oracle.graal.python.builtins.modules.BuiltinConstructors.StrNode;
|
|
112 | 111 | import com.oracle.graal.python.builtins.modules.SysModuleBuiltins;
|
113 | 112 | import com.oracle.graal.python.builtins.modules.SysModuleBuiltins.InternNode;
|
114 | 113 | import com.oracle.graal.python.builtins.modules.cext.PythonCextBuiltinsFactory.CreateFunctionNodeGen;
|
115 |
| -import static com.oracle.graal.python.builtins.modules.cext.PythonCextDictBuiltins.PYTHON_CEXT_DICT; |
116 |
| -import static com.oracle.graal.python.builtins.modules.cext.PythonCextSetBuiltins.PYTHON_CEXT_SET; |
| 114 | +import static com.oracle.graal.python.builtins.modules.cext.PythonCextBytesBuiltins.PYTHON_CEXT_BYTES; |
117 | 115 | import com.oracle.graal.python.builtins.objects.PNone;
|
118 | 116 | import com.oracle.graal.python.builtins.objects.PNotImplemented;
|
119 | 117 | import com.oracle.graal.python.builtins.objects.bytes.BytesBuiltins;
|
|
288 | 286 | import com.oracle.graal.python.lib.PyObjectDelItem;
|
289 | 287 | import com.oracle.graal.python.lib.PyObjectGetAttr;
|
290 | 288 | import com.oracle.graal.python.lib.PyObjectLookupAttr;
|
291 |
| -import com.oracle.graal.python.lib.PyObjectSizeNode; |
292 | 289 | import com.oracle.graal.python.lib.PySequenceCheckNode;
|
293 | 290 | import com.oracle.graal.python.nodes.BuiltinNames;
|
294 | 291 | import com.oracle.graal.python.nodes.ErrorMessages;
|
@@ -437,6 +434,7 @@ public void initialize(Python3Core core) {
|
437 | 434 | @Override
|
438 | 435 | public void postInitialize(Python3Core core) {
|
439 | 436 | PythonModule cext = core.lookupBuiltinModule(PYTHON_CEXT);
|
| 437 | + addModuleDict(cext, PYTHON_CEXT_BYTES, core); |
440 | 438 | addModuleDict(cext, PYTHON_CEXT_DICT, core);
|
441 | 439 | addModuleDict(cext, PYTHON_CEXT_SET, core);
|
442 | 440 | }
|
@@ -786,204 +784,6 @@ public Object values(Object obj) {
|
786 | 784 | }
|
787 | 785 | }
|
788 | 786 |
|
789 |
| - ///////////// bytes ///////////// |
790 |
| - |
791 |
| - @Builtin(name = "PyBytes_Size", minNumOfPositionalArgs = 1) |
792 |
| - @GenerateNodeFactory |
793 |
| - public abstract static class PyBytesSizeNode extends PythonUnaryBuiltinNode { |
794 |
| - @Specialization |
795 |
| - public int size(VirtualFrame frame, PBytes obj, |
796 |
| - @Cached PyObjectSizeNode sizeNode) { |
797 |
| - return sizeNode.execute(frame, obj); |
798 |
| - } |
799 |
| - |
800 |
| - @Specialization(guards = {"!isPBytes(obj)", "isBytesSubtype(frame, obj, getClassNode, isSubtypeNode)"}) |
801 |
| - public int sizeNative(VirtualFrame frame, @SuppressWarnings("unused") Object obj, |
802 |
| - @SuppressWarnings("unused") @Cached GetClassNode getClassNode, |
803 |
| - @SuppressWarnings("unused") @Cached IsSubtypeNode isSubtypeNode, |
804 |
| - @Cached PRaiseNativeNode raiseNativeNode) { |
805 |
| - return raiseNativeNode.raiseInt(frame, -1, PythonBuiltinClassType.NotImplementedError, NATIVE_S_SUBTYPES_NOT_IMPLEMENTED, "bytes"); |
806 |
| - } |
807 |
| - |
808 |
| - @Specialization(guards = {"!isPBytes(obj)", "!isBytesSubtype(frame, obj, getClassNode, isSubtypeNode)"}) |
809 |
| - public int size(VirtualFrame frame, Object obj, |
810 |
| - @SuppressWarnings("unused") @Cached GetClassNode getClassNode, |
811 |
| - @SuppressWarnings("unused") @Cached IsSubtypeNode isSubtypeNode, |
812 |
| - @Cached StrNode strNode, |
813 |
| - @Cached PRaiseNativeNode raiseNativeNode) { |
814 |
| - return raiseNativeNode.raiseInt(frame, -1, SystemError, BAD_ARG_TO_INTERNAL_FUNC_WAS_S_P, strNode.executeWith(frame, obj), obj); |
815 |
| - } |
816 |
| - |
817 |
| - protected boolean isBytesSubtype(VirtualFrame frame, Object obj, GetClassNode getClassNode, IsSubtypeNode isSubtypeNode) { |
818 |
| - return isSubtypeNode.execute(frame, getClassNode.execute(obj), PythonBuiltinClassType.PBytes); |
819 |
| - } |
820 |
| - } |
821 |
| - |
822 |
| - @Builtin(name = "PyBytes_Check", minNumOfPositionalArgs = 1) |
823 |
| - @GenerateNodeFactory |
824 |
| - public abstract static class PyBytesCheckNode extends PythonUnaryBuiltinNode { |
825 |
| - @SuppressWarnings("unused") |
826 |
| - @Specialization |
827 |
| - public Object check(PBytes obj) { |
828 |
| - return true; |
829 |
| - } |
830 |
| - |
831 |
| - @Specialization(guards = "!isPBytes(obj)") |
832 |
| - public Object check(VirtualFrame frame, Object obj, |
833 |
| - @Cached GetClassNode getClassNode, |
834 |
| - @Cached IsSubtypeNode isSubtypeNode) { |
835 |
| - return isSubtypeNode.execute(frame, getClassNode.execute(obj), PythonBuiltinClassType.PBytes); |
836 |
| - } |
837 |
| - } |
838 |
| - |
839 |
| - @Builtin(name = "PyBytes_Concat", minNumOfPositionalArgs = 2) |
840 |
| - @GenerateNodeFactory |
841 |
| - public abstract static class PyBytesConcatNode extends PythonBinaryBuiltinNode { |
842 |
| - @Specialization |
843 |
| - public Object concat(VirtualFrame frame, PBytes original, Object newPart, |
844 |
| - @Cached BytesBuiltins.AddNode addNode, |
845 |
| - @Cached TransformExceptionToNativeNode transformExceptionToNativeNode, |
846 |
| - @Cached GetNativeNullNode getNativeNullNode) { |
847 |
| - try { |
848 |
| - return addNode.execute(frame, original, newPart); |
849 |
| - } catch (PException e) { |
850 |
| - transformExceptionToNativeNode.execute(e); |
851 |
| - return getNativeNullNode.execute(); |
852 |
| - } |
853 |
| - } |
854 |
| - |
855 |
| - @Specialization(guards = {"!isPBytes(original)", "isBytesSubtype(frame, original, getClassNode, isSubtypeNode)"}) |
856 |
| - public Object concatNative(VirtualFrame frame, @SuppressWarnings("unused") Object original, @SuppressWarnings("unused") Object newPart, |
857 |
| - @SuppressWarnings("unused") @Cached GetClassNode getClassNode, |
858 |
| - @SuppressWarnings("unused") @Cached IsSubtypeNode isSubtypeNode, |
859 |
| - @Cached GetNativeNullNode getNativeNullNode, |
860 |
| - @Cached PRaiseNativeNode raiseNativeNode) { |
861 |
| - return raiseNativeNode.raise(frame, getNativeNullNode.execute(), PythonBuiltinClassType.NotImplementedError, NATIVE_S_SUBTYPES_NOT_IMPLEMENTED, "bytes"); |
862 |
| - } |
863 |
| - |
864 |
| - @Specialization(guards = {"!isPBytes(original)", "!isBytesSubtype(frame, original, getClassNode, isSubtypeNode)"}) |
865 |
| - public Object concat(VirtualFrame frame, Object original, @SuppressWarnings("unused") Object newPart, |
866 |
| - @SuppressWarnings("unused") @Cached GetClassNode getClassNode, |
867 |
| - @SuppressWarnings("unused") @Cached IsSubtypeNode isSubtypeNode, |
868 |
| - @Cached StrNode strNode, |
869 |
| - @Cached GetNativeNullNode getNativeNullNode, |
870 |
| - @Cached PRaiseNativeNode raiseNativeNode) { |
871 |
| - return raiseNativeNode.raise(frame, getNativeNullNode.execute(), SystemError, BAD_ARG_TO_INTERNAL_FUNC_WAS_S_P, strNode.executeWith(frame, original), original); |
872 |
| - } |
873 |
| - |
874 |
| - protected boolean isBytesSubtype(VirtualFrame frame, Object obj, GetClassNode getClassNode, IsSubtypeNode isSubtypeNode) { |
875 |
| - return isSubtypeNode.execute(frame, getClassNode.execute(obj), PythonBuiltinClassType.PBytes); |
876 |
| - } |
877 |
| - } |
878 |
| - |
879 |
| - @Builtin(name = "PyBytes_Join", minNumOfPositionalArgs = 2) |
880 |
| - @GenerateNodeFactory |
881 |
| - public abstract static class PyBytesJoinNode extends PythonBinaryBuiltinNode { |
882 |
| - @Specialization |
883 |
| - public Object join(VirtualFrame frame, PBytes original, Object newPart, |
884 |
| - @Cached BytesBuiltins.JoinNode joinNode, |
885 |
| - @Cached TransformExceptionToNativeNode transformExceptionToNativeNode, |
886 |
| - @Cached GetNativeNullNode getNativeNullNode) { |
887 |
| - try { |
888 |
| - return joinNode.execute(frame, original, newPart); |
889 |
| - } catch (PException e) { |
890 |
| - transformExceptionToNativeNode.execute(e); |
891 |
| - return getNativeNullNode.execute(); |
892 |
| - } |
893 |
| - } |
894 |
| - |
895 |
| - @Specialization(guards = {"!isPBytes(original)", "isBytesSubtype(frame, original, getClassNode, isSubtypeNode)"}) |
896 |
| - public Object joinNative(VirtualFrame frame, @SuppressWarnings("unused") Object original, @SuppressWarnings("unused") Object newPart, |
897 |
| - @SuppressWarnings("unused") @Cached GetClassNode getClassNode, |
898 |
| - @SuppressWarnings("unused") @Cached IsSubtypeNode isSubtypeNode, |
899 |
| - @Cached PRaiseNativeNode raiseNativeNode, |
900 |
| - @Cached GetNativeNullNode getNativeNullNode) { |
901 |
| - return raiseNativeNode.raise(frame, getNativeNullNode.execute(), PythonBuiltinClassType.NotImplementedError, NATIVE_S_SUBTYPES_NOT_IMPLEMENTED, "bytes"); |
902 |
| - } |
903 |
| - |
904 |
| - @Specialization(guards = {"!isPBytes(original)", "!isBytesSubtype(frame, original, getClassNode, isSubtypeNode)"}) |
905 |
| - public Object join(VirtualFrame frame, @SuppressWarnings("unused") Object original, @SuppressWarnings("unused") Object newPart, |
906 |
| - @SuppressWarnings("unused") @Cached GetClassNode getClassNode, |
907 |
| - @SuppressWarnings("unused") @Cached IsSubtypeNode isSubtypeNode, |
908 |
| - @Cached StrNode strNode, |
909 |
| - @Cached PRaiseNativeNode raiseNativeNode, |
910 |
| - @Cached GetNativeNullNode getNativeNullNode) { |
911 |
| - return raiseNativeNode.raise(frame, getNativeNullNode.execute(), SystemError, BAD_ARG_TO_INTERNAL_FUNC_WAS_S_P, strNode.executeWith(frame, original), original); |
912 |
| - } |
913 |
| - |
914 |
| - protected boolean isBytesSubtype(VirtualFrame frame, Object obj, GetClassNode getClassNode, IsSubtypeNode isSubtypeNode) { |
915 |
| - return isSubtypeNode.execute(frame, getClassNode.execute(obj), PythonBuiltinClassType.PBytes); |
916 |
| - } |
917 |
| - } |
918 |
| - |
919 |
| - @Builtin(name = "PyBytes_FromFormat", minNumOfPositionalArgs = 2) |
920 |
| - @GenerateNodeFactory |
921 |
| - public abstract static class PyBytesFromFormatNode extends PythonBinaryBuiltinNode { |
922 |
| - @Specialization |
923 |
| - public Object fromFormat(VirtualFrame frame, String fmt, Object args, |
924 |
| - @Cached ModNode modeNode, |
925 |
| - @Cached EncodeNode encodeNode, |
926 |
| - @Cached TransformExceptionToNativeNode transformExceptionToNativeNode, |
927 |
| - @Cached GetNativeNullNode getNativeNullNode) { |
928 |
| - try { |
929 |
| - Object formated = modeNode.execute(frame, fmt, args); |
930 |
| - return encodeNode.execute(frame, formated, PNone.NONE, PNone.NONE); |
931 |
| - } catch (PException e) { |
932 |
| - transformExceptionToNativeNode.execute(e); |
933 |
| - return getNativeNullNode.execute(); |
934 |
| - } |
935 |
| - } |
936 |
| - } |
937 |
| - |
938 |
| - @Builtin(name = "PyBytes_FromObject", minNumOfPositionalArgs = 1) |
939 |
| - @GenerateNodeFactory |
940 |
| - public abstract static class PyBytesFromObjectNode extends PythonUnaryBuiltinNode { |
941 |
| - @Specialization(guards = {"isPBytes(obj) || isBytesSubtype(frame, obj, getClassNode, isSubtypeNode)"}) |
942 |
| - public Object fromObject(@SuppressWarnings("unused") VirtualFrame frame, @SuppressWarnings("unused") Object obj, |
943 |
| - @SuppressWarnings("unused") @Cached GetClassNode getClassNode, |
944 |
| - @SuppressWarnings("unused") @Cached IsSubtypeNode isSubtypeNode) { |
945 |
| - return obj; |
946 |
| - } |
947 |
| - |
948 |
| - @Specialization(guards = {"!isPBytes(obj)", "!isBytesSubtype(frame, obj, getClassNode, isSubtypeNode)", "isAcceptedSubtype(frame, obj, getClassNode, isSubtypeNode, lookupAttrNode)"}) |
949 |
| - public Object fromObject(VirtualFrame frame, Object obj, |
950 |
| - @Cached BytesNode bytesNode, |
951 |
| - @SuppressWarnings("unused") @Cached GetClassNode getClassNode, |
952 |
| - @SuppressWarnings("unused") @Cached IsSubtypeNode isSubtypeNode, |
953 |
| - @SuppressWarnings("unused") @Cached PyObjectLookupAttr lookupAttrNode, |
954 |
| - @Cached TransformExceptionToNativeNode transformExceptionToNativeNode, |
955 |
| - @Cached GetNativeNullNode getNativeNullNode) { |
956 |
| - try { |
957 |
| - return bytesNode.execute(frame, PythonBuiltinClassType.PBytes, obj, PNone.NO_VALUE, PNone.NO_VALUE); |
958 |
| - } catch (PException e) { |
959 |
| - transformExceptionToNativeNode.execute(e); |
960 |
| - return getNativeNullNode.execute(); |
961 |
| - } |
962 |
| - } |
963 |
| - |
964 |
| - @Specialization(guards = {"!isPBytes(obj)", "!isBytesSubtype(frame, obj, getClassNode, isSubtypeNode)", "!isAcceptedSubtype(frame, obj, getClassNode, isSubtypeNode, lookupAttrNode)"}) |
965 |
| - public Object fromObject(VirtualFrame frame, Object obj, |
966 |
| - @SuppressWarnings("unused") @Cached GetClassNode getClassNode, |
967 |
| - @SuppressWarnings("unused") @Cached IsSubtypeNode isSubtypeNode, |
968 |
| - @SuppressWarnings("unused") @Cached PyObjectLookupAttr lookupAttrNode, |
969 |
| - @Cached PRaiseNativeNode raiseNativeNode, |
970 |
| - @Cached GetNativeNullNode getNativeNullNode) { |
971 |
| - return raiseNativeNode.raise(frame, getNativeNullNode.execute(), TypeError, CANNOT_CONVERT_P_OBJ_TO_S, obj, "bytes"); |
972 |
| - } |
973 |
| - |
974 |
| - protected boolean isBytesSubtype(VirtualFrame frame, Object obj, GetClassNode getClassNode, IsSubtypeNode isSubtypeNode) { |
975 |
| - return isSubtypeNode.execute(frame, getClassNode.execute(obj), PythonBuiltinClassType.PBytes); |
976 |
| - } |
977 |
| - |
978 |
| - protected boolean isAcceptedSubtype(VirtualFrame frame, Object obj, GetClassNode getClassNode, IsSubtypeNode isSubtypeNode, PyObjectLookupAttr lookupAttrNode) { |
979 |
| - Object klass = getClassNode.execute(obj); |
980 |
| - return isSubtypeNode.execute(frame, klass, PythonBuiltinClassType.PList) || |
981 |
| - isSubtypeNode.execute(frame, klass, PythonBuiltinClassType.PTuple) || |
982 |
| - isSubtypeNode.execute(frame, klass, PythonBuiltinClassType.PMemoryView) || |
983 |
| - (!isSubtypeNode.execute(frame, klass, PythonBuiltinClassType.PString) && lookupAttrNode.execute(frame, obj, __ITER__) != PNone.NO_VALUE); |
984 |
| - } |
985 |
| - } |
986 |
| - |
987 | 787 | ///////////// list /////////////
|
988 | 788 | @Builtin(name = "PyList_New", minNumOfPositionalArgs = 1)
|
989 | 789 | @TypeSystemReference(PythonTypes.class)
|
|
0 commit comments