Commit 438a12dd authored by Victor Stinner's avatar Victor Stinner Committed by GitHub

bpo-36710: Add tstate parameter in ceval.c (GH-13547)

* Fix a possible reference leak in _PyErr_Print() if exception
  is NULL.
* PyErr_BadInternalCall(): replace PyErr_Format() with _PyErr_SetString().
* Add pycore_pyerrors.h header file.
* New functions:

  * _PyErr_Clear()
  * _PyErr_Fetch()
  * _PyErr_Print()
  * _PyErr_Restore()
  * _PyErr_SetObject()
  * _PyErr_SetString()

* Add 'tstate' parameter to _PyEval_AddPendingCall().
parent 13d4e6a4
...@@ -19,6 +19,7 @@ PyAPI_FUNC(void) _PyEval_FiniThreads( ...@@ -19,6 +19,7 @@ PyAPI_FUNC(void) _PyEval_FiniThreads(
PyAPI_FUNC(void) _PyEval_SignalReceived( PyAPI_FUNC(void) _PyEval_SignalReceived(
struct _ceval_runtime_state *ceval); struct _ceval_runtime_state *ceval);
PyAPI_FUNC(int) _PyEval_AddPendingCall( PyAPI_FUNC(int) _PyEval_AddPendingCall(
PyThreadState *tstate,
struct _ceval_runtime_state *ceval, struct _ceval_runtime_state *ceval,
int (*func)(void *), int (*func)(void *),
void *arg); void *arg);
......
#ifndef Py_INTERNAL_PYERRORS_H
#define Py_INTERNAL_PYERRORS_H
#ifdef __cplusplus
extern "C" {
#endif
#ifndef Py_BUILD_CORE
# error "this header requires Py_BUILD_CORE define"
#endif
static inline PyObject* _PyErr_Occurred(PyThreadState *tstate)
{
return tstate == NULL ? NULL : tstate->curexc_type;
}
PyAPI_FUNC(void) _PyErr_Fetch(
PyThreadState *tstate,
PyObject **type,
PyObject **value,
PyObject **traceback);
PyAPI_FUNC(int) _PyErr_ExceptionMatches(
PyThreadState *tstate,
PyObject *exc);
PyAPI_FUNC(void) _PyErr_Restore(
PyThreadState *tstate,
PyObject *type,
PyObject *value,
PyObject *traceback);
PyAPI_FUNC(void) _PyErr_SetObject(
PyThreadState *tstate,
PyObject *type,
PyObject *value);
PyAPI_FUNC(void) _PyErr_Clear(PyThreadState *tstate);
PyAPI_FUNC(void) _PyErr_SetNone(PyThreadState *tstate, PyObject *exception);
PyAPI_FUNC(void) _PyErr_SetString(
PyThreadState *tstate,
PyObject *exception,
const char *string);
PyAPI_FUNC(PyObject *) _PyErr_Format(
PyThreadState *tstate,
PyObject *exception,
const char *format,
...);
PyAPI_FUNC(void) _PyErr_NormalizeException(
PyThreadState *tstate,
PyObject **exc,
PyObject **val,
PyObject **tb);
#ifdef __cplusplus
}
#endif
#endif /* !Py_INTERNAL_PYERRORS_H */
...@@ -106,6 +106,8 @@ PyAPI_FUNC(int) _Py_HandleSystemExit(int *exitcode_p); ...@@ -106,6 +106,8 @@ PyAPI_FUNC(int) _Py_HandleSystemExit(int *exitcode_p);
PyAPI_FUNC(PyObject*) _PyErr_WriteUnraisableDefaultHook(PyObject *unraisable); PyAPI_FUNC(PyObject*) _PyErr_WriteUnraisableDefaultHook(PyObject *unraisable);
PyAPI_FUNC(void) _PyErr_Print(PyThreadState *tstate);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
......
#ifndef Py_INTERNAL_MEM_H #ifndef Py_INTERNAL_PYMEM_H
#define Py_INTERNAL_MEM_H #define Py_INTERNAL_PYMEM_H
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
...@@ -191,4 +191,4 @@ PyAPI_FUNC(int) _PyMem_SetupAllocators(PyMemAllocatorName allocator); ...@@ -191,4 +191,4 @@ PyAPI_FUNC(int) _PyMem_SetupAllocators(PyMemAllocatorName allocator);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif /* !Py_INTERNAL_MEM_H */ #endif /* !Py_INTERNAL_PYMEM_H */
...@@ -1077,6 +1077,7 @@ PYTHON_HEADERS= \ ...@@ -1077,6 +1077,7 @@ PYTHON_HEADERS= \
$(srcdir)/Include/internal/pycore_hamt.h \ $(srcdir)/Include/internal/pycore_hamt.h \
$(srcdir)/Include/internal/pycore_object.h \ $(srcdir)/Include/internal/pycore_object.h \
$(srcdir)/Include/internal/pycore_pathconfig.h \ $(srcdir)/Include/internal/pycore_pathconfig.h \
$(srcdir)/Include/internal/pycore_pyerrors.h \
$(srcdir)/Include/internal/pycore_pyhash.h \ $(srcdir)/Include/internal/pycore_pyhash.h \
$(srcdir)/Include/internal/pycore_pylifecycle.h \ $(srcdir)/Include/internal/pycore_pylifecycle.h \
$(srcdir)/Include/internal/pycore_pymem.h \ $(srcdir)/Include/internal/pycore_pymem.h \
......
...@@ -258,6 +258,7 @@ trip_signal(int sig_num) ...@@ -258,6 +258,7 @@ trip_signal(int sig_num)
/* Notify ceval.c */ /* Notify ceval.c */
_PyRuntimeState *runtime = &_PyRuntime; _PyRuntimeState *runtime = &_PyRuntime;
PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
_PyEval_SignalReceived(&runtime->ceval); _PyEval_SignalReceived(&runtime->ceval);
/* And then write to the wakeup fd *after* setting all the globals and /* And then write to the wakeup fd *after* setting all the globals and
...@@ -298,7 +299,7 @@ trip_signal(int sig_num) ...@@ -298,7 +299,7 @@ trip_signal(int sig_num)
{ {
/* Py_AddPendingCall() isn't signal-safe, but we /* Py_AddPendingCall() isn't signal-safe, but we
still use it for this exceptional case. */ still use it for this exceptional case. */
_PyEval_AddPendingCall(&runtime->ceval, _PyEval_AddPendingCall(tstate, &runtime->ceval,
report_wakeup_send_error, report_wakeup_send_error,
(void *)(intptr_t) last_error); (void *)(intptr_t) last_error);
} }
...@@ -317,7 +318,7 @@ trip_signal(int sig_num) ...@@ -317,7 +318,7 @@ trip_signal(int sig_num)
{ {
/* Py_AddPendingCall() isn't signal-safe, but we /* Py_AddPendingCall() isn't signal-safe, but we
still use it for this exceptional case. */ still use it for this exceptional case. */
_PyEval_AddPendingCall(&runtime->ceval, _PyEval_AddPendingCall(tstate, &runtime->ceval,
report_wakeup_write_error, report_wakeup_write_error,
(void *)(intptr_t)errno); (void *)(intptr_t)errno);
} }
......
...@@ -168,6 +168,7 @@ ...@@ -168,6 +168,7 @@
<ClInclude Include="..\Include\internal\pycore_hamt.h" /> <ClInclude Include="..\Include\internal\pycore_hamt.h" />
<ClInclude Include="..\Include\internal\pycore_object.h" /> <ClInclude Include="..\Include\internal\pycore_object.h" />
<ClInclude Include="..\Include\internal\pycore_pathconfig.h" /> <ClInclude Include="..\Include\internal\pycore_pathconfig.h" />
<ClInclude Include="..\Include\internal\pycore_pyerrors.h" />
<ClInclude Include="..\Include\internal\pycore_pyhash.h" /> <ClInclude Include="..\Include\internal\pycore_pyhash.h" />
<ClInclude Include="..\Include\internal\pycore_pylifecycle.h" /> <ClInclude Include="..\Include\internal\pycore_pylifecycle.h" />
<ClInclude Include="..\Include\internal\pycore_pymem.h" /> <ClInclude Include="..\Include\internal\pycore_pymem.h" />
......
...@@ -207,6 +207,9 @@ ...@@ -207,6 +207,9 @@
<ClInclude Include="..\Include\internal\pycore_pathconfig.h"> <ClInclude Include="..\Include\internal\pycore_pathconfig.h">
<Filter>Include</Filter> <Filter>Include</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\Include\internal\pycore_pyerrors.h">
<Filter>Include</Filter>
</ClInclude>
<ClInclude Include="..\Include\internal\pycore_pyhash.h"> <ClInclude Include="..\Include\internal\pycore_pyhash.h">
<Filter>Include</Filter> <Filter>Include</Filter>
</ClInclude> </ClInclude>
......
...@@ -12,6 +12,8 @@ ...@@ -12,6 +12,8 @@
#include "Python.h" #include "Python.h"
#include "pycore_ceval.h" #include "pycore_ceval.h"
#include "pycore_object.h" #include "pycore_object.h"
#include "pycore_pyerrors.h"
#include "pycore_pylifecycle.h"
#include "pycore_pystate.h" #include "pycore_pystate.h"
#include "pycore_tupleobject.h" #include "pycore_tupleobject.h"
...@@ -50,7 +52,7 @@ static PyObject * do_call_core( ...@@ -50,7 +52,7 @@ static PyObject * do_call_core(
#ifdef LLTRACE #ifdef LLTRACE
static int lltrace; static int lltrace;
static int prtrace(PyObject *, const char *); static int prtrace(PyThreadState *, PyObject *, const char *);
#endif #endif
static int call_trace(Py_tracefunc, PyObject *, static int call_trace(Py_tracefunc, PyObject *,
PyThreadState *, PyFrameObject *, PyThreadState *, PyFrameObject *,
...@@ -67,19 +69,19 @@ static void maybe_dtrace_line(PyFrameObject *, int *, int *, int *); ...@@ -67,19 +69,19 @@ static void maybe_dtrace_line(PyFrameObject *, int *, int *, int *);
static void dtrace_function_entry(PyFrameObject *); static void dtrace_function_entry(PyFrameObject *);
static void dtrace_function_return(PyFrameObject *); static void dtrace_function_return(PyFrameObject *);
static PyObject * cmp_outcome(int, PyObject *, PyObject *); static PyObject * cmp_outcome(PyThreadState *, int, PyObject *, PyObject *);
static PyObject * import_name(PyFrameObject *, PyObject *, PyObject *, static PyObject * import_name(PyThreadState *, PyFrameObject *,
PyObject *); PyObject *, PyObject *, PyObject *);
static PyObject * import_from(PyObject *, PyObject *); static PyObject * import_from(PyThreadState *, PyObject *, PyObject *);
static int import_all_from(PyObject *, PyObject *); static int import_all_from(PyThreadState *, PyObject *, PyObject *);
static void format_exc_check_arg(PyObject *, const char *, PyObject *); static void format_exc_check_arg(PyThreadState *, PyObject *, const char *, PyObject *);
static void format_exc_unbound(PyCodeObject *co, int oparg); static void format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg);
static PyObject * unicode_concatenate(PyObject *, PyObject *, static PyObject * unicode_concatenate(PyThreadState *, PyObject *, PyObject *,
PyFrameObject *, const _Py_CODEUNIT *); PyFrameObject *, const _Py_CODEUNIT *);
static PyObject * special_lookup(PyObject *, _Py_Identifier *); static PyObject * special_lookup(PyThreadState *, PyObject *, _Py_Identifier *);
static int check_args_iterable(PyObject *func, PyObject *vararg); static int check_args_iterable(PyThreadState *, PyObject *func, PyObject *vararg);
static void format_kwargs_error(PyObject *func, PyObject *kwargs); static void format_kwargs_error(PyThreadState *, PyObject *func, PyObject *kwargs);
static void format_awaitable_error(PyTypeObject *, int); static void format_awaitable_error(PyThreadState *, PyTypeObject *, int);
#define NAME_ERROR_MSG \ #define NAME_ERROR_MSG \
"name '%.200s' is not defined" "name '%.200s' is not defined"
...@@ -420,7 +422,8 @@ _pop_pending_call(struct _pending_calls *pending, ...@@ -420,7 +422,8 @@ _pop_pending_call(struct _pending_calls *pending,
*/ */
int int
_PyEval_AddPendingCall(struct _ceval_runtime_state *ceval, _PyEval_AddPendingCall(PyThreadState *tstate,
struct _ceval_runtime_state *ceval,
int (*func)(void *), void *arg) int (*func)(void *), void *arg)
{ {
struct _pending_calls *pending = &ceval->pending; struct _pending_calls *pending = &ceval->pending;
...@@ -430,12 +433,12 @@ _PyEval_AddPendingCall(struct _ceval_runtime_state *ceval, ...@@ -430,12 +433,12 @@ _PyEval_AddPendingCall(struct _ceval_runtime_state *ceval,
PyThread_release_lock(pending->lock); PyThread_release_lock(pending->lock);
PyObject *exc, *val, *tb; PyObject *exc, *val, *tb;
PyErr_Fetch(&exc, &val, &tb); _PyErr_Fetch(tstate, &exc, &val, &tb);
PyErr_SetString(PyExc_SystemError, _PyErr_SetString(tstate, PyExc_SystemError,
"Py_AddPendingCall: cannot add pending calls " "Py_AddPendingCall: cannot add pending calls "
"(Python shutting down)"); "(Python shutting down)");
PyErr_Print(); _PyErr_Print(tstate);
PyErr_Restore(exc, val, tb); _PyErr_Restore(tstate, exc, val, tb);
return -1; return -1;
} }
int result = _push_pending_call(pending, func, arg); int result = _push_pending_call(pending, func, arg);
...@@ -449,7 +452,9 @@ _PyEval_AddPendingCall(struct _ceval_runtime_state *ceval, ...@@ -449,7 +452,9 @@ _PyEval_AddPendingCall(struct _ceval_runtime_state *ceval,
int int
Py_AddPendingCall(int (*func)(void *), void *arg) Py_AddPendingCall(int (*func)(void *), void *arg)
{ {
return _PyEval_AddPendingCall(&_PyRuntime.ceval, func, arg); _PyRuntimeState *runtime = &_PyRuntime;
PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
return _PyEval_AddPendingCall(tstate, &runtime->ceval, func, arg);
} }
static int static int
...@@ -535,6 +540,7 @@ _Py_FinishPendingCalls(_PyRuntimeState *runtime) ...@@ -535,6 +540,7 @@ _Py_FinishPendingCalls(_PyRuntimeState *runtime)
{ {
assert(PyGILState_Check()); assert(PyGILState_Check());
PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
struct _pending_calls *pending = &runtime->ceval.pending; struct _pending_calls *pending = &runtime->ceval.pending;
PyThread_acquire_lock(pending->lock, WAIT_LOCK); PyThread_acquire_lock(pending->lock, WAIT_LOCK);
...@@ -547,10 +553,10 @@ _Py_FinishPendingCalls(_PyRuntimeState *runtime) ...@@ -547,10 +553,10 @@ _Py_FinishPendingCalls(_PyRuntimeState *runtime)
if (make_pending_calls(runtime) < 0) { if (make_pending_calls(runtime) < 0) {
PyObject *exc, *val, *tb; PyObject *exc, *val, *tb;
PyErr_Fetch(&exc, &val, &tb); _PyErr_Fetch(tstate, &exc, &val, &tb);
PyErr_BadInternalCall(); PyErr_BadInternalCall();
_PyErr_ChainExceptions(exc, val, tb); _PyErr_ChainExceptions(exc, val, tb);
PyErr_Print(); _PyErr_Print(tstate);
} }
} }
...@@ -623,7 +629,7 @@ _Py_CheckRecursiveCall(const char *where) ...@@ -623,7 +629,7 @@ _Py_CheckRecursiveCall(const char *where)
tstate->stackcheck_counter = 0; tstate->stackcheck_counter = 0;
if (PyOS_CheckStack()) { if (PyOS_CheckStack()) {
--tstate->recursion_depth; --tstate->recursion_depth;
PyErr_SetString(PyExc_MemoryError, "Stack overflow"); _PyErr_SetString(tstate, PyExc_MemoryError, "Stack overflow");
return -1; return -1;
} }
/* Needed for ABI backwards-compatibility (see bpo-31857) */ /* Needed for ABI backwards-compatibility (see bpo-31857) */
...@@ -642,16 +648,16 @@ _Py_CheckRecursiveCall(const char *where) ...@@ -642,16 +648,16 @@ _Py_CheckRecursiveCall(const char *where)
if (tstate->recursion_depth > recursion_limit) { if (tstate->recursion_depth > recursion_limit) {
--tstate->recursion_depth; --tstate->recursion_depth;
tstate->overflowed = 1; tstate->overflowed = 1;
PyErr_Format(PyExc_RecursionError, _PyErr_Format(tstate, PyExc_RecursionError,
"maximum recursion depth exceeded%s", "maximum recursion depth exceeded%s",
where); where);
return -1; return -1;
} }
return 0; return 0;
} }
static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause); static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause);
static int unpack_iterable(PyObject *, int, int, PyObject **); static int unpack_iterable(PyThreadState *, PyObject *, int, int, PyObject **);
#define _Py_TracingPossible(ceval) ((ceval)->tracing_possible) #define _Py_TracingPossible(ceval) ((ceval)->tracing_possible)
...@@ -908,24 +914,24 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag) ...@@ -908,24 +914,24 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
#ifdef LLTRACE #ifdef LLTRACE
#define PUSH(v) { (void)(BASIC_PUSH(v), \ #define PUSH(v) { (void)(BASIC_PUSH(v), \
lltrace && prtrace(TOP(), "push")); \ lltrace && prtrace(tstate, TOP(), "push")); \
assert(STACK_LEVEL() <= co->co_stacksize); } assert(STACK_LEVEL() <= co->co_stacksize); }
#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \ #define POP() ((void)(lltrace && prtrace(tstate, TOP(), "pop")), \
BASIC_POP()) BASIC_POP())
#define STACK_GROW(n) do { \ #define STACK_GROW(n) do { \
assert(n >= 0); \ assert(n >= 0); \
(void)(BASIC_STACKADJ(n), \ (void)(BASIC_STACKADJ(n), \
lltrace && prtrace(TOP(), "stackadj")); \ lltrace && prtrace(tstate, TOP(), "stackadj")); \
assert(STACK_LEVEL() <= co->co_stacksize); \ assert(STACK_LEVEL() <= co->co_stacksize); \
} while (0) } while (0)
#define STACK_SHRINK(n) do { \ #define STACK_SHRINK(n) do { \
assert(n >= 0); \ assert(n >= 0); \
(void)(lltrace && prtrace(TOP(), "stackadj")); \ (void)(lltrace && prtrace(tstate, TOP(), "stackadj")); \
(void)(BASIC_STACKADJ(-n)); \ (void)(BASIC_STACKADJ(-n)); \
assert(STACK_LEVEL() <= co->co_stacksize); \ assert(STACK_LEVEL() <= co->co_stacksize); \
} while (0) } while (0)
#define EXT_POP(STACK_POINTER) ((void)(lltrace && \ #define EXT_POP(STACK_POINTER) ((void)(lltrace && \
prtrace((STACK_POINTER)[-1], "ext_pop")), \ prtrace(tstate, (STACK_POINTER)[-1], "ext_pop")), \
*--(STACK_POINTER)) *--(STACK_POINTER))
#else #else
#define PUSH(v) BASIC_PUSH(v) #define PUSH(v) BASIC_PUSH(v)
...@@ -1070,14 +1076,14 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag) ...@@ -1070,14 +1076,14 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
/* PyEval_EvalFrameEx() must not be called with an exception set, /* PyEval_EvalFrameEx() must not be called with an exception set,
because it can clear it (directly or indirectly) and so the because it can clear it (directly or indirectly) and so the
caller loses its exception */ caller loses its exception */
assert(!PyErr_Occurred()); assert(!_PyErr_Occurred(tstate));
#endif #endif
main_loop: main_loop:
for (;;) { for (;;) {
assert(stack_pointer >= f->f_valuestack); /* else underflow */ assert(stack_pointer >= f->f_valuestack); /* else underflow */
assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */ assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
assert(!PyErr_Occurred()); assert(!_PyErr_Occurred(tstate));
/* Do periodic things. Doing this every time through /* Do periodic things. Doing this every time through
the loop would add too much overhead, so we do it the loop would add too much overhead, so we do it
...@@ -1146,7 +1152,7 @@ main_loop: ...@@ -1146,7 +1152,7 @@ main_loop:
PyObject *exc = tstate->async_exc; PyObject *exc = tstate->async_exc;
tstate->async_exc = NULL; tstate->async_exc = NULL;
UNSIGNAL_ASYNC_EXC(ceval); UNSIGNAL_ASYNC_EXC(ceval);
PyErr_SetNone(exc); _PyErr_SetNone(tstate, exc);
Py_DECREF(exc); Py_DECREF(exc);
goto error; goto error;
} }
...@@ -1222,7 +1228,7 @@ main_loop: ...@@ -1222,7 +1228,7 @@ main_loop:
case TARGET(LOAD_FAST): { case TARGET(LOAD_FAST): {
PyObject *value = GETLOCAL(oparg); PyObject *value = GETLOCAL(oparg);
if (value == NULL) { if (value == NULL) {
format_exc_check_arg(PyExc_UnboundLocalError, format_exc_check_arg(tstate, PyExc_UnboundLocalError,
UNBOUNDLOCAL_ERROR_MSG, UNBOUNDLOCAL_ERROR_MSG,
PyTuple_GetItem(co->co_varnames, oparg)); PyTuple_GetItem(co->co_varnames, oparg));
goto error; goto error;
...@@ -1441,7 +1447,7 @@ main_loop: ...@@ -1441,7 +1447,7 @@ main_loop:
speedup on microbenchmarks. */ speedup on microbenchmarks. */
if (PyUnicode_CheckExact(left) && if (PyUnicode_CheckExact(left) &&
PyUnicode_CheckExact(right)) { PyUnicode_CheckExact(right)) {
sum = unicode_concatenate(left, right, f, next_instr); sum = unicode_concatenate(tstate, left, right, f, next_instr);
/* unicode_concatenate consumed the ref to left */ /* unicode_concatenate consumed the ref to left */
} }
else { else {
...@@ -1640,7 +1646,7 @@ main_loop: ...@@ -1640,7 +1646,7 @@ main_loop:
PyObject *left = TOP(); PyObject *left = TOP();
PyObject *sum; PyObject *sum;
if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) { if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
sum = unicode_concatenate(left, right, f, next_instr); sum = unicode_concatenate(tstate, left, right, f, next_instr);
/* unicode_concatenate consumed the ref to left */ /* unicode_concatenate consumed the ref to left */
} }
else { else {
...@@ -1762,8 +1768,8 @@ main_loop: ...@@ -1762,8 +1768,8 @@ main_loop:
PyObject *hook = _PySys_GetObjectId(&PyId_displayhook); PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
PyObject *res; PyObject *res;
if (hook == NULL) { if (hook == NULL) {
PyErr_SetString(PyExc_RuntimeError, _PyErr_SetString(tstate, PyExc_RuntimeError,
"lost sys.displayhook"); "lost sys.displayhook");
Py_DECREF(value); Py_DECREF(value);
goto error; goto error;
} }
...@@ -1790,8 +1796,8 @@ main_loop: ...@@ -1790,8 +1796,8 @@ main_loop:
} }
break; break;
default: default:
PyErr_SetString(PyExc_SystemError, _PyErr_SetString(tstate, PyExc_SystemError,
"bad RAISE_VARARGS oparg"); "bad RAISE_VARARGS oparg");
break; break;
} }
goto error; goto error;
...@@ -1823,11 +1829,10 @@ main_loop: ...@@ -1823,11 +1829,10 @@ main_loop:
} }
else { else {
SET_TOP(NULL); SET_TOP(NULL);
PyErr_Format( _PyErr_Format(tstate, PyExc_TypeError,
PyExc_TypeError, "'async for' requires an object with "
"'async for' requires an object with " "__aiter__ method, got %.100s",
"__aiter__ method, got %.100s", type->tp_name);
type->tp_name);
Py_DECREF(obj); Py_DECREF(obj);
goto error; goto error;
} }
...@@ -1836,11 +1841,10 @@ main_loop: ...@@ -1836,11 +1841,10 @@ main_loop:
Py_TYPE(iter)->tp_as_async->am_anext == NULL) { Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
SET_TOP(NULL); SET_TOP(NULL);
PyErr_Format( _PyErr_Format(tstate, PyExc_TypeError,
PyExc_TypeError, "'async for' received an object from __aiter__ "
"'async for' received an object from __aiter__ " "that does not implement __anext__: %.100s",
"that does not implement __anext__: %.100s", Py_TYPE(iter)->tp_name);
Py_TYPE(iter)->tp_name);
Py_DECREF(iter); Py_DECREF(iter);
goto error; goto error;
} }
...@@ -1873,11 +1877,10 @@ main_loop: ...@@ -1873,11 +1877,10 @@ main_loop:
} }
} }
else { else {
PyErr_Format( _PyErr_Format(tstate, PyExc_TypeError,
PyExc_TypeError, "'async for' requires an iterator with "
"'async for' requires an iterator with " "__anext__ method, got %.100s",
"__anext__ method, got %.100s", type->tp_name);
type->tp_name);
goto error; goto error;
} }
...@@ -1907,7 +1910,7 @@ main_loop: ...@@ -1907,7 +1910,7 @@ main_loop:
PyObject *iter = _PyCoro_GetAwaitableIter(iterable); PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
if (iter == NULL) { if (iter == NULL) {
format_awaitable_error(Py_TYPE(iterable), format_awaitable_error(tstate, Py_TYPE(iterable),
_Py_OPCODE(next_instr[-2])); _Py_OPCODE(next_instr[-2]));
} }
...@@ -1921,9 +1924,8 @@ main_loop: ...@@ -1921,9 +1924,8 @@ main_loop:
being awaited on. */ being awaited on. */
Py_DECREF(yf); Py_DECREF(yf);
Py_CLEAR(iter); Py_CLEAR(iter);
PyErr_SetString( _PyErr_SetString(tstate, PyExc_RuntimeError,
PyExc_RuntimeError, "coroutine is being awaited already");
"coroutine is being awaited already");
/* The code below jumps to `error` if `iter` is NULL. */ /* The code below jumps to `error` if `iter` is NULL. */
} }
} }
...@@ -1955,7 +1957,7 @@ main_loop: ...@@ -1955,7 +1957,7 @@ main_loop:
if (retval == NULL) { if (retval == NULL) {
PyObject *val; PyObject *val;
if (tstate->c_tracefunc != NULL if (tstate->c_tracefunc != NULL
&& PyErr_ExceptionMatches(PyExc_StopIteration)) && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration))
call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f); call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
err = _PyGen_FetchStopIterationValue(&val); err = _PyGen_FetchStopIterationValue(&val);
if (err < 0) if (err < 0)
...@@ -1994,8 +1996,8 @@ main_loop: ...@@ -1994,8 +1996,8 @@ main_loop:
_PyErr_StackItem *exc_info; _PyErr_StackItem *exc_info;
PyTryBlock *b = PyFrame_BlockPop(f); PyTryBlock *b = PyFrame_BlockPop(f);
if (b->b_type != EXCEPT_HANDLER) { if (b->b_type != EXCEPT_HANDLER) {
PyErr_SetString(PyExc_SystemError, _PyErr_SetString(tstate, PyExc_SystemError,
"popped block is not an except handler"); "popped block is not an except handler");
goto error; goto error;
} }
assert(STACK_LEVEL() >= (b)->b_level + 3 && assert(STACK_LEVEL() >= (b)->b_level + 3 &&
...@@ -2047,8 +2049,8 @@ main_loop: ...@@ -2047,8 +2049,8 @@ main_loop:
_PyErr_StackItem *exc_info; _PyErr_StackItem *exc_info;
PyTryBlock *b = PyFrame_BlockPop(f); PyTryBlock *b = PyFrame_BlockPop(f);
if (b->b_type != EXCEPT_HANDLER) { if (b->b_type != EXCEPT_HANDLER) {
PyErr_SetString(PyExc_SystemError, _PyErr_SetString(tstate, PyExc_SystemError,
"popped block is not an except handler"); "popped block is not an except handler");
Py_XDECREF(res); Py_XDECREF(res);
goto error; goto error;
} }
...@@ -2104,7 +2106,7 @@ main_loop: ...@@ -2104,7 +2106,7 @@ main_loop:
else if (PyLong_CheckExact(exc)) { else if (PyLong_CheckExact(exc)) {
int ret = _PyLong_AsInt(exc); int ret = _PyLong_AsInt(exc);
Py_DECREF(exc); Py_DECREF(exc);
if (ret == -1 && PyErr_Occurred()) { if (ret == -1 && _PyErr_Occurred(tstate)) {
goto error; goto error;
} }
JUMPTO(ret); JUMPTO(ret);
...@@ -2114,7 +2116,7 @@ main_loop: ...@@ -2114,7 +2116,7 @@ main_loop:
assert(PyExceptionClass_Check(exc)); assert(PyExceptionClass_Check(exc));
PyObject *val = POP(); PyObject *val = POP();
PyObject *tb = POP(); PyObject *tb = POP();
PyErr_Restore(exc, val, tb); _PyErr_Restore(tstate, exc, val, tb);
goto exception_unwind; goto exception_unwind;
} }
} }
...@@ -2134,7 +2136,7 @@ main_loop: ...@@ -2134,7 +2136,7 @@ main_loop:
else { else {
PyObject *val = POP(); PyObject *val = POP();
PyObject *tb = POP(); PyObject *tb = POP();
PyErr_Restore(exc, val, tb); _PyErr_Restore(tstate, exc, val, tb);
goto exception_unwind; goto exception_unwind;
} }
} }
...@@ -2146,9 +2148,9 @@ main_loop: ...@@ -2146,9 +2148,9 @@ main_loop:
if (PyDict_CheckExact(f->f_builtins)) { if (PyDict_CheckExact(f->f_builtins)) {
bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__); bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
if (bc == NULL) { if (bc == NULL) {
if (!PyErr_Occurred()) { if (!_PyErr_Occurred(tstate)) {
PyErr_SetString(PyExc_NameError, _PyErr_SetString(tstate, PyExc_NameError,
"__build_class__ not found"); "__build_class__ not found");
} }
goto error; goto error;
} }
...@@ -2160,9 +2162,9 @@ main_loop: ...@@ -2160,9 +2162,9 @@ main_loop:
goto error; goto error;
bc = PyObject_GetItem(f->f_builtins, build_class_str); bc = PyObject_GetItem(f->f_builtins, build_class_str);
if (bc == NULL) { if (bc == NULL) {
if (PyErr_ExceptionMatches(PyExc_KeyError)) if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
PyErr_SetString(PyExc_NameError, _PyErr_SetString(tstate, PyExc_NameError,
"__build_class__ not found"); "__build_class__ not found");
goto error; goto error;
} }
} }
...@@ -2176,8 +2178,8 @@ main_loop: ...@@ -2176,8 +2178,8 @@ main_loop:
PyObject *ns = f->f_locals; PyObject *ns = f->f_locals;
int err; int err;
if (ns == NULL) { if (ns == NULL) {
PyErr_Format(PyExc_SystemError, _PyErr_Format(tstate, PyExc_SystemError,
"no locals found when storing %R", name); "no locals found when storing %R", name);
Py_DECREF(v); Py_DECREF(v);
goto error; goto error;
} }
...@@ -2196,13 +2198,13 @@ main_loop: ...@@ -2196,13 +2198,13 @@ main_loop:
PyObject *ns = f->f_locals; PyObject *ns = f->f_locals;
int err; int err;
if (ns == NULL) { if (ns == NULL) {
PyErr_Format(PyExc_SystemError, _PyErr_Format(tstate, PyExc_SystemError,
"no locals when deleting %R", name); "no locals when deleting %R", name);
goto error; goto error;
} }
err = PyObject_DelItem(ns, name); err = PyObject_DelItem(ns, name);
if (err != 0) { if (err != 0) {
format_exc_check_arg(PyExc_NameError, format_exc_check_arg(tstate, PyExc_NameError,
NAME_ERROR_MSG, NAME_ERROR_MSG,
name); name);
goto error; goto error;
...@@ -2229,7 +2231,7 @@ main_loop: ...@@ -2229,7 +2231,7 @@ main_loop:
Py_INCREF(item); Py_INCREF(item);
PUSH(item); PUSH(item);
} }
} else if (unpack_iterable(seq, oparg, -1, } else if (unpack_iterable(tstate, seq, oparg, -1,
stack_pointer + oparg)) { stack_pointer + oparg)) {
STACK_GROW(oparg); STACK_GROW(oparg);
} else { } else {
...@@ -2245,7 +2247,7 @@ main_loop: ...@@ -2245,7 +2247,7 @@ main_loop:
int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8); int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
PyObject *seq = POP(); PyObject *seq = POP();
if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8, if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8,
stack_pointer + totalargs)) { stack_pointer + totalargs)) {
stack_pointer += totalargs; stack_pointer += totalargs;
} else { } else {
...@@ -2297,9 +2299,9 @@ main_loop: ...@@ -2297,9 +2299,9 @@ main_loop:
int err; int err;
err = PyDict_DelItem(f->f_globals, name); err = PyDict_DelItem(f->f_globals, name);
if (err != 0) { if (err != 0) {
if (PyErr_ExceptionMatches(PyExc_KeyError)) { if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
format_exc_check_arg( format_exc_check_arg(tstate, PyExc_NameError,
PyExc_NameError, NAME_ERROR_MSG, name); NAME_ERROR_MSG, name);
} }
goto error; goto error;
} }
...@@ -2311,8 +2313,8 @@ main_loop: ...@@ -2311,8 +2313,8 @@ main_loop:
PyObject *locals = f->f_locals; PyObject *locals = f->f_locals;
PyObject *v; PyObject *v;
if (locals == NULL) { if (locals == NULL) {
PyErr_Format(PyExc_SystemError, _PyErr_Format(tstate, PyExc_SystemError,
"no locals when loading %R", name); "no locals when loading %R", name);
goto error; goto error;
} }
if (PyDict_CheckExact(locals)) { if (PyDict_CheckExact(locals)) {
...@@ -2320,16 +2322,16 @@ main_loop: ...@@ -2320,16 +2322,16 @@ main_loop:
if (v != NULL) { if (v != NULL) {
Py_INCREF(v); Py_INCREF(v);
} }
else if (PyErr_Occurred()) { else if (_PyErr_Occurred(tstate)) {
goto error; goto error;
} }
} }
else { else {
v = PyObject_GetItem(locals, name); v = PyObject_GetItem(locals, name);
if (v == NULL) { if (v == NULL) {
if (!PyErr_ExceptionMatches(PyExc_KeyError)) if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
goto error; goto error;
PyErr_Clear(); _PyErr_Clear(tstate);
} }
} }
if (v == NULL) { if (v == NULL) {
...@@ -2337,16 +2339,16 @@ main_loop: ...@@ -2337,16 +2339,16 @@ main_loop:
if (v != NULL) { if (v != NULL) {
Py_INCREF(v); Py_INCREF(v);
} }
else if (PyErr_Occurred()) { else if (_PyErr_Occurred(tstate)) {
goto error; goto error;
} }
else { else {
if (PyDict_CheckExact(f->f_builtins)) { if (PyDict_CheckExact(f->f_builtins)) {
v = PyDict_GetItemWithError(f->f_builtins, name); v = PyDict_GetItemWithError(f->f_builtins, name);
if (v == NULL) { if (v == NULL) {
if (!PyErr_Occurred()) { if (!_PyErr_Occurred(tstate)) {
format_exc_check_arg( format_exc_check_arg(
PyExc_NameError, tstate, PyExc_NameError,
NAME_ERROR_MSG, name); NAME_ERROR_MSG, name);
} }
goto error; goto error;
...@@ -2356,10 +2358,11 @@ main_loop: ...@@ -2356,10 +2358,11 @@ main_loop:
else { else {
v = PyObject_GetItem(f->f_builtins, name); v = PyObject_GetItem(f->f_builtins, name);
if (v == NULL) { if (v == NULL) {
if (PyErr_ExceptionMatches(PyExc_KeyError)) if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
format_exc_check_arg( format_exc_check_arg(
PyExc_NameError, tstate, PyExc_NameError,
NAME_ERROR_MSG, name); NAME_ERROR_MSG, name);
}
goto error; goto error;
} }
} }
...@@ -2382,7 +2385,7 @@ main_loop: ...@@ -2382,7 +2385,7 @@ main_loop:
if (!_PyErr_OCCURRED()) { if (!_PyErr_OCCURRED()) {
/* _PyDict_LoadGlobal() returns NULL without raising /* _PyDict_LoadGlobal() returns NULL without raising
* an exception if the key doesn't exist */ * an exception if the key doesn't exist */
format_exc_check_arg(PyExc_NameError, format_exc_check_arg(tstate, PyExc_NameError,
NAME_ERROR_MSG, name); NAME_ERROR_MSG, name);
} }
goto error; goto error;
...@@ -2395,17 +2398,19 @@ main_loop: ...@@ -2395,17 +2398,19 @@ main_loop:
/* namespace 1: globals */ /* namespace 1: globals */
v = PyObject_GetItem(f->f_globals, name); v = PyObject_GetItem(f->f_globals, name);
if (v == NULL) { if (v == NULL) {
if (!PyErr_ExceptionMatches(PyExc_KeyError)) if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
goto error; goto error;
PyErr_Clear(); }
_PyErr_Clear(tstate);
/* namespace 2: builtins */ /* namespace 2: builtins */
v = PyObject_GetItem(f->f_builtins, name); v = PyObject_GetItem(f->f_builtins, name);
if (v == NULL) { if (v == NULL) {
if (PyErr_ExceptionMatches(PyExc_KeyError)) if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
format_exc_check_arg( format_exc_check_arg(
PyExc_NameError, tstate, PyExc_NameError,
NAME_ERROR_MSG, name); NAME_ERROR_MSG, name);
}
goto error; goto error;
} }
} }
...@@ -2421,7 +2426,7 @@ main_loop: ...@@ -2421,7 +2426,7 @@ main_loop:
DISPATCH(); DISPATCH();
} }
format_exc_check_arg( format_exc_check_arg(
PyExc_UnboundLocalError, tstate, PyExc_UnboundLocalError,
UNBOUNDLOCAL_ERROR_MSG, UNBOUNDLOCAL_ERROR_MSG,
PyTuple_GetItem(co->co_varnames, oparg) PyTuple_GetItem(co->co_varnames, oparg)
); );
...@@ -2436,7 +2441,7 @@ main_loop: ...@@ -2436,7 +2441,7 @@ main_loop:
Py_DECREF(oldobj); Py_DECREF(oldobj);
DISPATCH(); DISPATCH();
} }
format_exc_unbound(co, oparg); format_exc_unbound(tstate, co, oparg);
goto error; goto error;
} }
...@@ -2460,23 +2465,24 @@ main_loop: ...@@ -2460,23 +2465,24 @@ main_loop:
if (value != NULL) { if (value != NULL) {
Py_INCREF(value); Py_INCREF(value);
} }
else if (PyErr_Occurred()) { else if (_PyErr_Occurred(tstate)) {
goto error; goto error;
} }
} }
else { else {
value = PyObject_GetItem(locals, name); value = PyObject_GetItem(locals, name);
if (value == NULL) { if (value == NULL) {
if (!PyErr_ExceptionMatches(PyExc_KeyError)) if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
goto error; goto error;
PyErr_Clear(); }
_PyErr_Clear(tstate);
} }
} }
if (!value) { if (!value) {
PyObject *cell = freevars[oparg]; PyObject *cell = freevars[oparg];
value = PyCell_GET(cell); value = PyCell_GET(cell);
if (value == NULL) { if (value == NULL) {
format_exc_unbound(co, oparg); format_exc_unbound(tstate, co, oparg);
goto error; goto error;
} }
Py_INCREF(value); Py_INCREF(value);
...@@ -2489,7 +2495,7 @@ main_loop: ...@@ -2489,7 +2495,7 @@ main_loop:
PyObject *cell = freevars[oparg]; PyObject *cell = freevars[oparg];
PyObject *value = PyCell_GET(cell); PyObject *value = PyCell_GET(cell);
if (value == NULL) { if (value == NULL) {
format_exc_unbound(co, oparg); format_exc_unbound(tstate, co, oparg);
goto error; goto error;
} }
Py_INCREF(value); Py_INCREF(value);
...@@ -2565,9 +2571,9 @@ main_loop: ...@@ -2565,9 +2571,9 @@ main_loop:
none_val = _PyList_Extend((PyListObject *)sum, PEEK(i)); none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
if (none_val == NULL) { if (none_val == NULL) {
if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL && if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
PyErr_ExceptionMatches(PyExc_TypeError)) _PyErr_ExceptionMatches(tstate, PyExc_TypeError))
{ {
check_args_iterable(PEEK(1 + oparg), PEEK(i)); check_args_iterable(tstate, PEEK(1 + oparg), PEEK(i));
} }
Py_DECREF(sum); Py_DECREF(sum);
goto error; goto error;
...@@ -2660,8 +2666,8 @@ main_loop: ...@@ -2660,8 +2666,8 @@ main_loop:
int err; int err;
PyObject *ann_dict; PyObject *ann_dict;
if (f->f_locals == NULL) { if (f->f_locals == NULL) {
PyErr_Format(PyExc_SystemError, _PyErr_Format(tstate, PyExc_SystemError,
"no locals found when setting up annotations"); "no locals found when setting up annotations");
goto error; goto error;
} }
/* check if __annotations__ in locals()... */ /* check if __annotations__ in locals()... */
...@@ -2669,7 +2675,7 @@ main_loop: ...@@ -2669,7 +2675,7 @@ main_loop:
ann_dict = _PyDict_GetItemIdWithError(f->f_locals, ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
&PyId___annotations__); &PyId___annotations__);
if (ann_dict == NULL) { if (ann_dict == NULL) {
if (PyErr_Occurred()) { if (_PyErr_Occurred(tstate)) {
goto error; goto error;
} }
/* ...if not, create a new one */ /* ...if not, create a new one */
...@@ -2693,10 +2699,10 @@ main_loop: ...@@ -2693,10 +2699,10 @@ main_loop:
} }
ann_dict = PyObject_GetItem(f->f_locals, ann_str); ann_dict = PyObject_GetItem(f->f_locals, ann_str);
if (ann_dict == NULL) { if (ann_dict == NULL) {
if (!PyErr_ExceptionMatches(PyExc_KeyError)) { if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
goto error; goto error;
} }
PyErr_Clear(); _PyErr_Clear(tstate);
ann_dict = PyDict_New(); ann_dict = PyDict_New();
if (ann_dict == NULL) { if (ann_dict == NULL) {
goto error; goto error;
...@@ -2720,8 +2726,8 @@ main_loop: ...@@ -2720,8 +2726,8 @@ main_loop:
PyObject *keys = TOP(); PyObject *keys = TOP();
if (!PyTuple_CheckExact(keys) || if (!PyTuple_CheckExact(keys) ||
PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) { PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
PyErr_SetString(PyExc_SystemError, _PyErr_SetString(tstate, PyExc_SystemError,
"bad BUILD_CONST_KEY_MAP keys argument"); "bad BUILD_CONST_KEY_MAP keys argument");
goto error; goto error;
} }
map = _PyDict_NewPresized((Py_ssize_t)oparg); map = _PyDict_NewPresized((Py_ssize_t)oparg);
...@@ -2756,10 +2762,10 @@ main_loop: ...@@ -2756,10 +2762,10 @@ main_loop:
for (i = oparg; i > 0; i--) { for (i = oparg; i > 0; i--) {
PyObject *arg = PEEK(i); PyObject *arg = PEEK(i);
if (PyDict_Update(sum, arg) < 0) { if (PyDict_Update(sum, arg) < 0) {
if (PyErr_ExceptionMatches(PyExc_AttributeError)) { if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
PyErr_Format(PyExc_TypeError, _PyErr_Format(tstate, PyExc_TypeError,
"'%.200s' object is not a mapping", "'%.200s' object is not a mapping",
arg->ob_type->tp_name); arg->ob_type->tp_name);
} }
Py_DECREF(sum); Py_DECREF(sum);
goto error; goto error;
...@@ -2782,7 +2788,7 @@ main_loop: ...@@ -2782,7 +2788,7 @@ main_loop:
PyObject *arg = PEEK(i); PyObject *arg = PEEK(i);
if (_PyDict_MergeEx(sum, arg, 2) < 0) { if (_PyDict_MergeEx(sum, arg, 2) < 0) {
Py_DECREF(sum); Py_DECREF(sum);
format_kwargs_error(PEEK(2 + oparg), arg); format_kwargs_error(tstate, PEEK(2 + oparg), arg);
goto error; goto error;
} }
} }
...@@ -2824,7 +2830,7 @@ main_loop: ...@@ -2824,7 +2830,7 @@ main_loop:
case TARGET(COMPARE_OP): { case TARGET(COMPARE_OP): {
PyObject *right = POP(); PyObject *right = POP();
PyObject *left = TOP(); PyObject *left = TOP();
PyObject *res = cmp_outcome(oparg, left, right); PyObject *res = cmp_outcome(tstate, oparg, left, right);
Py_DECREF(left); Py_DECREF(left);
Py_DECREF(right); Py_DECREF(right);
SET_TOP(res); SET_TOP(res);
...@@ -2840,7 +2846,7 @@ main_loop: ...@@ -2840,7 +2846,7 @@ main_loop:
PyObject *fromlist = POP(); PyObject *fromlist = POP();
PyObject *level = TOP(); PyObject *level = TOP();
PyObject *res; PyObject *res;
res = import_name(f, name, fromlist, level); res = import_name(tstate, f, name, fromlist, level);
Py_DECREF(level); Py_DECREF(level);
Py_DECREF(fromlist); Py_DECREF(fromlist);
SET_TOP(res); SET_TOP(res);
...@@ -2859,12 +2865,12 @@ main_loop: ...@@ -2859,12 +2865,12 @@ main_loop:
locals = f->f_locals; locals = f->f_locals;
if (locals == NULL) { if (locals == NULL) {
PyErr_SetString(PyExc_SystemError, _PyErr_SetString(tstate, PyExc_SystemError,
"no locals found during 'import *'"); "no locals found during 'import *'");
Py_DECREF(from); Py_DECREF(from);
goto error; goto error;
} }
err = import_all_from(locals, from); err = import_all_from(tstate, locals, from);
PyFrame_LocalsToFast(f, 0); PyFrame_LocalsToFast(f, 0);
Py_DECREF(from); Py_DECREF(from);
if (err != 0) if (err != 0)
...@@ -2876,7 +2882,7 @@ main_loop: ...@@ -2876,7 +2882,7 @@ main_loop:
PyObject *name = GETITEM(names, oparg); PyObject *name = GETITEM(names, oparg);
PyObject *from = TOP(); PyObject *from = TOP();
PyObject *res; PyObject *res;
res = import_from(from, name); res = import_from(tstate, from, name);
PUSH(res); PUSH(res);
if (res == NULL) if (res == NULL)
goto error; goto error;
...@@ -3027,9 +3033,9 @@ main_loop: ...@@ -3027,9 +3033,9 @@ main_loop:
regular generator. */ regular generator. */
Py_DECREF(iterable); Py_DECREF(iterable);
SET_TOP(NULL); SET_TOP(NULL);
PyErr_SetString(PyExc_TypeError, _PyErr_SetString(tstate, PyExc_TypeError,
"cannot 'yield from' a coroutine object " "cannot 'yield from' a coroutine object "
"in a non-coroutine generator"); "in a non-coroutine generator");
goto error; goto error;
} }
} }
...@@ -3056,12 +3062,14 @@ main_loop: ...@@ -3056,12 +3062,14 @@ main_loop:
PREDICT(UNPACK_SEQUENCE); PREDICT(UNPACK_SEQUENCE);
DISPATCH(); DISPATCH();
} }
if (PyErr_Occurred()) { if (_PyErr_Occurred(tstate)) {
if (!PyErr_ExceptionMatches(PyExc_StopIteration)) if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
goto error; goto error;
else if (tstate->c_tracefunc != NULL) }
else if (tstate->c_tracefunc != NULL) {
call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f); call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
PyErr_Clear(); }
_PyErr_Clear(tstate);
} }
/* iterator ended normally */ /* iterator ended normally */
STACK_SHRINK(1); STACK_SHRINK(1);
...@@ -3087,13 +3095,13 @@ main_loop: ...@@ -3087,13 +3095,13 @@ main_loop:
_Py_IDENTIFIER(__aenter__); _Py_IDENTIFIER(__aenter__);
PyObject *mgr = TOP(); PyObject *mgr = TOP();
PyObject *exit = special_lookup(mgr, &PyId___aexit__), PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__),
*enter; *enter;
PyObject *res; PyObject *res;
if (exit == NULL) if (exit == NULL)
goto error; goto error;
SET_TOP(exit); SET_TOP(exit);
enter = special_lookup(mgr, &PyId___aenter__); enter = special_lookup(tstate, mgr, &PyId___aenter__);
Py_DECREF(mgr); Py_DECREF(mgr);
if (enter == NULL) if (enter == NULL)
goto error; goto error;
...@@ -3120,11 +3128,12 @@ main_loop: ...@@ -3120,11 +3128,12 @@ main_loop:
_Py_IDENTIFIER(__exit__); _Py_IDENTIFIER(__exit__);
_Py_IDENTIFIER(__enter__); _Py_IDENTIFIER(__enter__);
PyObject *mgr = TOP(); PyObject *mgr = TOP();
PyObject *enter = special_lookup(mgr, &PyId___enter__), *exit; PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__);
PyObject *res; PyObject *res;
if (enter == NULL) if (enter == NULL) {
goto error; goto error;
exit = special_lookup(mgr, &PyId___exit__); }
PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__);
if (exit == NULL) { if (exit == NULL) {
Py_DECREF(enter); Py_DECREF(enter);
goto error; goto error;
...@@ -3380,7 +3389,7 @@ main_loop: ...@@ -3380,7 +3389,7 @@ main_loop:
goto error; goto error;
if (_PyDict_MergeEx(d, kwargs, 2) < 0) { if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Py_DECREF(d); Py_DECREF(d);
format_kwargs_error(SECOND(), kwargs); format_kwargs_error(tstate, SECOND(), kwargs);
Py_DECREF(kwargs); Py_DECREF(kwargs);
goto error; goto error;
} }
...@@ -3392,7 +3401,7 @@ main_loop: ...@@ -3392,7 +3401,7 @@ main_loop:
callargs = POP(); callargs = POP();
func = TOP(); func = TOP();
if (!PyTuple_CheckExact(callargs)) { if (!PyTuple_CheckExact(callargs)) {
if (check_args_iterable(func, callargs) < 0) { if (check_args_iterable(tstate, func, callargs) < 0) {
Py_DECREF(callargs); Py_DECREF(callargs);
goto error; goto error;
} }
...@@ -3485,9 +3494,9 @@ main_loop: ...@@ -3485,9 +3494,9 @@ main_loop:
case FVC_REPR: conv_fn = PyObject_Repr; break; case FVC_REPR: conv_fn = PyObject_Repr; break;
case FVC_ASCII: conv_fn = PyObject_ASCII; break; case FVC_ASCII: conv_fn = PyObject_ASCII; break;
default: default:
PyErr_Format(PyExc_SystemError, _PyErr_Format(tstate, PyExc_SystemError,
"unexpected conversion flag %d", "unexpected conversion flag %d",
which_conversion); which_conversion);
goto error; goto error;
} }
...@@ -3542,7 +3551,7 @@ main_loop: ...@@ -3542,7 +3551,7 @@ main_loop:
"XXX lineno: %d, opcode: %d\n", "XXX lineno: %d, opcode: %d\n",
PyFrame_GetLineNumber(f), PyFrame_GetLineNumber(f),
opcode); opcode);
PyErr_SetString(PyExc_SystemError, "unknown opcode"); _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
goto error; goto error;
} /* switch */ } /* switch */
...@@ -3554,11 +3563,12 @@ main_loop: ...@@ -3554,11 +3563,12 @@ main_loop:
error: error:
/* Double-check exception status. */ /* Double-check exception status. */
#ifdef NDEBUG #ifdef NDEBUG
if (!PyErr_Occurred()) if (!_PyErr_Occurred(tstate)) {
PyErr_SetString(PyExc_SystemError, _PyErr_SetString(tstate, PyExc_SystemError,
"error return without exception set"); "error return without exception set");
}
#else #else
assert(PyErr_Occurred()); assert(_PyErr_Occurred(tstate));
#endif #endif
/* Log traceback info. */ /* Log traceback info. */
...@@ -3594,13 +3604,12 @@ exception_unwind: ...@@ -3594,13 +3604,12 @@ exception_unwind:
Py_INCREF(Py_None); Py_INCREF(Py_None);
PUSH(Py_None); PUSH(Py_None);
} }
PyErr_Fetch(&exc, &val, &tb); _PyErr_Fetch(tstate, &exc, &val, &tb);
/* Make the raw exception data /* Make the raw exception data
available to the handler, available to the handler,
so a program can emulate the so a program can emulate the
Python main loop. */ Python main loop. */
PyErr_NormalizeException( _PyErr_NormalizeException(tstate, &exc, &val, &tb);
&exc, &val, &tb);
if (tb != NULL) if (tb != NULL)
PyException_SetTraceback(val, tb); PyException_SetTraceback(val, tb);
else else
...@@ -3627,7 +3636,7 @@ exception_unwind: ...@@ -3627,7 +3636,7 @@ exception_unwind:
} /* main loop */ } /* main loop */
assert(retval == NULL); assert(retval == NULL);
assert(PyErr_Occurred()); assert(_PyErr_Occurred(tstate));
exit_returning: exit_returning:
...@@ -3665,7 +3674,8 @@ exit_eval_frame: ...@@ -3665,7 +3674,8 @@ exit_eval_frame:
} }
static void static void
format_missing(const char *kind, PyCodeObject *co, PyObject *names) format_missing(PyThreadState *tstate, const char *kind,
PyCodeObject *co, PyObject *names)
{ {
int err; int err;
Py_ssize_t len = PyList_GET_SIZE(names); Py_ssize_t len = PyList_GET_SIZE(names);
...@@ -3716,18 +3726,19 @@ format_missing(const char *kind, PyCodeObject *co, PyObject *names) ...@@ -3716,18 +3726,19 @@ format_missing(const char *kind, PyCodeObject *co, PyObject *names)
} }
if (name_str == NULL) if (name_str == NULL)
return; return;
PyErr_Format(PyExc_TypeError, _PyErr_Format(tstate, PyExc_TypeError,
"%U() missing %i required %s argument%s: %U", "%U() missing %i required %s argument%s: %U",
co->co_name, co->co_name,
len, len,
kind, kind,
len == 1 ? "" : "s", len == 1 ? "" : "s",
name_str); name_str);
Py_DECREF(name_str); Py_DECREF(name_str);
} }
static void static void
missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount, missing_arguments(PyThreadState *tstate, PyCodeObject *co,
Py_ssize_t missing, Py_ssize_t defcount,
PyObject **fastlocals) PyObject **fastlocals)
{ {
Py_ssize_t i, j = 0; Py_ssize_t i, j = 0;
...@@ -3760,12 +3771,13 @@ missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount, ...@@ -3760,12 +3771,13 @@ missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount,
} }
} }
assert(j == missing); assert(j == missing);
format_missing(kind, co, missing_names); format_missing(tstate, kind, co, missing_names);
Py_DECREF(missing_names); Py_DECREF(missing_names);
} }
static void static void
too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount, too_many_positional(PyThreadState *tstate, PyCodeObject *co,
Py_ssize_t given, Py_ssize_t defcount,
PyObject **fastlocals) PyObject **fastlocals)
{ {
int plural; int plural;
...@@ -3810,21 +3822,21 @@ too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount, ...@@ -3810,21 +3822,21 @@ too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount,
kwonly_sig = PyUnicode_FromString(""); kwonly_sig = PyUnicode_FromString("");
assert(kwonly_sig != NULL); assert(kwonly_sig != NULL);
} }
PyErr_Format(PyExc_TypeError, _PyErr_Format(tstate, PyExc_TypeError,
"%U() takes %U positional argument%s but %zd%U %s given", "%U() takes %U positional argument%s but %zd%U %s given",
co->co_name, co->co_name,
sig, sig,
plural ? "s" : "", plural ? "s" : "",
given, given,
kwonly_sig, kwonly_sig,
given == 1 && !kwonly_given ? "was" : "were"); given == 1 && !kwonly_given ? "was" : "were");
Py_DECREF(sig); Py_DECREF(sig);
Py_DECREF(kwonly_sig); Py_DECREF(kwonly_sig);
} }
static int static int
positional_only_passed_as_keyword(PyCodeObject *co, Py_ssize_t kwcount, positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co,
PyObject* const* kwnames) Py_ssize_t kwcount, PyObject* const* kwnames)
{ {
int posonly_conflicts = 0; int posonly_conflicts = 0;
PyObject* posonly_names = PyList_New(0); PyObject* posonly_names = PyList_New(0);
...@@ -3866,10 +3878,10 @@ positional_only_passed_as_keyword(PyCodeObject *co, Py_ssize_t kwcount, ...@@ -3866,10 +3878,10 @@ positional_only_passed_as_keyword(PyCodeObject *co, Py_ssize_t kwcount,
if (error_names == NULL) { if (error_names == NULL) {
goto fail; goto fail;
} }
PyErr_Format(PyExc_TypeError, _PyErr_Format(tstate, PyExc_TypeError,
"%U() got some positional-only arguments passed" "%U() got some positional-only arguments passed"
" as keyword arguments: '%U'", " as keyword arguments: '%U'",
co->co_name, error_names); co->co_name, error_names);
Py_DECREF(error_names); Py_DECREF(error_names);
goto fail; goto fail;
} }
...@@ -3905,15 +3917,16 @@ _PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals, ...@@ -3905,15 +3917,16 @@ _PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Py_ssize_t i, j, n; Py_ssize_t i, j, n;
PyObject *kwdict; PyObject *kwdict;
PyThreadState *tstate = _PyThreadState_GET();
assert(tstate != NULL);
if (globals == NULL) { if (globals == NULL) {
PyErr_SetString(PyExc_SystemError, _PyErr_SetString(tstate, PyExc_SystemError,
"PyEval_EvalCodeEx: NULL globals"); "PyEval_EvalCodeEx: NULL globals");
return NULL; return NULL;
} }
/* Create the frame */ /* Create the frame */
PyThreadState *tstate = _PyThreadState_GET();
assert(tstate != NULL);
f = _PyFrame_New_NoTrack(tstate, co, globals, locals); f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
if (f == NULL) { if (f == NULL) {
return NULL; return NULL;
...@@ -3981,9 +3994,9 @@ _PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals, ...@@ -3981,9 +3994,9 @@ _PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Py_ssize_t j; Py_ssize_t j;
if (keyword == NULL || !PyUnicode_Check(keyword)) { if (keyword == NULL || !PyUnicode_Check(keyword)) {
PyErr_Format(PyExc_TypeError, _PyErr_Format(tstate, PyExc_TypeError,
"%U() keywords must be strings", "%U() keywords must be strings",
co->co_name); co->co_name);
goto fail; goto fail;
} }
...@@ -4012,13 +4025,16 @@ _PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals, ...@@ -4012,13 +4025,16 @@ _PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
assert(j >= total_args); assert(j >= total_args);
if (kwdict == NULL) { if (kwdict == NULL) {
if (co->co_posonlyargcount && positional_only_passed_as_keyword(co, kwcount, kwnames)) { if (co->co_posonlyargcount
&& positional_only_passed_as_keyword(tstate, co,
kwcount, kwnames))
{
goto fail; goto fail;
} }
PyErr_Format(PyExc_TypeError, _PyErr_Format(tstate, PyExc_TypeError,
"%U() got an unexpected keyword argument '%S'", "%U() got an unexpected keyword argument '%S'",
co->co_name, keyword); co->co_name, keyword);
goto fail; goto fail;
} }
...@@ -4029,9 +4045,9 @@ _PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals, ...@@ -4029,9 +4045,9 @@ _PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
kw_found: kw_found:
if (GETLOCAL(j) != NULL) { if (GETLOCAL(j) != NULL) {
PyErr_Format(PyExc_TypeError, _PyErr_Format(tstate, PyExc_TypeError,
"%U() got multiple values for argument '%S'", "%U() got multiple values for argument '%S'",
co->co_name, keyword); co->co_name, keyword);
goto fail; goto fail;
} }
Py_INCREF(value); Py_INCREF(value);
...@@ -4040,7 +4056,7 @@ _PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals, ...@@ -4040,7 +4056,7 @@ _PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
/* Check the number of positional arguments */ /* Check the number of positional arguments */
if ((argcount > co->co_argcount + co->co_posonlyargcount) && !(co->co_flags & CO_VARARGS)) { if ((argcount > co->co_argcount + co->co_posonlyargcount) && !(co->co_flags & CO_VARARGS)) {
too_many_positional(co, argcount, defcount, fastlocals); too_many_positional(tstate, co, argcount, defcount, fastlocals);
goto fail; goto fail;
} }
...@@ -4054,7 +4070,7 @@ _PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals, ...@@ -4054,7 +4070,7 @@ _PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
} }
} }
if (missing) { if (missing) {
missing_arguments(co, missing, defcount, fastlocals); missing_arguments(tstate, co, missing, defcount, fastlocals);
goto fail; goto fail;
} }
if (n > m) if (n > m)
...@@ -4085,14 +4101,14 @@ _PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals, ...@@ -4085,14 +4101,14 @@ _PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
SETLOCAL(i, def); SETLOCAL(i, def);
continue; continue;
} }
else if (PyErr_Occurred()) { else if (_PyErr_Occurred(tstate)) {
goto fail; goto fail;
} }
} }
missing++; missing++;
} }
if (missing) { if (missing) {
missing_arguments(co, missing, -1, fastlocals); missing_arguments(tstate, co, missing, -1, fastlocals);
goto fail; goto fail;
} }
} }
...@@ -4132,11 +4148,11 @@ _PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals, ...@@ -4132,11 +4148,11 @@ _PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
if (is_coro && tstate->in_coroutine_wrapper) { if (is_coro && tstate->in_coroutine_wrapper) {
assert(coro_wrapper != NULL); assert(coro_wrapper != NULL);
PyErr_Format(PyExc_RuntimeError, _PyErr_Format(tstate, PyExc_RuntimeError,
"coroutine wrapper %.200R attempted " "coroutine wrapper %.200R attempted "
"to recursively wrap %.200R", "to recursively wrap %.200R",
coro_wrapper, coro_wrapper,
co); co);
goto fail; goto fail;
} }
...@@ -4209,12 +4225,12 @@ PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals, ...@@ -4209,12 +4225,12 @@ PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
} }
static PyObject * static PyObject *
special_lookup(PyObject *o, _Py_Identifier *id) special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id)
{ {
PyObject *res; PyObject *res;
res = _PyObject_LookupSpecial(o, id); res = _PyObject_LookupSpecial(o, id);
if (res == NULL && !PyErr_Occurred()) { if (res == NULL && !_PyErr_Occurred(tstate)) {
PyErr_SetObject(PyExc_AttributeError, id->object); _PyErr_SetObject(tstate, PyExc_AttributeError, id->object);
return NULL; return NULL;
} }
return res; return res;
...@@ -4236,14 +4252,14 @@ do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause) ...@@ -4236,14 +4252,14 @@ do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
value = exc_info->exc_value; value = exc_info->exc_value;
tb = exc_info->exc_traceback; tb = exc_info->exc_traceback;
if (type == Py_None || type == NULL) { if (type == Py_None || type == NULL) {
PyErr_SetString(PyExc_RuntimeError, _PyErr_SetString(tstate, PyExc_RuntimeError,
"No active exception to reraise"); "No active exception to reraise");
return 0; return 0;
} }
Py_XINCREF(type); Py_XINCREF(type);
Py_XINCREF(value); Py_XINCREF(value);
Py_XINCREF(tb); Py_XINCREF(tb);
PyErr_Restore(type, value, tb); _PyErr_Restore(tstate, type, value, tb);
return 1; return 1;
} }
...@@ -4258,11 +4274,11 @@ do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause) ...@@ -4258,11 +4274,11 @@ do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
if (value == NULL) if (value == NULL)
goto raise_error; goto raise_error;
if (!PyExceptionInstance_Check(value)) { if (!PyExceptionInstance_Check(value)) {
PyErr_Format(PyExc_TypeError, _PyErr_Format(tstate, PyExc_TypeError,
"calling %R should have returned an instance of " "calling %R should have returned an instance of "
"BaseException, not %R", "BaseException, not %R",
type, Py_TYPE(value)); type, Py_TYPE(value));
goto raise_error; goto raise_error;
} }
} }
else if (PyExceptionInstance_Check(exc)) { else if (PyExceptionInstance_Check(exc)) {
...@@ -4274,8 +4290,8 @@ do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause) ...@@ -4274,8 +4290,8 @@ do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
/* Not something you can raise. You get an exception /* Not something you can raise. You get an exception
anyway, just not what you specified :-) */ anyway, just not what you specified :-) */
Py_DECREF(exc); Py_DECREF(exc);
PyErr_SetString(PyExc_TypeError, _PyErr_SetString(tstate, PyExc_TypeError,
"exceptions must derive from BaseException"); "exceptions must derive from BaseException");
goto raise_error; goto raise_error;
} }
...@@ -4298,15 +4314,15 @@ do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause) ...@@ -4298,15 +4314,15 @@ do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
fixed_cause = NULL; fixed_cause = NULL;
} }
else { else {
PyErr_SetString(PyExc_TypeError, _PyErr_SetString(tstate, PyExc_TypeError,
"exception causes must derive from " "exception causes must derive from "
"BaseException"); "BaseException");
goto raise_error; goto raise_error;
} }
PyException_SetCause(value, fixed_cause); PyException_SetCause(value, fixed_cause);
} }
PyErr_SetObject(type, value); _PyErr_SetObject(tstate, type, value);
/* PyErr_SetObject incref's its arguments */ /* PyErr_SetObject incref's its arguments */
Py_DECREF(value); Py_DECREF(value);
Py_DECREF(type); Py_DECREF(type);
...@@ -4327,7 +4343,8 @@ raise_error: ...@@ -4327,7 +4343,8 @@ raise_error:
*/ */
static int static int
unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp) unpack_iterable(PyThreadState *tstate, PyObject *v,
int argcnt, int argcntafter, PyObject **sp)
{ {
int i = 0, j = 0; int i = 0, j = 0;
Py_ssize_t ll = 0; Py_ssize_t ll = 0;
...@@ -4339,12 +4356,12 @@ unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp) ...@@ -4339,12 +4356,12 @@ unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
it = PyObject_GetIter(v); it = PyObject_GetIter(v);
if (it == NULL) { if (it == NULL) {
if (PyErr_ExceptionMatches(PyExc_TypeError) && if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
v->ob_type->tp_iter == NULL && !PySequence_Check(v)) v->ob_type->tp_iter == NULL && !PySequence_Check(v))
{ {
PyErr_Format(PyExc_TypeError, _PyErr_Format(tstate, PyExc_TypeError,
"cannot unpack non-iterable %.200s object", "cannot unpack non-iterable %.200s object",
v->ob_type->tp_name); v->ob_type->tp_name);
} }
return 0; return 0;
} }
...@@ -4353,17 +4370,18 @@ unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp) ...@@ -4353,17 +4370,18 @@ unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
w = PyIter_Next(it); w = PyIter_Next(it);
if (w == NULL) { if (w == NULL) {
/* Iterator done, via error or exhaustion. */ /* Iterator done, via error or exhaustion. */
if (!PyErr_Occurred()) { if (!_PyErr_Occurred(tstate)) {
if (argcntafter == -1) { if (argcntafter == -1) {
PyErr_Format(PyExc_ValueError, _PyErr_Format(tstate, PyExc_ValueError,
"not enough values to unpack (expected %d, got %d)", "not enough values to unpack "
argcnt, i); "(expected %d, got %d)",
argcnt, i);
} }
else { else {
PyErr_Format(PyExc_ValueError, _PyErr_Format(tstate, PyExc_ValueError,
"not enough values to unpack " "not enough values to unpack "
"(expected at least %d, got %d)", "(expected at least %d, got %d)",
argcnt + argcntafter, i); argcnt + argcntafter, i);
} }
} }
goto Error; goto Error;
...@@ -4375,15 +4393,15 @@ unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp) ...@@ -4375,15 +4393,15 @@ unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
/* We better have exhausted the iterator now. */ /* We better have exhausted the iterator now. */
w = PyIter_Next(it); w = PyIter_Next(it);
if (w == NULL) { if (w == NULL) {
if (PyErr_Occurred()) if (_PyErr_Occurred(tstate))
goto Error; goto Error;
Py_DECREF(it); Py_DECREF(it);
return 1; return 1;
} }
Py_DECREF(w); Py_DECREF(w);
PyErr_Format(PyExc_ValueError, _PyErr_Format(tstate, PyExc_ValueError,
"too many values to unpack (expected %d)", "too many values to unpack (expected %d)",
argcnt); argcnt);
goto Error; goto Error;
} }
...@@ -4395,7 +4413,7 @@ unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp) ...@@ -4395,7 +4413,7 @@ unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
ll = PyList_GET_SIZE(l); ll = PyList_GET_SIZE(l);
if (ll < argcntafter) { if (ll < argcntafter) {
PyErr_Format(PyExc_ValueError, _PyErr_Format(tstate, PyExc_ValueError,
"not enough values to unpack (expected at least %d, got %zd)", "not enough values to unpack (expected at least %d, got %zd)",
argcnt + argcntafter, argcnt + ll); argcnt + argcntafter, argcnt + ll);
goto Error; goto Error;
...@@ -4420,11 +4438,13 @@ Error: ...@@ -4420,11 +4438,13 @@ Error:
#ifdef LLTRACE #ifdef LLTRACE
static int static int
prtrace(PyObject *v, const char *str) prtrace(PyThreadState *tstate, PyObject *v, const char *str)
{ {
printf("%s ", str); printf("%s ", str);
if (PyObject_Print(v, stdout, 0) != 0) if (PyObject_Print(v, stdout, 0) != 0) {
PyErr_Clear(); /* Don't know what else to do */ /* Don't know what else to do */
_PyErr_Clear(tstate);
}
printf("\n"); printf("\n");
return 1; return 1;
} }
...@@ -4436,22 +4456,23 @@ call_exc_trace(Py_tracefunc func, PyObject *self, ...@@ -4436,22 +4456,23 @@ call_exc_trace(Py_tracefunc func, PyObject *self,
{ {
PyObject *type, *value, *traceback, *orig_traceback, *arg; PyObject *type, *value, *traceback, *orig_traceback, *arg;
int err; int err;
PyErr_Fetch(&type, &value, &orig_traceback); _PyErr_Fetch(tstate, &type, &value, &orig_traceback);
if (value == NULL) { if (value == NULL) {
value = Py_None; value = Py_None;
Py_INCREF(value); Py_INCREF(value);
} }
PyErr_NormalizeException(&type, &value, &orig_traceback); _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback);
traceback = (orig_traceback != NULL) ? orig_traceback : Py_None; traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
arg = PyTuple_Pack(3, type, value, traceback); arg = PyTuple_Pack(3, type, value, traceback);
if (arg == NULL) { if (arg == NULL) {
PyErr_Restore(type, value, orig_traceback); _PyErr_Restore(tstate, type, value, orig_traceback);
return; return;
} }
err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg); err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Py_DECREF(arg); Py_DECREF(arg);
if (err == 0) if (err == 0) {
PyErr_Restore(type, value, orig_traceback); _PyErr_Restore(tstate, type, value, orig_traceback);
}
else { else {
Py_XDECREF(type); Py_XDECREF(type);
Py_XDECREF(value); Py_XDECREF(value);
...@@ -4466,11 +4487,11 @@ call_trace_protected(Py_tracefunc func, PyObject *obj, ...@@ -4466,11 +4487,11 @@ call_trace_protected(Py_tracefunc func, PyObject *obj,
{ {
PyObject *type, *value, *traceback; PyObject *type, *value, *traceback;
int err; int err;
PyErr_Fetch(&type, &value, &traceback); _PyErr_Fetch(tstate, &type, &value, &traceback);
err = call_trace(func, obj, tstate, frame, what, arg); err = call_trace(func, obj, tstate, frame, what, arg);
if (err == 0) if (err == 0)
{ {
PyErr_Restore(type, value, traceback); _PyErr_Restore(tstate, type, value, traceback);
return 0; return 0;
} }
else { else {
...@@ -4672,12 +4693,26 @@ _PyEval_GetAsyncGenFinalizer(void) ...@@ -4672,12 +4693,26 @@ _PyEval_GetAsyncGenFinalizer(void)
return tstate->async_gen_finalizer; return tstate->async_gen_finalizer;
} }
static PyFrameObject *
_PyEval_GetFrame(PyThreadState *tstate)
{
return _PyRuntime.gilstate.getframe(tstate);
}
PyFrameObject *
PyEval_GetFrame(void)
{
PyThreadState *tstate = _PyThreadState_GET();
return _PyEval_GetFrame(tstate);
}
PyObject * PyObject *
PyEval_GetBuiltins(void) PyEval_GetBuiltins(void)
{ {
PyFrameObject *current_frame = PyEval_GetFrame(); PyThreadState *tstate = _PyThreadState_GET();
PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
if (current_frame == NULL) if (current_frame == NULL)
return _PyInterpreterState_GET_UNSAFE()->builtins; return tstate->interp->builtins;
else else
return current_frame->f_builtins; return current_frame->f_builtins;
} }
...@@ -4686,12 +4721,13 @@ PyEval_GetBuiltins(void) ...@@ -4686,12 +4721,13 @@ PyEval_GetBuiltins(void)
PyObject * PyObject *
_PyEval_GetBuiltinId(_Py_Identifier *name) _PyEval_GetBuiltinId(_Py_Identifier *name)
{ {
PyThreadState *tstate = _PyThreadState_GET();
PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name); PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
if (attr) { if (attr) {
Py_INCREF(attr); Py_INCREF(attr);
} }
else if (!PyErr_Occurred()) { else if (!_PyErr_Occurred(tstate)) {
PyErr_SetObject(PyExc_AttributeError, _PyUnicode_FromId(name)); _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name));
} }
return attr; return attr;
} }
...@@ -4699,14 +4735,16 @@ _PyEval_GetBuiltinId(_Py_Identifier *name) ...@@ -4699,14 +4735,16 @@ _PyEval_GetBuiltinId(_Py_Identifier *name)
PyObject * PyObject *
PyEval_GetLocals(void) PyEval_GetLocals(void)
{ {
PyFrameObject *current_frame = PyEval_GetFrame(); PyThreadState *tstate = _PyThreadState_GET();
PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
if (current_frame == NULL) { if (current_frame == NULL) {
PyErr_SetString(PyExc_SystemError, "frame does not exist"); _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist");
return NULL; return NULL;
} }
if (PyFrame_FastToLocalsWithError(current_frame) < 0) if (PyFrame_FastToLocalsWithError(current_frame) < 0) {
return NULL; return NULL;
}
assert(current_frame->f_locals != NULL); assert(current_frame->f_locals != NULL);
return current_frame->f_locals; return current_frame->f_locals;
...@@ -4715,26 +4753,21 @@ PyEval_GetLocals(void) ...@@ -4715,26 +4753,21 @@ PyEval_GetLocals(void)
PyObject * PyObject *
PyEval_GetGlobals(void) PyEval_GetGlobals(void)
{ {
PyFrameObject *current_frame = PyEval_GetFrame(); PyThreadState *tstate = _PyThreadState_GET();
if (current_frame == NULL) PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
if (current_frame == NULL) {
return NULL; return NULL;
}
assert(current_frame->f_globals != NULL); assert(current_frame->f_globals != NULL);
return current_frame->f_globals; return current_frame->f_globals;
} }
PyFrameObject *
PyEval_GetFrame(void)
{
_PyRuntimeState *runtime = &_PyRuntime;
PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
return runtime->gilstate.getframe(tstate);
}
int int
PyEval_MergeCompilerFlags(PyCompilerFlags *cf) PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
{ {
PyFrameObject *current_frame = PyEval_GetFrame(); PyThreadState *tstate = _PyThreadState_GET();
PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
int result = cf->cf_flags != 0; int result = cf->cf_flags != 0;
if (current_frame != NULL) { if (current_frame != NULL) {
...@@ -4883,7 +4916,7 @@ call_function(PyThreadState *tstate, PyObject ***pp_stack, Py_ssize_t oparg, PyO ...@@ -4883,7 +4916,7 @@ call_function(PyThreadState *tstate, PyObject ***pp_stack, Py_ssize_t oparg, PyO
Py_DECREF(func); Py_DECREF(func);
} }
assert((x != NULL) ^ (PyErr_Occurred() != NULL)); assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
/* Clear the stack of the function object. */ /* Clear the stack of the function object. */
while ((*pp_stack) > pfunc) { while ((*pp_stack) > pfunc) {
...@@ -4939,17 +4972,18 @@ do_call_core(PyThreadState *tstate, PyObject *func, PyObject *callargs, PyObject ...@@ -4939,17 +4972,18 @@ do_call_core(PyThreadState *tstate, PyObject *func, PyObject *callargs, PyObject
int int
_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi) _PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
{ {
PyThreadState *tstate = _PyThreadState_GET();
if (v != Py_None) { if (v != Py_None) {
Py_ssize_t x; Py_ssize_t x;
if (PyIndex_Check(v)) { if (PyIndex_Check(v)) {
x = PyNumber_AsSsize_t(v, NULL); x = PyNumber_AsSsize_t(v, NULL);
if (x == -1 && PyErr_Occurred()) if (x == -1 && _PyErr_Occurred(tstate))
return 0; return 0;
} }
else { else {
PyErr_SetString(PyExc_TypeError, _PyErr_SetString(tstate, PyExc_TypeError,
"slice indices must be integers or " "slice indices must be integers or "
"None or have an __index__ method"); "None or have an __index__ method");
return 0; return 0;
} }
*pi = x; *pi = x;
...@@ -4960,16 +4994,17 @@ _PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi) ...@@ -4960,16 +4994,17 @@ _PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
int int
_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi) _PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
{ {
PyThreadState *tstate = _PyThreadState_GET();
Py_ssize_t x; Py_ssize_t x;
if (PyIndex_Check(v)) { if (PyIndex_Check(v)) {
x = PyNumber_AsSsize_t(v, NULL); x = PyNumber_AsSsize_t(v, NULL);
if (x == -1 && PyErr_Occurred()) if (x == -1 && _PyErr_Occurred(tstate))
return 0; return 0;
} }
else { else {
PyErr_SetString(PyExc_TypeError, _PyErr_SetString(tstate, PyExc_TypeError,
"slice indices must be integers or " "slice indices must be integers or "
"have an __index__ method"); "have an __index__ method");
return 0; return 0;
} }
*pi = x; *pi = x;
...@@ -4981,7 +5016,7 @@ _PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi) ...@@ -4981,7 +5016,7 @@ _PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
"BaseException is not allowed" "BaseException is not allowed"
static PyObject * static PyObject *
cmp_outcome(int op, PyObject *v, PyObject *w) cmp_outcome(PyThreadState *tstate, int op, PyObject *v, PyObject *w)
{ {
int res = 0; int res = 0;
switch (op) { switch (op) {
...@@ -5009,16 +5044,16 @@ cmp_outcome(int op, PyObject *v, PyObject *w) ...@@ -5009,16 +5044,16 @@ cmp_outcome(int op, PyObject *v, PyObject *w)
for (i = 0; i < length; i += 1) { for (i = 0; i < length; i += 1) {
PyObject *exc = PyTuple_GET_ITEM(w, i); PyObject *exc = PyTuple_GET_ITEM(w, i);
if (!PyExceptionClass_Check(exc)) { if (!PyExceptionClass_Check(exc)) {
PyErr_SetString(PyExc_TypeError, _PyErr_SetString(tstate, PyExc_TypeError,
CANNOT_CATCH_MSG); CANNOT_CATCH_MSG);
return NULL; return NULL;
} }
} }
} }
else { else {
if (!PyExceptionClass_Check(w)) { if (!PyExceptionClass_Check(w)) {
PyErr_SetString(PyExc_TypeError, _PyErr_SetString(tstate, PyExc_TypeError,
CANNOT_CATCH_MSG); CANNOT_CATCH_MSG);
return NULL; return NULL;
} }
} }
...@@ -5033,7 +5068,8 @@ cmp_outcome(int op, PyObject *v, PyObject *w) ...@@ -5033,7 +5068,8 @@ cmp_outcome(int op, PyObject *v, PyObject *w)
} }
static PyObject * static PyObject *
import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level) import_name(PyThreadState *tstate, PyFrameObject *f,
PyObject *name, PyObject *fromlist, PyObject *level)
{ {
_Py_IDENTIFIER(__import__); _Py_IDENTIFIER(__import__);
PyObject *import_func, *res; PyObject *import_func, *res;
...@@ -5041,16 +5077,16 @@ import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *leve ...@@ -5041,16 +5077,16 @@ import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *leve
import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__); import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
if (import_func == NULL) { if (import_func == NULL) {
if (!PyErr_Occurred()) { if (!_PyErr_Occurred(tstate)) {
PyErr_SetString(PyExc_ImportError, "__import__ not found"); _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
} }
return NULL; return NULL;
} }
/* Fast path for not overloaded __import__. */ /* Fast path for not overloaded __import__. */
if (import_func == _PyInterpreterState_GET_UNSAFE()->import_func) { if (import_func == tstate->interp->import_func) {
int ilevel = _PyLong_AsInt(level); int ilevel = _PyLong_AsInt(level);
if (ilevel == -1 && PyErr_Occurred()) { if (ilevel == -1 && _PyErr_Occurred(tstate)) {
return NULL; return NULL;
} }
res = PyImport_ImportModuleLevelObject( res = PyImport_ImportModuleLevelObject(
...@@ -5075,7 +5111,7 @@ import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *leve ...@@ -5075,7 +5111,7 @@ import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *leve
} }
static PyObject * static PyObject *
import_from(PyObject *v, PyObject *name) import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
{ {
PyObject *x; PyObject *x;
_Py_IDENTIFIER(__name__); _Py_IDENTIFIER(__name__);
...@@ -5102,7 +5138,7 @@ import_from(PyObject *v, PyObject *name) ...@@ -5102,7 +5138,7 @@ import_from(PyObject *v, PyObject *name)
} }
x = PyImport_GetModule(fullmodname); x = PyImport_GetModule(fullmodname);
Py_DECREF(fullmodname); Py_DECREF(fullmodname);
if (x == NULL && !PyErr_Occurred()) { if (x == NULL && !_PyErr_Occurred(tstate)) {
goto error; goto error;
} }
Py_DECREF(pkgname); Py_DECREF(pkgname);
...@@ -5120,7 +5156,7 @@ import_from(PyObject *v, PyObject *name) ...@@ -5120,7 +5156,7 @@ import_from(PyObject *v, PyObject *name)
} }
if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) { if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
PyErr_Clear(); _PyErr_Clear(tstate);
errmsg = PyUnicode_FromFormat( errmsg = PyUnicode_FromFormat(
"cannot import name %R from %R (unknown location)", "cannot import name %R from %R (unknown location)",
name, pkgname_or_unknown name, pkgname_or_unknown
...@@ -5144,7 +5180,7 @@ import_from(PyObject *v, PyObject *name) ...@@ -5144,7 +5180,7 @@ import_from(PyObject *v, PyObject *name)
} }
static int static int
import_all_from(PyObject *locals, PyObject *v) import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
{ {
_Py_IDENTIFIER(__all__); _Py_IDENTIFIER(__all__);
_Py_IDENTIFIER(__dict__); _Py_IDENTIFIER(__dict__);
...@@ -5161,7 +5197,7 @@ import_all_from(PyObject *locals, PyObject *v) ...@@ -5161,7 +5197,7 @@ import_all_from(PyObject *locals, PyObject *v)
return -1; return -1;
} }
if (dict == NULL) { if (dict == NULL) {
PyErr_SetString(PyExc_ImportError, _PyErr_SetString(tstate, PyExc_ImportError,
"from-import-* object has no __dict__ and no __all__"); "from-import-* object has no __dict__ and no __all__");
return -1; return -1;
} }
...@@ -5175,10 +5211,12 @@ import_all_from(PyObject *locals, PyObject *v) ...@@ -5175,10 +5211,12 @@ import_all_from(PyObject *locals, PyObject *v)
for (pos = 0, err = 0; ; pos++) { for (pos = 0, err = 0; ; pos++) {
name = PySequence_GetItem(all, pos); name = PySequence_GetItem(all, pos);
if (name == NULL) { if (name == NULL) {
if (!PyErr_ExceptionMatches(PyExc_IndexError)) if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) {
err = -1; err = -1;
else }
PyErr_Clear(); else {
_PyErr_Clear(tstate);
}
break; break;
} }
if (!PyUnicode_Check(name)) { if (!PyUnicode_Check(name)) {
...@@ -5189,17 +5227,17 @@ import_all_from(PyObject *locals, PyObject *v) ...@@ -5189,17 +5227,17 @@ import_all_from(PyObject *locals, PyObject *v)
break; break;
} }
if (!PyUnicode_Check(modname)) { if (!PyUnicode_Check(modname)) {
PyErr_Format(PyExc_TypeError, _PyErr_Format(tstate, PyExc_TypeError,
"module __name__ must be a string, not %.100s", "module __name__ must be a string, not %.100s",
Py_TYPE(modname)->tp_name); Py_TYPE(modname)->tp_name);
} }
else { else {
PyErr_Format(PyExc_TypeError, _PyErr_Format(tstate, PyExc_TypeError,
"%s in %U.%s must be str, not %.100s", "%s in %U.%s must be str, not %.100s",
skip_leading_underscores ? "Key" : "Item", skip_leading_underscores ? "Key" : "Item",
modname, modname,
skip_leading_underscores ? "__dict__" : "__all__", skip_leading_underscores ? "__dict__" : "__all__",
Py_TYPE(name)->tp_name); Py_TYPE(name)->tp_name);
} }
Py_DECREF(modname); Py_DECREF(modname);
Py_DECREF(name); Py_DECREF(name);
...@@ -5234,22 +5272,22 @@ import_all_from(PyObject *locals, PyObject *v) ...@@ -5234,22 +5272,22 @@ import_all_from(PyObject *locals, PyObject *v)
} }
static int static int
check_args_iterable(PyObject *func, PyObject *args) check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
{ {
if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) { if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) {
PyErr_Format(PyExc_TypeError, _PyErr_Format(tstate, PyExc_TypeError,
"%.200s%.200s argument after * " "%.200s%.200s argument after * "
"must be an iterable, not %.200s", "must be an iterable, not %.200s",
PyEval_GetFuncName(func), PyEval_GetFuncName(func),
PyEval_GetFuncDesc(func), PyEval_GetFuncDesc(func),
args->ob_type->tp_name); args->ob_type->tp_name);
return -1; return -1;
} }
return 0; return 0;
} }
static void static void
format_kwargs_error(PyObject *func, PyObject *kwargs) format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
{ {
/* _PyDict_MergeEx raises attribute /* _PyDict_MergeEx raises attribute
* error (percolated from an attempt * error (percolated from an attempt
...@@ -5257,44 +5295,46 @@ format_kwargs_error(PyObject *func, PyObject *kwargs) ...@@ -5257,44 +5295,46 @@ format_kwargs_error(PyObject *func, PyObject *kwargs)
* a type error if its second argument * a type error if its second argument
* is not a mapping. * is not a mapping.
*/ */
if (PyErr_ExceptionMatches(PyExc_AttributeError)) { if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
PyErr_Format(PyExc_TypeError, _PyErr_Format(tstate, PyExc_TypeError,
"%.200s%.200s argument after ** " "%.200s%.200s argument after ** "
"must be a mapping, not %.200s", "must be a mapping, not %.200s",
PyEval_GetFuncName(func), PyEval_GetFuncName(func),
PyEval_GetFuncDesc(func), PyEval_GetFuncDesc(func),
kwargs->ob_type->tp_name); kwargs->ob_type->tp_name);
} }
else if (PyErr_ExceptionMatches(PyExc_KeyError)) { else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
PyObject *exc, *val, *tb; PyObject *exc, *val, *tb;
PyErr_Fetch(&exc, &val, &tb); _PyErr_Fetch(tstate, &exc, &val, &tb);
if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) { if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
PyObject *key = PyTuple_GET_ITEM(val, 0); PyObject *key = PyTuple_GET_ITEM(val, 0);
if (!PyUnicode_Check(key)) { if (!PyUnicode_Check(key)) {
PyErr_Format(PyExc_TypeError, _PyErr_Format(tstate, PyExc_TypeError,
"%.200s%.200s keywords must be strings", "%.200s%.200s keywords must be strings",
PyEval_GetFuncName(func), PyEval_GetFuncName(func),
PyEval_GetFuncDesc(func)); PyEval_GetFuncDesc(func));
} else { }
PyErr_Format(PyExc_TypeError, else {
"%.200s%.200s got multiple " _PyErr_Format(tstate, PyExc_TypeError,
"values for keyword argument '%U'", "%.200s%.200s got multiple "
PyEval_GetFuncName(func), "values for keyword argument '%U'",
PyEval_GetFuncDesc(func), PyEval_GetFuncName(func),
key); PyEval_GetFuncDesc(func),
key);
} }
Py_XDECREF(exc); Py_XDECREF(exc);
Py_XDECREF(val); Py_XDECREF(val);
Py_XDECREF(tb); Py_XDECREF(tb);
} }
else { else {
PyErr_Restore(exc, val, tb); _PyErr_Restore(tstate, exc, val, tb);
} }
} }
} }
static void static void
format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj) format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
const char *format_str, PyObject *obj)
{ {
const char *obj_str; const char *obj_str;
...@@ -5305,52 +5345,52 @@ format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj) ...@@ -5305,52 +5345,52 @@ format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
if (!obj_str) if (!obj_str)
return; return;
PyErr_Format(exc, format_str, obj_str); _PyErr_Format(tstate, exc, format_str, obj_str);
} }
static void static void
format_exc_unbound(PyCodeObject *co, int oparg) format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
{ {
PyObject *name; PyObject *name;
/* Don't stomp existing exception */ /* Don't stomp existing exception */
if (PyErr_Occurred()) if (_PyErr_Occurred(tstate))
return; return;
if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) { if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
name = PyTuple_GET_ITEM(co->co_cellvars, name = PyTuple_GET_ITEM(co->co_cellvars,
oparg); oparg);
format_exc_check_arg( format_exc_check_arg(tstate,
PyExc_UnboundLocalError, PyExc_UnboundLocalError,
UNBOUNDLOCAL_ERROR_MSG, UNBOUNDLOCAL_ERROR_MSG,
name); name);
} else { } else {
name = PyTuple_GET_ITEM(co->co_freevars, oparg - name = PyTuple_GET_ITEM(co->co_freevars, oparg -
PyTuple_GET_SIZE(co->co_cellvars)); PyTuple_GET_SIZE(co->co_cellvars));
format_exc_check_arg(PyExc_NameError, format_exc_check_arg(tstate, PyExc_NameError,
UNBOUNDFREE_ERROR_MSG, name); UNBOUNDFREE_ERROR_MSG, name);
} }
} }
static void static void
format_awaitable_error(PyTypeObject *type, int prevopcode) format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevopcode)
{ {
if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) { if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
if (prevopcode == BEFORE_ASYNC_WITH) { if (prevopcode == BEFORE_ASYNC_WITH) {
PyErr_Format(PyExc_TypeError, _PyErr_Format(tstate, PyExc_TypeError,
"'async with' received an object from __aenter__ " "'async with' received an object from __aenter__ "
"that does not implement __await__: %.100s", "that does not implement __await__: %.100s",
type->tp_name); type->tp_name);
} }
else if (prevopcode == WITH_CLEANUP_START) { else if (prevopcode == WITH_CLEANUP_START) {
PyErr_Format(PyExc_TypeError, _PyErr_Format(tstate, PyExc_TypeError,
"'async with' received an object from __aexit__ " "'async with' received an object from __aexit__ "
"that does not implement __await__: %.100s", "that does not implement __await__: %.100s",
type->tp_name); type->tp_name);
} }
} }
} }
static PyObject * static PyObject *
unicode_concatenate(PyObject *v, PyObject *w, unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w,
PyFrameObject *f, const _Py_CODEUNIT *next_instr) PyFrameObject *f, const _Py_CODEUNIT *next_instr)
{ {
PyObject *res; PyObject *res;
...@@ -5390,7 +5430,7 @@ unicode_concatenate(PyObject *v, PyObject *w, ...@@ -5390,7 +5430,7 @@ unicode_concatenate(PyObject *v, PyObject *w,
if (locals && PyDict_CheckExact(locals)) { if (locals && PyDict_CheckExact(locals)) {
PyObject *w = PyDict_GetItemWithError(locals, name); PyObject *w = PyDict_GetItemWithError(locals, name);
if ((w == v && PyDict_DelItem(locals, name) != 0) || if ((w == v && PyDict_DelItem(locals, name) != 0) ||
(w == NULL && PyErr_Occurred())) (w == NULL && _PyErr_Occurred(tstate)))
{ {
Py_DECREF(v); Py_DECREF(v);
return NULL; return NULL;
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#include "Python.h" #include "Python.h"
#include "pycore_coreconfig.h" #include "pycore_coreconfig.h"
#include "pycore_pyerrors.h"
#include "pycore_pystate.h" #include "pycore_pystate.h"
#include "pycore_traceback.h" #include "pycore_traceback.h"
...@@ -27,13 +28,13 @@ _Py_IDENTIFIER(builtins); ...@@ -27,13 +28,13 @@ _Py_IDENTIFIER(builtins);
_Py_IDENTIFIER(stderr); _Py_IDENTIFIER(stderr);
/* Forward declaration */ /* Forward declarations */
static void _PyErr_Fetch(PyThreadState *tstate, PyObject **p_type, static PyObject *
PyObject **p_value, PyObject **p_traceback); _PyErr_FormatV(PyThreadState *tstate, PyObject *exception,
static void _PyErr_Clear(PyThreadState *tstate); const char *format, va_list vargs);
static void void
_PyErr_Restore(PyThreadState *tstate, PyObject *type, PyObject *value, _PyErr_Restore(PyThreadState *tstate, PyObject *type, PyObject *value,
PyObject *traceback) PyObject *traceback)
{ {
...@@ -95,7 +96,7 @@ _PyErr_CreateException(PyObject *exception, PyObject *value) ...@@ -95,7 +96,7 @@ _PyErr_CreateException(PyObject *exception, PyObject *value)
} }
} }
static void void
_PyErr_SetObject(PyThreadState *tstate, PyObject *exception, PyObject *value) _PyErr_SetObject(PyThreadState *tstate, PyObject *exception, PyObject *value)
{ {
PyObject *exc_value; PyObject *exc_value;
...@@ -103,9 +104,9 @@ _PyErr_SetObject(PyThreadState *tstate, PyObject *exception, PyObject *value) ...@@ -103,9 +104,9 @@ _PyErr_SetObject(PyThreadState *tstate, PyObject *exception, PyObject *value)
if (exception != NULL && if (exception != NULL &&
!PyExceptionClass_Check(exception)) { !PyExceptionClass_Check(exception)) {
PyErr_Format(PyExc_SystemError, _PyErr_Format(tstate, PyExc_SystemError,
"exception %R not a BaseException subclass", "exception %R not a BaseException subclass",
exception); exception);
return; return;
} }
...@@ -181,7 +182,7 @@ _PyErr_SetKeyError(PyObject *arg) ...@@ -181,7 +182,7 @@ _PyErr_SetKeyError(PyObject *arg)
Py_DECREF(tup); Py_DECREF(tup);
} }
static void void
_PyErr_SetNone(PyThreadState *tstate, PyObject *exception) _PyErr_SetNone(PyThreadState *tstate, PyObject *exception)
{ {
_PyErr_SetObject(tstate, exception, (PyObject *)NULL); _PyErr_SetObject(tstate, exception, (PyObject *)NULL);
...@@ -196,7 +197,7 @@ PyErr_SetNone(PyObject *exception) ...@@ -196,7 +197,7 @@ PyErr_SetNone(PyObject *exception)
} }
static void void
_PyErr_SetString(PyThreadState *tstate, PyObject *exception, _PyErr_SetString(PyThreadState *tstate, PyObject *exception,
const char *string) const char *string)
{ {
...@@ -213,13 +214,6 @@ PyErr_SetString(PyObject *exception, const char *string) ...@@ -213,13 +214,6 @@ PyErr_SetString(PyObject *exception, const char *string)
} }
static PyObject*
_PyErr_Occurred(PyThreadState *tstate)
{
return tstate == NULL ? NULL : tstate->curexc_type;
}
PyObject* _Py_HOT_FUNCTION PyObject* _Py_HOT_FUNCTION
PyErr_Occurred(void) PyErr_Occurred(void)
{ {
...@@ -260,11 +254,18 @@ PyErr_GivenExceptionMatches(PyObject *err, PyObject *exc) ...@@ -260,11 +254,18 @@ PyErr_GivenExceptionMatches(PyObject *err, PyObject *exc)
} }
int
_PyErr_ExceptionMatches(PyThreadState *tstate, PyObject *exc)
{
return PyErr_GivenExceptionMatches(_PyErr_Occurred(tstate), exc);
}
int int
PyErr_ExceptionMatches(PyObject *exc) PyErr_ExceptionMatches(PyObject *exc)
{ {
PyThreadState *tstate = _PyThreadState_GET(); PyThreadState *tstate = _PyThreadState_GET();
return PyErr_GivenExceptionMatches(_PyErr_Occurred(tstate), exc); return _PyErr_ExceptionMatches(tstate, exc);
} }
...@@ -278,7 +279,7 @@ PyErr_ExceptionMatches(PyObject *exc) ...@@ -278,7 +279,7 @@ 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?
*/ */
static void void
_PyErr_NormalizeException(PyThreadState *tstate, PyObject **exc, _PyErr_NormalizeException(PyThreadState *tstate, PyObject **exc,
PyObject **val, PyObject **tb) PyObject **val, PyObject **tb)
{ {
...@@ -390,7 +391,7 @@ PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb) ...@@ -390,7 +391,7 @@ PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb)
} }
static void void
_PyErr_Fetch(PyThreadState *tstate, PyObject **p_type, PyObject **p_value, _PyErr_Fetch(PyThreadState *tstate, PyObject **p_type, PyObject **p_value,
PyObject **p_traceback) PyObject **p_traceback)
{ {
...@@ -412,7 +413,7 @@ PyErr_Fetch(PyObject **p_type, PyObject **p_value, PyObject **p_traceback) ...@@ -412,7 +413,7 @@ PyErr_Fetch(PyObject **p_type, PyObject **p_value, PyObject **p_traceback)
} }
static void void
_PyErr_Clear(PyThreadState *tstate) _PyErr_Clear(PyThreadState *tstate)
{ {
_PyErr_Restore(tstate, NULL, NULL, NULL); _PyErr_Restore(tstate, NULL, NULL, NULL);
...@@ -506,7 +507,7 @@ _PyErr_FormatVFromCause(PyThreadState *tstate, PyObject *exception, ...@@ -506,7 +507,7 @@ _PyErr_FormatVFromCause(PyThreadState *tstate, PyObject *exception,
Py_DECREF(exc); Py_DECREF(exc);
assert(!_PyErr_Occurred(tstate)); assert(!_PyErr_Occurred(tstate));
PyErr_FormatV(exception, format, vargs); _PyErr_FormatV(tstate, exception, format, vargs);
_PyErr_Fetch(tstate, &exc, &val2, &tb); _PyErr_Fetch(tstate, &exc, &val2, &tb);
_PyErr_NormalizeException(tstate, &exc, &val2, &tb); _PyErr_NormalizeException(tstate, &exc, &val2, &tb);
...@@ -895,9 +896,10 @@ PyErr_SetImportError(PyObject *msg, PyObject *name, PyObject *path) ...@@ -895,9 +896,10 @@ PyErr_SetImportError(PyObject *msg, PyObject *name, PyObject *path)
void void
_PyErr_BadInternalCall(const char *filename, int lineno) _PyErr_BadInternalCall(const char *filename, int lineno)
{ {
PyErr_Format(PyExc_SystemError, PyThreadState *tstate = _PyThreadState_GET();
"%s:%d: bad argument to internal function", _PyErr_Format(tstate, PyExc_SystemError,
filename, lineno); "%s:%d: bad argument to internal function",
filename, lineno);
} }
/* Remove the preprocessor macro for PyErr_BadInternalCall() so that we can /* Remove the preprocessor macro for PyErr_BadInternalCall() so that we can
...@@ -907,16 +909,17 @@ void ...@@ -907,16 +909,17 @@ void
PyErr_BadInternalCall(void) PyErr_BadInternalCall(void)
{ {
assert(0 && "bad argument to internal function"); assert(0 && "bad argument to internal function");
PyErr_Format(PyExc_SystemError, PyThreadState *tstate = _PyThreadState_GET();
"bad argument to internal function"); _PyErr_SetString(tstate, PyExc_SystemError,
"bad argument to internal function");
} }
#define PyErr_BadInternalCall() _PyErr_BadInternalCall(__FILE__, __LINE__) #define PyErr_BadInternalCall() _PyErr_BadInternalCall(__FILE__, __LINE__)
PyObject * static PyObject *
PyErr_FormatV(PyObject *exception, const char *format, va_list vargs) _PyErr_FormatV(PyThreadState *tstate, 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
...@@ -931,16 +934,41 @@ PyErr_FormatV(PyObject *exception, const char *format, va_list vargs) ...@@ -931,16 +934,41 @@ PyErr_FormatV(PyObject *exception, const char *format, va_list vargs)
} }
PyObject *
PyErr_FormatV(PyObject *exception, const char *format, va_list vargs)
{
PyThreadState *tstate = _PyThreadState_GET();
return _PyErr_FormatV(tstate, exception, format, vargs);
}
PyObject *
_PyErr_Format(PyThreadState *tstate, PyObject *exception,
const char *format, ...)
{
va_list vargs;
#ifdef HAVE_STDARG_PROTOTYPES
va_start(vargs, format);
#else
va_start(vargs);
#endif
_PyErr_FormatV(tstate, exception, format, vargs);
va_end(vargs);
return NULL;
}
PyObject * PyObject *
PyErr_Format(PyObject *exception, const char *format, ...) PyErr_Format(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_FormatV(exception, format, vargs); _PyErr_FormatV(tstate, exception, format, vargs);
va_end(vargs); va_end(vargs);
return NULL; return NULL;
} }
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include "Python-ast.h" #include "Python-ast.h"
#undef Yield /* undefine macro conflicting with <winbase.h> */ #undef Yield /* undefine macro conflicting with <winbase.h> */
#include "pycore_pyerrors.h"
#include "pycore_pylifecycle.h" #include "pycore_pylifecycle.h"
#include "pycore_pystate.h" #include "pycore_pystate.h"
#include "grammar.h" #include "grammar.h"
...@@ -542,12 +543,6 @@ finally: ...@@ -542,12 +543,6 @@ finally:
return 0; return 0;
} }
void
PyErr_Print(void)
{
PyErr_PrintEx(1);
}
static void static void
print_error_text(PyObject *f, int offset, PyObject *text_obj) print_error_text(PyObject *f, int offset, PyObject *text_obj)
{ {
...@@ -667,34 +662,38 @@ handle_system_exit(void) ...@@ -667,34 +662,38 @@ handle_system_exit(void)
} }
void static void
PyErr_PrintEx(int set_sys_last_vars) _PyErr_PrintEx(PyThreadState *tstate, int set_sys_last_vars)
{ {
PyObject *exception, *v, *tb, *hook; PyObject *exception, *v, *tb, *hook;
handle_system_exit(); handle_system_exit();
PyErr_Fetch(&exception, &v, &tb); _PyErr_Fetch(tstate, &exception, &v, &tb);
if (exception == NULL) if (exception == NULL) {
return; goto done;
PyErr_NormalizeException(&exception, &v, &tb); }
_PyErr_NormalizeException(tstate, &exception, &v, &tb);
if (tb == NULL) { if (tb == NULL) {
tb = Py_None; tb = Py_None;
Py_INCREF(tb); Py_INCREF(tb);
} }
PyException_SetTraceback(v, tb); PyException_SetTraceback(v, tb);
if (exception == NULL) if (exception == NULL) {
return; goto done;
}
/* Now we know v != NULL too */ /* Now we know v != NULL too */
if (set_sys_last_vars) { if (set_sys_last_vars) {
if (_PySys_SetObjectId(&PyId_last_type, exception) < 0) { if (_PySys_SetObjectId(&PyId_last_type, exception) < 0) {
PyErr_Clear(); _PyErr_Clear(tstate);
} }
if (_PySys_SetObjectId(&PyId_last_value, v) < 0) { if (_PySys_SetObjectId(&PyId_last_value, v) < 0) {
PyErr_Clear(); _PyErr_Clear(tstate);
} }
if (_PySys_SetObjectId(&PyId_last_traceback, tb) < 0) { if (_PySys_SetObjectId(&PyId_last_traceback, tb) < 0) {
PyErr_Clear(); _PyErr_Clear(tstate);
} }
} }
hook = _PySys_GetObjectId(&PyId_excepthook); hook = _PySys_GetObjectId(&PyId_excepthook);
...@@ -710,8 +709,8 @@ PyErr_PrintEx(int set_sys_last_vars) ...@@ -710,8 +709,8 @@ PyErr_PrintEx(int set_sys_last_vars)
handle_system_exit(); handle_system_exit();
PyObject *exception2, *v2, *tb2; PyObject *exception2, *v2, *tb2;
PyErr_Fetch(&exception2, &v2, &tb2); _PyErr_Fetch(tstate, &exception2, &v2, &tb2);
PyErr_NormalizeException(&exception2, &v2, &tb2); _PyErr_NormalizeException(tstate, &exception2, &v2, &tb2);
/* It should not be possible for exception2 or v2 /* It should not be possible for exception2 or v2
to be NULL. However PyErr_Display() can't to be NULL. However PyErr_Display() can't
tolerate NULLs, so just be safe. */ tolerate NULLs, so just be safe. */
...@@ -733,15 +732,37 @@ PyErr_PrintEx(int set_sys_last_vars) ...@@ -733,15 +732,37 @@ PyErr_PrintEx(int set_sys_last_vars)
Py_XDECREF(tb2); Py_XDECREF(tb2);
} }
Py_XDECREF(result); Py_XDECREF(result);
} else { }
else {
PySys_WriteStderr("sys.excepthook is missing\n"); PySys_WriteStderr("sys.excepthook is missing\n");
PyErr_Display(exception, v, tb); PyErr_Display(exception, v, tb);
} }
done:
Py_XDECREF(exception); Py_XDECREF(exception);
Py_XDECREF(v); Py_XDECREF(v);
Py_XDECREF(tb); Py_XDECREF(tb);
} }
void
_PyErr_Print(PyThreadState *tstate)
{
_PyErr_PrintEx(tstate, 1);
}
void
PyErr_PrintEx(int set_sys_last_vars)
{
PyThreadState *tstate = _PyThreadState_GET();
_PyErr_PrintEx(tstate, set_sys_last_vars);
}
void
PyErr_Print(void)
{
PyErr_PrintEx(1);
}
static void static void
print_exception(PyObject *f, PyObject *value) print_exception(PyObject *f, PyObject *value)
{ {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment