Skip to content

Commit b4a51b3

Browse files
committed
Add tests for PyException_Set*
1 parent 0029871 commit b4a51b3

File tree

1 file changed

+71
-8
lines changed

1 file changed

+71
-8
lines changed

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

Lines changed: 71 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@
4646

4747
try:
4848
raise TypeError
49-
except:
49+
except TypeError as e:
5050
TB = sys.exc_info()[2]
51+
exception_with_traceback = e
5152

5253

5354
class TestExceptionobject(object):
@@ -148,18 +149,80 @@ def raise_exception_without_cause():
148149
return e
149150

150151

152+
exception_with_cause = AssertionError()
153+
exception_with_cause.__cause__ = NameError()
154+
exception_with_context = AssertionError()
155+
exception_with_context.__context__ = AttributeError()
156+
151157
class TestExceptionobjectFunctions(CPyExtTestCase):
152158

153159
test_PyException_SetTraceback = CPyExtFunction(
154-
lambda args: 0,
160+
lambda args: args[1],
161+
lambda: (
162+
(AssertionError(), TB),
163+
(exception_with_traceback, None),
164+
),
165+
code='''
166+
static PyObject* wrap_PyException_SetTraceback(PyObject* exc, PyObject* traceback) {
167+
PyException_SetTraceback(exc, traceback);
168+
traceback = PyException_GetTraceback(exc);
169+
if (traceback == NULL) {
170+
Py_RETURN_NONE;
171+
} else {
172+
return traceback;
173+
}
174+
}
175+
''',
176+
callfunction='wrap_PyException_SetTraceback',
177+
argspec='OO',
178+
arguments=['PyObject* exc', 'PyObject* traceback'],
179+
resultspec='O',
180+
)
181+
182+
test_PyException_SetCause = CPyExtFunction(
183+
lambda args: args[1],
184+
lambda: (
185+
(AssertionError(), NameError()),
186+
(exception_with_context, None),
187+
),
188+
code='''
189+
static PyObject* wrap_PyException_SetCause(PyObject* exc, PyObject* cause) {
190+
PyException_SetCause(exc, cause != Py_None ? cause : NULL);
191+
cause = PyException_GetCause(exc);
192+
if (cause == NULL) {
193+
Py_RETURN_NONE;
194+
} else {
195+
return cause;
196+
}
197+
}
198+
''',
199+
callfunction='wrap_PyException_SetCause',
200+
argspec='OO',
201+
arguments=['PyObject* exc', 'PyObject* cause'],
202+
resultspec='O',
203+
)
204+
205+
test_PyException_SetContext = CPyExtFunction(
206+
lambda args: args[1],
155207
lambda: (
156-
(
157-
AssertionError(), TB
158-
),
208+
(AssertionError(), NameError()),
209+
(exception_with_cause, None),
159210
),
160-
resultspec="i",
161-
argspec="OO",
162-
arguments=["PyObject* exc", "PyObject* tb"],
211+
code='''
212+
static PyObject* wrap_PyException_SetContext(PyObject* exc, PyObject* context) {
213+
PyException_SetContext(exc, context != Py_None ? context : NULL);
214+
context = PyException_GetContext(exc);
215+
if (context == NULL) {
216+
Py_RETURN_NONE;
217+
} else {
218+
return context;
219+
}
220+
}
221+
''',
222+
callfunction='wrap_PyException_SetContext',
223+
argspec='OO',
224+
arguments=['PyObject* exc', 'PyObject* context'],
225+
resultspec='O',
163226
)
164227

165228

0 commit comments

Comments
 (0)