Skip to content

Commit 1d0b0e0

Browse files
Add test for unraisablehook
1 parent f13cdbb commit 1d0b0e0

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

Lib/test/test_sys_getattr.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,37 @@ def main():
127127
128128
''')
129129

130+
unraisable_hook_code = textwrap.dedent('''
131+
import sys
132+
133+
class UnraisableHookInitiator:
134+
def __del__(self):
135+
raise Exception('1')
136+
137+
class UnraisableHook:
138+
def __call__(self, *args, **kwds):
139+
print('X', *args)
140+
141+
def __repr__(self):
142+
h = sys.unraisablehook
143+
setattr(sys, 'unraisablehook', sys.__unraisablehook__)
144+
del h
145+
return 'UnraisableHook'
146+
147+
def audit(event, args):
148+
repr(args)
149+
150+
def main():
151+
sys.addaudithook(audit)
152+
setattr(sys, 'unraisablehook', UnraisableHook())
153+
x = UnraisableHookInitiator()
154+
del x
155+
156+
if __name__ == "__main__":
157+
main()
158+
159+
''')
160+
130161

131162
def test_print_deleted_stdout(self):
132163
# print should use strong reference to the stdout.
@@ -244,5 +275,12 @@ def test_input_stderr(self):
244275
self.assertNotIn(b"Segmentation fault", err)
245276
self.assertNotIn(b"access violation", err)
246277

278+
def test_errors_unraisablehook(self):
279+
test_code = self.unraisable_hook_code
280+
rc, _, err = assert_python_ok('-c', test_code)
281+
self.assertEqual(rc, 0)
282+
self.assertNotIn(b"Segmentation fault", err)
283+
self.assertNotIn(b"access violation", err)
284+
247285
if __name__ == "__main__":
248286
unittest.main()

0 commit comments

Comments
 (0)