Skip to content

Commit 51f93e8

Browse files
committed
no need to do function result check in native object downcalls in PySequence/Mapping_Size/Check
1 parent 5cdce27 commit 51f93e8

File tree

2 files changed

+93
-22
lines changed

2 files changed

+93
-22
lines changed

graalpython/com.oracle.graal.python.test/src/tests/cpyext/test_abstract.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,50 @@ def test_sequence_size(self):
421421
tester = TestSequenceSize()
422422
assert tester.callSize(tester) == 10
423423

424+
def test_sequence_size_err(self):
425+
TestSequenceSizeErr = CPyExtType("TestSequenceSizeErr",
426+
"""
427+
Py_ssize_t test_sq_length(PyObject* a) {
428+
PyErr_Format(PyExc_TypeError, "test type error");
429+
return -1;
430+
}
431+
PyObject* callSize(PyObject* a) {
432+
return PyLong_FromSsize_t(PySequence_Size(a));
433+
}
434+
""",
435+
tp_methods='{"callSize", (PyCFunction)callSize, METH_O, ""}',
436+
sq_length="&test_sq_length",
437+
)
438+
tester = TestSequenceSizeErr()
439+
try:
440+
tester.callSize(tester)
441+
except SystemError:
442+
assert True
443+
else:
444+
assert False
445+
446+
def test_sequence_size_err2(self):
447+
TestSequenceSizeErr2 = CPyExtType("TestSequenceSizeErr2",
448+
"""
449+
Py_ssize_t test_sq_length(PyObject* a) {
450+
PyErr_Format(PyExc_TypeError, "test type error");
451+
return -2;
452+
}
453+
PyObject* callSize(PyObject* a) {
454+
return PyLong_FromSsize_t(PySequence_Size(a));
455+
}
456+
""",
457+
tp_methods='{"callSize", (PyCFunction)callSize, METH_O, ""}',
458+
sq_length="&test_sq_length",
459+
)
460+
tester = TestSequenceSizeErr2()
461+
try:
462+
tester.callSize(tester)
463+
except SystemError:
464+
assert True
465+
else:
466+
assert False
467+
424468
def test_mapping_check(self):
425469
TestMappingCheck = CPyExtType("TestMappingCheck",
426470
"""
@@ -454,6 +498,28 @@ def test_mapping_size(self):
454498
tester = TestMappingSize()
455499
assert tester.callSize(tester) == 11
456500

501+
def test_mapping_size_err(self):
502+
TestMappingSizeErr = CPyExtType("TestMappingSizeErr",
503+
"""
504+
Py_ssize_t test_mp_length(PyObject* a) {
505+
PyErr_Format(PyExc_TypeError, "test type error");
506+
return -1;
507+
}
508+
PyObject* callSize(PyObject* a) {
509+
return PyLong_FromSsize_t(PyMapping_Size(a));
510+
}
511+
""",
512+
tp_methods='{"callSize", (PyCFunction)callSize, METH_O, ""}',
513+
mp_length="&test_mp_length",
514+
)
515+
tester = TestMappingSizeErr()
516+
try:
517+
tester.callSize(tester)
518+
except SystemError:
519+
assert True
520+
else:
521+
assert False
522+
457523
def test_object_size_sq(self):
458524
TestObjectSizeSQ = CPyExtType("TestObjectSizeSQ",
459525
"""
@@ -486,6 +552,28 @@ def test_object_size_mp(self):
486552
tester = TestObjectSizeMP()
487553
assert tester.callSize(tester) == 13
488554

555+
def test_object_size_err(self):
556+
TestObjectSizeErr = CPyExtType("TestObjectSizeErr",
557+
"""
558+
Py_ssize_t test_sq_length(PyObject* a) {
559+
PyErr_Format(PyExc_TypeError, "test type error");
560+
return -1;
561+
}
562+
PyObject* callSize(PyObject* a) {
563+
return PyLong_FromSsize_t(PyObject_Size(a));
564+
}
565+
""",
566+
tp_methods='{"callSize", (PyCFunction)callSize, METH_O, ""}',
567+
mp_length="&test_sq_length",
568+
)
569+
tester = TestObjectSizeErr()
570+
try:
571+
tester.callSize(tester)
572+
except SystemError:
573+
assert True
574+
else:
575+
assert False
576+
489577
class TestAbstract(CPyExtTestCase):
490578

491579
def compile_module(self, name):

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

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@
8383
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.ToSulongNode;
8484
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.TransformExceptionToNativeNode;
8585
import com.oracle.graal.python.builtins.objects.cext.capi.DynamicObjectNativeWrapper.PrimitiveNativeWrapper;
86-
import com.oracle.graal.python.builtins.objects.cext.capi.ExternalFunctionNodes.CheckPrimitiveFunctionResultNode;
8786
import com.oracle.graal.python.builtins.objects.dict.DictBuiltins.ItemsNode;
8887
import com.oracle.graal.python.builtins.objects.dict.DictBuiltins.KeysNode;
8988
import com.oracle.graal.python.builtins.objects.dict.DictBuiltins.ValuesNode;
@@ -117,7 +116,6 @@
117116
import com.oracle.graal.python.nodes.subscript.SliceLiteralNode;
118117
import com.oracle.graal.python.nodes.truffle.PythonTypes;
119118
import com.oracle.graal.python.runtime.ExecutionContext.IndirectCallContext;
120-
import com.oracle.graal.python.runtime.PythonContext;
121119
import com.oracle.graal.python.runtime.exception.PException;
122120
import com.oracle.truffle.api.CompilerDirectives;
123121
import com.oracle.truffle.api.dsl.Cached;
@@ -945,11 +943,8 @@ static boolean check(Object object,
945943
static Object doNative(VirtualFrame frame, Object object,
946944
@Cached ToSulongNode toSulongNode,
947945
@Cached PCallCapiFunction callCapiFunction,
948-
@Cached CheckPrimitiveFunctionResultNode checkFunctionResultNode,
949946
@Cached TransformExceptionToNativeNode transformExceptionToNativeNode) {
950-
Object result = callCapiFunction.call(FUN_PY_TRUFFLE_PY_SEQUENCE_CHECK, toSulongNode.execute(object));
951-
checkFunctionResultNode.execute(PythonContext.get(callCapiFunction), FUN_PY_TRUFFLE_PY_SEQUENCE_CHECK.getName(), result);
952-
return result;
947+
return callCapiFunction.call(FUN_PY_TRUFFLE_PY_SEQUENCE_CHECK, toSulongNode.execute(object));
953948
}
954949
}
955950

@@ -1004,13 +999,10 @@ static Object doSequence(VirtualFrame frame, Object obj,
1004999
Object doNative(VirtualFrame frame, Object obj,
10051000
@Cached ToSulongNode toSulongNode,
10061001
@Cached PCallCapiFunction callCapiFunction,
1007-
@Cached CheckPrimitiveFunctionResultNode checkFunctionResultNode,
10081002
@Cached TransformExceptionToNativeNode transformExceptionToNativeNode) {
10091003
Object state = IndirectCallContext.enter(frame, this);
10101004
try {
1011-
Object result = callCapiFunction.call(FUN_PY_TRUFFLE_PY_SEQUENCE_SIZE, toSulongNode.execute(obj));
1012-
checkFunctionResultNode.execute(PythonContext.get(callCapiFunction), FUN_PY_TRUFFLE_PY_SEQUENCE_SIZE.getName(), result);
1013-
return result;
1005+
return callCapiFunction.call(FUN_PY_TRUFFLE_PY_SEQUENCE_SIZE, toSulongNode.execute(obj));
10141006
} catch (PException e) {
10151007
transformExceptionToNativeNode.execute(frame, e);
10161008
return -1;
@@ -1079,13 +1071,10 @@ Object size(VirtualFrame frame, Object obj,
10791071
@Cached ToSulongNode toSulongNode,
10801072
@Cached AsPythonObjectNode asPythonObjectNode,
10811073
@Cached PCallCapiFunction callCapiFunction,
1082-
@Cached CheckPrimitiveFunctionResultNode checkFunctionResultNode,
10831074
@Cached TransformExceptionToNativeNode transformExceptionToNativeNode) {
10841075
Object state = IndirectCallContext.enter(frame, this);
10851076
try {
1086-
Object result = callCapiFunction.call(FUN_PY_TRUFFLE_PY_OBJECT_SIZE, toSulongNode.execute(obj));
1087-
checkFunctionResultNode.execute(PythonContext.get(callCapiFunction), FUN_PY_TRUFFLE_PY_OBJECT_SIZE.getName(), result);
1088-
return result;
1077+
return callCapiFunction.call(FUN_PY_TRUFFLE_PY_OBJECT_SIZE, toSulongNode.execute(obj));
10891078
} catch (PException e) {
10901079
transformExceptionToNativeNode.execute(frame, e);
10911080
return -1;
@@ -1213,11 +1202,8 @@ static boolean doPythonObject(Object object,
12131202
static Object doNative(VirtualFrame frame, Object obj,
12141203
@Cached ToSulongNode toSulongNode,
12151204
@Cached PCallCapiFunction callCapiFunction,
1216-
@Cached CheckPrimitiveFunctionResultNode checkFunctionResultNode,
12171205
@Cached TransformExceptionToNativeNode transformExceptionToNativeNode) {
1218-
Object result = callCapiFunction.call(FUN_PY_TRUFFLE_PY_MAPPING_CHECK, toSulongNode.execute(obj));
1219-
checkFunctionResultNode.execute(PythonContext.get(callCapiFunction), FUN_PY_TRUFFLE_PY_MAPPING_CHECK.getName(), result);
1220-
return result;
1206+
return callCapiFunction.call(FUN_PY_TRUFFLE_PY_MAPPING_CHECK, toSulongNode.execute(obj));
12211207
}
12221208
}
12231209

@@ -1247,12 +1233,9 @@ static int doMapping(VirtualFrame frame, Object obj,
12471233
static Object doNative(VirtualFrame frame, Object obj,
12481234
@Cached ToSulongNode toSulongNode,
12491235
@Cached PCallCapiFunction callCapiFunction,
1250-
@Cached CheckPrimitiveFunctionResultNode checkFunctionResultNode,
12511236
@Cached TransformExceptionToNativeNode transformExceptionToNativeNode) {
12521237
try {
1253-
Object result = callCapiFunction.call(FUN_PY_TRUFFLE_PY_MAPPING_SIZE, toSulongNode.execute(obj));
1254-
checkFunctionResultNode.execute(PythonContext.get(callCapiFunction), FUN_PY_TRUFFLE_PY_MAPPING_SIZE.getName(), result);
1255-
return result;
1238+
return callCapiFunction.call(FUN_PY_TRUFFLE_PY_MAPPING_SIZE, toSulongNode.execute(obj));
12561239
} catch (PException e) {
12571240
transformExceptionToNativeNode.execute(frame, e);
12581241
return -1;

0 commit comments

Comments
 (0)