Skip to content

Commit c298fcd

Browse files
committed
moved PyMapping nodes to PythonCextAbstractBuiltins
1 parent c766445 commit c298fcd

File tree

2 files changed

+170
-129
lines changed

2 files changed

+170
-129
lines changed

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

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import com.oracle.graal.python.builtins.modules.BuiltinFunctions.BinNode;
6262
import com.oracle.graal.python.builtins.modules.BuiltinFunctions.DivModNode;
6363
import com.oracle.graal.python.builtins.modules.BuiltinFunctions.HexNode;
64+
import com.oracle.graal.python.builtins.modules.BuiltinFunctions.NextNode;
6465
import com.oracle.graal.python.builtins.modules.BuiltinFunctions.OctNode;
6566
import com.oracle.graal.python.builtins.modules.cext.PythonCextBuiltins.NativeBuiltin;
6667
import com.oracle.graal.python.builtins.objects.PNone;
@@ -71,12 +72,21 @@
7172
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.ToNewRefNode;
7273
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.TransformExceptionToNativeNode;
7374
import com.oracle.graal.python.builtins.objects.cext.capi.DynamicObjectNativeWrapper.PrimitiveNativeWrapper;
75+
import com.oracle.graal.python.builtins.objects.dict.DictBuiltins.ItemsNode;
76+
import com.oracle.graal.python.builtins.objects.dict.DictBuiltins.KeysNode;
77+
import com.oracle.graal.python.builtins.objects.dict.DictBuiltins.ValuesNode;
78+
import com.oracle.graal.python.builtins.objects.dict.PDict;
79+
import com.oracle.graal.python.builtins.objects.list.PList;
7480
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
7581
import com.oracle.graal.python.lib.PyNumberFloatNode;
7682
import com.oracle.graal.python.lib.PyObjectDelItem;
83+
import com.oracle.graal.python.lib.PyObjectGetAttr;
7784
import com.oracle.graal.python.lib.PyObjectLookupAttr;
7885
import com.oracle.graal.python.lib.PySequenceCheckNode;
7986
import com.oracle.graal.python.nodes.ErrorMessages;
87+
import static com.oracle.graal.python.nodes.SpecialMethodNames.ITEMS;
88+
import static com.oracle.graal.python.nodes.SpecialMethodNames.KEYS;
89+
import static com.oracle.graal.python.nodes.SpecialMethodNames.VALUES;
8090
import com.oracle.graal.python.nodes.builtins.ListNodes.ConstructListNode;
8191
import com.oracle.graal.python.nodes.call.CallNode;
8292
import com.oracle.graal.python.nodes.expression.BinaryArithmetic;
@@ -97,6 +107,7 @@
97107
import com.oracle.graal.python.runtime.exception.PException;
98108
import com.oracle.truffle.api.CompilerDirectives;
99109
import com.oracle.truffle.api.dsl.Cached;
110+
import com.oracle.truffle.api.dsl.Cached.Shared;
100111
import com.oracle.truffle.api.dsl.Fallback;
101112
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
102113
import com.oracle.truffle.api.dsl.NodeFactory;
@@ -973,5 +984,164 @@ Object doManaged(VirtualFrame frame, Object listWrapper, Object position,
973984
}
974985
}
975986
}
987+
988+
/////// PyObject ///////
989+
990+
@Builtin(name = "PyObject_GetItem", minNumOfPositionalArgs = 2)
991+
@GenerateNodeFactory
992+
abstract static class PyObjectGetItem extends PythonBinaryBuiltinNode {
993+
@Specialization
994+
Object doManaged(VirtualFrame frame, Object listWrapper, Object key,
995+
@Cached com.oracle.graal.python.lib.PyObjectGetItem getItem,
996+
@Cached AsPythonObjectNode listWrapperAsPythonObjectNode,
997+
@Cached AsPythonObjectNode keyAsPythonObjectNode,
998+
@Cached ToNewRefNode toNewRefNode,
999+
@Cached GetNativeNullNode getNativeNullNode,
1000+
@Cached TransformExceptionToNativeNode transformExceptionToNativeNode) {
1001+
try {
1002+
Object delegate = listWrapperAsPythonObjectNode.execute(listWrapper);
1003+
Object item = getItem.execute(frame, delegate, keyAsPythonObjectNode.execute(key));
1004+
return toNewRefNode.execute(item);
1005+
} catch (PException e) {
1006+
transformExceptionToNativeNode.execute(frame, e);
1007+
return toNewRefNode.execute(getNativeNullNode.execute());
1008+
}
1009+
}
1010+
}
1011+
1012+
/////// PyMapping ///////
1013+
1014+
@Builtin(name = "PyMapping_Keys", minNumOfPositionalArgs = 1)
1015+
@GenerateNodeFactory
1016+
public abstract static class PyMappingKeysNode extends PythonUnaryBuiltinNode {
1017+
@Specialization
1018+
public Object keys(VirtualFrame frame, PDict obj,
1019+
@Cached KeysNode keysNode,
1020+
@Cached ConstructListNode listNode,
1021+
@Cached TransformExceptionToNativeNode transformExceptionToNativeNode,
1022+
@Shared("nativeNull") @Cached GetNativeNullNode getNativeNullNode) {
1023+
try {
1024+
return listNode.execute(frame, keysNode.execute(frame, obj));
1025+
} catch (PException e) {
1026+
transformExceptionToNativeNode.execute(e);
1027+
return getNativeNullNode.execute();
1028+
}
1029+
}
1030+
1031+
@Specialization(guards = "!isDict(obj)")
1032+
public Object keys(VirtualFrame frame, Object obj,
1033+
@Cached PyObjectGetAttr getAttrNode,
1034+
@Cached CallNode callNode,
1035+
@Cached ConstructListNode listNode,
1036+
@Cached TransformExceptionToNativeNode transformExceptionToNativeNode,
1037+
@Shared("nativeNull") @Cached GetNativeNullNode getNativeNullNode) {
1038+
try {
1039+
return getKeys(frame, obj, getAttrNode, callNode, listNode);
1040+
} catch (PException e) {
1041+
transformExceptionToNativeNode.execute(e);
1042+
return getNativeNullNode.execute();
1043+
}
1044+
}
1045+
1046+
}
9761047

1048+
private static PList getKeys(VirtualFrame frame, Object obj, PyObjectGetAttr getAttrNode, CallNode callNode, ConstructListNode listNode) {
1049+
Object attr = getAttrNode.execute(frame, obj, KEYS);
1050+
return listNode.execute(frame, callNode.execute(frame, attr));
1051+
}
1052+
1053+
@Builtin(name = "PyMapping_Items", minNumOfPositionalArgs = 1)
1054+
@GenerateNodeFactory
1055+
public abstract static class PyMappingItemsNode extends PythonUnaryBuiltinNode {
1056+
@Specialization
1057+
public Object items(VirtualFrame frame, PDict obj,
1058+
@Cached ItemsNode itemsNode,
1059+
@Cached ConstructListNode listNode,
1060+
@Cached TransformExceptionToNativeNode transformExceptionToNativeNode,
1061+
@Shared("nativeNull") @Cached GetNativeNullNode getNativeNullNode) {
1062+
try {
1063+
return listNode.execute(frame, itemsNode.execute(frame, obj));
1064+
} catch (PException e) {
1065+
transformExceptionToNativeNode.execute(e);
1066+
return getNativeNullNode.execute();
1067+
}
1068+
}
1069+
1070+
@Specialization(guards = "!isDict(obj)")
1071+
public Object items(VirtualFrame frame, Object obj,
1072+
@Cached PyObjectGetAttr getAttrNode,
1073+
@Cached CallNode callNode,
1074+
@Cached ConstructListNode listNode,
1075+
@Cached TransformExceptionToNativeNode transformExceptionToNativeNode,
1076+
@Shared("nativeNull") @Cached GetNativeNullNode getNativeNullNode) {
1077+
try {
1078+
Object attr = getAttrNode.execute(frame, obj, ITEMS);
1079+
return listNode.execute(frame, callNode.execute(frame, attr));
1080+
} catch (PException e) {
1081+
transformExceptionToNativeNode.execute(e);
1082+
return getNativeNullNode.execute();
1083+
}
1084+
}
1085+
}
1086+
1087+
@Builtin(name = "PyMapping_Values", minNumOfPositionalArgs = 1)
1088+
@GenerateNodeFactory
1089+
public abstract static class PyMappingValuesNode extends PythonUnaryBuiltinNode {
1090+
@Specialization
1091+
public Object values(VirtualFrame frame, PDict obj,
1092+
@Cached ConstructListNode listNode,
1093+
@Cached ValuesNode valuesNode,
1094+
@Cached TransformExceptionToNativeNode transformExceptionToNativeNode,
1095+
@Shared("nativeNull") @Cached GetNativeNullNode getNativeNullNode) {
1096+
try {
1097+
return listNode.execute(frame, valuesNode.execute(frame, obj));
1098+
} catch (PException e) {
1099+
transformExceptionToNativeNode.execute(e);
1100+
return getNativeNullNode.execute();
1101+
}
1102+
}
1103+
1104+
@Specialization(guards = "!isDict(obj)")
1105+
public Object values(VirtualFrame frame, Object obj,
1106+
@Cached PyObjectGetAttr getAttrNode,
1107+
@Cached CallNode callNode,
1108+
@Cached ConstructListNode listNode,
1109+
@Cached TransformExceptionToNativeNode transformExceptionToNativeNode,
1110+
@Shared("nativeNull") @Cached GetNativeNullNode getNativeNullNode) {
1111+
try {
1112+
Object attr = getAttrNode.execute(frame, obj, VALUES);
1113+
return listNode.execute(frame, callNode.execute(frame, attr));
1114+
} catch (PException e) {
1115+
transformExceptionToNativeNode.execute(e);
1116+
return getNativeNullNode.execute();
1117+
}
1118+
}
1119+
}
1120+
1121+
/////// PyIter ///////
1122+
1123+
//@may_raise
1124+
//def PyIter_Next(itObj):
1125+
// try:
1126+
// return next(itObj)
1127+
// except StopIteration:
1128+
// PyErr_Restore(None, None, None)
1129+
// return native_null
1130+
1131+
@Builtin(name = "PyIter_Next", minNumOfPositionalArgs = 1)
1132+
@GenerateNodeFactory
1133+
abstract static class PyIterNextCheck extends PythonUnaryBuiltinNode {
1134+
@Specialization
1135+
static Object check(VirtualFrame frame, Object object,
1136+
@Cached NextNode nextNode,
1137+
@Cached TransformExceptionToNativeNode transformExceptionToNativeNode,
1138+
@Cached GetNativeNullNode getNativeNullNode) {
1139+
try {
1140+
return nextNode.execute(frame, object, PNone.NO_VALUE);
1141+
} catch (PException e) {
1142+
transformExceptionToNativeNode.execute(e);
1143+
return getNativeNullNode.execute();
1144+
}
1145+
}
1146+
}
9771147
}

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

Lines changed: 0 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -579,113 +579,6 @@ Object run(VirtualFrame frame, String name,
579579
}
580580
}
581581

582-
@Builtin(name = "PyMapping_Keys", minNumOfPositionalArgs = 1)
583-
@GenerateNodeFactory
584-
public abstract static class PyMappingKeysNode extends PythonUnaryBuiltinNode {
585-
@Specialization
586-
public Object keys(VirtualFrame frame, PDict obj,
587-
@Cached KeysNode keysNode,
588-
@Cached ConstructListNode listNode,
589-
@Cached TransformExceptionToNativeNode transformExceptionToNativeNode,
590-
@Shared("nativeNull") @Cached GetNativeNullNode getNativeNullNode) {
591-
try {
592-
return listNode.execute(frame, keysNode.execute(frame, obj));
593-
} catch (PException e) {
594-
transformExceptionToNativeNode.execute(e);
595-
return getNativeNullNode.execute();
596-
}
597-
}
598-
599-
@Specialization(guards = "!isDict(obj)")
600-
public Object keys(VirtualFrame frame, Object obj,
601-
@Cached PyObjectGetAttr getAttrNode,
602-
@Cached CallNode callNode,
603-
@Cached ConstructListNode listNode,
604-
@Cached TransformExceptionToNativeNode transformExceptionToNativeNode,
605-
@Shared("nativeNull") @Cached GetNativeNullNode getNativeNullNode) {
606-
try {
607-
return getKeys(frame, obj, getAttrNode, callNode, listNode);
608-
} catch (PException e) {
609-
transformExceptionToNativeNode.execute(e);
610-
return getNativeNullNode.execute();
611-
}
612-
}
613-
614-
}
615-
616-
private static PList getKeys(VirtualFrame frame, Object obj, PyObjectGetAttr getAttrNode, CallNode callNode, ConstructListNode listNode) {
617-
Object attr = getAttrNode.execute(frame, obj, KEYS);
618-
return listNode.execute(frame, callNode.execute(frame, attr));
619-
}
620-
621-
@Builtin(name = "PyMapping_Items", minNumOfPositionalArgs = 1)
622-
@GenerateNodeFactory
623-
public abstract static class PyMappingItemsNode extends PythonUnaryBuiltinNode {
624-
@Specialization
625-
public Object items(VirtualFrame frame, PDict obj,
626-
@Cached ItemsNode itemsNode,
627-
@Cached ConstructListNode listNode,
628-
@Cached TransformExceptionToNativeNode transformExceptionToNativeNode,
629-
@Shared("nativeNull") @Cached GetNativeNullNode getNativeNullNode) {
630-
try {
631-
return listNode.execute(frame, itemsNode.execute(frame, obj));
632-
} catch (PException e) {
633-
transformExceptionToNativeNode.execute(e);
634-
return getNativeNullNode.execute();
635-
}
636-
}
637-
638-
@Specialization(guards = "!isDict(obj)")
639-
public Object items(VirtualFrame frame, Object obj,
640-
@Cached PyObjectGetAttr getAttrNode,
641-
@Cached CallNode callNode,
642-
@Cached ConstructListNode listNode,
643-
@Cached TransformExceptionToNativeNode transformExceptionToNativeNode,
644-
@Shared("nativeNull") @Cached GetNativeNullNode getNativeNullNode) {
645-
try {
646-
Object attr = getAttrNode.execute(frame, obj, ITEMS);
647-
return listNode.execute(frame, callNode.execute(frame, attr));
648-
} catch (PException e) {
649-
transformExceptionToNativeNode.execute(e);
650-
return getNativeNullNode.execute();
651-
}
652-
}
653-
}
654-
655-
@Builtin(name = "PyMapping_Values", minNumOfPositionalArgs = 1)
656-
@GenerateNodeFactory
657-
public abstract static class PyMappingValuesNode extends PythonUnaryBuiltinNode {
658-
@Specialization
659-
public Object values(VirtualFrame frame, PDict obj,
660-
@Cached ConstructListNode listNode,
661-
@Cached ValuesNode valuesNode,
662-
@Cached TransformExceptionToNativeNode transformExceptionToNativeNode,
663-
@Shared("nativeNull") @Cached GetNativeNullNode getNativeNullNode) {
664-
try {
665-
return listNode.execute(frame, valuesNode.execute(frame, obj));
666-
} catch (PException e) {
667-
transformExceptionToNativeNode.execute(e);
668-
return getNativeNullNode.execute();
669-
}
670-
}
671-
672-
@Specialization(guards = "!isDict(obj)")
673-
public Object values(VirtualFrame frame, Object obj,
674-
@Cached PyObjectGetAttr getAttrNode,
675-
@Cached CallNode callNode,
676-
@Cached ConstructListNode listNode,
677-
@Cached TransformExceptionToNativeNode transformExceptionToNativeNode,
678-
@Shared("nativeNull") @Cached GetNativeNullNode getNativeNullNode) {
679-
try {
680-
Object attr = getAttrNode.execute(frame, obj, VALUES);
681-
return listNode.execute(frame, callNode.execute(frame, attr));
682-
} catch (PException e) {
683-
transformExceptionToNativeNode.execute(e);
684-
return getNativeNullNode.execute();
685-
}
686-
}
687-
}
688-
689582
@Builtin(name = "PyModule_GetNameObject", minNumOfPositionalArgs = 1)
690583
@GenerateNodeFactory
691584
public abstract static class PyModule_GetNameObjectNode extends PythonUnaryBuiltinNode {
@@ -3049,28 +2942,6 @@ int trace(Object ptr, Object classNameObj,
30492942
}
30502943
}
30512944

3052-
@Builtin(name = "PyObject_GetItem", minNumOfPositionalArgs = 3, declaresExplicitSelf = true)
3053-
@GenerateNodeFactory
3054-
abstract static class PyObjectGetItem extends PythonTernaryBuiltinNode {
3055-
@Specialization
3056-
Object doManaged(VirtualFrame frame, Object module, Object listWrapper, Object key,
3057-
@Cached com.oracle.graal.python.lib.PyObjectGetItem getItem,
3058-
@Cached AsPythonObjectNode listWrapperAsPythonObjectNode,
3059-
@Cached AsPythonObjectNode keyAsPythonObjectNode,
3060-
@Cached ToNewRefNode toNewRefNode,
3061-
@Cached GetNativeNullNode getNativeNullNode,
3062-
@Cached TransformExceptionToNativeNode transformExceptionToNativeNode) {
3063-
try {
3064-
Object delegate = listWrapperAsPythonObjectNode.execute(listWrapper);
3065-
Object item = getItem.execute(frame, delegate, keyAsPythonObjectNode.execute(key));
3066-
return toNewRefNode.execute(item);
3067-
} catch (PException e) {
3068-
transformExceptionToNativeNode.execute(frame, e);
3069-
return toNewRefNode.execute(getNativeNullNode.execute(module));
3070-
}
3071-
}
3072-
}
3073-
30742945
@Builtin(name = "wrap_PyDateTime_CAPI", minNumOfPositionalArgs = 1)
30752946
@GenerateNodeFactory
30762947
abstract static class WrapPyDateTimeCAPI extends PythonBuiltinNode {

0 commit comments

Comments
 (0)