bpo-36829: Add sys.unraisablehook() (GH-13187)

Add new sys.unraisablehook() function which can be overridden to
control how "unraisable exceptions" are handled. It is called when an
exception has occurred but there is no way for Python to handle it.
For example, when a destructor raises an exception or during garbage
collection (gc.collect()).

Changes:

* Add an internal UnraisableHookArgs type used to pass arguments to
  sys.unraisablehook.
* Add _PyErr_WriteUnraisableDefaultHook().
* The default hook now ignores exception on writing the traceback.
* test_sys now uses unittest.main() to automatically discover tests:
  remove test_main().
* Add _PyErr_Init().
* Fix PyErr_WriteUnraisable(): hold a strong reference to sys.stderr
  while using it
This commit is contained in:
Victor Stinner 2019-05-22 11:28:22 +02:00 committed by GitHub
parent 2725cb01d7
commit ef9d9b6312
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 497 additions and 123 deletions

120
Python/importlib.h generated
View file

@ -1326,8 +1326,8 @@ const unsigned char _Py_M__importlib_bootstrap[] = {
97,114,100,108,101,115,115,32,111,102,32,97,110,121,32,114,
97,105,115,101,100,32,101,120,99,101,112,116,105,111,110,115,
46,78,41,2,114,57,0,0,0,114,60,0,0,0,41,4,
114,30,0,0,0,90,8,101,120,99,95,116,121,112,101,90,
9,101,120,99,95,118,97,108,117,101,90,13,101,120,99,95,
114,30,0,0,0,218,8,101,120,99,95,116,121,112,101,218,
9,101,120,99,95,118,97,108,117,101,218,13,101,120,99,95,
116,114,97,99,101,98,97,99,107,114,10,0,0,0,114,10,
0,0,0,114,11,0,0,0,114,56,0,0,0,99,3,0,
0,115,2,0,0,0,0,2,122,27,95,73,109,112,111,114,
@ -1358,7 +1358,7 @@ const unsigned char _Py_M__importlib_bootstrap[] = {
98,97,115,101,114,10,0,0,0,114,10,0,0,0,114,11,
0,0,0,218,13,95,114,101,115,111,108,118,101,95,110,97,
109,101,104,3,0,0,115,10,0,0,0,0,2,16,1,12,
1,8,1,8,1,114,185,0,0,0,99,3,0,0,0,0,
1,8,1,8,1,114,188,0,0,0,99,3,0,0,0,0,
0,0,0,0,0,0,0,4,0,0,0,4,0,0,0,67,
0,0,0,115,34,0,0,0,124,0,160,0,124,1,124,2,
161,2,125,3,124,3,100,0,107,8,114,24,100,0,83,0,
@ -1368,7 +1368,7 @@ const unsigned char _Py_M__importlib_bootstrap[] = {
109,0,0,0,114,10,0,0,0,114,10,0,0,0,114,11,
0,0,0,218,17,95,102,105,110,100,95,115,112,101,99,95,
108,101,103,97,99,121,113,3,0,0,115,8,0,0,0,0,
3,12,1,8,1,4,1,114,187,0,0,0,99,3,0,0,
3,12,1,8,1,4,1,114,190,0,0,0,99,3,0,0,
0,0,0,0,0,0,0,0,0,10,0,0,0,10,0,0,
0,67,0,0,0,115,12,1,0,0,116,0,106,1,125,3,
124,3,100,1,107,8,114,22,116,2,100,2,131,1,130,1,
@ -1398,17 +1398,17 @@ const unsigned char _Py_M__importlib_bootstrap[] = {
104,114,79,0,0,0,218,9,95,119,97,114,110,105,110,103,
115,218,4,119,97,114,110,218,13,73,109,112,111,114,116,87,
97,114,110,105,110,103,114,92,0,0,0,114,178,0,0,0,
114,166,0,0,0,114,106,0,0,0,114,187,0,0,0,114,
114,166,0,0,0,114,106,0,0,0,114,190,0,0,0,114,
105,0,0,0,41,10,114,17,0,0,0,114,164,0,0,0,
114,165,0,0,0,114,188,0,0,0,90,9,105,115,95,114,
101,108,111,97,100,114,186,0,0,0,114,166,0,0,0,114,
114,165,0,0,0,114,191,0,0,0,90,9,105,115,95,114,
101,108,111,97,100,114,189,0,0,0,114,166,0,0,0,114,
95,0,0,0,114,96,0,0,0,114,105,0,0,0,114,10,
0,0,0,114,10,0,0,0,114,11,0,0,0,218,10,95,
102,105,110,100,95,115,112,101,99,122,3,0,0,115,54,0,
0,0,0,2,6,1,8,2,8,3,4,1,12,5,10,1,
8,1,8,1,2,1,10,1,14,1,12,1,8,1,20,2,
22,1,8,2,18,1,10,1,2,1,10,1,14,4,14,2,
8,1,8,2,10,2,10,2,114,192,0,0,0,99,3,0,
8,1,8,2,10,2,10,2,114,195,0,0,0,99,3,0,
0,0,0,0,0,0,0,0,0,0,3,0,0,0,5,0,
0,0,67,0,0,0,115,108,0,0,0,116,0,124,0,116,
1,131,2,115,28,116,2,100,1,160,3,116,4,124,0,131,
@ -1432,12 +1432,12 @@ const unsigned char _Py_M__importlib_bootstrap[] = {
121,32,109,111,100,117,108,101,32,110,97,109,101,78,41,7,
218,10,105,115,105,110,115,116,97,110,99,101,218,3,115,116,
114,218,9,84,121,112,101,69,114,114,111,114,114,45,0,0,
0,114,14,0,0,0,114,182,0,0,0,114,79,0,0,0,
169,3,114,17,0,0,0,114,183,0,0,0,114,184,0,0,
0,114,14,0,0,0,114,185,0,0,0,114,79,0,0,0,
169,3,114,17,0,0,0,114,186,0,0,0,114,187,0,0,
0,114,10,0,0,0,114,10,0,0,0,114,11,0,0,0,
218,13,95,115,97,110,105,116,121,95,99,104,101,99,107,169,
3,0,0,115,22,0,0,0,0,2,10,1,18,1,8,1,
8,1,8,1,10,1,10,1,4,1,8,2,12,1,114,197,
8,1,8,1,10,1,10,1,4,1,8,2,12,1,114,200,
0,0,0,122,16,78,111,32,109,111,100,117,108,101,32,110,
97,109,101,100,32,122,4,123,33,114,125,99,2,0,0,0,
0,0,0,0,0,0,0,0,8,0,0,0,8,0,0,0,
@ -1462,7 +1462,7 @@ const unsigned char _Py_M__importlib_bootstrap[] = {
0,114,92,0,0,0,114,67,0,0,0,114,141,0,0,0,
114,106,0,0,0,218,8,95,69,82,82,95,77,83,71,114,
45,0,0,0,218,19,77,111,100,117,108,101,78,111,116,70,
111,117,110,100,69,114,114,111,114,114,192,0,0,0,114,159,
111,117,110,100,69,114,114,111,114,114,195,0,0,0,114,159,
0,0,0,114,5,0,0,0,41,8,114,17,0,0,0,218,
7,105,109,112,111,114,116,95,114,164,0,0,0,114,130,0,
0,0,90,13,112,97,114,101,110,116,95,109,111,100,117,108,
@ -1472,7 +1472,7 @@ const unsigned char _Py_M__importlib_bootstrap[] = {
117,110,108,111,99,107,101,100,188,3,0,0,115,42,0,0,
0,0,1,4,1,14,1,4,1,10,1,10,2,10,1,10,
1,10,1,2,1,10,1,14,1,16,1,20,1,10,1,8,
1,20,2,8,1,4,2,10,1,22,1,114,202,0,0,0,
1,20,2,8,1,4,2,10,1,22,1,114,205,0,0,0,
99,2,0,0,0,0,0,0,0,0,0,0,0,4,0,0,
0,10,0,0,0,67,0,0,0,115,106,0,0,0,116,0,
124,0,131,1,143,50,1,0,116,1,106,2,160,3,124,0,
@ -1488,14 +1488,14 @@ const unsigned char _Py_M__importlib_bootstrap[] = {
101,32,105,110,32,115,121,115,46,109,111,100,117,108,101,115,
114,16,0,0,0,41,9,114,50,0,0,0,114,15,0,0,
0,114,92,0,0,0,114,34,0,0,0,218,14,95,78,69,
69,68,83,95,76,79,65,68,73,78,71,114,202,0,0,0,
114,45,0,0,0,114,200,0,0,0,114,65,0,0,0,41,
4,114,17,0,0,0,114,201,0,0,0,114,96,0,0,0,
69,68,83,95,76,79,65,68,73,78,71,114,205,0,0,0,
114,45,0,0,0,114,203,0,0,0,114,65,0,0,0,41,
4,114,17,0,0,0,114,204,0,0,0,114,96,0,0,0,
114,75,0,0,0,114,10,0,0,0,114,10,0,0,0,114,
11,0,0,0,218,14,95,102,105,110,100,95,97,110,100,95,
108,111,97,100,218,3,0,0,115,22,0,0,0,0,2,10,
1,14,1,8,1,32,2,8,1,4,1,2,255,4,2,12,
2,8,1,114,204,0,0,0,114,22,0,0,0,99,3,0,
2,8,1,114,207,0,0,0,114,22,0,0,0,99,3,0,
0,0,0,0,0,0,0,0,0,0,3,0,0,0,4,0,
0,0,67,0,0,0,115,42,0,0,0,116,0,124,0,124,
1,124,2,131,3,1,0,124,2,100,1,107,4,114,32,116,
@ -1520,11 +1520,11 @@ const unsigned char _Py_M__importlib_bootstrap[] = {
103,32,95,95,112,97,99,107,97,103,101,95,95,32,105,102,
10,32,32,32,32,116,104,101,32,108,111,97,100,101,114,32,
100,105,100,32,110,111,116,46,10,10,32,32,32,32,114,22,
0,0,0,41,4,114,197,0,0,0,114,185,0,0,0,114,
204,0,0,0,218,11,95,103,99,100,95,105,109,112,111,114,
116,114,196,0,0,0,114,10,0,0,0,114,10,0,0,0,
114,11,0,0,0,114,205,0,0,0,234,3,0,0,115,8,
0,0,0,0,9,12,1,8,1,12,1,114,205,0,0,0,
0,0,0,41,4,114,200,0,0,0,114,188,0,0,0,114,
207,0,0,0,218,11,95,103,99,100,95,105,109,112,111,114,
116,114,199,0,0,0,114,10,0,0,0,114,10,0,0,0,
114,11,0,0,0,114,208,0,0,0,234,3,0,0,115,8,
0,0,0,0,9,12,1,8,1,12,1,114,208,0,0,0,
169,1,218,9,114,101,99,117,114,115,105,118,101,99,3,0,
0,0,0,0,0,0,1,0,0,0,8,0,0,0,11,0,
0,0,67,0,0,0,115,226,0,0,0,124,1,68,0,93,
@ -1561,21 +1561,21 @@ const unsigned char _Py_M__importlib_bootstrap[] = {
109,32,108,105,115,116,39,39,122,8,73,116,101,109,32,105,
110,32,122,18,32,109,117,115,116,32,98,101,32,115,116,114,
44,32,110,111,116,32,250,1,42,218,7,95,95,97,108,108,
95,95,84,114,206,0,0,0,114,179,0,0,0,78,41,16,
114,193,0,0,0,114,194,0,0,0,114,1,0,0,0,114,
195,0,0,0,114,14,0,0,0,114,4,0,0,0,218,16,
95,95,84,114,209,0,0,0,114,182,0,0,0,78,41,16,
114,196,0,0,0,114,197,0,0,0,114,1,0,0,0,114,
198,0,0,0,114,14,0,0,0,114,4,0,0,0,218,16,
95,104,97,110,100,108,101,95,102,114,111,109,108,105,115,116,
114,209,0,0,0,114,45,0,0,0,114,67,0,0,0,114,
200,0,0,0,114,17,0,0,0,114,15,0,0,0,114,92,
0,0,0,114,34,0,0,0,114,203,0,0,0,41,8,114,
96,0,0,0,218,8,102,114,111,109,108,105,115,116,114,201,
0,0,0,114,207,0,0,0,218,1,120,90,5,119,104,101,
114,212,0,0,0,114,45,0,0,0,114,67,0,0,0,114,
203,0,0,0,114,17,0,0,0,114,15,0,0,0,114,92,
0,0,0,114,34,0,0,0,114,206,0,0,0,41,8,114,
96,0,0,0,218,8,102,114,111,109,108,105,115,116,114,204,
0,0,0,114,210,0,0,0,218,1,120,90,5,119,104,101,
114,101,90,9,102,114,111,109,95,110,97,109,101,90,3,101,
120,99,114,10,0,0,0,114,10,0,0,0,114,11,0,0,
0,114,210,0,0,0,249,3,0,0,115,44,0,0,0,0,
0,114,213,0,0,0,249,3,0,0,115,44,0,0,0,0,
10,8,1,10,1,4,1,12,2,4,1,28,2,8,1,14,
1,10,1,2,255,8,2,10,1,14,1,2,1,14,1,16,
4,10,1,16,255,2,2,8,1,22,1,114,210,0,0,0,
4,10,1,16,255,2,2,8,1,22,1,114,213,0,0,0,
99,1,0,0,0,0,0,0,0,0,0,0,0,3,0,0,
0,6,0,0,0,67,0,0,0,115,146,0,0,0,124,0,
160,0,100,1,161,1,125,1,124,0,160,0,100,2,161,1,
@ -1610,14 +1610,14 @@ const unsigned char _Py_M__importlib_bootstrap[] = {
95,95,32,97,110,100,32,95,95,112,97,116,104,95,95,114,
1,0,0,0,114,141,0,0,0,114,128,0,0,0,114,22,
0,0,0,41,6,114,34,0,0,0,114,130,0,0,0,114,
189,0,0,0,114,190,0,0,0,114,191,0,0,0,114,129,
0,0,0,41,3,218,7,103,108,111,98,97,108,115,114,183,
192,0,0,0,114,193,0,0,0,114,194,0,0,0,114,129,
0,0,0,41,3,218,7,103,108,111,98,97,108,115,114,186,
0,0,0,114,95,0,0,0,114,10,0,0,0,114,10,0,
0,0,114,11,0,0,0,218,17,95,99,97,108,99,95,95,
95,112,97,99,107,97,103,101,95,95,30,4,0,0,115,38,
0,0,0,0,7,10,1,10,1,8,1,18,1,22,2,2,
0,2,254,6,3,4,1,8,1,6,2,6,2,2,0,2,
254,6,3,8,1,8,1,14,1,114,216,0,0,0,114,10,
254,6,3,8,1,8,1,14,1,114,219,0,0,0,114,10,
0,0,0,99,5,0,0,0,0,0,0,0,0,0,0,0,
9,0,0,0,5,0,0,0,67,0,0,0,115,180,0,0,
0,124,4,100,1,107,2,114,18,116,0,124,0,131,1,125,
@ -1662,18 +1662,18 @@ const unsigned char _Py_M__importlib_bootstrap[] = {
96,32,119,111,117,108,100,32,104,97,118,101,32,97,32,39,
108,101,118,101,108,39,32,111,102,32,50,41,46,10,10,32,
32,32,32,114,22,0,0,0,78,114,128,0,0,0,114,141,
0,0,0,41,9,114,205,0,0,0,114,216,0,0,0,218,
9,112,97,114,116,105,116,105,111,110,114,181,0,0,0,114,
0,0,0,41,9,114,208,0,0,0,114,219,0,0,0,218,
9,112,97,114,116,105,116,105,111,110,114,184,0,0,0,114,
15,0,0,0,114,92,0,0,0,114,1,0,0,0,114,4,
0,0,0,114,210,0,0,0,41,9,114,17,0,0,0,114,
215,0,0,0,218,6,108,111,99,97,108,115,114,211,0,0,
0,114,184,0,0,0,114,96,0,0,0,90,8,103,108,111,
98,97,108,115,95,114,183,0,0,0,90,7,99,117,116,95,
0,0,0,114,213,0,0,0,41,9,114,17,0,0,0,114,
218,0,0,0,218,6,108,111,99,97,108,115,114,214,0,0,
0,114,187,0,0,0,114,96,0,0,0,90,8,103,108,111,
98,97,108,115,95,114,186,0,0,0,90,7,99,117,116,95,
111,102,102,114,10,0,0,0,114,10,0,0,0,114,11,0,
0,0,218,10,95,95,105,109,112,111,114,116,95,95,57,4,
0,0,115,30,0,0,0,0,11,8,1,10,2,16,1,8,
1,12,1,4,3,8,1,18,1,4,1,4,4,26,3,32,
1,10,1,12,2,114,219,0,0,0,99,1,0,0,0,0,
1,10,1,12,2,114,222,0,0,0,99,1,0,0,0,0,
0,0,0,0,0,0,0,2,0,0,0,3,0,0,0,67,
0,0,0,115,38,0,0,0,116,0,160,1,124,0,161,1,
125,1,124,1,100,0,107,8,114,30,116,2,100,1,124,0,
@ -1685,7 +1685,7 @@ const unsigned char _Py_M__importlib_bootstrap[] = {
0,0,0,114,10,0,0,0,114,11,0,0,0,218,18,95,
98,117,105,108,116,105,110,95,102,114,111,109,95,110,97,109,
101,94,4,0,0,115,8,0,0,0,0,1,10,1,8,1,
12,1,114,220,0,0,0,99,2,0,0,0,0,0,0,0,
12,1,114,223,0,0,0,99,2,0,0,0,0,0,0,0,
0,0,0,0,10,0,0,0,5,0,0,0,67,0,0,0,
115,166,0,0,0,124,1,97,0,124,0,97,1,116,2,116,
1,131,1,125,2,116,1,106,3,160,4,161,0,68,0,93,
@ -1714,12 +1714,12 @@ const unsigned char _Py_M__importlib_bootstrap[] = {
100,117,108,101,115,32,109,117,115,116,32,98,101,32,101,120,
112,108,105,99,105,116,108,121,32,112,97,115,115,101,100,32,
105,110,46,10,10,32,32,32,32,41,3,114,23,0,0,0,
114,189,0,0,0,114,64,0,0,0,78,41,15,114,57,0,
114,192,0,0,0,114,64,0,0,0,78,41,15,114,57,0,
0,0,114,15,0,0,0,114,14,0,0,0,114,92,0,0,
0,218,5,105,116,101,109,115,114,193,0,0,0,114,78,0,
0,218,5,105,116,101,109,115,114,196,0,0,0,114,78,0,
0,0,114,160,0,0,0,114,88,0,0,0,114,173,0,0,
0,114,142,0,0,0,114,148,0,0,0,114,1,0,0,0,
114,220,0,0,0,114,5,0,0,0,41,10,218,10,115,121,
114,223,0,0,0,114,5,0,0,0,41,10,218,10,115,121,
115,95,109,111,100,117,108,101,218,11,95,105,109,112,95,109,
111,100,117,108,101,90,11,109,111,100,117,108,101,95,116,121,
112,101,114,17,0,0,0,114,96,0,0,0,114,109,0,0,
@ -1730,7 +1730,7 @@ const unsigned char _Py_M__importlib_bootstrap[] = {
218,6,95,115,101,116,117,112,101,4,0,0,115,36,0,0,
0,0,9,4,1,4,3,8,1,18,1,10,1,10,1,6,
1,10,1,6,2,2,1,10,1,12,3,10,1,8,1,10,
1,10,2,10,1,114,224,0,0,0,99,2,0,0,0,0,
1,10,2,10,1,114,227,0,0,0,99,2,0,0,0,0,
0,0,0,0,0,0,0,2,0,0,0,3,0,0,0,67,
0,0,0,115,38,0,0,0,116,0,124,0,124,1,131,2,
1,0,116,1,106,2,160,3,116,4,161,1,1,0,116,1,
@ -1738,12 +1738,12 @@ const unsigned char _Py_M__importlib_bootstrap[] = {
122,48,73,110,115,116,97,108,108,32,105,109,112,111,114,116,
101,114,115,32,102,111,114,32,98,117,105,108,116,105,110,32,
97,110,100,32,102,114,111,122,101,110,32,109,111,100,117,108,
101,115,78,41,6,114,224,0,0,0,114,15,0,0,0,114,
188,0,0,0,114,120,0,0,0,114,160,0,0,0,114,173,
0,0,0,41,2,114,222,0,0,0,114,223,0,0,0,114,
101,115,78,41,6,114,227,0,0,0,114,15,0,0,0,114,
191,0,0,0,114,120,0,0,0,114,160,0,0,0,114,173,
0,0,0,41,2,114,225,0,0,0,114,226,0,0,0,114,
10,0,0,0,114,10,0,0,0,114,11,0,0,0,218,8,
95,105,110,115,116,97,108,108,136,4,0,0,115,6,0,0,
0,0,2,10,2,12,1,114,225,0,0,0,99,0,0,0,
0,0,2,10,2,12,1,114,228,0,0,0,99,0,0,0,
0,0,0,0,0,0,0,0,0,1,0,0,0,4,0,0,
0,67,0,0,0,115,32,0,0,0,100,1,100,2,108,0,
125,0,124,0,97,1,124,0,160,2,116,3,106,4,116,5,
@ -1754,12 +1754,12 @@ const unsigned char _Py_M__importlib_bootstrap[] = {
32,97,99,99,101,115,115,114,22,0,0,0,78,41,6,218,
26,95,102,114,111,122,101,110,95,105,109,112,111,114,116,108,
105,98,95,101,120,116,101,114,110,97,108,114,126,0,0,0,
114,225,0,0,0,114,15,0,0,0,114,92,0,0,0,114,
1,0,0,0,41,1,114,226,0,0,0,114,10,0,0,0,
114,228,0,0,0,114,15,0,0,0,114,92,0,0,0,114,
1,0,0,0,41,1,114,229,0,0,0,114,10,0,0,0,
114,10,0,0,0,114,11,0,0,0,218,27,95,105,110,115,
116,97,108,108,95,101,120,116,101,114,110,97,108,95,105,109,
112,111,114,116,101,114,115,144,4,0,0,115,6,0,0,0,
0,3,8,1,4,1,114,227,0,0,0,41,2,78,78,41,
0,3,8,1,4,1,114,230,0,0,0,41,2,78,78,41,
1,78,41,2,78,114,22,0,0,0,41,4,78,78,114,10,
0,0,0,114,22,0,0,0,41,50,114,3,0,0,0,114,
126,0,0,0,114,12,0,0,0,114,18,0,0,0,114,59,
@ -1771,13 +1771,13 @@ const unsigned char _Py_M__importlib_bootstrap[] = {
0,0,0,114,142,0,0,0,114,148,0,0,0,114,152,0,
0,0,114,107,0,0,0,114,93,0,0,0,114,158,0,0,
0,114,159,0,0,0,114,94,0,0,0,114,160,0,0,0,
114,173,0,0,0,114,178,0,0,0,114,185,0,0,0,114,
187,0,0,0,114,192,0,0,0,114,197,0,0,0,90,15,
114,173,0,0,0,114,178,0,0,0,114,188,0,0,0,114,
190,0,0,0,114,195,0,0,0,114,200,0,0,0,90,15,
95,69,82,82,95,77,83,71,95,80,82,69,70,73,88,114,
199,0,0,0,114,202,0,0,0,218,6,111,98,106,101,99,
116,114,203,0,0,0,114,204,0,0,0,114,205,0,0,0,
114,210,0,0,0,114,216,0,0,0,114,219,0,0,0,114,
220,0,0,0,114,224,0,0,0,114,225,0,0,0,114,227,
202,0,0,0,114,205,0,0,0,218,6,111,98,106,101,99,
116,114,206,0,0,0,114,207,0,0,0,114,208,0,0,0,
114,213,0,0,0,114,219,0,0,0,114,222,0,0,0,114,
223,0,0,0,114,227,0,0,0,114,228,0,0,0,114,230,
0,0,0,114,10,0,0,0,114,10,0,0,0,114,10,0,
0,0,114,11,0,0,0,218,8,60,109,111,100,117,108,101,
62,1,0,0,0,115,94,0,0,0,4,24,4,2,8,8,