| 1 |
import pytest |
| 2 |
import sys |
| 3 |
import StringIO |
| 4 |
|
| 5 |
from pypy.module.cpyext.state import State |
| 6 |
from pypy.module.cpyext.pyobject import make_ref |
| 7 |
from pypy.module.cpyext.test.test_api import BaseApiTest |
| 8 |
from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase |
| 9 |
from rpython.rtyper.lltypesystem import rffi |
| 10 |
|
| 11 |
class TestExceptions(BaseApiTest): |
| 12 |
def test_GivenExceptionMatches(self, space, api): |
| 13 |
exc_matches = api.PyErr_GivenExceptionMatches |
| 14 |
|
| 15 |
string_exception = space.wrap('exception') |
| 16 |
instance = space.call_function(space.w_ValueError) |
| 17 |
assert exc_matches(string_exception, string_exception) |
| 18 |
assert exc_matches(instance, space.w_ValueError) |
| 19 |
assert exc_matches(space.w_ValueError, space.w_ValueError) |
| 20 |
assert exc_matches(space.w_IndexError, space.w_LookupError) |
| 21 |
assert not exc_matches(space.w_ValueError, space.w_LookupError) |
| 22 |
|
| 23 |
exceptions = space.newtuple([space.w_LookupError, space.w_ValueError]) |
| 24 |
assert exc_matches(space.w_ValueError, exceptions) |
| 25 |
|
| 26 |
def test_ExceptionMatches(self, space, api): |
| 27 |
api.PyErr_SetObject(space.w_ValueError, space.wrap("message")) |
| 28 |
assert api.PyErr_ExceptionMatches(space.w_Exception) |
| 29 |
assert api.PyErr_ExceptionMatches(space.w_ValueError) |
| 30 |
assert not api.PyErr_ExceptionMatches(space.w_TypeError) |
| 31 |
|
| 32 |
api.PyErr_Clear() |
| 33 |
|
| 34 |
def test_Occurred(self, space, api): |
| 35 |
assert not api.PyErr_Occurred() |
| 36 |
string = rffi.str2charp("spam and eggs") |
| 37 |
api.PyErr_SetString(space.w_ValueError, string) |
| 38 |
rffi.free_charp(string) |
| 39 |
assert api.PyErr_Occurred() is space.w_ValueError |
| 40 |
|
| 41 |
api.PyErr_Clear() |
| 42 |
|
| 43 |
def test_SetObject(self, space, api): |
| 44 |
api.PyErr_SetObject(space.w_ValueError, space.wrap("a value")) |
| 45 |
assert api.PyErr_Occurred() is space.w_ValueError |
| 46 |
state = space.fromcache(State) |
| 47 |
operror = state.get_exception() |
| 48 |
assert space.eq_w(operror.get_w_value(space), |
| 49 |
space.wrap("a value")) |
| 50 |
|
| 51 |
api.PyErr_Clear() |
| 52 |
|
| 53 |
def test_SetNone(self, space, api): |
| 54 |
api.PyErr_SetNone(space.w_KeyError) |
| 55 |
state = space.fromcache(State) |
| 56 |
operror = state.get_exception() |
| 57 |
assert space.eq_w(operror.w_type, space.w_KeyError) |
| 58 |
assert space.eq_w(operror.get_w_value(space), space.w_None) |
| 59 |
api.PyErr_Clear() |
| 60 |
|
| 61 |
api.PyErr_NoMemory() |
| 62 |
operror = state.get_exception() |
| 63 |
assert space.eq_w(operror.w_type, space.w_MemoryError) |
| 64 |
api.PyErr_Clear() |
| 65 |
|
| 66 |
def test_Warning(self, space, api, capfd): |
| 67 |
message = rffi.str2charp("this is a warning") |
| 68 |
api.PyErr_WarnEx(None, message, 1) |
| 69 |
space.call_method(space.sys.get('stderr'), "flush") |
| 70 |
out, err = capfd.readouterr() |
| 71 |
assert ": UserWarning: this is a warning" in err |
| 72 |
rffi.free_charp(message) |
| 73 |
|
| 74 |
def test_WarnExplicit(self, space, api, capfd): |
| 75 |
message = rffi.str2charp("this is a warning") |
| 76 |
filename = rffi.str2charp("file.py") |
| 77 |
lineno = 12 |
| 78 |
module = None |
| 79 |
api.PyErr_WarnExplicit(None, message, filename, lineno, None, None) |
| 80 |
space.call_method(space.sys.get('stderr'), "flush") |
| 81 |
out, err = capfd.readouterr() |
| 82 |
assert ": UserWarning: this is a warning" in err |
| 83 |
rffi.free_charp(message) |
| 84 |
rffi.free_charp(filename) |
| 85 |
|
| 86 |
def test_print_err(self, space, api, capfd): |
| 87 |
api.PyErr_SetObject(space.w_Exception, space.wrap("cpyext is cool")) |
| 88 |
api.PyErr_Print() |
| 89 |
space.call_method(space.sys.get('stderr'), "flush") |
| 90 |
out, err = capfd.readouterr() |
| 91 |
assert "cpyext is cool" in err |
| 92 |
assert not api.PyErr_Occurred() |
| 93 |
|
| 94 |
@pytest.mark.skipif(True, reason='not implemented yet') |
| 95 |
def test_interrupt_occurred(self, space, api): |
| 96 |
assert not api.PyOS_InterruptOccurred() |
| 97 |
import signal, os |
| 98 |
recieved = [] |
| 99 |
def default_int_handler(*args): |
| 100 |
recieved.append('ok') |
| 101 |
signal.signal(signal.SIGINT, default_int_handler) |
| 102 |
os.kill(os.getpid(), signal.SIGINT) |
| 103 |
assert recieved == ['ok'] |
| 104 |
assert api.PyOS_InterruptOccurred() |
| 105 |
|
| 106 |
def test_restore_traceback(self, space, api): |
| 107 |
string = rffi.str2charp("spam and eggs") |
| 108 |
api.PyErr_SetString(space.w_ValueError, string) |
| 109 |
|
| 110 |
state = space.fromcache(State) |
| 111 |
operror = state.clear_exception() |
| 112 |
|
| 113 |
|
| 114 |
operror.set_traceback(space.w_True) |
| 115 |
|
| 116 |
w_type = operror.w_type |
| 117 |
w_value = operror.get_w_value(space) |
| 118 |
w_tb = operror.get_w_traceback(space) |
| 119 |
|
| 120 |
assert not space.eq_w(w_tb, space.w_None) |
| 121 |
|
| 122 |
api.PyErr_Restore(make_ref(space, w_type), make_ref(space, w_value), make_ref(space, w_tb)) |
| 123 |
|
| 124 |
operror = state.clear_exception() |
| 125 |
w_tb_restored = operror.get_w_traceback(space) |
| 126 |
|
| 127 |
assert space.eq_w(w_tb_restored, w_tb) |
| 128 |
rffi.free_charp(string) |
| 129 |
|
| 130 |
class AppTestFetch(AppTestCpythonExtensionBase): |
| 131 |
def setup_class(cls): |
| 132 |
from pypy.interpreter.test.test_fsencode import get_special_char |
| 133 |
space = cls.space |
| 134 |
cls.special_char = get_special_char() |
| 135 |
cls.w_special_char = space.wrap(cls.special_char) |
| 136 |
AppTestCpythonExtensionBase.setup_class.im_func(cls) |
| 137 |
cls.w_runappdirect = cls.space.wrap(cls.runappdirect) |
| 138 |
|
| 139 |
|
| 140 |
def test_occurred(self): |
| 141 |
module = self.import_extension('foo', [ |
| 142 |
("check_error", "METH_NOARGS", |
| 143 |
''' |
| 144 |
PyErr_SetString(PyExc_TypeError, "message"); |
| 145 |
PyErr_Occurred(); |
| 146 |
PyErr_Clear(); |
| 147 |
Py_RETURN_TRUE; |
| 148 |
''' |
| 149 |
), |
| 150 |
]) |
| 151 |
assert module.check_error() |
| 152 |
|
| 153 |
def test_fetch_and_restore(self): |
| 154 |
module = self.import_extension('foo', [ |
| 155 |
("check_error", "METH_NOARGS", |
| 156 |
''' |
| 157 |
PyObject *type, *val, *tb; |
| 158 |
PyErr_SetString(PyExc_TypeError, "message"); |
| 159 |
|
| 160 |
PyErr_Fetch(&type, &val, &tb); |
| 161 |
if (PyErr_Occurred()) |
| 162 |
return NULL; |
| 163 |
if (type != PyExc_TypeError) |
| 164 |
Py_RETURN_FALSE; |
| 165 |
PyErr_Restore(type, val, tb); |
| 166 |
if (!PyErr_Occurred()) |
| 167 |
Py_RETURN_FALSE; |
| 168 |
PyErr_Clear(); |
| 169 |
Py_RETURN_TRUE; |
| 170 |
''' |
| 171 |
), |
| 172 |
]) |
| 173 |
assert module.check_error() |
| 174 |
|
| 175 |
|
| 176 |
def test_normalize(self): |
| 177 |
module = self.import_extension('foo', [ |
| 178 |
("check_error", "METH_NOARGS", |
| 179 |
''' |
| 180 |
PyObject *type, *val, *tb; |
| 181 |
PyErr_SetString(PyExc_TypeError, "message"); |
| 182 |
|
| 183 |
PyErr_Fetch(&type, &val, &tb); |
| 184 |
if (type != PyExc_TypeError) |
| 185 |
Py_RETURN_FALSE; |
| 186 |
if (!PyUnicode_Check(val)) |
| 187 |
Py_RETURN_FALSE; |
| 188 |
/* Normalize */ |
| 189 |
PyErr_NormalizeException(&type, &val, &tb); |
| 190 |
if (type != PyExc_TypeError) |
| 191 |
Py_RETURN_FALSE; |
| 192 |
if ((PyObject*)Py_TYPE(val) != PyExc_TypeError) |
| 193 |
Py_RETURN_FALSE; |
| 194 |
|
| 195 |
/* Normalize again */ |
| 196 |
PyErr_NormalizeException(&type, &val, &tb); |
| 197 |
if (type != PyExc_TypeError) |
| 198 |
Py_RETURN_FALSE; |
| 199 |
if ((PyObject*)Py_TYPE(val) != PyExc_TypeError) |
| 200 |
Py_RETURN_FALSE; |
| 201 |
|
| 202 |
PyErr_Restore(type, val, tb); |
| 203 |
PyErr_Clear(); |
| 204 |
Py_RETURN_TRUE; |
| 205 |
''' |
| 206 |
), |
| 207 |
]) |
| 208 |
assert module.check_error() |
| 209 |
|
| 210 |
def test_normalize_no_exception(self): |
| 211 |
module = self.import_extension('foo', [ |
| 212 |
("check_error", "METH_NOARGS", |
| 213 |
''' |
| 214 |
PyObject *type, *val, *tb; |
| 215 |
PyErr_Fetch(&type, &val, &tb); |
| 216 |
if (type != NULL) |
| 217 |
Py_RETURN_FALSE; |
| 218 |
if (val != NULL) |
| 219 |
Py_RETURN_FALSE; |
| 220 |
PyErr_NormalizeException(&type, &val, &tb); |
| 221 |
Py_RETURN_TRUE; |
| 222 |
''' |
| 223 |
), |
| 224 |
]) |
| 225 |
assert module.check_error() |
| 226 |
|
| 227 |
def test_SetFromErrno(self): |
| 228 |
import sys |
| 229 |
if not self.runappdirect: |
| 230 |
skip("callbacks through ll2ctypes modify errno") |
| 231 |
import errno, os |
| 232 |
|
| 233 |
module = self.import_extension('foo', [ |
| 234 |
("set_from_errno", "METH_NOARGS", |
| 235 |
''' |
| 236 |
errno = EBADF; |
| 237 |
PyErr_SetFromErrno(PyExc_OSError); |
| 238 |
return NULL; |
| 239 |
'''), |
| 240 |
], |
| 241 |
prologue="#include <errno.h>") |
| 242 |
try: |
| 243 |
module.set_from_errno() |
| 244 |
except OSError as e: |
| 245 |
print(e.errno, errno.EBADF) |
| 246 |
assert e.errno == errno.EBADF |
| 247 |
assert e.strerror == os.strerror(errno.EBADF) |
| 248 |
assert e.filename is None |
| 249 |
|
| 250 |
def test_SetFromErrnoWithFilename_basic(self): |
| 251 |
char = self.special_char |
| 252 |
if char is None: |
| 253 |
char = "a" |
| 254 |
import errno, os, sys |
| 255 |
codestr = [ |
| 256 |
("set_from_errno", "METH_NOARGS", |
| 257 |
''' |
| 258 |
errno = EBADF; |
| 259 |
PyErr_SetFromErrnoWithFilename(PyExc_OSError, "/path/to/file"); |
| 260 |
return NULL; |
| 261 |
'''), |
| 262 |
("set_from_errno_special", "METH_NOARGS", |
| 263 |
''' |
| 264 |
errno = EBADF; |
| 265 |
PyErr_SetFromErrnoWithFilename(PyExc_OSError, "/path/to/%s"); |
| 266 |
return NULL; |
| 267 |
''' % (char, )), |
| 268 |
] |
| 269 |
if sys.platform == "win32": |
| 270 |
codestr += [ |
| 271 |
("set_from_windowserr", "METH_NOARGS", |
| 272 |
""" |
| 273 |
/* this error code has no message, Python formats it |
| 274 |
as hexadecimal */ |
| 275 |
int code = 376526934; |
| 276 |
PyObject *ret = PyErr_SetFromWindowsErr(code); |
| 277 |
return NULL; |
| 278 |
"""), |
| 279 |
("set_from_windowserr_filename", "METH_O", |
| 280 |
""" |
| 281 |
int code = 376526934; |
| 282 |
char *filename = NULL; |
| 283 |
if (args != Py_None) |
| 284 |
filename = PyBytes_AsString(args); |
| 285 |
PyObject *ret = PyErr_SetFromWindowsErrWithFilename(code, filename); |
| 286 |
return NULL; |
| 287 |
"""), |
| 288 |
("set_from_windowserr_filename_object", "METH_O", |
| 289 |
""" |
| 290 |
int code = 376526934; |
| 291 |
PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError, code, args); |
| 292 |
return NULL; |
| 293 |
"""), |
| 294 |
("set_from_windowserr_filename_objects", "METH_VARARGS", |
| 295 |
""" |
| 296 |
int code = 376526934; |
| 297 |
PyObject *arg1, *arg2; |
| 298 |
if (!PyArg_ParseTuple(args, "OO", &arg1, &arg2)) { |
| 299 |
return NULL; |
| 300 |
} |
| 301 |
PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObjects(PyExc_OSError, code, arg1, arg2); |
| 302 |
return NULL; |
| 303 |
"""), |
| 304 |
] |
| 305 |
module = self.import_extension('foo', codestr, |
| 306 |
prologue="#include <errno.h>") |
| 307 |
exc_info = raises(OSError, module.set_from_errno) |
| 308 |
assert exc_info.value.filename == "/path/to/file" |
| 309 |
if self.runappdirect: |
| 310 |
|
| 311 |
assert exc_info.value.errno == errno.EBADF |
| 312 |
assert exc_info.value.strerror == os.strerror(errno.EBADF) |
| 313 |
|
| 314 |
exc_info = raises(OSError, module.set_from_errno_special) |
| 315 |
assert exc_info.value.filename == "/path/to/%s" % (char, ) |
| 316 |
if self.runappdirect: |
| 317 |
|
| 318 |
assert exc_info.value.errno == errno.EBADF |
| 319 |
assert exc_info.value.strerror == os.strerror(errno.EBADF) |
| 320 |
if sys.platform == "win32": |
| 321 |
exc_info = raises(OSError, module.set_from_windowserr) |
| 322 |
if self.runappdirect: |
| 323 |
|
| 324 |
assert exc_info.value.errno == 376526934 |
| 325 |
|
| 326 |
exc_info = raises(OSError, module.set_from_windowserr_filename, |
| 327 |
None) |
| 328 |
if self.runappdirect: |
| 329 |
assert exc_info.value.errno == 376526934 |
| 330 |
|
| 331 |
exc_info = raises(OSError, module.set_from_windowserr_filename, |
| 332 |
b"myfile.py") |
| 333 |
if self.runappdirect: |
| 334 |
assert exc_info.value.errno == 376526934 |
| 335 |
assert exc_info.value.filename == "myfile.py" |
| 336 |
|
| 337 |
exc_info = raises(OSError, module.set_from_windowserr_filename_object, |
| 338 |
"myfile.py") |
| 339 |
print(exc_info.value.filename) |
| 340 |
if self.runappdirect: |
| 341 |
assert exc_info.value.errno == 376526934 |
| 342 |
assert exc_info.value.filename == "myfile.py" |
| 343 |
|
| 344 |
exc_info = raises(OSError, module.set_from_windowserr_filename_objects, |
| 345 |
"myfile.py", "myfile2.py") |
| 346 |
print(exc_info.value, exc_info.value.filename) |
| 347 |
if self.runappdirect: |
| 348 |
assert exc_info.value.errno == 376526934 |
| 349 |
assert exc_info.value.filename == "myfile.py" |
| 350 |
assert exc_info.value.filename2== "myfile2.py" |
| 351 |
|
| 352 |
|
| 353 |
def test_SetFromErrnoWithFilename_NULL(self): |
| 354 |
import errno, os |
| 355 |
|
| 356 |
module = self.import_extension('foo', [ |
| 357 |
("set_from_errno", "METH_NOARGS", |
| 358 |
''' |
| 359 |
errno = EBADF; |
| 360 |
PyErr_SetFromErrnoWithFilename(PyExc_OSError, NULL); |
| 361 |
return NULL; |
| 362 |
'''), |
| 363 |
], |
| 364 |
prologue="#include <errno.h>") |
| 365 |
exc_info = raises(OSError, module.set_from_errno) |
| 366 |
assert exc_info.value.filename is None |
| 367 |
if self.runappdirect: |
| 368 |
|
| 369 |
assert exc_info.value.errno == errno.EBADF |
| 370 |
assert exc_info.value.strerror == os.strerror(errno.EBADF) |
| 371 |
|
| 372 |
def test_SetFromErrnoWithFilenameObject__PyUnicode(self): |
| 373 |
import errno, os |
| 374 |
|
| 375 |
module = self.import_extension('foo', [ |
| 376 |
("set_from_errno", "METH_NOARGS", |
| 377 |
''' |
| 378 |
PyObject *filenameObject = PyUnicode_FromString("/path/to/file"); |
| 379 |
errno = EBADF; |
| 380 |
PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, filenameObject); |
| 381 |
Py_DECREF(filenameObject); |
| 382 |
return NULL; |
| 383 |
'''), |
| 384 |
("set_from_errno2", "METH_NOARGS", |
| 385 |
''' |
| 386 |
PyObject *filenameObject = PyUnicode_FromString("/path/to/file"); |
| 387 |
errno = EBADF; |
| 388 |
PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, |
| 389 |
filenameObject, filenameObject); |
| 390 |
Py_DECREF(filenameObject); |
| 391 |
return NULL; |
| 392 |
'''), |
| 393 |
], |
| 394 |
prologue="#include <errno.h>") |
| 395 |
exc_info = raises(OSError, module.set_from_errno) |
| 396 |
assert exc_info.value.filename == "/path/to/file" |
| 397 |
exc_info = raises(OSError, module.set_from_errno2) |
| 398 |
assert exc_info.value.filename == "/path/to/file" |
| 399 |
assert exc_info.value.filename2 == "/path/to/file" |
| 400 |
if self.runappdirect: |
| 401 |
|
| 402 |
assert exc_info.value.errno == errno.EBADF |
| 403 |
assert exc_info.value.strerror == os.strerror(errno.EBADF) |
| 404 |
|
| 405 |
def test_SetFromErrnoWithFilenameObject__PyLong(self): |
| 406 |
import errno, os |
| 407 |
|
| 408 |
module = self.import_extension('foo', [ |
| 409 |
("set_from_errno", "METH_NOARGS", |
| 410 |
''' |
| 411 |
PyObject *intObject = PyLong_FromLong(3); |
| 412 |
errno = EBADF; |
| 413 |
PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, intObject); |
| 414 |
Py_DECREF(intObject); |
| 415 |
return NULL; |
| 416 |
'''), |
| 417 |
], |
| 418 |
prologue="#include <errno.h>") |
| 419 |
exc_info = raises(OSError, module.set_from_errno) |
| 420 |
assert exc_info.value.filename == 3 |
| 421 |
if self.runappdirect: |
| 422 |
|
| 423 |
assert exc_info.value.errno == errno.EBADF |
| 424 |
assert exc_info.value.strerror == os.strerror(errno.EBADF) |
| 425 |
|
| 426 |
def test_SetFromErrnoWithFilenameObject__PyList(self): |
| 427 |
import errno, os |
| 428 |
|
| 429 |
module = self.import_extension('foo', [ |
| 430 |
("set_from_errno", "METH_NOARGS", |
| 431 |
''' |
| 432 |
PyObject *lst = Py_BuildValue("[iis]", 1, 2, "three"); |
| 433 |
errno = EBADF; |
| 434 |
PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, lst); |
| 435 |
Py_DECREF(lst); |
| 436 |
return NULL; |
| 437 |
'''), |
| 438 |
], |
| 439 |
prologue="#include <errno.h>") |
| 440 |
exc_info = raises(OSError, module.set_from_errno) |
| 441 |
assert exc_info.value.filename == [1, 2, "three"] |
| 442 |
if self.runappdirect: |
| 443 |
|
| 444 |
assert exc_info.value.errno == errno.EBADF |
| 445 |
assert exc_info.value.strerror == os.strerror(errno.EBADF) |
| 446 |
|
| 447 |
def test_SetFromErrnoWithFilenameObject__PyTuple(self): |
| 448 |
import errno, os |
| 449 |
|
| 450 |
module = self.import_extension('foo', [ |
| 451 |
("set_from_errno", "METH_NOARGS", |
| 452 |
''' |
| 453 |
PyObject *tuple = Py_BuildValue("(iis)", 1, 2, "three"); |
| 454 |
errno = EBADF; |
| 455 |
PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, tuple); |
| 456 |
Py_DECREF(tuple); |
| 457 |
return NULL; |
| 458 |
'''), |
| 459 |
], |
| 460 |
prologue="#include <errno.h>") |
| 461 |
exc_info = raises(OSError, module.set_from_errno) |
| 462 |
assert exc_info.value.filename == (1, 2, "three") |
| 463 |
if self.runappdirect: |
| 464 |
|
| 465 |
assert exc_info.value.errno == errno.EBADF |
| 466 |
assert exc_info.value.strerror == os.strerror(errno.EBADF) |
| 467 |
|
| 468 |
def test_SetFromErrnoWithFilenameObject__Py_None(self): |
| 469 |
import errno, os |
| 470 |
|
| 471 |
module = self.import_extension('foo', [ |
| 472 |
("set_from_errno", "METH_NOARGS", |
| 473 |
''' |
| 474 |
PyObject *none = Py_BuildValue(""); |
| 475 |
errno = EBADF; |
| 476 |
PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, none); |
| 477 |
Py_DECREF(none); |
| 478 |
return NULL; |
| 479 |
'''), |
| 480 |
], |
| 481 |
prologue="#include <errno.h>") |
| 482 |
exc_info = raises(OSError, module.set_from_errno) |
| 483 |
assert exc_info.value.filename is None |
| 484 |
if self.runappdirect: |
| 485 |
|
| 486 |
assert exc_info.value.errno == errno.EBADF |
| 487 |
assert exc_info.value.strerror == os.strerror(errno.EBADF) |
| 488 |
|
| 489 |
def test_PyErr_Display(self): |
| 490 |
from sys import version_info |
| 491 |
module = self.import_extension('foo', [ |
| 492 |
("display_error", "METH_VARARGS", |
| 493 |
r''' |
| 494 |
PyObject *type, *val, *tb; |
| 495 |
PyErr_GetExcInfo(&type, &val, &tb); |
| 496 |
PyErr_Display(type, val, tb); |
| 497 |
Py_XDECREF(type); |
| 498 |
Py_XDECREF(val); |
| 499 |
Py_XDECREF(tb); |
| 500 |
Py_RETURN_NONE; |
| 501 |
'''), |
| 502 |
]) |
| 503 |
import io, sys |
| 504 |
sys.stderr = io.StringIO() |
| 505 |
try: |
| 506 |
1 / 0 |
| 507 |
except ZeroDivisionError: |
| 508 |
module.display_error() |
| 509 |
finally: |
| 510 |
output = sys.stderr.getvalue() |
| 511 |
sys.stderr = sys.__stderr__ |
| 512 |
assert "in test_PyErr_Display\n" in output |
| 513 |
assert "ZeroDivisionError" in output |
| 514 |
|
| 515 |
@pytest.mark.skipif(True, reason= |
| 516 |
"XXX seems to pass, but doesn't: 'py.test -s' shows errors in PyObject_Free") |
| 517 |
def test_GetSetExcInfo(self): |
| 518 |
import sys |
| 519 |
module = self.import_extension('foo', [ |
| 520 |
("getset_exc_info", "METH_VARARGS", |
| 521 |
r''' |
| 522 |
PyObject *type, *val, *tb; |
| 523 |
PyObject *new_type, *new_val, *new_tb; |
| 524 |
PyObject *result; |
| 525 |
|
| 526 |
if (!PyArg_ParseTuple(args, "OOO", &new_type, &new_val, &new_tb)) |
| 527 |
return NULL; |
| 528 |
|
| 529 |
PyErr_GetExcInfo(&type, &val, &tb); |
| 530 |
|
| 531 |
Py_INCREF(new_type); |
| 532 |
Py_INCREF(new_val); |
| 533 |
Py_INCREF(new_tb); |
| 534 |
PyErr_SetExcInfo(new_type, new_val, new_tb); |
| 535 |
|
| 536 |
result = Py_BuildValue("OOO", |
| 537 |
type ? type : Py_None, |
| 538 |
val ? val : Py_None, |
| 539 |
tb ? tb : Py_None); |
| 540 |
Py_XDECREF(type); |
| 541 |
Py_XDECREF(val); |
| 542 |
Py_XDECREF(tb); |
| 543 |
return result; |
| 544 |
''' |
| 545 |
), |
| 546 |
]) |
| 547 |
try: |
| 548 |
raise ValueError(5) |
| 549 |
except ValueError as old_exc: |
| 550 |
new_exc = TypeError("TEST") |
| 551 |
orig_sys_exc_info = sys.exc_info() |
| 552 |
orig_exc_info = module.getset_exc_info(new_exc.__class__, |
| 553 |
new_exc, None) |
| 554 |
new_sys_exc_info = sys.exc_info() |
| 555 |
new_exc_info = module.getset_exc_info(*orig_exc_info) |
| 556 |
reset_sys_exc_info = sys.exc_info() |
| 557 |
|
| 558 |
assert orig_exc_info[0] is old_exc.__class__ |
| 559 |
assert orig_exc_info[1] is old_exc |
| 560 |
assert orig_exc_info == orig_sys_exc_info |
| 561 |
assert orig_exc_info == reset_sys_exc_info |
| 562 |
assert new_exc_info == (new_exc.__class__, new_exc, None) |
| 563 |
assert new_exc_info == new_sys_exc_info |
| 564 |
|
| 565 |
def test_PyErr_Format(self): |
| 566 |
import warnings |
| 567 |
|
| 568 |
module = self.import_extension('foo', [ |
| 569 |
("test_warning", "METH_NOARGS", |
| 570 |
''' |
| 571 |
PyErr_WarnFormat(PyExc_UserWarning, 1, "foo %d bar", 42); |
| 572 |
Py_RETURN_NONE; |
| 573 |
'''), |
| 574 |
("test_err", "METH_NOARGS", |
| 575 |
''' |
| 576 |
return helper(PyExc_ValueError, "foo %d bar %d", 42, 11); |
| 577 |
''')], |
| 578 |
prologue=""" |
| 579 |
PyObject * helper(PyObject *exception, char * fmt, ...); |
| 580 |
PyObject * helper(PyObject *exception, char * fmt, ...) { |
| 581 |
va_list va; |
| 582 |
va_start(va, fmt); |
| 583 |
PyErr_FormatV(exception, fmt, va); |
| 584 |
va_end(va); |
| 585 |
return NULL; |
| 586 |
} |
| 587 |
""" |
| 588 |
) |
| 589 |
with warnings.catch_warnings(record=True) as l: |
| 590 |
module.test_warning() |
| 591 |
assert len(l) == 1 |
| 592 |
assert "foo 42 bar" in str(l[0]) |
| 593 |
raises (ValueError, module.test_err) |
| 594 |
|
| 595 |
def test_StopIteration_value(self): |
| 596 |
module = self.import_extension('foo', [ |
| 597 |
("test", "METH_O", |
| 598 |
''' |
| 599 |
PyObject *o = ((PyStopIterationObject *)args)->value; |
| 600 |
Py_INCREF(o); |
| 601 |
return o; |
| 602 |
'''), |
| 603 |
]) |
| 604 |
res = module.test(StopIteration("foo!")) |
| 605 |
assert res == "foo!" |
| 606 |
|
| 607 |
def test_PyErr_BadInternalCall(self): |
| 608 |
|
| 609 |
|
| 610 |
module = self.import_extension('foo', [ |
| 611 |
("oops", "METH_NOARGS", |
| 612 |
r''' |
| 613 |
PyErr_BadInternalCall(); |
| 614 |
return NULL; |
| 615 |
'''), |
| 616 |
]) |
| 617 |
raises(SystemError, module.oops) |
| 618 |
|
| 619 |
@pytest.mark.skipif("not config.option.runappdirect", reason='-A only') |
| 620 |
def test_error_thread_race(self): |
| 621 |
|
| 622 |
|
| 623 |
module = self.import_extension('foo', [ |
| 624 |
("emit_error", "METH_VARARGS", |
| 625 |
''' |
| 626 |
PyThreadState *save = NULL; |
| 627 |
PyGILState_STATE gilsave; |
| 628 |
|
| 629 |
/* NB. synchronization due to GIL */ |
| 630 |
static volatile int flag = 0; |
| 631 |
int id; |
| 632 |
|
| 633 |
if (!PyArg_ParseTuple(args, "i", &id)) |
| 634 |
return NULL; |
| 635 |
|
| 636 |
/* Proceed in thread 1 first */ |
| 637 |
save = PyEval_SaveThread(); |
| 638 |
if (save == NULL) abort(); |
| 639 |
while (id == 0 && flag == 0); |
| 640 |
gilsave = PyGILState_Ensure(); |
| 641 |
if (gilsave != PyGILState_UNLOCKED) abort(); |
| 642 |
|
| 643 |
PyErr_Format(PyExc_ValueError, "%d", id); |
| 644 |
|
| 645 |
/* Proceed in thread 0 first */ |
| 646 |
if (id == 1) flag = 1; |
| 647 |
PyGILState_Release(gilsave); |
| 648 |
while (id == 1 && flag == 1); |
| 649 |
PyEval_RestoreThread(save); |
| 650 |
|
| 651 |
if (id == 0) flag = 0; |
| 652 |
return NULL; |
| 653 |
''' |
| 654 |
), |
| 655 |
]) |
| 656 |
|
| 657 |
import threading |
| 658 |
|
| 659 |
failures = [] |
| 660 |
|
| 661 |
def worker(arg): |
| 662 |
try: |
| 663 |
module.emit_error(arg) |
| 664 |
failures.append(True) |
| 665 |
except Exception as exc: |
| 666 |
if str(exc) != str(arg): |
| 667 |
failures.append(exc) |
| 668 |
|
| 669 |
threads = [threading.Thread(target=worker, args=(j,)) |
| 670 |
for j in (0, 1)] |
| 671 |
for t in threads: |
| 672 |
t.start() |
| 673 |
for t in threads: |
| 674 |
t.join() |
| 675 |
|
| 676 |
assert not failures |
| 677 |
|
| 678 |
def test_format(self): |
| 679 |
module = self.import_extension('foo', [ |
| 680 |
("raises", "METH_NOARGS", |
| 681 |
''' |
| 682 |
PyErr_Format(PyExc_IndexError, |
| 683 |
"v1 %ld is out of bounds " |
| 684 |
"for v2 %d with v3 %ld", |
| 685 |
4, 0, 4); |
| 686 |
return NULL; |
| 687 |
'''), |
| 688 |
]) |
| 689 |
|
| 690 |
raises(IndexError, module.raises) |
| 691 |
|
| 692 |
def test_WriteUnraisable(self): |
| 693 |
|
| 694 |
|
| 695 |
module = self.import_extension('foo', [ |
| 696 |
("unraisable", "METH_O", |
| 697 |
''' |
| 698 |
PyErr_SetString(PyExc_ValueError, "message"); |
| 699 |
/* args is "location" */ |
| 700 |
PyErr_WriteUnraisable(args); |
| 701 |
PyErr_Clear(); |
| 702 |
Py_RETURN_NONE; |
| 703 |
'''), |
| 704 |
("unraisable_exc", "METH_VARARGS", |
| 705 |
''' |
| 706 |
PyObject *exc, *err_msg, *obj; |
| 707 |
if (!PyArg_ParseTuple(args, "OOO", &exc, &err_msg, &obj)) { |
| 708 |
return NULL; |
| 709 |
} |
| 710 |
|
| 711 |
const char *err_msg_utf8; |
| 712 |
if (err_msg != Py_None) { |
| 713 |
err_msg_utf8 = PyUnicode_AsUTF8(err_msg); |
| 714 |
if (err_msg_utf8 == NULL) { |
| 715 |
return NULL; |
| 716 |
} |
| 717 |
} |
| 718 |
else { |
| 719 |
err_msg_utf8 = NULL; |
| 720 |
} |
| 721 |
|
| 722 |
PyErr_SetObject((PyObject *)Py_TYPE(exc), exc); |
| 723 |
_PyErr_WriteUnraisableMsg(err_msg_utf8, obj); |
| 724 |
Py_RETURN_NONE; |
| 725 |
'''), |
| 726 |
]) |
| 727 |
import sys |
| 728 |
import io, sys |
| 729 |
old = sys.stderr |
| 730 |
sys.stderr = io.StringIO() |
| 731 |
module.unraisable('location') |
| 732 |
output = sys.stderr.getvalue() |
| 733 |
sys.stderr = old |
| 734 |
msg = output.strip().replace('\r', '').splitlines() |
| 735 |
assert msg[0] == "Exception ignored in: 'location'" |
| 736 |
assert msg[-1] == "ValueError: message" |
| 737 |
|
| 738 |
|
| 739 |
def unraisablehook(hookargs): |
| 740 |
pass |
| 741 |
|
| 742 |
def hook(event, args): |
| 743 |
if event == "sys.unraisablehook": |
| 744 |
if args[0] != unraisablehook: |
| 745 |
raise ValueError("Expected {} == {}".format(args[0], unraisablehook)) |
| 746 |
print(event, repr(args[1].exc_value), args[1].err_msg) |
| 747 |
|
| 748 |
old = sys.stdout |
| 749 |
sys.stdout = io.StringIO() |
| 750 |
sys.addaudithook(hook) |
| 751 |
sys.unraisablehook = unraisablehook |
| 752 |
module.unraisable_exc(RuntimeError("nonfatal-error"), "sometext", None) |
| 753 |
output = sys.stdout.getvalue() |
| 754 |
sys.stdout = old |
| 755 |
msg = output.strip().replace('\r', '').splitlines() |
| 756 |
assert msg[0] == "sys.unraisablehook RuntimeError('nonfatal-error') Exception ignored sometext" |
| 757 |
|
| 758 |
def test_fetch_normalized(self): |
| 759 |
module = self.import_extension('foo', [ |
| 760 |
("clevel_error", "METH_O", |
| 761 |
''' |
| 762 |
const char *fname = PyUnicode_AsUTF8(args); |
| 763 |
char buffer[1024]; |
| 764 |
sprintf(buffer, "open('%s', 'r')", fname); |
| 765 |
// printf("calling %s\\n", buffer); |
| 766 |
PyObject *ret = PyRun_String(buffer, Py_eval_input, pyx_d, pyx_d); |
| 767 |
if (ret) { |
| 768 |
Py_DECREF(ret); |
| 769 |
PyErr_SetString(PyExc_AssertionError, "should raise"); |
| 770 |
return NULL; |
| 771 |
} |
| 772 |
PyObject *type, *value, *tb; |
| 773 |
PyErr_Fetch(&type, &value, &tb); |
| 774 |
if (type != PyExc_FileNotFoundError) |
| 775 |
{ |
| 776 |
// printf("type is %s\\n", ((PyTypeObject*)type)->tp_name); |
| 777 |
return value; |
| 778 |
} |
| 779 |
PyErr_Clear(); |
| 780 |
// decrefs? |
| 781 |
Py_RETURN_TRUE; |
| 782 |
'''), |
| 783 |
], prologue=""" |
| 784 |
static PyObject * pyx_d; |
| 785 |
""", more_init=""" |
| 786 |
pyx_d = PyModule_GetDict(mod); |
| 787 |
""") |
| 788 |
import os |
| 789 |
fname = 'this file should not exist' |
| 790 |
assert not os.path.exists(fname) |
| 791 |
ret = module.clevel_error(fname) |
| 792 |
print(ret) |
| 793 |
assert ret is True |
| 794 |
|
| 795 |
def test_set_get(self): |
| 796 |
|
| 797 |
module = self.import_extension('foo', [ |
| 798 |
('set_exception', "METH_O", |
| 799 |
""" |
| 800 |
PyObject *exc = PyErr_GetHandledException(); |
| 801 |
assert(PyExceptionInstance_Check(exc) || exc == NULL); |
| 802 |
|
| 803 |
PyErr_SetHandledException(args); |
| 804 |
return exc; |
| 805 |
""")]) |
| 806 |
import sys |
| 807 |
|
| 808 |
raised_exception = ValueError("5") |
| 809 |
new_exc = TypeError("TEST") |
| 810 |
try: |
| 811 |
raise raised_exception |
| 812 |
except ValueError as e: |
| 813 |
orig_sys_exception = sys.exception() |
| 814 |
orig_exception = module.set_exception(new_exc) |
| 815 |
new_sys_exception = sys.exception() |
| 816 |
new_exception = module.set_exception(orig_exception) |
| 817 |
reset_sys_exception = sys.exception() |
| 818 |
|
| 819 |
assert orig_exception == e |
| 820 |
|
| 821 |
assert orig_exception == raised_exception |
| 822 |
assert orig_sys_exception == orig_exception |
| 823 |
print(repr(orig_exception), type(reset_sys_exception), repr(reset_sys_exception)) |
| 824 |
assert reset_sys_exception == orig_exception |
| 825 |
assert new_exception == new_exc |
| 826 |
assert new_sys_exception == new_exception |
| 827 |
else: |
| 828 |
assert False |
| 829 |
|
| 830 |
def test_exc_set_object_broken_subclasscheck(self): |
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
module = self.import_extension('foo', [ |
| 836 |
('exc_set_object', "METH_VARARGS", |
| 837 |
""" |
| 838 |
PyObject *exc, *obj; |
| 839 |
if (!PyArg_ParseTuple(args, "OO:exc_set_object", &exc, &obj)) |
| 840 |
return NULL; |
| 841 |
PyErr_SetObject(exc, obj); |
| 842 |
return NULL; |
| 843 |
"""), |
| 844 |
]) |
| 845 |
def broken_subclasscheck(cls, sub): |
| 846 |
1/0 |
| 847 |
Meta = type('Meta', (type,), {'__subclasscheck__': broken_subclasscheck}) |
| 848 |
Broken = Meta('Broken', (Exception,), {}) |
| 849 |
raises(ZeroDivisionError, module.exc_set_object, Broken, Broken()) |