| 1 | import __pypy__ |
| 2 | import sys |
| 3 | import io |
| 4 | |
| 5 | def test_simple(): |
| 6 | unraisables = [] |
| 7 | def ownhook(hookargs): |
| 8 | unraisables.append(hookargs) |
| 9 | oldhook = sys.unraisablehook |
| 10 | sys.unraisablehook = ownhook |
| 11 | try: |
| 12 | raise ValueError |
| 13 | except Exception as e: |
| 14 | __pypy__.write_unraisable("in: testplace", e, None) |
| 15 | finally: |
| 16 | sys.unraisablehook = oldhook |
| 17 | output = unraisables[0] |
| 18 | assert "Exception ignored in: testplace" in output.err_msg |
| 19 | assert isinstance(output.exc_value, ValueError) |
| 20 | |
| 21 | def test_custom_unraisablehook(): |
| 22 | l = [] |
| 23 | def ownhook(hookargs): |
| 24 | l.append(hookargs) |
| 25 | sys.unraisablehook = ownhook |
| 26 | try: |
| 27 | try: |
| 28 | raise ValueError |
| 29 | except Exception as e: |
| 30 | obj = object() |
| 31 | __pypy__.write_unraisable("testplace", e, obj) |
| 32 | assert len(l) == 1 |
| 33 | args, = l |
| 34 | assert args.exc_type is type(e) |
| 35 | assert args.exc_value is e |
| 36 | assert "testplace" in args.err_msg |
| 37 | assert args.object is obj |
| 38 | finally: |
| 39 | sys.unraisablehook = sys.__unraisablehook__ |
| 40 | |
| 41 | def test_custom_unraisablehook_fails(): |
| 42 | def ownhook(hookargs): |
| 43 | raise IndexError |
| 44 | sys.unraisablehook = ownhook |
| 45 | sys.stderr = stringio = io.StringIO() |
| 46 | oldstderr = sys.stderr |
| 47 | try: |
| 48 | try: |
| 49 | raise ValueError |
| 50 | except Exception as e: |
| 51 | obj = object() |
| 52 | __pypy__.write_unraisable("never used", e, obj) |
| 53 | output = stringio.getvalue() |
| 54 | print(output) |
| 55 | assert "Exception ignored in sys.unraisablehook" in output |
| 56 | assert "ownhook" in output |
| 57 | assert "IndexError" in output |
| 58 | finally: |
| 59 | sys.unraisablehook = sys.__unraisablehook__ |
| 60 | sys.stderr = oldstderr |
| 61 | |
| 62 | def test_del_object_is_unbound_method(): |
| 63 | import gc |
| 64 | l = [] |
| 65 | def ownhook(hookargs): |
| 66 | l.append(hookargs) |
| 67 | class A: |
| 68 | def __del__(self): |
| 69 | raise IndexError |
| 70 | sys.unraisablehook = ownhook |
| 71 | try: |
| 72 | A() |
| 73 | gc.collect() |
| 74 | assert len(l) == 1 |
| 75 | args, = l |
| 76 | print(args.object) |
| 77 | assert args.object is A.__del__ |
| 78 | finally: |
| 79 | sys.unraisablehook = sys.__unraisablehook__ |
| 80 |