1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
|
from rpython.rlib.nonconst import NonConstant
from rpython.rtyper.lltypesystem import rffi, lltype, ll2ctypes
from rpython.rlib.objectmodel import we_are_translated
from rpython.rlib import rposix
from pypy.interpreter.error import (
OperationError, oefmt, new_exception_class, strerror as _strerror)
from pypy.module._hpy_universal.apiset import API
#from pypy.module._hpy_universal.bridge import BRIDGE
from pypy.module._hpy_universal import llapi
from pypy.module._hpy_universal.interp_unicode import _maybe_utf8_to_w
from .state import State
## HPy exceptions in PyPy
## ======================
##
## HPy exceptions are implemented using normal RPython exceptions, which means
## that e.g. HPyErr_SetString simply raises an OperationError: see
## e.g. test_exception_transform.test_llhelper_can_raise for a test which
## ensure that exceptions correctly propagate.
##
## Moreover, we need to ensure that it is NOT possible to call RPython code
## when an RPython exception is set, else you get unexpected results. The plan
## is to document that it's forbidden to call most HPy functions if an
## exception has been set, apart for few functions, such as:
##
## - HPyErr_Occurred()
## - HPyErr_Fetch()
## - HPyErr_Clear()
##
## We need to enforce this in debug mode.
##
## ~~~ Implementation ~~~
##
## HPyErr_FatalError is implemented in C. See also:
## - src/hpyerr.c for the source code
## - state.py:setup.ctx which explicitly stores the C functions in the ctx
@API.func("HPy HPyErr_SetString(HPyContext *ctx, HPy type, const char *message)")
def HPyErr_SetString(space, handles, ctx, h_exc_type, utf8):
w_obj = _maybe_utf8_to_w(space, utf8)
w_exc_type = handles.deref(h_exc_type)
operr = OperationError(w_exc_type, w_obj)
operr.record_context(space, space.getexecutioncontext())
state = space.fromcache(State)
state.set_exception(operr)
return 0 # HPy_NULL
@API.func("HPy HPyErr_SetObject(HPyContext *ctx, HPy type, HPy value)")
def HPyErr_SetObject(space, handles, ctx, h_exc_type, h_exc_value):
w_exc_type = handles.deref(h_exc_type)
w_obj = handles.deref(h_exc_value)
operr = OperationError(w_exc_type, w_obj)
operr.record_context(space, space.getexecutioncontext())
state = space.fromcache(State)
state.set_exception(operr)
return 0 # HPy_NULL
@API.func("HPy HPyErr_SetFromErrnoWithFilename(HPyContext *ctx, HPy type, const char *filename)")
def HPyErr_SetFromErrnoWithFilename(space, handles, ctx, h_exc_type, utf8):
w_fname = _maybe_utf8_to_w(space, utf8)
w_exc_type = handles.deref(h_exc_type)
errno = rffi.cast(lltype.Signed, rposix._get_errno())
msg, lgt = _strerror(errno)
if w_fname:
w_error = space.call_function(w_exc_type,
space.newint(errno),
space.newtext(msg, lgt),
w_fname)
else:
w_error = space.call_function(w_exc_type,
space.newint(errno),
space.newtext(msg, lgt))
raise OperationError(w_exc_type, w_error)
@API.func("HPy HPyErr_SetFromErrnoWithFilenameObjects(HPyContext *ctx, HPy type, HPy filename1, HPy filename2)")
def HPyErr_SetFromErrnoWithFilenameObjects(space, handles, ctx, h_exc_type, h_fname1, h_fname2):
w_exc_type = handles.deref(h_exc_type)
if h_fname1:
w_fname1 = handles.deref(h_fname1)
else:
w_fname1 = None
if h_fname2:
w_fname2 = handles.deref(h_fname2)
else:
w_fname2 = None
errno = rffi.cast(lltype.Signed, rposix._get_errno())
msg, lgt = _strerror(errno)
if w_fname1:
if w_fname2:
w_error = space.call_function(w_exc_type,
space.newint(errno),
space.newtext(msg, lgt),
w_fname1, None, w_fname2)
else:
w_error = space.call_function(w_exc_type,
space.newint(errno),
space.newtext(msg, lgt),
w_fname1)
else:
w_error = space.call_function(w_exc_type,
space.newint(errno),
space.newtext(msg, lgt))
raise OperationError(w_exc_type, w_error)
@API.func("int HPyErr_Occurred(HPyContext *ctx)", error_value=API.int(-1))
def HPyErr_Occurred(space, handles, ctx):
state = space.fromcache(State)
operror = state.get_exception()
return API.int(operror is not None)
@API.func("void HPyErr_Clear(HPyContext *ctx)")
def HPyErr_Clear(space, handles, ctx):
state = space.fromcache(State)
state.clear_exception()
## ~~~ API Functions ~~~~
## The following are normal @API functions, so they contain the "real"
## implementation.
@API.func("HPy HPyErr_NoMemory(HPyContext *ctx)")
def HPyErr_NoMemory(space, handles, ctx):
# hack to convince the annotator that this function returns an HPy (i.e.,
# a Signed)
if NonConstant(False):
return -42
raise OperationError(space.w_MemoryError, space.w_None)
@API.func("HPy HPyErr_NewException(HPyContext *ctx, const char *name, HPy base, HPy dict)")
def HPyErr_NewException(space, handles, ctx, c_name, h_base, h_dict):
name = rffi.constcharp2str(c_name)
if '.' not in name:
raise oefmt(space.w_SystemError,
"HPyErr_NewException: name must be module.class")
if h_base:
w_base = handles.deref(h_base)
else:
w_base = space.w_Exception
if h_dict:
w_dict = handles.deref(h_dict)
else:
w_dict = None
return handles.new(new_exception_class(space, name, w_base, w_dict))
@API.func("HPy HPyErr_NewExceptionWithDoc("
"HPyContext *ctx, const char *name, const char* doc, HPy base, HPy dict)")
def HPyErr_NewExceptionWithDoc(space, handles, ctx, c_name, c_doc, h_base, h_dict):
name = rffi.constcharp2str(c_name)
if '.' not in name:
raise oefmt(space.w_SystemError,
"HPyErr_NewException: name must be module.class")
if h_base:
w_base = handles.deref(h_base)
else:
w_base = space.w_Exception
if h_dict:
w_dict = handles.deref(h_dict)
else:
w_dict = space.newdict()
if c_doc:
doc = rffi.constcharp2str(c_doc)
space.setitem_str(w_dict, "__doc__", space.newtext(doc))
return handles.new(new_exception_class(space, name, w_base, w_dict))
@API.func("HPy HPyErr_ExceptionMatches(HPyContext *ctx, HPy exc)")
def HPyErr_ExceptionMatches(space, handles, ctx, h_exc):
w_exc = handles.deref(h_exc)
if not w_exc:
return 0
w_exc = handles.deref(h_exc)
# this is taken from PyErr_Ocurred
state = space.fromcache(State)
operror = state.get_exception()
if operror is None:
return 0
w_given = operror.w_type
# this is taken from PyErr_GivenExceptionMatches
if space.isinstance_w(w_given, space.w_BaseException):
w_given_type = space.type(w_given)
else:
w_given_type = w_given
try:
return int(space.exception_match(w_given_type, w_exc))
except:
return 0
@API.func("int HPyErr_WarnEx(HPyContext *ctx, HPy category, const char *message, HPy_ssize_t stack_level)", error_value=API.int(-1))
def HPyErr_WarnEx(space, handles, ctx, h_category, message, stack_level):
from pypy.module._hpy_universal.interp_import import import_name
if h_category:
w_category = handles.deref(h_category)
else:
w_category = space.w_None
w_message = _maybe_utf8_to_w(space, message)
w_stacklevel = space.newint(rffi.cast(lltype.Signed, stack_level))
w_module = import_name(space, space.newtext("warnings"))
w_warn = space.getattr(w_module, space.newtext("warn"))
space.call_function(w_warn, w_message, w_category, w_stacklevel)
return API.int(0)
@API.func("void HPyErr_WriteUnraisable(HPyContext *ctx, HPy obj)")
def HPyErr_WriteUnraisable(space, handles, ctx, h_where):
"""This utility function prints a warning message to sys.stderr when an
exception has been set but it is impossible for the interpreter to actually
raise the exception. It is used, for example, when an exception occurs in
an __del__() method.
The function is called with a single argument obj that identifies the
context in which the unraisable exception occurred. The repr of obj will be
printed in the warning message."""
if not h_where:
where = ''
else:
w_where = handles.deref(h_where)
where = space.text_w(space.repr(w_where))
state = space.fromcache(State)
operror = state.clear_exception()
if operror:
operror.write_unraisable(space, where)
|