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
|
import __pypy__
import sys
import io
def test_simple():
unraisables = []
def ownhook(hookargs):
unraisables.append(hookargs)
oldhook = sys.unraisablehook
sys.unraisablehook = ownhook
try:
raise ValueError
except Exception as e:
__pypy__.write_unraisable("in: testplace", e, None)
finally:
sys.unraisablehook = oldhook
output = unraisables[0]
assert "Exception ignored in: testplace" in output.err_msg
assert isinstance(output.exc_value, ValueError)
def test_custom_unraisablehook():
l = []
def ownhook(hookargs):
l.append(hookargs)
sys.unraisablehook = ownhook
try:
try:
raise ValueError
except Exception as e:
obj = object()
__pypy__.write_unraisable("testplace", e, obj)
assert len(l) == 1
args, = l
assert args.exc_type is type(e)
assert args.exc_value is e
assert "testplace" in args.err_msg
assert args.object is obj
finally:
sys.unraisablehook = sys.__unraisablehook__
def test_custom_unraisablehook_fails():
def ownhook(hookargs):
raise IndexError
sys.unraisablehook = ownhook
sys.stderr = stringio = io.StringIO()
oldstderr = sys.stderr
try:
try:
raise ValueError
except Exception as e:
obj = object()
__pypy__.write_unraisable("never used", e, obj)
output = stringio.getvalue()
print(output)
assert "Exception ignored in sys.unraisablehook" in output
assert "ownhook" in output
assert "IndexError" in output
finally:
sys.unraisablehook = sys.__unraisablehook__
sys.stderr = oldstderr
def test_del_object_is_unbound_method():
import gc
l = []
def ownhook(hookargs):
l.append(hookargs)
class A:
def __del__(self):
raise IndexError
sys.unraisablehook = ownhook
try:
A()
gc.collect()
assert len(l) == 1
args, = l
print(args.object)
assert args.object is A.__del__
finally:
sys.unraisablehook = sys.__unraisablehook__
|