mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 21:51:50 +00:00 
			
		
		
		
	gh-102192: Replace PyErr_Fetch/Restore etc by more efficient alternatives (in Python/) (#102193)
This commit is contained in:
		
							parent
							
								
									85b1fc1fc5
								
							
						
					
					
						commit
						4c87537efb
					
				
					 8 changed files with 42 additions and 72 deletions
				
			
		|  | @ -23,7 +23,7 @@ Data members: | |||
| #include "pycore_namespace.h"     // _PyNamespace_New() | ||||
| #include "pycore_object.h"        // _PyObject_IS_GC() | ||||
| #include "pycore_pathconfig.h"    // _PyPathConfig_ComputeSysPath0() | ||||
| #include "pycore_pyerrors.h"      // _PyErr_Fetch() | ||||
| #include "pycore_pyerrors.h"      // _PyErr_GetRaisedException() | ||||
| #include "pycore_pylifecycle.h"   // _PyErr_WriteUnraisableDefaultHook() | ||||
| #include "pycore_pymath.h"        // _PY_SHORT_FLOAT_REPR | ||||
| #include "pycore_pymem.h"         // _PyMem_SetDefaultAllocator() | ||||
|  | @ -89,12 +89,11 @@ PySys_GetObject(const char *name) | |||
| { | ||||
|     PyThreadState *tstate = _PyThreadState_GET(); | ||||
| 
 | ||||
|     PyObject *exc_type, *exc_value, *exc_tb; | ||||
|     _PyErr_Fetch(tstate, &exc_type, &exc_value, &exc_tb); | ||||
|     PyObject *exc = _PyErr_GetRaisedException(tstate); | ||||
|     PyObject *value = _PySys_GetObject(tstate->interp, name); | ||||
|     /* XXX Suppress a new exception if it was raised and restore
 | ||||
|      * the old one. */ | ||||
|     _PyErr_Restore(tstate, exc_type, exc_value, exc_tb); | ||||
|     _PyErr_SetRaisedException(tstate, exc); | ||||
|     return value; | ||||
| } | ||||
| 
 | ||||
|  | @ -203,8 +202,8 @@ sys_audit_tstate(PyThreadState *ts, const char *event, | |||
| 
 | ||||
|     int dtrace = PyDTrace_AUDIT_ENABLED(); | ||||
| 
 | ||||
|     PyObject *exc_type, *exc_value, *exc_tb; | ||||
|     _PyErr_Fetch(ts, &exc_type, &exc_value, &exc_tb); | ||||
| 
 | ||||
|     PyObject *exc = _PyErr_GetRaisedException(ts); | ||||
| 
 | ||||
|     /* Initialize event args now */ | ||||
|     if (argFormat && argFormat[0]) { | ||||
|  | @ -287,13 +286,11 @@ sys_audit_tstate(PyThreadState *ts, const char *event, | |||
|     Py_XDECREF(eventArgs); | ||||
| 
 | ||||
|     if (!res) { | ||||
|         _PyErr_Restore(ts, exc_type, exc_value, exc_tb); | ||||
|         _PyErr_SetRaisedException(ts, exc); | ||||
|     } | ||||
|     else { | ||||
|         assert(_PyErr_Occurred(ts)); | ||||
|         Py_XDECREF(exc_type); | ||||
|         Py_XDECREF(exc_value); | ||||
|         Py_XDECREF(exc_tb); | ||||
|         Py_XDECREF(exc); | ||||
|     } | ||||
| 
 | ||||
|     return res; | ||||
|  | @ -3661,12 +3658,11 @@ static void | |||
| sys_write(PyObject *key, FILE *fp, const char *format, va_list va) | ||||
| { | ||||
|     PyObject *file; | ||||
|     PyObject *error_type, *error_value, *error_traceback; | ||||
|     char buffer[1001]; | ||||
|     int written; | ||||
|     PyThreadState *tstate = _PyThreadState_GET(); | ||||
| 
 | ||||
|     _PyErr_Fetch(tstate, &error_type, &error_value, &error_traceback); | ||||
|     PyObject *exc = _PyErr_GetRaisedException(tstate); | ||||
|     file = _PySys_GetAttr(tstate, key); | ||||
|     written = PyOS_vsnprintf(buffer, sizeof(buffer), format, va); | ||||
|     if (sys_pyfile_write(buffer, file) != 0) { | ||||
|  | @ -3678,7 +3674,7 @@ sys_write(PyObject *key, FILE *fp, const char *format, va_list va) | |||
|         if (sys_pyfile_write(truncated, file) != 0) | ||||
|             fputs(truncated, fp); | ||||
|     } | ||||
|     _PyErr_Restore(tstate, error_type, error_value, error_traceback); | ||||
|     _PyErr_SetRaisedException(tstate, exc); | ||||
| } | ||||
| 
 | ||||
| void | ||||
|  | @ -3708,7 +3704,7 @@ sys_format(PyObject *key, FILE *fp, const char *format, va_list va) | |||
|     const char *utf8; | ||||
|     PyThreadState *tstate = _PyThreadState_GET(); | ||||
| 
 | ||||
|     PyObject *error = _PyErr_GetRaisedException(tstate); | ||||
|     PyObject *exc = _PyErr_GetRaisedException(tstate); | ||||
|     file = _PySys_GetAttr(tstate, key); | ||||
|     message = PyUnicode_FromFormatV(format, va); | ||||
|     if (message != NULL) { | ||||
|  | @ -3720,7 +3716,7 @@ sys_format(PyObject *key, FILE *fp, const char *format, va_list va) | |||
|         } | ||||
|         Py_DECREF(message); | ||||
|     } | ||||
|     _PyErr_SetRaisedException(tstate, error); | ||||
|     _PyErr_SetRaisedException(tstate, exc); | ||||
| } | ||||
| 
 | ||||
| void | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Irit Katriel
						Irit Katriel