mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 05:31:20 +00:00 
			
		
		
		
	gh-102192: Replace PyErr_Fetch/Restore etc by more efficient alternatives (#102769)
This commit is contained in:
		
							parent
							
								
									e1e9bab006
								
							
						
					
					
						commit
						1cb75a9ce0
					
				
					 1 changed files with 26 additions and 41 deletions
				
			
		|  | @ -666,17 +666,15 @@ _PyErr_ChainExceptions(PyObject *typ, PyObject *val, PyObject *tb) | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     if (_PyErr_Occurred(tstate)) { |     if (_PyErr_Occurred(tstate)) { | ||||||
|         PyObject *typ2, *val2, *tb2; |  | ||||||
|         _PyErr_Fetch(tstate, &typ2, &val2, &tb2); |  | ||||||
|         _PyErr_NormalizeException(tstate, &typ, &val, &tb); |         _PyErr_NormalizeException(tstate, &typ, &val, &tb); | ||||||
|         if (tb != NULL) { |         if (tb != NULL) { | ||||||
|             PyException_SetTraceback(val, tb); |             PyException_SetTraceback(val, tb); | ||||||
|             Py_DECREF(tb); |             Py_DECREF(tb); | ||||||
|         } |         } | ||||||
|         Py_DECREF(typ); |         Py_DECREF(typ); | ||||||
|         _PyErr_NormalizeException(tstate, &typ2, &val2, &tb2); |         PyObject *exc2 = _PyErr_GetRaisedException(tstate); | ||||||
|         PyException_SetContext(val2, val); |         PyException_SetContext(exc2, val); | ||||||
|         _PyErr_Restore(tstate, typ2, val2, tb2); |         _PyErr_SetRaisedException(tstate, exc2); | ||||||
|     } |     } | ||||||
|     else { |     else { | ||||||
|         _PyErr_Restore(tstate, typ, val, tb); |         _PyErr_Restore(tstate, typ, val, tb); | ||||||
|  | @ -757,27 +755,15 @@ static PyObject * | ||||||
| _PyErr_FormatVFromCause(PyThreadState *tstate, PyObject *exception, | _PyErr_FormatVFromCause(PyThreadState *tstate, PyObject *exception, | ||||||
|                         const char *format, va_list vargs) |                         const char *format, va_list vargs) | ||||||
| { | { | ||||||
|     PyObject *exc, *val, *val2, *tb; |  | ||||||
| 
 |  | ||||||
|     assert(_PyErr_Occurred(tstate)); |     assert(_PyErr_Occurred(tstate)); | ||||||
|     _PyErr_Fetch(tstate, &exc, &val, &tb); |     PyObject *exc = _PyErr_GetRaisedException(tstate); | ||||||
|     _PyErr_NormalizeException(tstate, &exc, &val, &tb); |  | ||||||
|     if (tb != NULL) { |  | ||||||
|         PyException_SetTraceback(val, tb); |  | ||||||
|         Py_DECREF(tb); |  | ||||||
|     } |  | ||||||
|     Py_DECREF(exc); |  | ||||||
|     assert(!_PyErr_Occurred(tstate)); |     assert(!_PyErr_Occurred(tstate)); | ||||||
| 
 |  | ||||||
|     _PyErr_FormatV(tstate, exception, format, vargs); |     _PyErr_FormatV(tstate, exception, format, vargs); | ||||||
| 
 |     PyObject *exc2 = _PyErr_GetRaisedException(tstate); | ||||||
|     _PyErr_Fetch(tstate, &exc, &val2, &tb); |     PyException_SetCause(exc2, Py_NewRef(exc)); | ||||||
|     _PyErr_NormalizeException(tstate, &exc, &val2, &tb); |     PyException_SetContext(exc2, Py_NewRef(exc)); | ||||||
|     PyException_SetCause(val2, Py_NewRef(val)); |     Py_DECREF(exc); | ||||||
|     PyException_SetContext(val2, Py_NewRef(val)); |     _PyErr_SetRaisedException(tstate, exc2); | ||||||
|     Py_DECREF(val); |  | ||||||
|     _PyErr_Restore(tstate, exc, val2, tb); |  | ||||||
| 
 |  | ||||||
|     return NULL; |     return NULL; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | @ -1698,19 +1684,18 @@ static void | ||||||
| PyErr_SyntaxLocationObjectEx(PyObject *filename, int lineno, int col_offset, | PyErr_SyntaxLocationObjectEx(PyObject *filename, int lineno, int col_offset, | ||||||
|                              int end_lineno, int end_col_offset) |                              int end_lineno, int end_col_offset) | ||||||
| { | { | ||||||
|     PyObject *exc, *v, *tb, *tmp; |  | ||||||
|     PyThreadState *tstate = _PyThreadState_GET(); |     PyThreadState *tstate = _PyThreadState_GET(); | ||||||
| 
 | 
 | ||||||
|     /* add attributes for the line number and filename for the error */ |     /* add attributes for the line number and filename for the error */ | ||||||
|     _PyErr_Fetch(tstate, &exc, &v, &tb); |     PyObject *exc = _PyErr_GetRaisedException(tstate); | ||||||
|     _PyErr_NormalizeException(tstate, &exc, &v, &tb); |  | ||||||
|     /* XXX check that it is, indeed, a syntax error. It might not
 |     /* XXX check that it is, indeed, a syntax error. It might not
 | ||||||
|      * be, though. */ |      * be, though. */ | ||||||
|     tmp = PyLong_FromLong(lineno); |     PyObject *tmp = PyLong_FromLong(lineno); | ||||||
|     if (tmp == NULL) |     if (tmp == NULL) { | ||||||
|         _PyErr_Clear(tstate); |         _PyErr_Clear(tstate); | ||||||
|  |     } | ||||||
|     else { |     else { | ||||||
|         if (PyObject_SetAttr(v, &_Py_ID(lineno), tmp)) { |         if (PyObject_SetAttr(exc, &_Py_ID(lineno), tmp)) { | ||||||
|             _PyErr_Clear(tstate); |             _PyErr_Clear(tstate); | ||||||
|         } |         } | ||||||
|         Py_DECREF(tmp); |         Py_DECREF(tmp); | ||||||
|  | @ -1722,7 +1707,7 @@ PyErr_SyntaxLocationObjectEx(PyObject *filename, int lineno, int col_offset, | ||||||
|             _PyErr_Clear(tstate); |             _PyErr_Clear(tstate); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|     if (PyObject_SetAttr(v, &_Py_ID(offset), tmp ? tmp : Py_None)) { |     if (PyObject_SetAttr(exc, &_Py_ID(offset), tmp ? tmp : Py_None)) { | ||||||
|         _PyErr_Clear(tstate); |         _PyErr_Clear(tstate); | ||||||
|     } |     } | ||||||
|     Py_XDECREF(tmp); |     Py_XDECREF(tmp); | ||||||
|  | @ -1734,7 +1719,7 @@ PyErr_SyntaxLocationObjectEx(PyObject *filename, int lineno, int col_offset, | ||||||
|             _PyErr_Clear(tstate); |             _PyErr_Clear(tstate); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|     if (PyObject_SetAttr(v, &_Py_ID(end_lineno), tmp ? tmp : Py_None)) { |     if (PyObject_SetAttr(exc, &_Py_ID(end_lineno), tmp ? tmp : Py_None)) { | ||||||
|         _PyErr_Clear(tstate); |         _PyErr_Clear(tstate); | ||||||
|     } |     } | ||||||
|     Py_XDECREF(tmp); |     Py_XDECREF(tmp); | ||||||
|  | @ -1746,20 +1731,20 @@ PyErr_SyntaxLocationObjectEx(PyObject *filename, int lineno, int col_offset, | ||||||
|             _PyErr_Clear(tstate); |             _PyErr_Clear(tstate); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|     if (PyObject_SetAttr(v, &_Py_ID(end_offset), tmp ? tmp : Py_None)) { |     if (PyObject_SetAttr(exc, &_Py_ID(end_offset), tmp ? tmp : Py_None)) { | ||||||
|         _PyErr_Clear(tstate); |         _PyErr_Clear(tstate); | ||||||
|     } |     } | ||||||
|     Py_XDECREF(tmp); |     Py_XDECREF(tmp); | ||||||
| 
 | 
 | ||||||
|     tmp = NULL; |     tmp = NULL; | ||||||
|     if (filename != NULL) { |     if (filename != NULL) { | ||||||
|         if (PyObject_SetAttr(v, &_Py_ID(filename), filename)) { |         if (PyObject_SetAttr(exc, &_Py_ID(filename), filename)) { | ||||||
|             _PyErr_Clear(tstate); |             _PyErr_Clear(tstate); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         tmp = PyErr_ProgramTextObject(filename, lineno); |         tmp = PyErr_ProgramTextObject(filename, lineno); | ||||||
|         if (tmp) { |         if (tmp) { | ||||||
|             if (PyObject_SetAttr(v, &_Py_ID(text), tmp)) { |             if (PyObject_SetAttr(exc, &_Py_ID(text), tmp)) { | ||||||
|                 _PyErr_Clear(tstate); |                 _PyErr_Clear(tstate); | ||||||
|             } |             } | ||||||
|             Py_DECREF(tmp); |             Py_DECREF(tmp); | ||||||
|  | @ -1768,17 +1753,17 @@ PyErr_SyntaxLocationObjectEx(PyObject *filename, int lineno, int col_offset, | ||||||
|             _PyErr_Clear(tstate); |             _PyErr_Clear(tstate); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|     if (exc != PyExc_SyntaxError) { |     if ((PyObject *)Py_TYPE(exc) != PyExc_SyntaxError) { | ||||||
|         if (_PyObject_LookupAttr(v, &_Py_ID(msg), &tmp) < 0) { |         if (_PyObject_LookupAttr(exc, &_Py_ID(msg), &tmp) < 0) { | ||||||
|             _PyErr_Clear(tstate); |             _PyErr_Clear(tstate); | ||||||
|         } |         } | ||||||
|         else if (tmp) { |         else if (tmp) { | ||||||
|             Py_DECREF(tmp); |             Py_DECREF(tmp); | ||||||
|         } |         } | ||||||
|         else { |         else { | ||||||
|             tmp = PyObject_Str(v); |             tmp = PyObject_Str(exc); | ||||||
|             if (tmp) { |             if (tmp) { | ||||||
|                 if (PyObject_SetAttr(v, &_Py_ID(msg), tmp)) { |                 if (PyObject_SetAttr(exc, &_Py_ID(msg), tmp)) { | ||||||
|                     _PyErr_Clear(tstate); |                     _PyErr_Clear(tstate); | ||||||
|                 } |                 } | ||||||
|                 Py_DECREF(tmp); |                 Py_DECREF(tmp); | ||||||
|  | @ -1788,19 +1773,19 @@ PyErr_SyntaxLocationObjectEx(PyObject *filename, int lineno, int col_offset, | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         if (_PyObject_LookupAttr(v, &_Py_ID(print_file_and_line), &tmp) < 0) { |         if (_PyObject_LookupAttr(exc, &_Py_ID(print_file_and_line), &tmp) < 0) { | ||||||
|             _PyErr_Clear(tstate); |             _PyErr_Clear(tstate); | ||||||
|         } |         } | ||||||
|         else if (tmp) { |         else if (tmp) { | ||||||
|             Py_DECREF(tmp); |             Py_DECREF(tmp); | ||||||
|         } |         } | ||||||
|         else { |         else { | ||||||
|             if (PyObject_SetAttr(v, &_Py_ID(print_file_and_line), Py_None)) { |             if (PyObject_SetAttr(exc, &_Py_ID(print_file_and_line), Py_None)) { | ||||||
|                 _PyErr_Clear(tstate); |                 _PyErr_Clear(tstate); | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|     _PyErr_Restore(tstate, exc, v, tb); |     _PyErr_SetRaisedException(tstate, exc); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void | void | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Irit Katriel
						Irit Katriel