Skip to content

Commit 58f679d

Browse files
committed
moved PyBytes_XXX nodes from PythonCextBuiltins to PythonCextBytesBuiltins
1 parent fab4917 commit 58f679d

File tree

3 files changed

+296
-204
lines changed

3 files changed

+296
-204
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
import com.oracle.graal.python.builtins.modules.bz2.BZ2CompressorBuiltins;
110110
import com.oracle.graal.python.builtins.modules.bz2.BZ2DecompressorBuiltins;
111111
import com.oracle.graal.python.builtins.modules.bz2.BZ2ModuleBuiltins;
112+
import com.oracle.graal.python.builtins.modules.cext.PythonCextBytesBuiltins;
112113
import com.oracle.graal.python.builtins.modules.cext.PythonCextDictBuiltins;
113114
import com.oracle.graal.python.builtins.modules.cext.PythonCextSetBuiltins;
114115
import com.oracle.graal.python.builtins.modules.csv.CSVDialectBuiltins;
@@ -469,6 +470,7 @@ private static PythonBuiltins[] initializeBuiltins(boolean nativeAccessAllowed)
469470
new RandomModuleBuiltins(),
470471
new RandomBuiltins(),
471472
new PythonCextBuiltins(),
473+
new PythonCextBytesBuiltins(),
472474
new PythonCextDictBuiltins(),
473475
new PythonCextSetBuiltins(),
474476
new WeakRefModuleBuiltins(),

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/cext/PythonCextBuiltins.java

Lines changed: 4 additions & 204 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
*/
4141
package com.oracle.graal.python.builtins.modules.cext;
4242

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;
4345
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.IndexError;
4446
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.SystemError;
4547
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.TypeError;
@@ -49,7 +51,6 @@
4951
import static com.oracle.graal.python.nodes.ErrorMessages.BAD_ARG_TO_INTERNAL_FUNC_WAS_S_P;
5052
import static com.oracle.graal.python.nodes.ErrorMessages.BASE_MUST_BE;
5153
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;
5354
import static com.oracle.graal.python.nodes.ErrorMessages.LIST_INDEX_OUT_OF_RANGE;
5455
import static com.oracle.graal.python.nodes.ErrorMessages.NATIVE_S_SUBTYPES_NOT_IMPLEMENTED;
5556
import static com.oracle.graal.python.nodes.ErrorMessages.P_OBJ_DOES_NOT_SUPPORT_ITEM_ASSIGMENT;
@@ -64,7 +65,6 @@
6465
import static com.oracle.graal.python.nodes.SpecialMethodNames.__GETITEM__;
6566
import static com.oracle.graal.python.nodes.SpecialMethodNames.__IADD__;
6667
import static com.oracle.graal.python.nodes.SpecialMethodNames.__IMUL__;
67-
import static com.oracle.graal.python.nodes.SpecialMethodNames.__ITER__;
6868
import static com.oracle.graal.python.nodes.SpecialMethodNames.__NEW__;
6969
import static com.oracle.graal.python.nodes.SpecialMethodNames.__SETITEM__;
7070
import static com.oracle.graal.python.runtime.exception.PythonErrorType.OverflowError;
@@ -96,7 +96,6 @@
9696
import com.oracle.graal.python.builtins.Python3Core;
9797
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
9898
import com.oracle.graal.python.builtins.PythonBuiltins;
99-
import com.oracle.graal.python.builtins.modules.BuiltinConstructors.BytesNode;
10099
import com.oracle.graal.python.builtins.modules.BuiltinConstructors.ComplexNode;
101100
import com.oracle.graal.python.builtins.modules.BuiltinConstructors.MappingproxyNode;
102101
import com.oracle.graal.python.builtins.modules.BuiltinConstructors.StrNode;
@@ -112,8 +111,7 @@
112111
import com.oracle.graal.python.builtins.modules.SysModuleBuiltins;
113112
import com.oracle.graal.python.builtins.modules.SysModuleBuiltins.InternNode;
114113
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;
117115
import com.oracle.graal.python.builtins.objects.PNone;
118116
import com.oracle.graal.python.builtins.objects.PNotImplemented;
119117
import com.oracle.graal.python.builtins.objects.bytes.BytesBuiltins;
@@ -288,7 +286,6 @@
288286
import com.oracle.graal.python.lib.PyObjectDelItem;
289287
import com.oracle.graal.python.lib.PyObjectGetAttr;
290288
import com.oracle.graal.python.lib.PyObjectLookupAttr;
291-
import com.oracle.graal.python.lib.PyObjectSizeNode;
292289
import com.oracle.graal.python.lib.PySequenceCheckNode;
293290
import com.oracle.graal.python.nodes.BuiltinNames;
294291
import com.oracle.graal.python.nodes.ErrorMessages;
@@ -437,6 +434,7 @@ public void initialize(Python3Core core) {
437434
@Override
438435
public void postInitialize(Python3Core core) {
439436
PythonModule cext = core.lookupBuiltinModule(PYTHON_CEXT);
437+
addModuleDict(cext, PYTHON_CEXT_BYTES, core);
440438
addModuleDict(cext, PYTHON_CEXT_DICT, core);
441439
addModuleDict(cext, PYTHON_CEXT_SET, core);
442440
}
@@ -786,204 +784,6 @@ public Object values(Object obj) {
786784
}
787785
}
788786

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-
987787
///////////// list /////////////
988788
@Builtin(name = "PyList_New", minNumOfPositionalArgs = 1)
989789
@TypeSystemReference(PythonTypes.class)

0 commit comments

Comments
 (0)