bpo-36710: Add tstate parameter in errors.c (GH-13540)

Add 'PyThreadState *tstate' parameter to errors.c functions to avoid
relying on global variables (indirectly on _PyRuntime).
This commit is contained in:
Victor Stinner 2019-05-24 13:44:24 +02:00 committed by GitHub
parent d8613dc86f
commit b4bdecd0fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 232 additions and 118 deletions

View File

@ -27,10 +27,16 @@ _Py_IDENTIFIER(builtins);
_Py_IDENTIFIER(stderr); _Py_IDENTIFIER(stderr);
void /* Forward declaration */
PyErr_Restore(PyObject *type, PyObject *value, PyObject *traceback) static void _PyErr_Fetch(PyThreadState *tstate, PyObject **p_type,
PyObject **p_value, PyObject **p_traceback);
static void _PyErr_Clear(PyThreadState *tstate);
static void
_PyErr_Restore(PyThreadState *tstate, PyObject *type, PyObject *value,
PyObject *traceback)
{ {
PyThreadState *tstate = _PyThreadState_GET();
PyObject *oldtype, *oldvalue, *oldtraceback; PyObject *oldtype, *oldvalue, *oldtraceback;
if (traceback != NULL && !PyTraceBack_Check(traceback)) { if (traceback != NULL && !PyTraceBack_Check(traceback)) {
@ -55,6 +61,14 @@ PyErr_Restore(PyObject *type, PyObject *value, PyObject *traceback)
Py_XDECREF(oldtraceback); Py_XDECREF(oldtraceback);
} }
void
PyErr_Restore(PyObject *type, PyObject *value, PyObject *traceback)
{
PyThreadState *tstate = _PyThreadState_GET();
_PyErr_Restore(tstate, type, value, traceback);
}
_PyErr_StackItem * _PyErr_StackItem *
_PyErr_GetTopmostException(PyThreadState *tstate) _PyErr_GetTopmostException(PyThreadState *tstate)
{ {
@ -81,10 +95,9 @@ _PyErr_CreateException(PyObject *exception, PyObject *value)
} }
} }
void static void
PyErr_SetObject(PyObject *exception, PyObject *value) _PyErr_SetObject(PyThreadState *tstate, PyObject *exception, PyObject *value)
{ {
PyThreadState *tstate = _PyThreadState_GET();
PyObject *exc_value; PyObject *exc_value;
PyObject *tb = NULL; PyObject *tb = NULL;
@ -107,7 +120,7 @@ PyErr_SetObject(PyObject *exception, PyObject *value)
/* Issue #23571: functions must not be called with an /* Issue #23571: functions must not be called with an
exception set */ exception set */
PyErr_Clear(); _PyErr_Clear(tstate);
fixed_value = _PyErr_CreateException(exception, value); fixed_value = _PyErr_CreateException(exception, value);
Py_XDECREF(value); Py_XDECREF(value);
@ -142,7 +155,14 @@ PyErr_SetObject(PyObject *exception, PyObject *value)
if (value != NULL && PyExceptionInstance_Check(value)) if (value != NULL && PyExceptionInstance_Check(value))
tb = PyException_GetTraceback(value); tb = PyException_GetTraceback(value);
Py_XINCREF(exception); Py_XINCREF(exception);
PyErr_Restore(exception, value, tb); _PyErr_Restore(tstate, exception, value, tb);
}
void
PyErr_SetObject(PyObject *exception, PyObject *value)
{
PyThreadState *tstate = _PyThreadState_GET();
_PyErr_SetObject(tstate, exception, value);
} }
/* Set a key error with the specified argument, wrapping it in a /* Set a key error with the specified argument, wrapping it in a
@ -151,26 +171,52 @@ PyErr_SetObject(PyObject *exception, PyObject *value)
void void
_PyErr_SetKeyError(PyObject *arg) _PyErr_SetKeyError(PyObject *arg)
{ {
PyObject *tup; PyThreadState *tstate = _PyThreadState_GET();
tup = PyTuple_Pack(1, arg); PyObject *tup = PyTuple_Pack(1, arg);
if (!tup) if (!tup) {
return; /* caller will expect error to be set anyway */ /* caller will expect error to be set anyway */
PyErr_SetObject(PyExc_KeyError, tup); return;
}
_PyErr_SetObject(tstate, PyExc_KeyError, tup);
Py_DECREF(tup); Py_DECREF(tup);
} }
static void
_PyErr_SetNone(PyThreadState *tstate, PyObject *exception)
{
_PyErr_SetObject(tstate, exception, (PyObject *)NULL);
}
void void
PyErr_SetNone(PyObject *exception) PyErr_SetNone(PyObject *exception)
{ {
PyErr_SetObject(exception, (PyObject *)NULL); PyThreadState *tstate = _PyThreadState_GET();
_PyErr_SetNone(tstate, exception);
}
static void
_PyErr_SetString(PyThreadState *tstate, PyObject *exception,
const char *string)
{
PyObject *value = PyUnicode_FromString(string);
_PyErr_SetObject(tstate, exception, value);
Py_XDECREF(value);
} }
void void
PyErr_SetString(PyObject *exception, const char *string) PyErr_SetString(PyObject *exception, const char *string)
{ {
PyObject *value = PyUnicode_FromString(string); PyThreadState *tstate = _PyThreadState_GET();
PyErr_SetObject(exception, value); _PyErr_SetString(tstate, exception, string);
Py_XDECREF(value); }
static PyObject*
_PyErr_Occurred(PyThreadState *tstate)
{
return tstate == NULL ? NULL : tstate->curexc_type;
} }
@ -178,7 +224,7 @@ PyObject* _Py_HOT_FUNCTION
PyErr_Occurred(void) PyErr_Occurred(void)
{ {
PyThreadState *tstate = _PyThreadState_GET(); PyThreadState *tstate = _PyThreadState_GET();
return tstate == NULL ? NULL : tstate->curexc_type; return _PyErr_Occurred(tstate);
} }
@ -217,7 +263,8 @@ PyErr_GivenExceptionMatches(PyObject *err, PyObject *exc)
int int
PyErr_ExceptionMatches(PyObject *exc) PyErr_ExceptionMatches(PyObject *exc)
{ {
return PyErr_GivenExceptionMatches(PyErr_Occurred(), exc); PyThreadState *tstate = _PyThreadState_GET();
return PyErr_GivenExceptionMatches(_PyErr_Occurred(tstate), exc);
} }
@ -231,8 +278,9 @@ PyErr_ExceptionMatches(PyObject *exc)
XXX: should PyErr_NormalizeException() also call XXX: should PyErr_NormalizeException() also call
PyException_SetTraceback() with the resulting value and tb? PyException_SetTraceback() with the resulting value and tb?
*/ */
void static void
PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb) _PyErr_NormalizeException(PyThreadState *tstate, PyObject **exc,
PyObject **val, PyObject **tb)
{ {
int recursion_depth = 0; int recursion_depth = 0;
PyObject *type, *value, *initial_tb; PyObject *type, *value, *initial_tb;
@ -299,15 +347,16 @@ PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb)
Py_DECREF(value); Py_DECREF(value);
recursion_depth++; recursion_depth++;
if (recursion_depth == Py_NORMALIZE_RECURSION_LIMIT) { if (recursion_depth == Py_NORMALIZE_RECURSION_LIMIT) {
PyErr_SetString(PyExc_RecursionError, "maximum recursion depth " _PyErr_SetString(tstate, PyExc_RecursionError,
"exceeded while normalizing an exception"); "maximum recursion depth exceeded "
"while normalizing an exception");
} }
/* If the new exception doesn't set a traceback and the old /* If the new exception doesn't set a traceback and the old
exception had a traceback, use the old traceback for the exception had a traceback, use the old traceback for the
new exception. It's better than nothing. new exception. It's better than nothing.
*/ */
initial_tb = *tb; initial_tb = *tb;
PyErr_Fetch(exc, val, tb); _PyErr_Fetch(tstate, exc, val, tb);
assert(*exc != NULL); assert(*exc != NULL);
if (initial_tb != NULL) { if (initial_tb != NULL) {
if (*tb == NULL) if (*tb == NULL)
@ -334,10 +383,17 @@ PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb)
void void
PyErr_Fetch(PyObject **p_type, PyObject **p_value, PyObject **p_traceback) PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb)
{ {
PyThreadState *tstate = _PyThreadState_GET(); PyThreadState *tstate = _PyThreadState_GET();
_PyErr_NormalizeException(tstate, exc, val, tb);
}
static void
_PyErr_Fetch(PyThreadState *tstate, PyObject **p_type, PyObject **p_value,
PyObject **p_traceback)
{
*p_type = tstate->curexc_type; *p_type = tstate->curexc_type;
*p_value = tstate->curexc_value; *p_value = tstate->curexc_value;
*p_traceback = tstate->curexc_traceback; *p_traceback = tstate->curexc_traceback;
@ -347,12 +403,30 @@ PyErr_Fetch(PyObject **p_type, PyObject **p_value, PyObject **p_traceback)
tstate->curexc_traceback = NULL; tstate->curexc_traceback = NULL;
} }
void
PyErr_Fetch(PyObject **p_type, PyObject **p_value, PyObject **p_traceback)
{
PyThreadState *tstate = _PyThreadState_GET();
_PyErr_Fetch(tstate, p_type, p_value, p_traceback);
}
static void
_PyErr_Clear(PyThreadState *tstate)
{
_PyErr_Restore(tstate, NULL, NULL, NULL);
}
void void
PyErr_Clear(void) PyErr_Clear(void)
{ {
PyErr_Restore(NULL, NULL, NULL); PyThreadState *tstate = _PyThreadState_GET();
_PyErr_Clear(tstate);
} }
void void
PyErr_GetExcInfo(PyObject **p_type, PyObject **p_value, PyObject **p_traceback) PyErr_GetExcInfo(PyObject **p_type, PyObject **p_value, PyObject **p_traceback)
{ {
@ -397,47 +471,49 @@ _PyErr_ChainExceptions(PyObject *exc, PyObject *val, PyObject *tb)
if (exc == NULL) if (exc == NULL)
return; return;
if (PyErr_Occurred()) { PyThreadState *tstate = _PyThreadState_GET();
if (_PyErr_Occurred(tstate)) {
PyObject *exc2, *val2, *tb2; PyObject *exc2, *val2, *tb2;
PyErr_Fetch(&exc2, &val2, &tb2); _PyErr_Fetch(tstate, &exc2, &val2, &tb2);
PyErr_NormalizeException(&exc, &val, &tb); _PyErr_NormalizeException(tstate, &exc, &val, &tb);
if (tb != NULL) { if (tb != NULL) {
PyException_SetTraceback(val, tb); PyException_SetTraceback(val, tb);
Py_DECREF(tb); Py_DECREF(tb);
} }
Py_DECREF(exc); Py_DECREF(exc);
PyErr_NormalizeException(&exc2, &val2, &tb2); _PyErr_NormalizeException(tstate, &exc2, &val2, &tb2);
PyException_SetContext(val2, val); PyException_SetContext(val2, val);
PyErr_Restore(exc2, val2, tb2); _PyErr_Restore(tstate, exc2, val2, tb2);
} }
else { else {
PyErr_Restore(exc, val, tb); _PyErr_Restore(tstate, exc, val, tb);
} }
} }
static PyObject * static PyObject *
_PyErr_FormatVFromCause(PyObject *exception, const char *format, va_list vargs) _PyErr_FormatVFromCause(PyThreadState *tstate, PyObject *exception,
const char *format, va_list vargs)
{ {
PyObject *exc, *val, *val2, *tb; PyObject *exc, *val, *val2, *tb;
assert(PyErr_Occurred()); assert(_PyErr_Occurred(tstate));
PyErr_Fetch(&exc, &val, &tb); _PyErr_Fetch(tstate, &exc, &val, &tb);
PyErr_NormalizeException(&exc, &val, &tb); _PyErr_NormalizeException(tstate, &exc, &val, &tb);
if (tb != NULL) { if (tb != NULL) {
PyException_SetTraceback(val, tb); PyException_SetTraceback(val, tb);
Py_DECREF(tb); Py_DECREF(tb);
} }
Py_DECREF(exc); Py_DECREF(exc);
assert(!PyErr_Occurred()); assert(!_PyErr_Occurred(tstate));
PyErr_FormatV(exception, format, vargs); PyErr_FormatV(exception, format, vargs);
PyErr_Fetch(&exc, &val2, &tb); _PyErr_Fetch(tstate, &exc, &val2, &tb);
PyErr_NormalizeException(&exc, &val2, &tb); _PyErr_NormalizeException(tstate, &exc, &val2, &tb);
Py_INCREF(val); Py_INCREF(val);
PyException_SetCause(val2, val); PyException_SetCause(val2, val);
PyException_SetContext(val2, val); PyException_SetContext(val2, val);
PyErr_Restore(exc, val2, tb); _PyErr_Restore(tstate, exc, val2, tb);
return NULL; return NULL;
} }
@ -445,13 +521,14 @@ _PyErr_FormatVFromCause(PyObject *exception, const char *format, va_list vargs)
PyObject * PyObject *
_PyErr_FormatFromCause(PyObject *exception, const char *format, ...) _PyErr_FormatFromCause(PyObject *exception, const char *format, ...)
{ {
PyThreadState *tstate = _PyThreadState_GET();
va_list vargs; va_list vargs;
#ifdef HAVE_STDARG_PROTOTYPES #ifdef HAVE_STDARG_PROTOTYPES
va_start(vargs, format); va_start(vargs, format);
#else #else
va_start(vargs); va_start(vargs);
#endif #endif
_PyErr_FormatVFromCause(exception, format, vargs); _PyErr_FormatVFromCause(tstate, exception, format, vargs);
va_end(vargs); va_end(vargs);
return NULL; return NULL;
} }
@ -461,21 +538,23 @@ _PyErr_FormatFromCause(PyObject *exception, const char *format, ...)
int int
PyErr_BadArgument(void) PyErr_BadArgument(void)
{ {
PyErr_SetString(PyExc_TypeError, PyThreadState *tstate = _PyThreadState_GET();
"bad argument type for built-in operation"); _PyErr_SetString(tstate, PyExc_TypeError,
"bad argument type for built-in operation");
return 0; return 0;
} }
PyObject * PyObject *
PyErr_NoMemory(void) PyErr_NoMemory(void)
{ {
PyThreadState *tstate = _PyThreadState_GET();
if (Py_TYPE(PyExc_MemoryError) == NULL) { if (Py_TYPE(PyExc_MemoryError) == NULL) {
/* PyErr_NoMemory() has been called before PyExc_MemoryError has been /* PyErr_NoMemory() has been called before PyExc_MemoryError has been
initialized by _PyExc_Init() */ initialized by _PyExc_Init() */
Py_FatalError("Out of memory and PyExc_MemoryError is not " Py_FatalError("Out of memory and PyExc_MemoryError is not "
"initialized yet"); "initialized yet");
} }
PyErr_SetNone(PyExc_MemoryError); _PyErr_SetNone(tstate, PyExc_MemoryError);
return NULL; return NULL;
} }
@ -488,6 +567,7 @@ PyErr_SetFromErrnoWithFilenameObject(PyObject *exc, PyObject *filenameObject)
PyObject * PyObject *
PyErr_SetFromErrnoWithFilenameObjects(PyObject *exc, PyObject *filenameObject, PyObject *filenameObject2) PyErr_SetFromErrnoWithFilenameObjects(PyObject *exc, PyObject *filenameObject, PyObject *filenameObject2)
{ {
PyThreadState *tstate = _PyThreadState_GET();
PyObject *message; PyObject *message;
PyObject *v, *args; PyObject *v, *args;
int i = errno; int i = errno;
@ -573,7 +653,7 @@ PyErr_SetFromErrnoWithFilenameObjects(PyObject *exc, PyObject *filenameObject, P
v = PyObject_Call(exc, args, NULL); v = PyObject_Call(exc, args, NULL);
Py_DECREF(args); Py_DECREF(args);
if (v != NULL) { if (v != NULL) {
PyErr_SetObject((PyObject *) Py_TYPE(v), v); _PyErr_SetObject(tstate, (PyObject *) Py_TYPE(v), v);
Py_DECREF(v); Py_DECREF(v);
} }
} }
@ -626,12 +706,17 @@ PyObject *PyErr_SetExcFromWindowsErrWithFilenameObjects(
PyObject *filenameObject, PyObject *filenameObject,
PyObject *filenameObject2) PyObject *filenameObject2)
{ {
PyThreadState *tstate = _PyThreadState_GET();
int len; int len;
WCHAR *s_buf = NULL; /* Free via LocalFree */ WCHAR *s_buf = NULL; /* Free via LocalFree */
PyObject *message; PyObject *message;
PyObject *args, *v; PyObject *args, *v;
DWORD err = (DWORD)ierr; DWORD err = (DWORD)ierr;
if (err==0) err = GetLastError(); if (err==0) {
err = GetLastError();
}
len = FormatMessageW( len = FormatMessageW(
/* Error API error */ /* Error API error */
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_ALLOCATE_BUFFER |
@ -676,7 +761,7 @@ PyObject *PyErr_SetExcFromWindowsErrWithFilenameObjects(
v = PyObject_Call(exc, args, NULL); v = PyObject_Call(exc, args, NULL);
Py_DECREF(args); Py_DECREF(args);
if (v != NULL) { if (v != NULL) {
PyErr_SetObject((PyObject *) Py_TYPE(v), v); _PyErr_SetObject(tstate, (PyObject *) Py_TYPE(v), v);
Py_DECREF(v); Py_DECREF(v);
} }
} }
@ -752,6 +837,7 @@ PyObject *
PyErr_SetImportErrorSubclass(PyObject *exception, PyObject *msg, PyErr_SetImportErrorSubclass(PyObject *exception, PyObject *msg,
PyObject *name, PyObject *path) PyObject *name, PyObject *path)
{ {
PyThreadState *tstate = _PyThreadState_GET();
int issubclass; int issubclass;
PyObject *kwargs, *error; PyObject *kwargs, *error;
@ -760,12 +846,14 @@ PyErr_SetImportErrorSubclass(PyObject *exception, PyObject *msg,
return NULL; return NULL;
} }
else if (!issubclass) { else if (!issubclass) {
PyErr_SetString(PyExc_TypeError, "expected a subclass of ImportError"); _PyErr_SetString(tstate, PyExc_TypeError,
"expected a subclass of ImportError");
return NULL; return NULL;
} }
if (msg == NULL) { if (msg == NULL) {
PyErr_SetString(PyExc_TypeError, "expected a message argument"); _PyErr_SetString(tstate, PyExc_TypeError,
"expected a message argument");
return NULL; return NULL;
} }
@ -789,7 +877,7 @@ PyErr_SetImportErrorSubclass(PyObject *exception, PyObject *msg,
error = _PyObject_FastCallDict(exception, &msg, 1, kwargs); error = _PyObject_FastCallDict(exception, &msg, 1, kwargs);
if (error != NULL) { if (error != NULL) {
PyErr_SetObject((PyObject *)Py_TYPE(error), error); _PyErr_SetObject(tstate, (PyObject *)Py_TYPE(error), error);
Py_DECREF(error); Py_DECREF(error);
} }
@ -828,15 +916,16 @@ PyErr_BadInternalCall(void)
PyObject * PyObject *
PyErr_FormatV(PyObject *exception, const char *format, va_list vargs) PyErr_FormatV(PyObject *exception, const char *format, va_list vargs)
{ {
PyThreadState *tstate = _PyThreadState_GET();
PyObject* string; PyObject* string;
/* Issue #23571: PyUnicode_FromFormatV() must not be called with an /* Issue #23571: PyUnicode_FromFormatV() must not be called with an
exception set, it calls arbitrary Python code like PyObject_Repr() */ exception set, it calls arbitrary Python code like PyObject_Repr() */
PyErr_Clear(); _PyErr_Clear(tstate);
string = PyUnicode_FromFormatV(format, vargs); string = PyUnicode_FromFormatV(format, vargs);
PyErr_SetObject(exception, string); _PyErr_SetObject(tstate, exception, string);
Py_XDECREF(string); Py_XDECREF(string);
return NULL; return NULL;
} }
@ -860,28 +949,31 @@ PyErr_Format(PyObject *exception, const char *format, ...)
PyObject * PyObject *
PyErr_NewException(const char *name, PyObject *base, PyObject *dict) PyErr_NewException(const char *name, PyObject *base, PyObject *dict)
{ {
PyThreadState *tstate = _PyThreadState_GET();
_Py_IDENTIFIER(__module__); _Py_IDENTIFIER(__module__);
const char *dot;
PyObject *modulename = NULL; PyObject *modulename = NULL;
PyObject *classname = NULL; PyObject *classname = NULL;
PyObject *mydict = NULL; PyObject *mydict = NULL;
PyObject *bases = NULL; PyObject *bases = NULL;
PyObject *result = NULL; PyObject *result = NULL;
dot = strrchr(name, '.');
const char *dot = strrchr(name, '.');
if (dot == NULL) { if (dot == NULL) {
PyErr_SetString(PyExc_SystemError, _PyErr_SetString(tstate, PyExc_SystemError,
"PyErr_NewException: name must be module.class"); "PyErr_NewException: name must be module.class");
return NULL; return NULL;
} }
if (base == NULL) if (base == NULL) {
base = PyExc_Exception; base = PyExc_Exception;
}
if (dict == NULL) { if (dict == NULL) {
dict = mydict = PyDict_New(); dict = mydict = PyDict_New();
if (dict == NULL) if (dict == NULL)
goto failure; goto failure;
} }
if (_PyDict_GetItemIdWithError(dict, &PyId___module__) == NULL) { if (_PyDict_GetItemIdWithError(dict, &PyId___module__) == NULL) {
if (PyErr_Occurred()) { if (_PyErr_Occurred(tstate)) {
goto failure; goto failure;
} }
modulename = PyUnicode_FromStringAndSize(name, modulename = PyUnicode_FromStringAndSize(name,
@ -983,8 +1075,8 @@ _PyErr_Init(void)
static PyObject * static PyObject *
make_unraisable_hook_args(PyObject *exc_type, PyObject *exc_value, make_unraisable_hook_args(PyThreadState *tstate, PyObject *exc_type,
PyObject *exc_tb, PyObject *obj) PyObject *exc_value, PyObject *exc_tb, PyObject *obj)
{ {
PyObject *args = PyStructSequence_New(&UnraisableHookArgsType); PyObject *args = PyStructSequence_New(&UnraisableHookArgsType);
if (args == NULL) { if (args == NULL) {
@ -1008,7 +1100,7 @@ make_unraisable_hook_args(PyObject *exc_type, PyObject *exc_value,
ADD_ITEM(obj); ADD_ITEM(obj);
#undef ADD_ITEM #undef ADD_ITEM
if (PyErr_Occurred()) { if (_PyErr_Occurred(tstate)) {
Py_DECREF(args); Py_DECREF(args);
return NULL; return NULL;
} }
@ -1023,8 +1115,9 @@ make_unraisable_hook_args(PyObject *exc_type, PyObject *exc_value,
Do nothing if sys.stderr attribute doesn't exist or is set to None. */ Do nothing if sys.stderr attribute doesn't exist or is set to None. */
static int static int
write_unraisable_exc_file(PyObject *exc_type, PyObject *exc_value, write_unraisable_exc_file(PyThreadState *tstate, PyObject *exc_type,
PyObject *exc_tb, PyObject *obj, PyObject *file) PyObject *exc_value, PyObject *exc_tb,
PyObject *obj, PyObject *file)
{ {
if (obj != NULL && obj != Py_None) { if (obj != NULL && obj != Py_None) {
if (PyFile_WriteString("Exception ignored in: ", file) < 0) { if (PyFile_WriteString("Exception ignored in: ", file) < 0) {
@ -1032,7 +1125,7 @@ write_unraisable_exc_file(PyObject *exc_type, PyObject *exc_value,
} }
if (PyFile_WriteObject(obj, file, 0) < 0) { if (PyFile_WriteObject(obj, file, 0) < 0) {
PyErr_Clear(); _PyErr_Clear(tstate);
if (PyFile_WriteString("<object repr() failed>", file) < 0) { if (PyFile_WriteString("<object repr() failed>", file) < 0) {
return -1; return -1;
} }
@ -1045,7 +1138,7 @@ write_unraisable_exc_file(PyObject *exc_type, PyObject *exc_value,
if (exc_tb != NULL && exc_tb != Py_None) { if (exc_tb != NULL && exc_tb != Py_None) {
if (PyTraceBack_Print(exc_tb, file) < 0) { if (PyTraceBack_Print(exc_tb, file) < 0) {
/* continue even if writing the traceback failed */ /* continue even if writing the traceback failed */
PyErr_Clear(); _PyErr_Clear(tstate);
} }
} }
@ -1065,7 +1158,7 @@ write_unraisable_exc_file(PyObject *exc_type, PyObject *exc_value,
PyObject *moduleName = _PyObject_GetAttrId(exc_type, &PyId___module__); PyObject *moduleName = _PyObject_GetAttrId(exc_type, &PyId___module__);
if (moduleName == NULL || !PyUnicode_Check(moduleName)) { if (moduleName == NULL || !PyUnicode_Check(moduleName)) {
Py_XDECREF(moduleName); Py_XDECREF(moduleName);
PyErr_Clear(); _PyErr_Clear(tstate);
if (PyFile_WriteString("<unknown>", file) < 0) { if (PyFile_WriteString("<unknown>", file) < 0) {
return -1; return -1;
} }
@ -1101,7 +1194,7 @@ write_unraisable_exc_file(PyObject *exc_type, PyObject *exc_value,
return -1; return -1;
} }
if (PyFile_WriteObject(exc_value, file, Py_PRINT_RAW) < 0) { if (PyFile_WriteObject(exc_value, file, Py_PRINT_RAW) < 0) {
PyErr_Clear(); _PyErr_Clear(tstate);
if (PyFile_WriteString("<exception str() failed>", file) < 0) { if (PyFile_WriteString("<exception str() failed>", file) < 0) {
return -1; return -1;
} }
@ -1116,8 +1209,8 @@ write_unraisable_exc_file(PyObject *exc_type, PyObject *exc_value,
static int static int
write_unraisable_exc(PyObject *exc_type, PyObject *exc_value, write_unraisable_exc(PyThreadState *tstate, PyObject *exc_type,
PyObject *exc_tb, PyObject *obj) PyObject *exc_value, PyObject *exc_tb, PyObject *obj)
{ {
PyObject *file = _PySys_GetObjectId(&PyId_stderr); PyObject *file = _PySys_GetObjectId(&PyId_stderr);
if (file == NULL || file == Py_None) { if (file == NULL || file == Py_None) {
@ -1127,7 +1220,7 @@ write_unraisable_exc(PyObject *exc_type, PyObject *exc_value,
/* Hold a strong reference to ensure that sys.stderr doesn't go away /* Hold a strong reference to ensure that sys.stderr doesn't go away
while we use it */ while we use it */
Py_INCREF(file); Py_INCREF(file);
int res = write_unraisable_exc_file(exc_type, exc_value, exc_tb, int res = write_unraisable_exc_file(tstate, exc_type, exc_value, exc_tb,
obj, file); obj, file);
Py_DECREF(file); Py_DECREF(file);
@ -1138,10 +1231,12 @@ write_unraisable_exc(PyObject *exc_type, PyObject *exc_value,
PyObject* PyObject*
_PyErr_WriteUnraisableDefaultHook(PyObject *args) _PyErr_WriteUnraisableDefaultHook(PyObject *args)
{ {
PyThreadState *tstate = _PyThreadState_GET();
if (Py_TYPE(args) != &UnraisableHookArgsType) { if (Py_TYPE(args) != &UnraisableHookArgsType) {
PyErr_SetString(PyExc_TypeError, _PyErr_SetString(tstate, PyExc_TypeError,
"sys.unraisablehook argument type " "sys.unraisablehook argument type "
"must be UnraisableHookArgs"); "must be UnraisableHookArgs");
return NULL; return NULL;
} }
@ -1151,7 +1246,7 @@ _PyErr_WriteUnraisableDefaultHook(PyObject *args)
PyObject *exc_tb = PyStructSequence_GET_ITEM(args, 2); PyObject *exc_tb = PyStructSequence_GET_ITEM(args, 2);
PyObject *obj = PyStructSequence_GET_ITEM(args, 3); PyObject *obj = PyStructSequence_GET_ITEM(args, 3);
if (write_unraisable_exc(exc_type, exc_value, exc_tb, obj) < 0) { if (write_unraisable_exc(tstate, exc_type, exc_value, exc_tb, obj) < 0) {
return NULL; return NULL;
} }
Py_RETURN_NONE; Py_RETURN_NONE;
@ -1168,9 +1263,11 @@ _PyErr_WriteUnraisableDefaultHook(PyObject *args)
void void
PyErr_WriteUnraisable(PyObject *obj) PyErr_WriteUnraisable(PyObject *obj)
{ {
PyObject *exc_type, *exc_value, *exc_tb; PyThreadState *tstate = _PyThreadState_GET();
assert(tstate != NULL);
PyErr_Fetch(&exc_type, &exc_value, &exc_tb); PyObject *exc_type, *exc_value, *exc_tb;
_PyErr_Fetch(tstate, &exc_type, &exc_value, &exc_tb);
assert(exc_type != NULL); assert(exc_type != NULL);
@ -1180,20 +1277,20 @@ PyErr_WriteUnraisable(PyObject *obj)
} }
if (exc_tb == NULL) { if (exc_tb == NULL) {
struct _frame *frame = _PyThreadState_GET()->frame; struct _frame *frame = tstate->frame;
if (frame != NULL) { if (frame != NULL) {
exc_tb = _PyTraceBack_FromFrame(NULL, frame); exc_tb = _PyTraceBack_FromFrame(NULL, frame);
if (exc_tb == NULL) { if (exc_tb == NULL) {
PyErr_Clear(); _PyErr_Clear(tstate);
} }
} }
} }
PyErr_NormalizeException(&exc_type, &exc_value, &exc_tb); _PyErr_NormalizeException(tstate, &exc_type, &exc_value, &exc_tb);
if (exc_tb != NULL && exc_tb != Py_None && PyTraceBack_Check(exc_tb)) { if (exc_tb != NULL && exc_tb != Py_None && PyTraceBack_Check(exc_tb)) {
if (PyException_SetTraceback(exc_value, exc_tb) < 0) { if (PyException_SetTraceback(exc_value, exc_tb) < 0) {
PyErr_Clear(); _PyErr_Clear(tstate);
} }
} }
@ -1202,7 +1299,8 @@ PyErr_WriteUnraisable(PyObject *obj)
if (hook != NULL && hook != Py_None) { if (hook != NULL && hook != Py_None) {
PyObject *hook_args; PyObject *hook_args;
hook_args = make_unraisable_hook_args(exc_type, exc_value, exc_tb, obj); hook_args = make_unraisable_hook_args(tstate, exc_type, exc_value,
exc_tb, obj);
if (hook_args != NULL) { if (hook_args != NULL) {
PyObject *args[1] = {hook_args}; PyObject *args[1] = {hook_args};
PyObject *res = _PyObject_FastCall(hook, args, 1); PyObject *res = _PyObject_FastCall(hook, args, 1);
@ -1217,20 +1315,20 @@ PyErr_WriteUnraisable(PyObject *obj)
Py_XDECREF(exc_type); Py_XDECREF(exc_type);
Py_XDECREF(exc_value); Py_XDECREF(exc_value);
Py_XDECREF(exc_tb); Py_XDECREF(exc_tb);
PyErr_Fetch(&exc_type, &exc_value, &exc_tb); _PyErr_Fetch(tstate, &exc_type, &exc_value, &exc_tb);
obj = hook; obj = hook;
} }
default_hook: default_hook:
/* Call the default unraisable hook (ignore failure) */ /* Call the default unraisable hook (ignore failure) */
(void)write_unraisable_exc(exc_type, exc_value, exc_tb, obj); (void)write_unraisable_exc(tstate, exc_type, exc_value, exc_tb, obj);
done: done:
Py_XDECREF(exc_type); Py_XDECREF(exc_type);
Py_XDECREF(exc_value); Py_XDECREF(exc_value);
Py_XDECREF(exc_tb); Py_XDECREF(exc_tb);
PyErr_Clear(); /* Just in case */ _PyErr_Clear(tstate); /* Just in case */
} }
extern PyObject *PyModule_GetWarningsModule(void); extern PyObject *PyModule_GetWarningsModule(void);
@ -1257,37 +1355,43 @@ PyErr_SyntaxLocationObject(PyObject *filename, int lineno, int col_offset)
_Py_IDENTIFIER(offset); _Py_IDENTIFIER(offset);
_Py_IDENTIFIER(print_file_and_line); _Py_IDENTIFIER(print_file_and_line);
_Py_IDENTIFIER(text); _Py_IDENTIFIER(text);
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(&exc, &v, &tb); _PyErr_Fetch(tstate, &exc, &v, &tb);
PyErr_NormalizeException(&exc, &v, &tb); _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); tmp = PyLong_FromLong(lineno);
if (tmp == NULL) if (tmp == NULL)
PyErr_Clear(); _PyErr_Clear(tstate);
else { else {
if (_PyObject_SetAttrId(v, &PyId_lineno, tmp)) if (_PyObject_SetAttrId(v, &PyId_lineno, tmp)) {
PyErr_Clear(); _PyErr_Clear(tstate);
}
Py_DECREF(tmp); Py_DECREF(tmp);
} }
tmp = NULL; tmp = NULL;
if (col_offset >= 0) { if (col_offset >= 0) {
tmp = PyLong_FromLong(col_offset); tmp = PyLong_FromLong(col_offset);
if (tmp == NULL) if (tmp == NULL) {
PyErr_Clear(); _PyErr_Clear(tstate);
}
}
if (_PyObject_SetAttrId(v, &PyId_offset, tmp ? tmp : Py_None)) {
_PyErr_Clear(tstate);
} }
if (_PyObject_SetAttrId(v, &PyId_offset, tmp ? tmp : Py_None))
PyErr_Clear();
Py_XDECREF(tmp); Py_XDECREF(tmp);
if (filename != NULL) { if (filename != NULL) {
if (_PyObject_SetAttrId(v, &PyId_filename, filename)) if (_PyObject_SetAttrId(v, &PyId_filename, filename)) {
PyErr_Clear(); _PyErr_Clear(tstate);
}
tmp = PyErr_ProgramTextObject(filename, lineno); tmp = PyErr_ProgramTextObject(filename, lineno);
if (tmp) { if (tmp) {
if (_PyObject_SetAttrId(v, &PyId_text, tmp)) if (_PyObject_SetAttrId(v, &PyId_text, tmp)) {
PyErr_Clear(); _PyErr_Clear(tstate);
}
Py_DECREF(tmp); Py_DECREF(tmp);
} }
} }
@ -1295,33 +1399,39 @@ PyErr_SyntaxLocationObject(PyObject *filename, int lineno, int col_offset)
if (!_PyObject_HasAttrId(v, &PyId_msg)) { if (!_PyObject_HasAttrId(v, &PyId_msg)) {
tmp = PyObject_Str(v); tmp = PyObject_Str(v);
if (tmp) { if (tmp) {
if (_PyObject_SetAttrId(v, &PyId_msg, tmp)) if (_PyObject_SetAttrId(v, &PyId_msg, tmp)) {
PyErr_Clear(); _PyErr_Clear(tstate);
}
Py_DECREF(tmp); Py_DECREF(tmp);
} else { }
PyErr_Clear(); else {
_PyErr_Clear(tstate);
} }
} }
if (!_PyObject_HasAttrId(v, &PyId_print_file_and_line)) { if (!_PyObject_HasAttrId(v, &PyId_print_file_and_line)) {
if (_PyObject_SetAttrId(v, &PyId_print_file_and_line, if (_PyObject_SetAttrId(v, &PyId_print_file_and_line,
Py_None)) Py_None)) {
PyErr_Clear(); _PyErr_Clear(tstate);
}
} }
} }
PyErr_Restore(exc, v, tb); _PyErr_Restore(tstate, exc, v, tb);
} }
void void
PyErr_SyntaxLocationEx(const char *filename, int lineno, int col_offset) PyErr_SyntaxLocationEx(const char *filename, int lineno, int col_offset)
{ {
PyThreadState *tstate = _PyThreadState_GET();
PyObject *fileobj; PyObject *fileobj;
if (filename != NULL) { if (filename != NULL) {
fileobj = PyUnicode_DecodeFSDefault(filename); fileobj = PyUnicode_DecodeFSDefault(filename);
if (fileobj == NULL) if (fileobj == NULL) {
PyErr_Clear(); _PyErr_Clear(tstate);
}
} }
else else {
fileobj = NULL; fileobj = NULL;
}
PyErr_SyntaxLocationObject(fileobj, lineno, col_offset); PyErr_SyntaxLocationObject(fileobj, lineno, col_offset);
Py_XDECREF(fileobj); Py_XDECREF(fileobj);
} }
@ -1333,7 +1443,7 @@ PyErr_SyntaxLocationEx(const char *filename, int lineno, int col_offset)
functionality in tb_displayline() in traceback.c. */ functionality in tb_displayline() in traceback.c. */
static PyObject * static PyObject *
err_programtext(FILE *fp, int lineno) err_programtext(PyThreadState *tstate, FILE *fp, int lineno)
{ {
int i; int i;
char linebuf[1000]; char linebuf[1000];
@ -1359,7 +1469,7 @@ err_programtext(FILE *fp, int lineno)
PyObject *res; PyObject *res;
res = PyUnicode_FromString(linebuf); res = PyUnicode_FromString(linebuf);
if (res == NULL) if (res == NULL)
PyErr_Clear(); _PyErr_Clear(tstate);
return res; return res;
} }
return NULL; return NULL;
@ -1369,24 +1479,28 @@ PyObject *
PyErr_ProgramText(const char *filename, int lineno) PyErr_ProgramText(const char *filename, int lineno)
{ {
FILE *fp; FILE *fp;
if (filename == NULL || *filename == '\0' || lineno <= 0) if (filename == NULL || *filename == '\0' || lineno <= 0) {
return NULL; return NULL;
}
PyThreadState *tstate = _PyThreadState_GET();
fp = _Py_fopen(filename, "r" PY_STDIOTEXTMODE); fp = _Py_fopen(filename, "r" PY_STDIOTEXTMODE);
return err_programtext(fp, lineno); return err_programtext(tstate, fp, lineno);
} }
PyObject * PyObject *
PyErr_ProgramTextObject(PyObject *filename, int lineno) PyErr_ProgramTextObject(PyObject *filename, int lineno)
{ {
FILE *fp; if (filename == NULL || lineno <= 0) {
if (filename == NULL || lineno <= 0)
return NULL;
fp = _Py_fopen_obj(filename, "r" PY_STDIOTEXTMODE);
if (fp == NULL) {
PyErr_Clear();
return NULL; return NULL;
} }
return err_programtext(fp, lineno);
PyThreadState *tstate = _PyThreadState_GET();
FILE *fp = _Py_fopen_obj(filename, "r" PY_STDIOTEXTMODE);
if (fp == NULL) {
_PyErr_Clear(tstate);
return NULL;
}
return err_programtext(tstate, fp, lineno);
} }
#ifdef __cplusplus #ifdef __cplusplus