Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 044c516

Browse files
committed
Issue #18609, #18408: _ctypes_add_traceback() now clears the current exception
while adding the traceback, because it may call indirectly a Python function and Python functions must not be called with an exception set. In the case of the issue #18609, _ctypes_add_traceback() called the iso8859-1 decoder which is implemented in Python. Python has a ISO-8859-1 codec implemented in C. It is not used because PyUnicode_Decode() only uses the C codec for other names (aliases) of this codec ("latin-1", "latin1" and "iso-8859-1").
1 parent 362532b commit 044c516

1 file changed

Lines changed: 20 additions & 3 deletions

File tree

Modules/_ctypes/callbacks.c

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,20 +98,37 @@ void _ctypes_add_traceback(char *funcname, char *filename, int lineno)
9898
PyObject *py_globals = 0;
9999
PyCodeObject *py_code = 0;
100100
PyFrameObject *py_frame = 0;
101+
PyObject *exception, *value, *tb;
102+
103+
/* (Save and) Clear the current exception. Python functions must not be
104+
called with an exception set. Calling Python functions happens when
105+
the codec of the filesystem encoding is implemented in pure Python. */
106+
PyErr_Fetch(&exception, &value, &tb);
101107

102108
py_globals = PyDict_New();
103-
if (!py_globals) goto bad;
109+
if (!py_globals)
110+
goto bad;
104111
py_code = PyCode_NewEmpty(filename, funcname, lineno);
105-
if (!py_code) goto bad;
112+
if (!py_code)
113+
goto bad;
106114
py_frame = PyFrame_New(
107115
PyThreadState_Get(), /*PyThreadState *tstate,*/
108116
py_code, /*PyCodeObject *code,*/
109117
py_globals, /*PyObject *globals,*/
110118
0 /*PyObject *locals*/
111119
);
112-
if (!py_frame) goto bad;
120+
if (!py_frame)
121+
goto bad;
113122
py_frame->f_lineno = lineno;
123+
124+
PyErr_Restore(exception, value, tb);
114125
PyTraceBack_Here(py_frame);
126+
127+
Py_DECREF(py_globals);
128+
Py_DECREF(py_code);
129+
Py_DECREF(py_frame);
130+
return;
131+
115132
bad:
116133
Py_XDECREF(py_globals);
117134
Py_XDECREF(py_code);

0 commit comments

Comments
 (0)