| 1 |
from rpython.rlib.nonconst import NonConstant |
| 2 |
from rpython.rtyper.lltypesystem import rffi, lltype, ll2ctypes |
| 3 |
from rpython.rlib.objectmodel import we_are_translated |
| 4 |
from rpython.rlib import rposix |
| 5 |
from pypy.interpreter.error import ( |
| 6 |
OperationError, oefmt, new_exception_class, strerror as _strerror) |
| 7 |
from pypy.module._hpy_universal.apiset import API |
| 8 |
|
| 9 |
from pypy.module._hpy_universal import llapi |
| 10 |
from pypy.module._hpy_universal.interp_unicode import _maybe_utf8_to_w |
| 11 |
from .state import State |
| 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 |
@API.func("HPy HPyErr_SetString(HPyContext *ctx, HPy type, const char *message)") |
| 40 |
def HPyErr_SetString(space, handles, ctx, h_exc_type, utf8): |
| 41 |
w_obj = _maybe_utf8_to_w(space, utf8) |
| 42 |
w_exc_type = handles.deref(h_exc_type) |
| 43 |
operr = OperationError(w_exc_type, w_obj) |
| 44 |
operr.record_context(space, space.getexecutioncontext()) |
| 45 |
state = space.fromcache(State) |
| 46 |
state.set_exception(operr) |
| 47 |
return 0 |
| 48 |
|
| 49 |
@API.func("HPy HPyErr_SetObject(HPyContext *ctx, HPy type, HPy value)") |
| 50 |
def HPyErr_SetObject(space, handles, ctx, h_exc_type, h_exc_value): |
| 51 |
w_exc_type = handles.deref(h_exc_type) |
| 52 |
w_obj = handles.deref(h_exc_value) |
| 53 |
operr = OperationError(w_exc_type, w_obj) |
| 54 |
operr.record_context(space, space.getexecutioncontext()) |
| 55 |
state = space.fromcache(State) |
| 56 |
state.set_exception(operr) |
| 57 |
return 0 |
| 58 |
|
| 59 |
@API.func("HPy HPyErr_SetFromErrnoWithFilename(HPyContext *ctx, HPy type, const char *filename)") |
| 60 |
def HPyErr_SetFromErrnoWithFilename(space, handles, ctx, h_exc_type, utf8): |
| 61 |
w_fname = _maybe_utf8_to_w(space, utf8) |
| 62 |
w_exc_type = handles.deref(h_exc_type) |
| 63 |
errno = rffi.cast(lltype.Signed, rposix._get_errno()) |
| 64 |
msg, lgt = _strerror(errno) |
| 65 |
if w_fname: |
| 66 |
w_error = space.call_function(w_exc_type, |
| 67 |
space.newint(errno), |
| 68 |
space.newtext(msg, lgt), |
| 69 |
w_fname) |
| 70 |
else: |
| 71 |
w_error = space.call_function(w_exc_type, |
| 72 |
space.newint(errno), |
| 73 |
space.newtext(msg, lgt)) |
| 74 |
raise OperationError(w_exc_type, w_error) |
| 75 |
|
| 76 |
@API.func("HPy HPyErr_SetFromErrnoWithFilenameObjects(HPyContext *ctx, HPy type, HPy filename1, HPy filename2)") |
| 77 |
def HPyErr_SetFromErrnoWithFilenameObjects(space, handles, ctx, h_exc_type, h_fname1, h_fname2): |
| 78 |
w_exc_type = handles.deref(h_exc_type) |
| 79 |
if h_fname1: |
| 80 |
w_fname1 = handles.deref(h_fname1) |
| 81 |
else: |
| 82 |
w_fname1 = None |
| 83 |
if h_fname2: |
| 84 |
w_fname2 = handles.deref(h_fname2) |
| 85 |
else: |
| 86 |
w_fname2 = None |
| 87 |
errno = rffi.cast(lltype.Signed, rposix._get_errno()) |
| 88 |
msg, lgt = _strerror(errno) |
| 89 |
if w_fname1: |
| 90 |
if w_fname2: |
| 91 |
w_error = space.call_function(w_exc_type, |
| 92 |
space.newint(errno), |
| 93 |
space.newtext(msg, lgt), |
| 94 |
w_fname1, space.w_None, w_fname2) |
| 95 |
else: |
| 96 |
w_error = space.call_function(w_exc_type, |
| 97 |
space.newint(errno), |
| 98 |
space.newtext(msg, lgt), |
| 99 |
w_fname1) |
| 100 |
else: |
| 101 |
w_error = space.call_function(w_exc_type, |
| 102 |
space.newint(errno), |
| 103 |
space.newtext(msg, lgt)) |
| 104 |
raise OperationError(w_exc_type, w_error) |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
@API.func("int HPyErr_Occurred(HPyContext *ctx)", error_value=API.int(-1)) |
| 109 |
def HPyErr_Occurred(space, handles, ctx): |
| 110 |
state = space.fromcache(State) |
| 111 |
operror = state.get_exception() |
| 112 |
return API.int(operror is not None) |
| 113 |
|
| 114 |
@API.func("void HPyErr_Clear(HPyContext *ctx)") |
| 115 |
def HPyErr_Clear(space, handles, ctx): |
| 116 |
state = space.fromcache(State) |
| 117 |
state.clear_exception() |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
@API.func("HPy HPyErr_NoMemory(HPyContext *ctx)") |
| 125 |
def HPyErr_NoMemory(space, handles, ctx): |
| 126 |
|
| 127 |
|
| 128 |
if NonConstant(False): |
| 129 |
return -42 |
| 130 |
raise OperationError(space.w_MemoryError, space.w_None) |
| 131 |
|
| 132 |
@API.func("HPy HPyErr_NewException(HPyContext *ctx, const char *name, HPy base, HPy dict)") |
| 133 |
def HPyErr_NewException(space, handles, ctx, c_name, h_base, h_dict): |
| 134 |
name = rffi.constcharp2str(c_name) |
| 135 |
if '.' not in name: |
| 136 |
raise oefmt(space.w_SystemError, |
| 137 |
"HPyErr_NewException: name must be module.class") |
| 138 |
if h_base: |
| 139 |
w_base = handles.deref(h_base) |
| 140 |
else: |
| 141 |
w_base = space.w_Exception |
| 142 |
if h_dict: |
| 143 |
w_dict = handles.deref(h_dict) |
| 144 |
else: |
| 145 |
w_dict = None |
| 146 |
|
| 147 |
return handles.new(new_exception_class(space, name, w_base, w_dict)) |
| 148 |
|
| 149 |
@API.func("HPy HPyErr_NewExceptionWithDoc(" |
| 150 |
"HPyContext *ctx, const char *name, const char* doc, HPy base, HPy dict)") |
| 151 |
def HPyErr_NewExceptionWithDoc(space, handles, ctx, c_name, c_doc, h_base, h_dict): |
| 152 |
name = rffi.constcharp2str(c_name) |
| 153 |
if '.' not in name: |
| 154 |
raise oefmt(space.w_SystemError, |
| 155 |
"HPyErr_NewException: name must be module.class") |
| 156 |
if h_base: |
| 157 |
w_base = handles.deref(h_base) |
| 158 |
else: |
| 159 |
w_base = space.w_Exception |
| 160 |
if h_dict: |
| 161 |
w_dict = handles.deref(h_dict) |
| 162 |
else: |
| 163 |
w_dict = space.newdict() |
| 164 |
if c_doc: |
| 165 |
doc = rffi.constcharp2str(c_doc) |
| 166 |
space.setitem_str(w_dict, "__doc__", space.newtext(doc)) |
| 167 |
|
| 168 |
return handles.new(new_exception_class(space, name, w_base, w_dict)) |
| 169 |
|
| 170 |
@API.func("HPy HPyErr_ExceptionMatches(HPyContext *ctx, HPy exc)") |
| 171 |
def HPyErr_ExceptionMatches(space, handles, ctx, h_exc): |
| 172 |
w_exc = handles.deref(h_exc) |
| 173 |
if not w_exc: |
| 174 |
return 0 |
| 175 |
w_exc = handles.deref(h_exc) |
| 176 |
|
| 177 |
state = space.fromcache(State) |
| 178 |
operror = state.get_exception() |
| 179 |
if operror is None: |
| 180 |
return 0 |
| 181 |
w_given = operror.w_type |
| 182 |
|
| 183 |
if space.isinstance_w(w_given, space.w_BaseException): |
| 184 |
w_given_type = space.type(w_given) |
| 185 |
else: |
| 186 |
w_given_type = w_given |
| 187 |
try: |
| 188 |
return int(space.exception_match(w_given_type, w_exc)) |
| 189 |
except: |
| 190 |
return 0 |
| 191 |
|
| 192 |
@API.func("int HPyErr_WarnEx(HPyContext *ctx, HPy category, const char *message, HPy_ssize_t stack_level)", error_value=API.int(-1)) |
| 193 |
def HPyErr_WarnEx(space, handles, ctx, h_category, message, stack_level): |
| 194 |
from pypy.module._hpy_universal.interp_import import import_name |
| 195 |
|
| 196 |
if h_category: |
| 197 |
w_category = handles.deref(h_category) |
| 198 |
else: |
| 199 |
w_category = space.w_None |
| 200 |
w_message = _maybe_utf8_to_w(space, message) |
| 201 |
w_stacklevel = space.newint(rffi.cast(lltype.Signed, stack_level)) |
| 202 |
|
| 203 |
w_module = import_name(space, space.newtext("warnings")) |
| 204 |
w_warn = space.getattr(w_module, space.newtext("warn")) |
| 205 |
space.call_function(w_warn, w_message, w_category, w_stacklevel) |
| 206 |
return API.int(0) |
| 207 |
|
| 208 |
@API.func("void HPyErr_WriteUnraisable(HPyContext *ctx, HPy obj)") |
| 209 |
def HPyErr_WriteUnraisable(space, handles, ctx, h_where): |
| 210 |
"""This utility function prints a warning message to sys.stderr when an |
| 211 |
exception has been set but it is impossible for the interpreter to actually |
| 212 |
raise the exception. It is used, for example, when an exception occurs in |
| 213 |
an __del__() method. |
| 214 |
|
| 215 |
The function is called with a single argument obj that identifies the |
| 216 |
context in which the unraisable exception occurred. The repr of obj will be |
| 217 |
printed in the warning message.""" |
| 218 |
if not h_where: |
| 219 |
where = '' |
| 220 |
else: |
| 221 |
w_where = handles.deref(h_where) |
| 222 |
where = space.text_w(space.repr(w_where)) |
| 223 |
state = space.fromcache(State) |
| 224 |
operror = state.clear_exception() |
| 225 |
if operror: |
| 226 |
operror.write_unraisable(space, where) |