| 1 |
|
| 2 |
|
| 3 |
from pypy.module.cpyext.api import cpython_api |
| 4 |
from pypy.module.cpyext.pyobject import PyObject, from_ref, decref |
| 5 |
from rpython.rtyper.lltypesystem import rffi, lltype |
| 6 |
|
| 7 |
|
| 8 |
@cpython_api([PyObject], PyObject) |
| 9 |
def PyExceptionInstance_Class(space, w_obj): |
| 10 |
return space.type(w_obj) |
| 11 |
|
| 12 |
|
| 13 |
@cpython_api([PyObject], PyObject) |
| 14 |
def PyException_GetTraceback(space, w_exc): |
| 15 |
"""Return the traceback associated with the exception as a new reference, as |
| 16 |
accessible from Python through __traceback__. If there is no |
| 17 |
traceback associated, this returns NULL.""" |
| 18 |
w_tb = space.getattr(w_exc, space.newtext('__traceback__')) |
| 19 |
if space.is_none(w_tb): |
| 20 |
return None |
| 21 |
return w_tb |
| 22 |
|
| 23 |
|
| 24 |
@cpython_api([PyObject, PyObject], rffi.INT_real, error=-1) |
| 25 |
def PyException_SetTraceback(space, w_exc, w_tb): |
| 26 |
"""Set the traceback associated with the exception to tb. Use Py_None to |
| 27 |
clear it.""" |
| 28 |
space.setattr(w_exc, space.newtext('__traceback__'), w_tb) |
| 29 |
return 0 |
| 30 |
|
| 31 |
|
| 32 |
@cpython_api([PyObject], PyObject) |
| 33 |
def PyException_GetContext(space, w_exc): |
| 34 |
"""Return the context (another exception instance during whose handling ex was |
| 35 |
raised) associated with the exception as a new reference, as accessible from |
| 36 |
Python through __context__. If there is no context associated, this |
| 37 |
returns NULL.""" |
| 38 |
w_ctx = space.getattr(w_exc, space.newtext('__context__')) |
| 39 |
if space.is_none(w_ctx): |
| 40 |
return None |
| 41 |
return w_ctx |
| 42 |
|
| 43 |
|
| 44 |
@cpython_api([PyObject, PyObject], lltype.Void) |
| 45 |
def PyException_SetContext(space, w_exc, ctx): |
| 46 |
"""Set the context associated with the exception to ctx. Use NULL to clear |
| 47 |
it. There is no type check to make sure that ctx is an exception instance. |
| 48 |
This steals a reference to ctx.""" |
| 49 |
if ctx: |
| 50 |
w_ctx = from_ref(space, ctx) |
| 51 |
decref(space, ctx) |
| 52 |
else: |
| 53 |
w_ctx = space.w_None |
| 54 |
space.setattr(w_exc, space.newtext('__context__'), w_ctx) |
| 55 |
|
| 56 |
@cpython_api([PyObject], PyObject) |
| 57 |
def PyException_GetCause(space, w_exc): |
| 58 |
"""Return the cause (another exception instance set by raise ... from ...) |
| 59 |
associated with the exception as a new reference, as accessible from Python |
| 60 |
through __cause__. If there is no cause associated, this returns |
| 61 |
NULL.""" |
| 62 |
w_cause = space.getattr(w_exc, space.newtext('__cause__')) |
| 63 |
if space.is_none(w_cause): |
| 64 |
return None |
| 65 |
return w_cause |
| 66 |
|
| 67 |
|
| 68 |
@cpython_api([PyObject, PyObject], lltype.Void) |
| 69 |
def PyException_SetCause(space, w_exc, cause): |
| 70 |
"""Set the cause associated with the exception to cause. Use NULL to clear |
| 71 |
it. There is no type check to make sure that cause is an exception instance. |
| 72 |
This steals a reference to cause.""" |
| 73 |
if cause: |
| 74 |
w_cause = from_ref(space, cause) |
| 75 |
decref(space, cause) |
| 76 |
else: |
| 77 |
w_cause = space.w_None |
| 78 |
space.setattr(w_exc, space.newtext('__cause__'), w_cause) |
| 79 |
|