Commit 838f2640 authored by Victor Stinner's avatar Victor Stinner Committed by GitHub

bpo-36710: Pass explicitly tstate in sysmodule.c (GH-14060)

* Replace global var Py_VerboseFlag with interp->config.verbose.
* Add _PyErr_NoMemory(tstate) function.
* Add tstate parameter to _PyEval_SetCoroutineOriginTrackingDepth()
  and move the function to the internal API.
* Replace _PySys_InitMain(runtime, interp)
  with _PySys_InitMain(runtime, tstate).
parent 3498c642
...@@ -31,7 +31,6 @@ PyAPI_FUNC(PyObject *) PyEval_CallMethod(PyObject *obj, ...@@ -31,7 +31,6 @@ PyAPI_FUNC(PyObject *) PyEval_CallMethod(PyObject *obj,
#ifndef Py_LIMITED_API #ifndef Py_LIMITED_API
PyAPI_FUNC(void) PyEval_SetProfile(Py_tracefunc, PyObject *); PyAPI_FUNC(void) PyEval_SetProfile(Py_tracefunc, PyObject *);
PyAPI_FUNC(void) PyEval_SetTrace(Py_tracefunc, PyObject *); PyAPI_FUNC(void) PyEval_SetTrace(Py_tracefunc, PyObject *);
PyAPI_FUNC(void) _PyEval_SetCoroutineOriginTrackingDepth(int new_depth);
PyAPI_FUNC(int) _PyEval_GetCoroutineOriginTrackingDepth(void); PyAPI_FUNC(int) _PyEval_GetCoroutineOriginTrackingDepth(void);
PyAPI_FUNC(void) _PyEval_SetAsyncGenFirstiter(PyObject *); PyAPI_FUNC(void) _PyEval_SetAsyncGenFirstiter(PyObject *);
PyAPI_FUNC(PyObject *) _PyEval_GetAsyncGenFirstiter(void); PyAPI_FUNC(PyObject *) _PyEval_GetAsyncGenFirstiter(void);
......
...@@ -27,6 +27,9 @@ PyAPI_FUNC(void) _PyEval_SignalAsyncExc( ...@@ -27,6 +27,9 @@ PyAPI_FUNC(void) _PyEval_SignalAsyncExc(
struct _ceval_runtime_state *ceval); struct _ceval_runtime_state *ceval);
PyAPI_FUNC(void) _PyEval_ReInitThreads( PyAPI_FUNC(void) _PyEval_ReInitThreads(
_PyRuntimeState *runtime); _PyRuntimeState *runtime);
PyAPI_FUNC(void) _PyEval_SetCoroutineOriginTrackingDepth(
PyThreadState *tstate,
int new_depth);
/* Private function */ /* Private function */
void _PyEval_Fini(void); void _PyEval_Fini(void);
......
...@@ -39,6 +39,8 @@ PyAPI_FUNC(void) _PyErr_Clear(PyThreadState *tstate); ...@@ -39,6 +39,8 @@ PyAPI_FUNC(void) _PyErr_Clear(PyThreadState *tstate);
PyAPI_FUNC(void) _PyErr_SetNone(PyThreadState *tstate, PyObject *exception); PyAPI_FUNC(void) _PyErr_SetNone(PyThreadState *tstate, PyObject *exception);
PyAPI_FUNC(PyObject *) _PyErr_NoMemory(PyThreadState *tstate);
PyAPI_FUNC(void) _PyErr_SetString( PyAPI_FUNC(void) _PyErr_SetString(
PyThreadState *tstate, PyThreadState *tstate,
PyObject *exception, PyObject *exception,
......
...@@ -45,7 +45,7 @@ extern PyStatus _PySys_Create( ...@@ -45,7 +45,7 @@ extern PyStatus _PySys_Create(
extern PyStatus _PySys_SetPreliminaryStderr(PyObject *sysdict); extern PyStatus _PySys_SetPreliminaryStderr(PyObject *sysdict);
extern int _PySys_InitMain( extern int _PySys_InitMain(
_PyRuntimeState *runtime, _PyRuntimeState *runtime,
PyInterpreterState *interp); PyThreadState *tstate);
extern PyStatus _PyImport_Init(PyInterpreterState *interp); extern PyStatus _PyImport_Init(PyInterpreterState *interp);
extern PyStatus _PyExc_Init(void); extern PyStatus _PyExc_Init(void);
extern PyStatus _PyErr_Init(void); extern PyStatus _PyErr_Init(void);
......
...@@ -4728,10 +4728,9 @@ PyEval_SetTrace(Py_tracefunc func, PyObject *arg) ...@@ -4728,10 +4728,9 @@ PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
} }
void void
_PyEval_SetCoroutineOriginTrackingDepth(int new_depth) _PyEval_SetCoroutineOriginTrackingDepth(PyThreadState *tstate, int new_depth)
{ {
assert(new_depth >= 0); assert(new_depth >= 0);
PyThreadState *tstate = _PyThreadState_GET();
tstate->coroutine_origin_tracking_depth = new_depth; tstate->coroutine_origin_tracking_depth = new_depth;
} }
......
...@@ -547,9 +547,8 @@ PyErr_BadArgument(void) ...@@ -547,9 +547,8 @@ PyErr_BadArgument(void)
} }
PyObject * PyObject *
PyErr_NoMemory(void) _PyErr_NoMemory(PyThreadState *tstate)
{ {
PyThreadState *tstate = _PyThreadState_GET();
if (Py_TYPE(PyExc_MemoryError) == NULL) { if (Py_TYPE(PyExc_MemoryError) == NULL) {
/* PyErr_NoMemory() has been called before PyExc_MemoryError has been /* PyErr_NoMemory() has been called before PyExc_MemoryError has been
initialized by _PyExc_Init() */ initialized by _PyExc_Init() */
...@@ -560,6 +559,13 @@ PyErr_NoMemory(void) ...@@ -560,6 +559,13 @@ PyErr_NoMemory(void)
return NULL; return NULL;
} }
PyObject *
PyErr_NoMemory(void)
{
PyThreadState *tstate = _PyThreadState_GET();
return _PyErr_NoMemory(tstate);
}
PyObject * PyObject *
PyErr_SetFromErrnoWithFilenameObject(PyObject *exc, PyObject *filenameObject) PyErr_SetFromErrnoWithFilenameObject(PyObject *exc, PyObject *filenameObject)
{ {
......
...@@ -899,6 +899,7 @@ pyinit_main(_PyRuntimeState *runtime, PyInterpreterState *interp) ...@@ -899,6 +899,7 @@ pyinit_main(_PyRuntimeState *runtime, PyInterpreterState *interp)
} }
/* Configure the main interpreter */ /* Configure the main interpreter */
PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
PyConfig *config = &interp->config; PyConfig *config = &interp->config;
if (runtime->initialized) { if (runtime->initialized) {
...@@ -919,7 +920,7 @@ pyinit_main(_PyRuntimeState *runtime, PyInterpreterState *interp) ...@@ -919,7 +920,7 @@ pyinit_main(_PyRuntimeState *runtime, PyInterpreterState *interp)
return _PyStatus_ERR("can't initialize time"); return _PyStatus_ERR("can't initialize time");
} }
if (_PySys_InitMain(runtime, interp) < 0) { if (_PySys_InitMain(runtime, tstate) < 0) {
return _PyStatus_ERR("can't finish initializing sys"); return _PyStatus_ERR("can't finish initializing sys");
} }
...@@ -1456,7 +1457,7 @@ new_interpreter(PyThreadState **tstate_p) ...@@ -1456,7 +1457,7 @@ new_interpreter(PyThreadState **tstate_p)
} }
Py_INCREF(interp->sysdict); Py_INCREF(interp->sysdict);
PyDict_SetItemString(interp->sysdict, "modules", modules); PyDict_SetItemString(interp->sysdict, "modules", modules);
if (_PySys_InitMain(runtime, interp) < 0) { if (_PySys_InitMain(runtime, tstate) < 0) {
return _PyStatus_ERR("can't finish initializing sys"); return _PyStatus_ERR("can't finish initializing sys");
} }
} }
......
...@@ -17,10 +17,12 @@ Data members: ...@@ -17,10 +17,12 @@ Data members:
#include "Python.h" #include "Python.h"
#include "code.h" #include "code.h"
#include "frameobject.h" #include "frameobject.h"
#include "pycore_ceval.h"
#include "pycore_initconfig.h" #include "pycore_initconfig.h"
#include "pycore_pathconfig.h"
#include "pycore_pyerrors.h"
#include "pycore_pylifecycle.h" #include "pycore_pylifecycle.h"
#include "pycore_pymem.h" #include "pycore_pymem.h"
#include "pycore_pathconfig.h"
#include "pycore_pystate.h" #include "pycore_pystate.h"
#include "pycore_tupleobject.h" #include "pycore_tupleobject.h"
#include "pythread.h" #include "pythread.h"
...@@ -59,30 +61,38 @@ _Py_IDENTIFIER(stderr); ...@@ -59,30 +61,38 @@ _Py_IDENTIFIER(stderr);
_Py_IDENTIFIER(warnoptions); _Py_IDENTIFIER(warnoptions);
_Py_IDENTIFIER(write); _Py_IDENTIFIER(write);
PyObject * static PyObject *
_PySys_GetObjectId(_Py_Identifier *key) sys_get_object_id(PyThreadState *tstate, _Py_Identifier *key)
{ {
PyObject *sd = _PyInterpreterState_GET_UNSAFE()->sysdict; PyObject *sd = tstate->interp->sysdict;
if (sd == NULL) { if (sd == NULL) {
return NULL; return NULL;
} }
return _PyDict_GetItemId(sd, key); return _PyDict_GetItemId(sd, key);
} }
PyObject *
_PySys_GetObjectId(_Py_Identifier *key)
{
PyThreadState *tstate = _PyThreadState_GET();
return sys_get_object_id(tstate, key);
}
PyObject * PyObject *
PySys_GetObject(const char *name) PySys_GetObject(const char *name)
{ {
PyObject *sd = _PyInterpreterState_GET_UNSAFE()->sysdict; PyThreadState *tstate = _PyThreadState_GET();
PyObject *sd = tstate->interp->sysdict;
if (sd == NULL) { if (sd == NULL) {
return NULL; return NULL;
} }
return PyDict_GetItemString(sd, name); return PyDict_GetItemString(sd, name);
} }
int static int
_PySys_SetObjectId(_Py_Identifier *key, PyObject *v) sys_set_object_id(PyThreadState *tstate, _Py_Identifier *key, PyObject *v)
{ {
PyObject *sd = _PyInterpreterState_GET_UNSAFE()->sysdict; PyObject *sd = tstate->interp->sysdict;
if (v == NULL) { if (v == NULL) {
if (_PyDict_GetItemId(sd, key) == NULL) { if (_PyDict_GetItemId(sd, key) == NULL) {
return 0; return 0;
...@@ -97,9 +107,16 @@ _PySys_SetObjectId(_Py_Identifier *key, PyObject *v) ...@@ -97,9 +107,16 @@ _PySys_SetObjectId(_Py_Identifier *key, PyObject *v)
} }
int int
PySys_SetObject(const char *name, PyObject *v) _PySys_SetObjectId(_Py_Identifier *key, PyObject *v)
{
PyThreadState *tstate = _PyThreadState_GET();
return sys_set_object_id(tstate, key, v);
}
static int
sys_set_object(PyThreadState *tstate, const char *name, PyObject *v)
{ {
PyObject *sd = _PyInterpreterState_GET_UNSAFE()->sysdict; PyObject *sd = tstate->interp->sysdict;
if (v == NULL) { if (v == NULL) {
if (PyDict_GetItemString(sd, name) == NULL) { if (PyDict_GetItemString(sd, name) == NULL) {
return 0; return 0;
...@@ -113,10 +130,16 @@ PySys_SetObject(const char *name, PyObject *v) ...@@ -113,10 +130,16 @@ PySys_SetObject(const char *name, PyObject *v)
} }
} }
int
PySys_SetObject(const char *name, PyObject *v)
{
PyThreadState *tstate = _PyThreadState_GET();
return sys_set_object(tstate, name, v);
}
static int static int
should_audit(void) should_audit(PyThreadState *ts)
{ {
PyThreadState *ts = _PyThreadState_GET();
if (!ts) { if (!ts) {
return 0; return 0;
} }
...@@ -134,6 +157,7 @@ PySys_Audit(const char *event, const char *argFormat, ...) ...@@ -134,6 +157,7 @@ PySys_Audit(const char *event, const char *argFormat, ...)
PyObject *hooks = NULL; PyObject *hooks = NULL;
PyObject *hook = NULL; PyObject *hook = NULL;
int res = -1; int res = -1;
PyThreadState *ts = _PyThreadState_GET();
/* N format is inappropriate, because you do not know /* N format is inappropriate, because you do not know
whether the reference is consumed by the call. whether the reference is consumed by the call.
...@@ -141,18 +165,16 @@ PySys_Audit(const char *event, const char *argFormat, ...) ...@@ -141,18 +165,16 @@ PySys_Audit(const char *event, const char *argFormat, ...)
assert(!argFormat || !strchr(argFormat, 'N')); assert(!argFormat || !strchr(argFormat, 'N'));
/* Early exit when no hooks are registered */ /* Early exit when no hooks are registered */
if (!should_audit()) { if (!should_audit(ts)) {
return 0; return 0;
} }
_Py_AuditHookEntry *e = _PyRuntime.audit_hook_head; _Py_AuditHookEntry *e = _PyRuntime.audit_hook_head;
PyThreadState *ts = _PyThreadState_GET();
PyInterpreterState *is = ts ? ts->interp : NULL;
int dtrace = PyDTrace_AUDIT_ENABLED(); int dtrace = PyDTrace_AUDIT_ENABLED();
PyObject *exc_type, *exc_value, *exc_tb; PyObject *exc_type, *exc_value, *exc_tb;
if (ts) { if (ts) {
PyErr_Fetch(&exc_type, &exc_value, &exc_tb); _PyErr_Fetch(ts, &exc_type, &exc_value, &exc_tb);
} }
/* Initialize event args now */ /* Initialize event args now */
...@@ -185,6 +207,7 @@ PySys_Audit(const char *event, const char *argFormat, ...) ...@@ -185,6 +207,7 @@ PySys_Audit(const char *event, const char *argFormat, ...)
} }
/* Call interpreter hooks */ /* Call interpreter hooks */
PyInterpreterState *is = ts ? ts->interp : NULL;
if (is && is->audit_hooks) { if (is && is->audit_hooks) {
eventName = PyUnicode_FromString(event); eventName = PyUnicode_FromString(event);
if (!eventName) { if (!eventName) {
...@@ -206,9 +229,9 @@ PySys_Audit(const char *event, const char *argFormat, ...) ...@@ -206,9 +229,9 @@ PySys_Audit(const char *event, const char *argFormat, ...)
if (o) { if (o) {
canTrace = PyObject_IsTrue(o); canTrace = PyObject_IsTrue(o);
Py_DECREF(o); Py_DECREF(o);
} else if (PyErr_Occurred() && } else if (_PyErr_Occurred(ts) &&
PyErr_ExceptionMatches(PyExc_AttributeError)) { _PyErr_ExceptionMatches(ts, PyExc_AttributeError)) {
PyErr_Clear(); _PyErr_Clear(ts);
canTrace = 0; canTrace = 0;
} }
if (canTrace < 0) { if (canTrace < 0) {
...@@ -232,7 +255,7 @@ PySys_Audit(const char *event, const char *argFormat, ...) ...@@ -232,7 +255,7 @@ PySys_Audit(const char *event, const char *argFormat, ...)
} }
ts->use_tracing = (ts->c_tracefunc || ts->c_profilefunc); ts->use_tracing = (ts->c_tracefunc || ts->c_profilefunc);
ts->tracing--; ts->tracing--;
if (PyErr_Occurred()) { if (_PyErr_Occurred(ts)) {
goto exit; goto exit;
} }
} }
...@@ -247,9 +270,9 @@ exit: ...@@ -247,9 +270,9 @@ exit:
if (ts) { if (ts) {
if (!res) { if (!res) {
PyErr_Restore(exc_type, exc_value, exc_tb); _PyErr_Restore(ts, exc_type, exc_value, exc_tb);
} else { } else {
assert(PyErr_Occurred()); assert(_PyErr_Occurred(ts));
Py_XDECREF(exc_type); Py_XDECREF(exc_type);
Py_XDECREF(exc_value); Py_XDECREF(exc_value);
Py_XDECREF(exc_tb); Py_XDECREF(exc_tb);
...@@ -263,22 +286,26 @@ exit: ...@@ -263,22 +286,26 @@ exit:
* finalization. In general, it should not need to be called, * finalization. In general, it should not need to be called,
* and as such it is not defined in any header files. * and as such it is not defined in any header files.
*/ */
void _PySys_ClearAuditHooks(void) { void
_PySys_ClearAuditHooks(void)
{
/* Must be finalizing to clear hooks */ /* Must be finalizing to clear hooks */
_PyRuntimeState *runtime = &_PyRuntime; _PyRuntimeState *runtime = &_PyRuntime;
PyThreadState *ts = _PyRuntimeState_GetThreadState(runtime); PyThreadState *ts = _PyRuntimeState_GetThreadState(runtime);
assert(!ts || _Py_CURRENTLY_FINALIZING(runtime, ts)); assert(!ts || _Py_CURRENTLY_FINALIZING(runtime, ts));
if (!ts || !_Py_CURRENTLY_FINALIZING(runtime, ts)) if (!ts || !_Py_CURRENTLY_FINALIZING(runtime, ts)) {
return; return;
}
if (Py_VerboseFlag) { const PyConfig *config = &ts->interp->config;
if (config->verbose) {
PySys_WriteStderr("# clear sys.audit hooks\n"); PySys_WriteStderr("# clear sys.audit hooks\n");
} }
/* Hooks can abort later hooks for this event, but cannot /* Hooks can abort later hooks for this event, but cannot
abort the clear operation itself. */ abort the clear operation itself. */
PySys_Audit("cpython._PySys_ClearAuditHooks", NULL); PySys_Audit("cpython._PySys_ClearAuditHooks", NULL);
PyErr_Clear(); _PyErr_Clear(ts);
_Py_AuditHookEntry *e = _PyRuntime.audit_hook_head, *n; _Py_AuditHookEntry *e = _PyRuntime.audit_hook_head, *n;
_PyRuntime.audit_hook_head = NULL; _PyRuntime.audit_hook_head = NULL;
...@@ -292,13 +319,16 @@ void _PySys_ClearAuditHooks(void) { ...@@ -292,13 +319,16 @@ void _PySys_ClearAuditHooks(void) {
int int
PySys_AddAuditHook(Py_AuditHookFunction hook, void *userData) PySys_AddAuditHook(Py_AuditHookFunction hook, void *userData)
{ {
_PyRuntimeState *runtime = &_PyRuntime;
PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
/* Invoke existing audit hooks to allow them an opportunity to abort. */ /* Invoke existing audit hooks to allow them an opportunity to abort. */
/* Cannot invoke hooks until we are initialized */ /* Cannot invoke hooks until we are initialized */
if (Py_IsInitialized()) { if (runtime->initialized) {
if (PySys_Audit("sys.addaudithook", NULL) < 0) { if (PySys_Audit("sys.addaudithook", NULL) < 0) {
if (PyErr_ExceptionMatches(PyExc_Exception)) { if (_PyErr_ExceptionMatches(tstate, PyExc_Exception)) {
/* We do not report errors derived from Exception */ /* We do not report errors derived from Exception */
PyErr_Clear(); _PyErr_Clear(tstate);
return 0; return 0;
} }
return -1; return -1;
...@@ -310,15 +340,17 @@ PySys_AddAuditHook(Py_AuditHookFunction hook, void *userData) ...@@ -310,15 +340,17 @@ PySys_AddAuditHook(Py_AuditHookFunction hook, void *userData)
e = (_Py_AuditHookEntry*)PyMem_RawMalloc(sizeof(_Py_AuditHookEntry)); e = (_Py_AuditHookEntry*)PyMem_RawMalloc(sizeof(_Py_AuditHookEntry));
_PyRuntime.audit_hook_head = e; _PyRuntime.audit_hook_head = e;
} else { } else {
while (e->next) while (e->next) {
e = e->next; e = e->next;
}
e = e->next = (_Py_AuditHookEntry*)PyMem_RawMalloc( e = e->next = (_Py_AuditHookEntry*)PyMem_RawMalloc(
sizeof(_Py_AuditHookEntry)); sizeof(_Py_AuditHookEntry));
} }
if (!e) { if (!e) {
if (Py_IsInitialized()) if (runtime->initialized) {
PyErr_NoMemory(); _PyErr_NoMemory(tstate);
}
return -1; return -1;
} }
...@@ -341,17 +373,19 @@ static PyObject * ...@@ -341,17 +373,19 @@ static PyObject *
sys_addaudithook_impl(PyObject *module, PyObject *hook) sys_addaudithook_impl(PyObject *module, PyObject *hook)
/*[clinic end generated code: output=4f9c17aaeb02f44e input=0f3e191217a45e34]*/ /*[clinic end generated code: output=4f9c17aaeb02f44e input=0f3e191217a45e34]*/
{ {
PyThreadState *tstate = _PyThreadState_GET();
/* Invoke existing audit hooks to allow them an opportunity to abort. */ /* Invoke existing audit hooks to allow them an opportunity to abort. */
if (PySys_Audit("sys.addaudithook", NULL) < 0) { if (PySys_Audit("sys.addaudithook", NULL) < 0) {
if (PyErr_ExceptionMatches(PyExc_Exception)) { if (_PyErr_ExceptionMatches(tstate, PyExc_Exception)) {
/* We do not report errors derived from Exception */ /* We do not report errors derived from Exception */
PyErr_Clear(); _PyErr_Clear(tstate);
Py_RETURN_NONE; Py_RETURN_NONE;
} }
return NULL; return NULL;
} }
PyInterpreterState *is = _PyInterpreterState_Get(); PyInterpreterState *is = tstate->interp;
if (is->audit_hooks == NULL) { if (is->audit_hooks == NULL) {
is->audit_hooks = PyList_New(0); is->audit_hooks = PyList_New(0);
...@@ -375,23 +409,29 @@ Passes the event to any audit hooks that are attached."); ...@@ -375,23 +409,29 @@ Passes the event to any audit hooks that are attached.");
static PyObject * static PyObject *
sys_audit(PyObject *self, PyObject *const *args, Py_ssize_t argc) sys_audit(PyObject *self, PyObject *const *args, Py_ssize_t argc)
{ {
PyThreadState *tstate = _PyThreadState_GET();
if (argc == 0) { if (argc == 0) {
PyErr_SetString(PyExc_TypeError, "audit() missing 1 required positional argument: 'event'"); _PyErr_SetString(tstate, PyExc_TypeError,
"audit() missing 1 required positional argument: "
"'event'");
return NULL; return NULL;
} }
if (!should_audit()) { if (!should_audit(tstate)) {
Py_RETURN_NONE; Py_RETURN_NONE;
} }
PyObject *auditEvent = args[0]; PyObject *auditEvent = args[0];
if (!auditEvent) { if (!auditEvent) {
PyErr_SetString(PyExc_TypeError, "expected str for argument 'event'"); _PyErr_SetString(tstate, PyExc_TypeError,
"expected str for argument 'event'");
return NULL; return NULL;
} }
if (!PyUnicode_Check(auditEvent)) { if (!PyUnicode_Check(auditEvent)) {
PyErr_Format(PyExc_TypeError, "expected str for argument 'event', not %.200s", _PyErr_Format(tstate, PyExc_TypeError,
Py_TYPE(auditEvent)->tp_name); "expected str for argument 'event', not %.200s",
Py_TYPE(auditEvent)->tp_name);
return NULL; return NULL;
} }
const char *event = PyUnicode_AsUTF8(auditEvent); const char *event = PyUnicode_AsUTF8(auditEvent);
...@@ -418,7 +458,8 @@ sys_audit(PyObject *self, PyObject *const *args, Py_ssize_t argc) ...@@ -418,7 +458,8 @@ sys_audit(PyObject *self, PyObject *const *args, Py_ssize_t argc)
static PyObject * static PyObject *
sys_breakpointhook(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *keywords) sys_breakpointhook(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *keywords)
{ {
assert(!PyErr_Occurred()); PyThreadState *tstate = _PyThreadState_GET();
assert(!_PyErr_Occurred(tstate));
char *envar = Py_GETENV("PYTHONBREAKPOINT"); char *envar = Py_GETENV("PYTHONBREAKPOINT");
if (envar == NULL || strlen(envar) == 0) { if (envar == NULL || strlen(envar) == 0) {
...@@ -434,7 +475,7 @@ sys_breakpointhook(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyOb ...@@ -434,7 +475,7 @@ sys_breakpointhook(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyOb
* we need to save a copy of envar. */ * we need to save a copy of envar. */
envar = _PyMem_RawStrdup(envar); envar = _PyMem_RawStrdup(envar);
if (envar == NULL) { if (envar == NULL) {
PyErr_NoMemory(); _PyErr_NoMemory(tstate);
return NULL; return NULL;
} }
const char *last_dot = strrchr(envar, '.'); const char *last_dot = strrchr(envar, '.');
...@@ -463,7 +504,7 @@ sys_breakpointhook(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyOb ...@@ -463,7 +504,7 @@ sys_breakpointhook(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyOb
Py_DECREF(modulepath); Py_DECREF(modulepath);
if (module == NULL) { if (module == NULL) {
if (PyErr_ExceptionMatches(PyExc_ImportError)) { if (_PyErr_ExceptionMatches(tstate, PyExc_ImportError)) {
goto warn; goto warn;
} }
PyMem_RawFree(envar); PyMem_RawFree(envar);
...@@ -474,7 +515,7 @@ sys_breakpointhook(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyOb ...@@ -474,7 +515,7 @@ sys_breakpointhook(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyOb
Py_DECREF(module); Py_DECREF(module);
if (hook == NULL) { if (hook == NULL) {
if (PyErr_ExceptionMatches(PyExc_AttributeError)) { if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
goto warn; goto warn;
} }
PyMem_RawFree(envar); PyMem_RawFree(envar);
...@@ -487,7 +528,7 @@ sys_breakpointhook(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyOb ...@@ -487,7 +528,7 @@ sys_breakpointhook(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyOb
warn: warn:
/* If any of the imports went wrong, then warn and ignore. */ /* If any of the imports went wrong, then warn and ignore. */
PyErr_Clear(); _PyErr_Clear(tstate);
int status = PyErr_WarnFormat( int status = PyErr_WarnFormat(
PyExc_RuntimeWarning, 0, PyExc_RuntimeWarning, 0,
"Ignoring unimportable $PYTHONBREAKPOINT: \"%s\"", envar); "Ignoring unimportable $PYTHONBREAKPOINT: \"%s\"", envar);
...@@ -513,7 +554,7 @@ PyDoc_STRVAR(breakpointhook_doc, ...@@ -513,7 +554,7 @@ PyDoc_STRVAR(breakpointhook_doc,
Helper function for sys_displayhook(). */ Helper function for sys_displayhook(). */
static int static int
sys_displayhook_unencodable(PyObject *outf, PyObject *o) sys_displayhook_unencodable(PyThreadState *tstate, PyObject *outf, PyObject *o)
{ {
PyObject *stdout_encoding = NULL; PyObject *stdout_encoding = NULL;
PyObject *encoded, *escaped_str, *repr_str, *buffer, *result; PyObject *encoded, *escaped_str, *repr_str, *buffer, *result;
...@@ -547,7 +588,7 @@ sys_displayhook_unencodable(PyObject *outf, PyObject *o) ...@@ -547,7 +588,7 @@ sys_displayhook_unencodable(PyObject *outf, PyObject *o)
Py_DECREF(result); Py_DECREF(result);
} }
else { else {
PyErr_Clear(); _PyErr_Clear(tstate);
escaped_str = PyUnicode_FromEncodedObject(encoded, escaped_str = PyUnicode_FromEncodedObject(encoded,
stdout_encoding_str, stdout_encoding_str,
"strict"); "strict");
...@@ -585,11 +626,13 @@ sys_displayhook(PyObject *module, PyObject *o) ...@@ -585,11 +626,13 @@ sys_displayhook(PyObject *module, PyObject *o)
PyObject *builtins; PyObject *builtins;
static PyObject *newline = NULL; static PyObject *newline = NULL;
int err; int err;
PyThreadState *tstate = _PyThreadState_GET();
builtins = _PyImport_GetModuleId(&PyId_builtins); builtins = _PyImport_GetModuleId(&PyId_builtins);
if (builtins == NULL) { if (builtins == NULL) {
if (!PyErr_Occurred()) { if (!_PyErr_Occurred(tstate)) {
PyErr_SetString(PyExc_RuntimeError, "lost builtins module"); _PyErr_SetString(tstate, PyExc_RuntimeError,
"lost builtins module");
} }
return NULL; return NULL;
} }
...@@ -603,19 +646,20 @@ sys_displayhook(PyObject *module, PyObject *o) ...@@ -603,19 +646,20 @@ sys_displayhook(PyObject *module, PyObject *o)
} }
if (_PyObject_SetAttrId(builtins, &PyId__, Py_None) != 0) if (_PyObject_SetAttrId(builtins, &PyId__, Py_None) != 0)
return NULL; return NULL;
outf = _PySys_GetObjectId(&PyId_stdout); outf = sys_get_object_id(tstate, &PyId_stdout);
if (outf == NULL || outf == Py_None) { if (outf == NULL || outf == Py_None) {
PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout"); _PyErr_SetString(tstate, PyExc_RuntimeError, "lost sys.stdout");
return NULL; return NULL;
} }
if (PyFile_WriteObject(o, outf, 0) != 0) { if (PyFile_WriteObject(o, outf, 0) != 0) {
if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) { if (_PyErr_ExceptionMatches(tstate, PyExc_UnicodeEncodeError)) {
/* repr(o) is not encodable to sys.stdout.encoding with /* repr(o) is not encodable to sys.stdout.encoding with
* sys.stdout.errors error handler (which is probably 'strict') */ * sys.stdout.errors error handler (which is probably 'strict') */
PyErr_Clear(); _PyErr_Clear(tstate);
err = sys_displayhook_unencodable(outf, o); err = sys_displayhook_unencodable(tstate, outf, o);
if (err) if (err) {
return NULL; return NULL;
}
} }
else { else {
return NULL; return NULL;
...@@ -722,7 +766,8 @@ sys_exit_impl(PyObject *module, PyObject *status) ...@@ -722,7 +766,8 @@ sys_exit_impl(PyObject *module, PyObject *status)
/*[clinic end generated code: output=13870986c1ab2ec0 input=a737351f86685e9c]*/ /*[clinic end generated code: output=13870986c1ab2ec0 input=a737351f86685e9c]*/
{ {
/* Raise SystemExit so callers may catch it or clean up. */ /* Raise SystemExit so callers may catch it or clean up. */
PyErr_SetObject(PyExc_SystemExit, status); PyThreadState *tstate = _PyThreadState_GET();
_PyErr_SetObject(tstate, PyExc_SystemExit, status);
return NULL; return NULL;
} }
...@@ -751,8 +796,8 @@ static PyObject * ...@@ -751,8 +796,8 @@ static PyObject *
sys_getfilesystemencoding_impl(PyObject *module) sys_getfilesystemencoding_impl(PyObject *module)
/*[clinic end generated code: output=1dc4bdbe9be44aa7 input=8475f8649b8c7d8c]*/ /*[clinic end generated code: output=1dc4bdbe9be44aa7 input=8475f8649b8c7d8c]*/
{ {
PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE(); PyThreadState *tstate = _PyThreadState_GET();
const PyConfig *config = &interp->config; const PyConfig *config = &tstate->interp->config;
return PyUnicode_FromWideChar(config->filesystem_encoding, -1); return PyUnicode_FromWideChar(config->filesystem_encoding, -1);
} }
...@@ -766,8 +811,8 @@ static PyObject * ...@@ -766,8 +811,8 @@ static PyObject *
sys_getfilesystemencodeerrors_impl(PyObject *module) sys_getfilesystemencodeerrors_impl(PyObject *module)
/*[clinic end generated code: output=ba77b36bbf7c96f5 input=22a1e8365566f1e5]*/ /*[clinic end generated code: output=ba77b36bbf7c96f5 input=22a1e8365566f1e5]*/
{ {
PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE(); PyThreadState *tstate = _PyThreadState_GET();
const PyConfig *config = &interp->config; const PyConfig *config = &tstate->interp->config;
return PyUnicode_FromWideChar(config->filesystem_errors, -1); return PyUnicode_FromWideChar(config->filesystem_errors, -1);
} }
...@@ -788,14 +833,15 @@ static PyObject * ...@@ -788,14 +833,15 @@ static PyObject *
sys_intern_impl(PyObject *module, PyObject *s) sys_intern_impl(PyObject *module, PyObject *s)
/*[clinic end generated code: output=be680c24f5c9e5d6 input=849483c006924e2f]*/ /*[clinic end generated code: output=be680c24f5c9e5d6 input=849483c006924e2f]*/
{ {
PyThreadState *tstate = _PyThreadState_GET();
if (PyUnicode_CheckExact(s)) { if (PyUnicode_CheckExact(s)) {
Py_INCREF(s); Py_INCREF(s);
PyUnicode_InternInPlace(&s); PyUnicode_InternInPlace(&s);
return s; return s;
} }
else { else {
PyErr_Format(PyExc_TypeError, _PyErr_Format(tstate, PyExc_TypeError,
"can't intern %.400s", s->ob_type->tp_name); "can't intern %.400s", s->ob_type->tp_name);
return NULL; return NULL;
} }
} }
...@@ -833,19 +879,17 @@ static PyObject * ...@@ -833,19 +879,17 @@ static PyObject *
call_trampoline(PyObject* callback, call_trampoline(PyObject* callback,
PyFrameObject *frame, int what, PyObject *arg) PyFrameObject *frame, int what, PyObject *arg)
{ {
PyObject *result;
PyObject *stack[3];
if (PyFrame_FastToLocalsWithError(frame) < 0) { if (PyFrame_FastToLocalsWithError(frame) < 0) {
return NULL; return NULL;
} }
PyObject *stack[3];
stack[0] = (PyObject *)frame; stack[0] = (PyObject *)frame;
stack[1] = whatstrings[what]; stack[1] = whatstrings[what];
stack[2] = (arg != NULL) ? arg : Py_None; stack[2] = (arg != NULL) ? arg : Py_None;
/* call the Python-level function */ /* call the Python-level function */
result = _PyObject_FastCall(callback, stack, 3); PyObject *result = _PyObject_FastCall(callback, stack, 3);
PyFrame_LocalsToFast(frame, 1); PyFrame_LocalsToFast(frame, 1);
if (result == NULL) { if (result == NULL) {
...@@ -1001,11 +1045,12 @@ sys_setcheckinterval_impl(PyObject *module, int n) ...@@ -1001,11 +1045,12 @@ sys_setcheckinterval_impl(PyObject *module, int n)
if (PyErr_WarnEx(PyExc_DeprecationWarning, if (PyErr_WarnEx(PyExc_DeprecationWarning,
"sys.getcheckinterval() and sys.setcheckinterval() " "sys.getcheckinterval() and sys.setcheckinterval() "
"are deprecated. Use sys.setswitchinterval() " "are deprecated. Use sys.setswitchinterval() "
"instead.", 1) < 0) "instead.", 1) < 0) {
return NULL; return NULL;
}
PyInterpreterState *interp = _PyInterpreterState_Get(); PyThreadState *tstate = _PyThreadState_GET();
interp->check_interval = n; tstate->interp->check_interval = n;
Py_RETURN_NONE; Py_RETURN_NONE;
} }
...@@ -1022,10 +1067,12 @@ sys_getcheckinterval_impl(PyObject *module) ...@@ -1022,10 +1067,12 @@ sys_getcheckinterval_impl(PyObject *module)
if (PyErr_WarnEx(PyExc_DeprecationWarning, if (PyErr_WarnEx(PyExc_DeprecationWarning,
"sys.getcheckinterval() and sys.setcheckinterval() " "sys.getcheckinterval() and sys.setcheckinterval() "
"are deprecated. Use sys.getswitchinterval() " "are deprecated. Use sys.getswitchinterval() "
"instead.", 1) < 0) "instead.", 1) < 0) {
return NULL; return NULL;
PyInterpreterState *interp = _PyInterpreterState_Get(); }
return PyLong_FromLong(interp->check_interval);
PyThreadState *tstate = _PyThreadState_GET();
return PyLong_FromLong(tstate->interp->check_interval);
} }
/*[clinic input] /*[clinic input]
...@@ -1048,9 +1095,10 @@ static PyObject * ...@@ -1048,9 +1095,10 @@ static PyObject *
sys_setswitchinterval_impl(PyObject *module, double interval) sys_setswitchinterval_impl(PyObject *module, double interval)
/*[clinic end generated code: output=65a19629e5153983 input=561b477134df91d9]*/ /*[clinic end generated code: output=65a19629e5153983 input=561b477134df91d9]*/
{ {
PyThreadState *tstate = _PyThreadState_GET();
if (interval <= 0.0) { if (interval <= 0.0) {
PyErr_SetString(PyExc_ValueError, _PyErr_SetString(tstate, PyExc_ValueError,
"switch interval must be strictly positive"); "switch interval must be strictly positive");
return NULL; return NULL;
} }
_PyEval_SetSwitchInterval((unsigned long) (1e6 * interval)); _PyEval_SetSwitchInterval((unsigned long) (1e6 * interval));
...@@ -1089,11 +1137,11 @@ sys_setrecursionlimit_impl(PyObject *module, int new_limit) ...@@ -1089,11 +1137,11 @@ sys_setrecursionlimit_impl(PyObject *module, int new_limit)
/*[clinic end generated code: output=35e1c64754800ace input=b0f7a23393924af3]*/ /*[clinic end generated code: output=35e1c64754800ace input=b0f7a23393924af3]*/
{ {
int mark; int mark;
PyThreadState *tstate; PyThreadState *tstate = _PyThreadState_GET();
if (new_limit < 1) { if (new_limit < 1) {
PyErr_SetString(PyExc_ValueError, _PyErr_SetString(tstate, PyExc_ValueError,
"recursion limit must be greater or equal than 1"); "recursion limit must be greater or equal than 1");
return NULL; return NULL;
} }
...@@ -1107,12 +1155,11 @@ sys_setrecursionlimit_impl(PyObject *module, int new_limit) ...@@ -1107,12 +1155,11 @@ sys_setrecursionlimit_impl(PyObject *module, int new_limit)
the new low-water mark. Otherwise it may not be possible anymore to the new low-water mark. Otherwise it may not be possible anymore to
reset the overflowed flag to 0. */ reset the overflowed flag to 0. */
mark = _Py_RecursionLimitLowerWaterMark(new_limit); mark = _Py_RecursionLimitLowerWaterMark(new_limit);
tstate = _PyThreadState_GET();
if (tstate->recursion_depth >= mark) { if (tstate->recursion_depth >= mark) {
PyErr_Format(PyExc_RecursionError, _PyErr_Format(tstate, PyExc_RecursionError,
"cannot set the recursion limit to %i at " "cannot set the recursion limit to %i at "
"the recursion depth %i: the limit is too low", "the recursion depth %i: the limit is too low",
new_limit, tstate->recursion_depth); new_limit, tstate->recursion_depth);
return NULL; return NULL;
} }
...@@ -1137,11 +1184,12 @@ static PyObject * ...@@ -1137,11 +1184,12 @@ static PyObject *
sys_set_coroutine_origin_tracking_depth_impl(PyObject *module, int depth) sys_set_coroutine_origin_tracking_depth_impl(PyObject *module, int depth)
/*[clinic end generated code: output=0a2123c1cc6759c5 input=a1d0a05f89d2c426]*/ /*[clinic end generated code: output=0a2123c1cc6759c5 input=a1d0a05f89d2c426]*/
{ {
PyThreadState *tstate = _PyThreadState_GET();
if (depth < 0) { if (depth < 0) {
PyErr_SetString(PyExc_ValueError, "depth must be >= 0"); _PyErr_SetString(tstate, PyExc_ValueError, "depth must be >= 0");
return NULL; return NULL;
} }
_PyEval_SetCoroutineOriginTrackingDepth(depth); _PyEval_SetCoroutineOriginTrackingDepth(tstate, depth);
Py_RETURN_NONE; Py_RETURN_NONE;
} }
...@@ -1185,6 +1233,7 @@ sys_set_asyncgen_hooks(PyObject *self, PyObject *args, PyObject *kw) ...@@ -1185,6 +1233,7 @@ sys_set_asyncgen_hooks(PyObject *self, PyObject *args, PyObject *kw)
static char *keywords[] = {"firstiter", "finalizer", NULL}; static char *keywords[] = {"firstiter", "finalizer", NULL};
PyObject *firstiter = NULL; PyObject *firstiter = NULL;
PyObject *finalizer = NULL; PyObject *finalizer = NULL;
PyThreadState *tstate = _PyThreadState_GET();
if (!PyArg_ParseTupleAndKeywords( if (!PyArg_ParseTupleAndKeywords(
args, kw, "|OO", keywords, args, kw, "|OO", keywords,
...@@ -1194,9 +1243,9 @@ sys_set_asyncgen_hooks(PyObject *self, PyObject *args, PyObject *kw) ...@@ -1194,9 +1243,9 @@ sys_set_asyncgen_hooks(PyObject *self, PyObject *args, PyObject *kw)
if (finalizer && finalizer != Py_None) { if (finalizer && finalizer != Py_None) {
if (!PyCallable_Check(finalizer)) { if (!PyCallable_Check(finalizer)) {
PyErr_Format(PyExc_TypeError, _PyErr_Format(tstate, PyExc_TypeError,
"callable finalizer expected, got %.50s", "callable finalizer expected, got %.50s",
Py_TYPE(finalizer)->tp_name); Py_TYPE(finalizer)->tp_name);
return NULL; return NULL;
} }
_PyEval_SetAsyncGenFinalizer(finalizer); _PyEval_SetAsyncGenFinalizer(finalizer);
...@@ -1207,9 +1256,9 @@ sys_set_asyncgen_hooks(PyObject *self, PyObject *args, PyObject *kw) ...@@ -1207,9 +1256,9 @@ sys_set_asyncgen_hooks(PyObject *self, PyObject *args, PyObject *kw)
if (firstiter && firstiter != Py_None) { if (firstiter && firstiter != Py_None) {
if (!PyCallable_Check(firstiter)) { if (!PyCallable_Check(firstiter)) {
PyErr_Format(PyExc_TypeError, _PyErr_Format(tstate, PyExc_TypeError,
"callable firstiter expected, got %.50s", "callable firstiter expected, got %.50s",
Py_TYPE(firstiter)->tp_name); Py_TYPE(firstiter)->tp_name);
return NULL; return NULL;
} }
_PyEval_SetAsyncGenFirstiter(firstiter); _PyEval_SetAsyncGenFirstiter(firstiter);
...@@ -1297,7 +1346,7 @@ static PyStructSequence_Desc hash_info_desc = { ...@@ -1297,7 +1346,7 @@ static PyStructSequence_Desc hash_info_desc = {
}; };
static PyObject * static PyObject *
get_hash_info(void) get_hash_info(PyThreadState *tstate)
{ {
PyObject *hash_info; PyObject *hash_info;
int field = 0; int field = 0;
...@@ -1324,7 +1373,7 @@ get_hash_info(void) ...@@ -1324,7 +1373,7 @@ get_hash_info(void)
PyLong_FromLong(hashfunc->seed_bits)); PyLong_FromLong(hashfunc->seed_bits));
PyStructSequence_SET_ITEM(hash_info, field++, PyStructSequence_SET_ITEM(hash_info, field++,
PyLong_FromLong(Py_HASH_CUTOFF)); PyLong_FromLong(Py_HASH_CUTOFF));
if (PyErr_Occurred()) { if (_PyErr_Occurred(tstate)) {
Py_CLEAR(hash_info); Py_CLEAR(hash_info);
return NULL; return NULL;
} }
...@@ -1408,6 +1457,7 @@ sys_getwindowsversion_impl(PyObject *module) ...@@ -1408,6 +1457,7 @@ sys_getwindowsversion_impl(PyObject *module)
wchar_t kernel32_path[MAX_PATH]; wchar_t kernel32_path[MAX_PATH];
LPVOID verblock; LPVOID verblock;
DWORD verblock_size; DWORD verblock_size;
PyThreadState *tstate = _PyThreadState_GET();
ver.dwOSVersionInfoSize = sizeof(ver); ver.dwOSVersionInfoSize = sizeof(ver);
if (!GetVersionExW((OSVERSIONINFOW*) &ver)) if (!GetVersionExW((OSVERSIONINFOW*) &ver))
...@@ -1458,7 +1508,7 @@ sys_getwindowsversion_impl(PyObject *module) ...@@ -1458,7 +1508,7 @@ sys_getwindowsversion_impl(PyObject *module)
realBuild realBuild
)); ));
if (PyErr_Occurred()) { if (_PyErr_Occurred(tstate)) {
Py_DECREF(version); Py_DECREF(version);
return NULL; return NULL;
} }
...@@ -1515,8 +1565,8 @@ static PyObject * ...@@ -1515,8 +1565,8 @@ static PyObject *
sys_setdlopenflags_impl(PyObject *module, int new_val) sys_setdlopenflags_impl(PyObject *module, int new_val)
/*[clinic end generated code: output=ec918b7fe0a37281 input=4c838211e857a77f]*/ /*[clinic end generated code: output=ec918b7fe0a37281 input=4c838211e857a77f]*/
{ {
PyInterpreterState *interp = _PyInterpreterState_Get(); PyThreadState *tstate = _PyThreadState_GET();
interp->dlopenflags = new_val; tstate->interp->dlopenflags = new_val;
Py_RETURN_NONE; Py_RETURN_NONE;
} }
...@@ -1533,8 +1583,8 @@ static PyObject * ...@@ -1533,8 +1583,8 @@ static PyObject *
sys_getdlopenflags_impl(PyObject *module) sys_getdlopenflags_impl(PyObject *module)
/*[clinic end generated code: output=e92cd1bc5005da6e input=dc4ea0899c53b4b6]*/ /*[clinic end generated code: output=e92cd1bc5005da6e input=dc4ea0899c53b4b6]*/
{ {
PyInterpreterState *interp = _PyInterpreterState_Get(); PyThreadState *tstate = _PyThreadState_GET();
return PyLong_FromLong(interp->dlopenflags); return PyLong_FromLong(tstate->interp->dlopenflags);
} }
#endif /* HAVE_DLOPEN */ #endif /* HAVE_DLOPEN */
...@@ -1566,17 +1616,20 @@ _PySys_GetSizeOf(PyObject *o) ...@@ -1566,17 +1616,20 @@ _PySys_GetSizeOf(PyObject *o)
PyObject *res = NULL; PyObject *res = NULL;
PyObject *method; PyObject *method;
Py_ssize_t size; Py_ssize_t size;
PyThreadState *tstate = _PyThreadState_GET();
/* Make sure the type is initialized. float gets initialized late */ /* Make sure the type is initialized. float gets initialized late */
if (PyType_Ready(Py_TYPE(o)) < 0) if (PyType_Ready(Py_TYPE(o)) < 0) {
return (size_t)-1; return (size_t)-1;
}
method = _PyObject_LookupSpecial(o, &PyId___sizeof__); method = _PyObject_LookupSpecial(o, &PyId___sizeof__);
if (method == NULL) { if (method == NULL) {
if (!PyErr_Occurred()) if (!_PyErr_Occurred(tstate)) {
PyErr_Format(PyExc_TypeError, _PyErr_Format(tstate, PyExc_TypeError,
"Type %.100s doesn't define __sizeof__", "Type %.100s doesn't define __sizeof__",
Py_TYPE(o)->tp_name); Py_TYPE(o)->tp_name);
}
} }
else { else {
res = _PyObject_CallNoArg(method); res = _PyObject_CallNoArg(method);
...@@ -1588,11 +1641,12 @@ _PySys_GetSizeOf(PyObject *o) ...@@ -1588,11 +1641,12 @@ _PySys_GetSizeOf(PyObject *o)
size = PyLong_AsSsize_t(res); size = PyLong_AsSsize_t(res);
Py_DECREF(res); Py_DECREF(res);
if (size == -1 && PyErr_Occurred()) if (size == -1 && _PyErr_Occurred(tstate))
return (size_t)-1; return (size_t)-1;
if (size < 0) { if (size < 0) {
PyErr_SetString(PyExc_ValueError, "__sizeof__() should return >= 0"); _PyErr_SetString(tstate, PyExc_ValueError,
"__sizeof__() should return >= 0");
return (size_t)-1; return (size_t)-1;
} }
...@@ -1608,17 +1662,19 @@ sys_getsizeof(PyObject *self, PyObject *args, PyObject *kwds) ...@@ -1608,17 +1662,19 @@ sys_getsizeof(PyObject *self, PyObject *args, PyObject *kwds)
static char *kwlist[] = {"object", "default", 0}; static char *kwlist[] = {"object", "default", 0};
size_t size; size_t size;
PyObject *o, *dflt = NULL; PyObject *o, *dflt = NULL;
PyThreadState *tstate = _PyThreadState_GET();
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:getsizeof", if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:getsizeof",
kwlist, &o, &dflt)) kwlist, &o, &dflt)) {
return NULL; return NULL;
}
size = _PySys_GetSizeOf(o); size = _PySys_GetSizeOf(o);
if (size == (size_t)-1 && PyErr_Occurred()) { if (size == (size_t)-1 && _PyErr_Occurred(tstate)) {
/* Has a default value been given */ /* Has a default value been given */
if (dflt != NULL && PyErr_ExceptionMatches(PyExc_TypeError)) { if (dflt != NULL && _PyErr_ExceptionMatches(tstate, PyExc_TypeError)) {
PyErr_Clear(); _PyErr_Clear(tstate);
Py_INCREF(dflt); Py_INCREF(dflt);
return dflt; return dflt;
} }
...@@ -1716,7 +1772,8 @@ static PyObject * ...@@ -1716,7 +1772,8 @@ static PyObject *
sys__getframe_impl(PyObject *module, int depth) sys__getframe_impl(PyObject *module, int depth)
/*[clinic end generated code: output=d438776c04d59804 input=c1be8a6464b11ee5]*/ /*[clinic end generated code: output=d438776c04d59804 input=c1be8a6464b11ee5]*/
{ {
PyFrameObject *f = _PyThreadState_GET()->frame; PyThreadState *tstate = _PyThreadState_GET();
PyFrameObject *f = tstate->frame;
if (PySys_Audit("sys._getframe", "O", f) < 0) { if (PySys_Audit("sys._getframe", "O", f) < 0) {
return NULL; return NULL;
...@@ -1727,8 +1784,8 @@ sys__getframe_impl(PyObject *module, int depth) ...@@ -1727,8 +1784,8 @@ sys__getframe_impl(PyObject *module, int depth)
--depth; --depth;
} }
if (f == NULL) { if (f == NULL) {
PyErr_SetString(PyExc_ValueError, _PyErr_SetString(tstate, PyExc_ValueError,
"call stack is not deep enough"); "call stack is not deep enough");
return NULL; return NULL;
} }
Py_INCREF(f); Py_INCREF(f);
...@@ -2078,10 +2135,9 @@ _clear_all_preinit_options(void) ...@@ -2078,10 +2135,9 @@ _clear_all_preinit_options(void)
} }
static int static int
_PySys_ReadPreInitOptions(void) sys_read_preinit_options(PyThreadState *tstate)
{ {
/* Rerun the add commands with the actual sys module available */ /* Rerun the add commands with the actual sys module available */
PyThreadState *tstate = _PyThreadState_GET();
if (tstate == NULL) { if (tstate == NULL) {
/* Still don't have a thread state, so something is wrong! */ /* Still don't have a thread state, so something is wrong! */
return -1; return -1;
...@@ -2102,9 +2158,9 @@ _PySys_ReadPreInitOptions(void) ...@@ -2102,9 +2158,9 @@ _PySys_ReadPreInitOptions(void)
} }
static PyObject * static PyObject *
get_warnoptions(void) get_warnoptions(PyThreadState *tstate)
{ {
PyObject *warnoptions = _PySys_GetObjectId(&PyId_warnoptions); PyObject *warnoptions = sys_get_object_id(tstate, &PyId_warnoptions);
if (warnoptions == NULL || !PyList_Check(warnoptions)) { if (warnoptions == NULL || !PyList_Check(warnoptions)) {
/* PEP432 TODO: we can reach this if warnoptions is NULL in the main /* PEP432 TODO: we can reach this if warnoptions is NULL in the main
* interpreter config. When that happens, we need to properly set * interpreter config. When that happens, we need to properly set
...@@ -2117,9 +2173,10 @@ get_warnoptions(void) ...@@ -2117,9 +2173,10 @@ get_warnoptions(void)
* reachable again. * reachable again.
*/ */
warnoptions = PyList_New(0); warnoptions = PyList_New(0);
if (warnoptions == NULL) if (warnoptions == NULL) {
return NULL; return NULL;
if (_PySys_SetObjectId(&PyId_warnoptions, warnoptions)) { }
if (sys_set_object_id(tstate, &PyId_warnoptions, warnoptions)) {
Py_DECREF(warnoptions); Py_DECREF(warnoptions);
return NULL; return NULL;
} }
...@@ -2137,16 +2194,16 @@ PySys_ResetWarnOptions(void) ...@@ -2137,16 +2194,16 @@ PySys_ResetWarnOptions(void)
return; return;
} }
PyObject *warnoptions = _PySys_GetObjectId(&PyId_warnoptions); PyObject *warnoptions = sys_get_object_id(tstate, &PyId_warnoptions);
if (warnoptions == NULL || !PyList_Check(warnoptions)) if (warnoptions == NULL || !PyList_Check(warnoptions))
return; return;
PyList_SetSlice(warnoptions, 0, PyList_GET_SIZE(warnoptions), NULL); PyList_SetSlice(warnoptions, 0, PyList_GET_SIZE(warnoptions), NULL);
} }
static int static int
_PySys_AddWarnOptionWithError(PyObject *option) _PySys_AddWarnOptionWithError(PyThreadState *tstate, PyObject *option)
{ {
PyObject *warnoptions = get_warnoptions(); PyObject *warnoptions = get_warnoptions(tstate);
if (warnoptions == NULL) { if (warnoptions == NULL) {
return -1; return -1;
} }
...@@ -2159,10 +2216,11 @@ _PySys_AddWarnOptionWithError(PyObject *option) ...@@ -2159,10 +2216,11 @@ _PySys_AddWarnOptionWithError(PyObject *option)
void void
PySys_AddWarnOptionUnicode(PyObject *option) PySys_AddWarnOptionUnicode(PyObject *option)
{ {
if (_PySys_AddWarnOptionWithError(option) < 0) { PyThreadState *tstate = _PyThreadState_GET();
if (_PySys_AddWarnOptionWithError(tstate, option) < 0) {
/* No return value, therefore clear error state if possible */ /* No return value, therefore clear error state if possible */
if (_PyThreadState_UncheckedGet()) { if (tstate) {
PyErr_Clear(); _PyErr_Clear(tstate);
} }
} }
} }
...@@ -2186,15 +2244,16 @@ PySys_AddWarnOption(const wchar_t *s) ...@@ -2186,15 +2244,16 @@ PySys_AddWarnOption(const wchar_t *s)
int int
PySys_HasWarnOptions(void) PySys_HasWarnOptions(void)
{ {
PyObject *warnoptions = _PySys_GetObjectId(&PyId_warnoptions); PyThreadState *tstate = _PyThreadState_GET();
PyObject *warnoptions = sys_get_object_id(tstate, &PyId_warnoptions);
return (warnoptions != NULL && PyList_Check(warnoptions) return (warnoptions != NULL && PyList_Check(warnoptions)
&& PyList_GET_SIZE(warnoptions) > 0); && PyList_GET_SIZE(warnoptions) > 0);
} }
static PyObject * static PyObject *
get_xoptions(void) get_xoptions(PyThreadState *tstate)
{ {
PyObject *xoptions = _PySys_GetObjectId(&PyId__xoptions); PyObject *xoptions = sys_get_object_id(tstate, &PyId__xoptions);
if (xoptions == NULL || !PyDict_Check(xoptions)) { if (xoptions == NULL || !PyDict_Check(xoptions)) {
/* PEP432 TODO: we can reach this if xoptions is NULL in the main /* PEP432 TODO: we can reach this if xoptions is NULL in the main
* interpreter config. When that happens, we need to properly set * interpreter config. When that happens, we need to properly set
...@@ -2207,9 +2266,10 @@ get_xoptions(void) ...@@ -2207,9 +2266,10 @@ get_xoptions(void)
* reachable again. * reachable again.
*/ */
xoptions = PyDict_New(); xoptions = PyDict_New();
if (xoptions == NULL) if (xoptions == NULL) {
return NULL; return NULL;
if (_PySys_SetObjectId(&PyId__xoptions, xoptions)) { }
if (sys_set_object_id(tstate, &PyId__xoptions, xoptions)) {
Py_DECREF(xoptions); Py_DECREF(xoptions);
return NULL; return NULL;
} }
...@@ -2223,7 +2283,8 @@ _PySys_AddXOptionWithError(const wchar_t *s) ...@@ -2223,7 +2283,8 @@ _PySys_AddXOptionWithError(const wchar_t *s)
{ {
PyObject *name = NULL, *value = NULL; PyObject *name = NULL, *value = NULL;
PyObject *opts = get_xoptions(); PyThreadState *tstate = _PyThreadState_GET();
PyObject *opts = get_xoptions(tstate);
if (opts == NULL) { if (opts == NULL) {
goto error; goto error;
} }
...@@ -2264,8 +2325,8 @@ PySys_AddXOption(const wchar_t *s) ...@@ -2264,8 +2325,8 @@ PySys_AddXOption(const wchar_t *s)
} }
if (_PySys_AddXOptionWithError(s) < 0) { if (_PySys_AddXOptionWithError(s) < 0) {
/* No return value, therefore clear error state if possible */ /* No return value, therefore clear error state if possible */
if (_PyThreadState_UncheckedGet()) { if (tstate) {
PyErr_Clear(); _PyErr_Clear(tstate);
} }
} }
} }
...@@ -2273,7 +2334,8 @@ PySys_AddXOption(const wchar_t *s) ...@@ -2273,7 +2334,8 @@ PySys_AddXOption(const wchar_t *s)
PyObject * PyObject *
PySys_GetXOptions(void) PySys_GetXOptions(void)
{ {
return get_xoptions(); PyThreadState *tstate = _PyThreadState_GET();
return get_xoptions(tstate);
} }
/* XXX This doc string is too long to be a single string literal in VC++ 5.0. /* XXX This doc string is too long to be a single string literal in VC++ 5.0.
...@@ -2413,12 +2475,12 @@ static PyStructSequence_Desc flags_desc = { ...@@ -2413,12 +2475,12 @@ static PyStructSequence_Desc flags_desc = {
}; };
static PyObject* static PyObject*
make_flags(_PyRuntimeState *runtime, PyInterpreterState *interp) make_flags(_PyRuntimeState *runtime, PyThreadState *tstate)
{ {
int pos = 0; int pos = 0;
PyObject *seq; PyObject *seq;
const PyPreConfig *preconfig = &runtime->preconfig; const PyPreConfig *preconfig = &runtime->preconfig;
const PyConfig *config = &interp->config; const PyConfig *config = &tstate->interp->config;
seq = PyStructSequence_New(&FlagsType); seq = PyStructSequence_New(&FlagsType);
if (seq == NULL) if (seq == NULL)
...@@ -2446,7 +2508,7 @@ make_flags(_PyRuntimeState *runtime, PyInterpreterState *interp) ...@@ -2446,7 +2508,7 @@ make_flags(_PyRuntimeState *runtime, PyInterpreterState *interp)
SetFlag(preconfig->utf8_mode); SetFlag(preconfig->utf8_mode);
#undef SetFlag #undef SetFlag
if (PyErr_Occurred()) { if (_PyErr_Occurred(tstate)) {
Py_DECREF(seq); Py_DECREF(seq);
return NULL; return NULL;
} }
...@@ -2477,7 +2539,7 @@ static PyStructSequence_Desc version_info_desc = { ...@@ -2477,7 +2539,7 @@ static PyStructSequence_Desc version_info_desc = {
}; };
static PyObject * static PyObject *
make_version_info(void) make_version_info(PyThreadState *tstate)
{ {
PyObject *version_info; PyObject *version_info;
char *s; char *s;
...@@ -2515,7 +2577,7 @@ make_version_info(void) ...@@ -2515,7 +2577,7 @@ make_version_info(void)
#undef SetIntItem #undef SetIntItem
#undef SetStrItem #undef SetStrItem
if (PyErr_Occurred()) { if (_PyErr_Occurred(tstate)) {
Py_CLEAR(version_info); Py_CLEAR(version_info);
return NULL; return NULL;
} }
...@@ -2633,7 +2695,7 @@ static struct PyModuleDef sysmodule = { ...@@ -2633,7 +2695,7 @@ static struct PyModuleDef sysmodule = {
} while (0) } while (0)
static PyStatus static PyStatus
_PySys_InitCore(_PyRuntimeState *runtime, PyInterpreterState *interp, _PySys_InitCore(_PyRuntimeState *runtime, PyThreadState *tstate,
PyObject *sysdict) PyObject *sysdict)
{ {
PyObject *version_info; PyObject *version_info;
...@@ -2678,7 +2740,7 @@ _PySys_InitCore(_PyRuntimeState *runtime, PyInterpreterState *interp, ...@@ -2678,7 +2740,7 @@ _PySys_InitCore(_PyRuntimeState *runtime, PyInterpreterState *interp,
} }
} }
SET_SYS_FROM_STRING("hash_info", SET_SYS_FROM_STRING("hash_info",
get_hash_info()); get_hash_info(tstate));
SET_SYS_FROM_STRING("maxunicode", SET_SYS_FROM_STRING("maxunicode",
PyLong_FromLong(0x10FFFF)); PyLong_FromLong(0x10FFFF));
SET_SYS_FROM_STRING("builtin_module_names", SET_SYS_FROM_STRING("builtin_module_names",
...@@ -2709,14 +2771,15 @@ _PySys_InitCore(_PyRuntimeState *runtime, PyInterpreterState *interp, ...@@ -2709,14 +2771,15 @@ _PySys_InitCore(_PyRuntimeState *runtime, PyInterpreterState *interp,
goto type_init_failed; goto type_init_failed;
} }
} }
version_info = make_version_info(); version_info = make_version_info(tstate);
SET_SYS_FROM_STRING("version_info", version_info); SET_SYS_FROM_STRING("version_info", version_info);
/* prevent user from creating new instances */ /* prevent user from creating new instances */
VersionInfoType.tp_init = NULL; VersionInfoType.tp_init = NULL;
VersionInfoType.tp_new = NULL; VersionInfoType.tp_new = NULL;
res = PyDict_DelItemString(VersionInfoType.tp_dict, "__new__"); res = PyDict_DelItemString(VersionInfoType.tp_dict, "__new__");
if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError)) if (res < 0 && _PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
PyErr_Clear(); _PyErr_Clear(tstate);
}
/* implementation */ /* implementation */
SET_SYS_FROM_STRING("implementation", make_impl_info(version_info)); SET_SYS_FROM_STRING("implementation", make_impl_info(version_info));
...@@ -2728,7 +2791,7 @@ _PySys_InitCore(_PyRuntimeState *runtime, PyInterpreterState *interp, ...@@ -2728,7 +2791,7 @@ _PySys_InitCore(_PyRuntimeState *runtime, PyInterpreterState *interp,
} }
} }
/* Set flags to their default values (updated by _PySys_InitMain()) */ /* Set flags to their default values (updated by _PySys_InitMain()) */
SET_SYS_FROM_STRING("flags", make_flags(runtime, interp)); SET_SYS_FROM_STRING("flags", make_flags(runtime, tstate));
#if defined(MS_WINDOWS) #if defined(MS_WINDOWS)
/* getwindowsversion */ /* getwindowsversion */
...@@ -2740,10 +2803,10 @@ _PySys_InitCore(_PyRuntimeState *runtime, PyInterpreterState *interp, ...@@ -2740,10 +2803,10 @@ _PySys_InitCore(_PyRuntimeState *runtime, PyInterpreterState *interp,
/* prevent user from creating new instances */ /* prevent user from creating new instances */
WindowsVersionType.tp_init = NULL; WindowsVersionType.tp_init = NULL;
WindowsVersionType.tp_new = NULL; WindowsVersionType.tp_new = NULL;
assert(!PyErr_Occurred()); assert(!_PyErr_Occurred(tstate));
res = PyDict_DelItemString(WindowsVersionType.tp_dict, "__new__"); res = PyDict_DelItemString(WindowsVersionType.tp_dict, "__new__");
if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError)) { if (res < 0 && _PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
PyErr_Clear(); _PyErr_Clear(tstate);
} }
#endif #endif
...@@ -2766,7 +2829,7 @@ _PySys_InitCore(_PyRuntimeState *runtime, PyInterpreterState *interp, ...@@ -2766,7 +2829,7 @@ _PySys_InitCore(_PyRuntimeState *runtime, PyInterpreterState *interp,
} }
} }
if (PyErr_Occurred()) { if (_PyErr_Occurred(tstate)) {
goto err_occurred; goto err_occurred;
} }
return _PyStatus_OK(); return _PyStatus_OK();
...@@ -2849,10 +2912,10 @@ sys_create_xoptions_dict(const PyConfig *config) ...@@ -2849,10 +2912,10 @@ sys_create_xoptions_dict(const PyConfig *config)
int int
_PySys_InitMain(_PyRuntimeState *runtime, PyInterpreterState *interp) _PySys_InitMain(_PyRuntimeState *runtime, PyThreadState *tstate)
{ {
PyObject *sysdict = interp->sysdict; PyObject *sysdict = tstate->interp->sysdict;
const PyConfig *config = &interp->config; const PyConfig *config = &tstate->interp->config;
int res; int res;
#define COPY_LIST(KEY, VALUE) \ #define COPY_LIST(KEY, VALUE) \
...@@ -2903,34 +2966,37 @@ _PySys_InitMain(_PyRuntimeState *runtime, PyInterpreterState *interp) ...@@ -2903,34 +2966,37 @@ _PySys_InitMain(_PyRuntimeState *runtime, PyInterpreterState *interp)
#undef SET_SYS_FROM_WSTR #undef SET_SYS_FROM_WSTR
/* Set flags to their final values */ /* Set flags to their final values */
SET_SYS_FROM_STRING_INT_RESULT("flags", make_flags(runtime, interp)); SET_SYS_FROM_STRING_INT_RESULT("flags", make_flags(runtime, tstate));
/* prevent user from creating new instances */ /* prevent user from creating new instances */
FlagsType.tp_init = NULL; FlagsType.tp_init = NULL;
FlagsType.tp_new = NULL; FlagsType.tp_new = NULL;
res = PyDict_DelItemString(FlagsType.tp_dict, "__new__"); res = PyDict_DelItemString(FlagsType.tp_dict, "__new__");
if (res < 0) { if (res < 0) {
if (!PyErr_ExceptionMatches(PyExc_KeyError)) { if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
return res; return res;
} }
PyErr_Clear(); _PyErr_Clear(tstate);
} }
SET_SYS_FROM_STRING_INT_RESULT("dont_write_bytecode", SET_SYS_FROM_STRING_INT_RESULT("dont_write_bytecode",
PyBool_FromLong(!config->write_bytecode)); PyBool_FromLong(!config->write_bytecode));
if (get_warnoptions() == NULL) if (get_warnoptions(tstate) == NULL) {
return -1; return -1;
}
if (get_xoptions() == NULL) if (get_xoptions(tstate) == NULL)
return -1; return -1;
/* Transfer any sys.warnoptions and sys._xoptions set directly /* Transfer any sys.warnoptions and sys._xoptions set directly
* by an embedding application from the linked list to the module. */ * by an embedding application from the linked list to the module. */
if (_PySys_ReadPreInitOptions() != 0) if (sys_read_preinit_options(tstate) != 0)
return -1; return -1;
if (PyErr_Occurred()) if (_PyErr_Occurred(tstate)) {
return -1; goto err_occurred;
}
return 0; return 0;
err_occurred: err_occurred:
...@@ -2973,6 +3039,8 @@ PyStatus ...@@ -2973,6 +3039,8 @@ PyStatus
_PySys_Create(_PyRuntimeState *runtime, PyInterpreterState *interp, _PySys_Create(_PyRuntimeState *runtime, PyInterpreterState *interp,
PyObject **sysmod_p) PyObject **sysmod_p)
{ {
PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
PyObject *modules = PyDict_New(); PyObject *modules = PyDict_New();
if (modules == NULL) { if (modules == NULL) {
return _PyStatus_ERR("can't make modules dictionary"); return _PyStatus_ERR("can't make modules dictionary");
...@@ -3000,7 +3068,7 @@ _PySys_Create(_PyRuntimeState *runtime, PyInterpreterState *interp, ...@@ -3000,7 +3068,7 @@ _PySys_Create(_PyRuntimeState *runtime, PyInterpreterState *interp,
return status; return status;
} }
status = _PySys_InitCore(runtime, interp, sysdict); status = _PySys_InitCore(runtime, tstate, sysdict);
if (_PyStatus_EXCEPTION(status)) { if (_PyStatus_EXCEPTION(status)) {
return status; return status;
} }
...@@ -3051,8 +3119,10 @@ PySys_SetPath(const wchar_t *path) ...@@ -3051,8 +3119,10 @@ PySys_SetPath(const wchar_t *path)
PyObject *v; PyObject *v;
if ((v = makepathobject(path, DELIM)) == NULL) if ((v = makepathobject(path, DELIM)) == NULL)
Py_FatalError("can't create sys.path"); Py_FatalError("can't create sys.path");
if (_PySys_SetObjectId(&PyId_path, v) != 0) PyThreadState *tstate = _PyThreadState_GET();
if (sys_set_object_id(tstate, &PyId_path, v) != 0) {
Py_FatalError("can't assign sys.path"); Py_FatalError("can't assign sys.path");
}
Py_DECREF(v); Py_DECREF(v);
} }
...@@ -3078,6 +3148,8 @@ make_sys_argv(int argc, wchar_t * const * argv) ...@@ -3078,6 +3148,8 @@ make_sys_argv(int argc, wchar_t * const * argv)
void void
PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath) PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath)
{ {
PyThreadState *tstate = _PyThreadState_GET();
if (argc < 1 || argv == NULL) { if (argc < 1 || argv == NULL) {
/* Ensure at least one (empty) argument is seen */ /* Ensure at least one (empty) argument is seen */
wchar_t* empty_argv[1] = {L""}; wchar_t* empty_argv[1] = {L""};
...@@ -3089,7 +3161,7 @@ PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath) ...@@ -3089,7 +3161,7 @@ PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath)
if (av == NULL) { if (av == NULL) {
Py_FatalError("no mem for sys.argv"); Py_FatalError("no mem for sys.argv");
} }
if (PySys_SetObject("argv", av) != 0) { if (sys_set_object(tstate, "argv", av) != 0) {
Py_DECREF(av); Py_DECREF(av);
Py_FatalError("can't assign sys.argv"); Py_FatalError("can't assign sys.argv");
} }
...@@ -3105,7 +3177,7 @@ PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath) ...@@ -3105,7 +3177,7 @@ PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath)
Py_FatalError("can't compute path0 from argv"); Py_FatalError("can't compute path0 from argv");
} }
PyObject *sys_path = _PySys_GetObjectId(&PyId_path); PyObject *sys_path = sys_get_object_id(tstate, &PyId_path);
if (sys_path != NULL) { if (sys_path != NULL) {
if (PyList_Insert(sys_path, 0, path0) < 0) { if (PyList_Insert(sys_path, 0, path0) < 0) {
Py_DECREF(path0); Py_DECREF(path0);
...@@ -3208,12 +3280,13 @@ sys_write(_Py_Identifier *key, FILE *fp, const char *format, va_list va) ...@@ -3208,12 +3280,13 @@ sys_write(_Py_Identifier *key, FILE *fp, const char *format, va_list va)
PyObject *error_type, *error_value, *error_traceback; PyObject *error_type, *error_value, *error_traceback;
char buffer[1001]; char buffer[1001];
int written; int written;
PyThreadState *tstate = _PyThreadState_GET();
PyErr_Fetch(&error_type, &error_value, &error_traceback); _PyErr_Fetch(tstate, &error_type, &error_value, &error_traceback);
file = _PySys_GetObjectId(key); file = sys_get_object_id(tstate, key);
written = PyOS_vsnprintf(buffer, sizeof(buffer), format, va); written = PyOS_vsnprintf(buffer, sizeof(buffer), format, va);
if (sys_pyfile_write(buffer, file) != 0) { if (sys_pyfile_write(buffer, file) != 0) {
PyErr_Clear(); _PyErr_Clear(tstate);
fputs(buffer, fp); fputs(buffer, fp);
} }
if (written < 0 || (size_t)written >= sizeof(buffer)) { if (written < 0 || (size_t)written >= sizeof(buffer)) {
...@@ -3221,7 +3294,7 @@ sys_write(_Py_Identifier *key, FILE *fp, const char *format, va_list va) ...@@ -3221,7 +3294,7 @@ sys_write(_Py_Identifier *key, FILE *fp, const char *format, va_list va)
if (sys_pyfile_write(truncated, file) != 0) if (sys_pyfile_write(truncated, file) != 0)
fputs(truncated, fp); fputs(truncated, fp);
} }
PyErr_Restore(error_type, error_value, error_traceback); _PyErr_Restore(tstate, error_type, error_value, error_traceback);
} }
void void
...@@ -3250,20 +3323,21 @@ sys_format(_Py_Identifier *key, FILE *fp, const char *format, va_list va) ...@@ -3250,20 +3323,21 @@ sys_format(_Py_Identifier *key, FILE *fp, const char *format, va_list va)
PyObject *file, *message; PyObject *file, *message;
PyObject *error_type, *error_value, *error_traceback; PyObject *error_type, *error_value, *error_traceback;
const char *utf8; const char *utf8;
PyThreadState *tstate = _PyThreadState_GET();
PyErr_Fetch(&error_type, &error_value, &error_traceback); _PyErr_Fetch(tstate, &error_type, &error_value, &error_traceback);
file = _PySys_GetObjectId(key); file = sys_get_object_id(tstate, key);
message = PyUnicode_FromFormatV(format, va); message = PyUnicode_FromFormatV(format, va);
if (message != NULL) { if (message != NULL) {
if (sys_pyfile_write_unicode(message, file) != 0) { if (sys_pyfile_write_unicode(message, file) != 0) {
PyErr_Clear(); _PyErr_Clear(tstate);
utf8 = PyUnicode_AsUTF8(message); utf8 = PyUnicode_AsUTF8(message);
if (utf8 != NULL) if (utf8 != NULL)
fputs(utf8, fp); fputs(utf8, fp);
} }
Py_DECREF(message); Py_DECREF(message);
} }
PyErr_Restore(error_type, error_value, error_traceback); _PyErr_Restore(tstate, error_type, error_value, error_traceback);
} }
void void
......
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