From d4be68e361f4258cf0848fc70018dfe37a2acc24 Mon Sep 17 00:00:00 2001 From: shadchin Date: Mon, 18 Apr 2022 12:39:32 +0300 Subject: IGNIETFERRO-1816 Update Python 3 from 3.9.12 to 3.10.4 ref:9f96be6d02ee8044fdd6f124b799b270c20ce641 --- contrib/tools/python3/src/Python/_warnings.c | 125 ++++++++++----------------- 1 file changed, 47 insertions(+), 78 deletions(-) (limited to 'contrib/tools/python3/src/Python/_warnings.c') diff --git a/contrib/tools/python3/src/Python/_warnings.c b/contrib/tools/python3/src/Python/_warnings.c index 91a78fe72b3..2c9a2a76872 100644 --- a/contrib/tools/python3/src/Python/_warnings.c +++ b/contrib/tools/python3/src/Python/_warnings.c @@ -1,6 +1,7 @@ #include "Python.h" #include "pycore_initconfig.h" #include "pycore_interp.h" // PyInterpreterState.warnings +#include "pycore_long.h" // _PyLong_GetZero() #include "pycore_pyerrors.h" #include "pycore_pystate.h" // _PyThreadState_GET() #include "frameobject.h" // PyFrame_GetBack() @@ -23,23 +24,20 @@ _Py_IDENTIFIER(ignore); typedef struct _warnings_runtime_state WarningsState; -/* Forward declaration of the _warnings module definition. */ -static struct PyModuleDef warningsmodule; - _Py_IDENTIFIER(__name__); /* Given a module object, get its per-module state. */ static WarningsState * warnings_get_state(void) { - PyThreadState *tstate = _PyThreadState_GET(); - if (tstate == NULL) { - _PyErr_SetString(tstate, PyExc_RuntimeError, - "warnings_get_state: could not identify " - "current interpreter"); + PyInterpreterState *interp = _PyInterpreterState_GET(); + if (interp == NULL) { + PyErr_SetString(PyExc_RuntimeError, + "warnings_get_state: could not identify " + "current interpreter"); return NULL; } - return &tstate->interp->warnings; + return &interp->warnings; } /* Clear the given warnings module state. */ @@ -68,13 +66,12 @@ create_filter(PyObject *category, _Py_Identifier *id, const char *modname) return NULL; } } else { - modname_obj = Py_None; - Py_INCREF(modname_obj); + modname_obj = Py_NewRef(Py_None); } /* This assumes the line number is zero for now. */ PyObject *filter = PyTuple_Pack(5, action_str, Py_None, - category, modname_obj, _PyLong_Zero); + category, modname_obj, _PyLong_GetZero()); Py_DECREF(modname_obj); return filter; } @@ -116,37 +113,34 @@ init_filters(void) } /* Initialize the given warnings module state. */ -static int -warnings_init_state(WarningsState *st) +int +_PyWarnings_InitState(PyInterpreterState *interp) { + WarningsState *st = &interp->warnings; + if (st->filters == NULL) { st->filters = init_filters(); if (st->filters == NULL) { - goto error; + return -1; } } if (st->once_registry == NULL) { st->once_registry = PyDict_New(); if (st->once_registry == NULL) { - goto error; + return -1; } } if (st->default_action == NULL) { st->default_action = PyUnicode_FromString("default"); if (st->default_action == NULL) { - goto error; + return -1; } } st->filters_version = 0; - return 0; - -error: - warnings_clear_state(st); - return -1; } @@ -475,7 +469,7 @@ update_registry(PyObject *registry, PyObject *text, PyObject *category, int rc; if (add_zero) - altkey = PyTuple_Pack(3, text, category, _PyLong_Zero); + altkey = PyTuple_Pack(3, text, category, _PyLong_GetZero()); else altkey = PyTuple_Pack(2, text, category); @@ -854,7 +848,7 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno, } if (f == NULL) { - globals = _PyInterpreterState_GET()->sysdict; + globals = tstate->interp->sysdict; *filename = PyUnicode_FromString("sys"); *lineno = 1; } @@ -1356,70 +1350,45 @@ static PyMethodDef warnings_functions[] = { }; -static struct PyModuleDef warningsmodule = { - PyModuleDef_HEAD_INIT, - MODULE_NAME, /* m_name */ - warnings__doc__, /* m_doc */ - 0, /* m_size */ - warnings_functions, /* m_methods */ - NULL, /* m_reload */ - NULL, /* m_traverse */ - NULL, /* m_clear */ - NULL /* m_free */ -}; - - -PyStatus -_PyWarnings_InitState(PyThreadState *tstate) -{ - if (warnings_init_state(&tstate->interp->warnings) < 0) { - return _PyStatus_ERR("can't initialize warnings"); - } - return _PyStatus_OK(); -} - - -PyMODINIT_FUNC -_PyWarnings_Init(void) +static int +warnings_module_exec(PyObject *module) { - PyObject *m; - - m = PyModule_Create(&warningsmodule); - if (m == NULL) { - return NULL; - } - WarningsState *st = warnings_get_state(); if (st == NULL) { - goto error; + return -1; } - if (warnings_init_state(st) < 0) { - goto error; + if (PyModule_AddObjectRef(module, "filters", st->filters) < 0) { + return -1; } - - Py_INCREF(st->filters); - if (PyModule_AddObject(m, "filters", st->filters) < 0) { - goto error; + if (PyModule_AddObjectRef(module, "_onceregistry", st->once_registry) < 0) { + return -1; } - - Py_INCREF(st->once_registry); - if (PyModule_AddObject(m, "_onceregistry", st->once_registry) < 0) { - goto error; + if (PyModule_AddObjectRef(module, "_defaultaction", st->default_action) < 0) { + return -1; } + return 0; +} - Py_INCREF(st->default_action); - if (PyModule_AddObject(m, "_defaultaction", st->default_action) < 0) { - goto error; - } - return m; +static PyModuleDef_Slot warnings_slots[] = { + {Py_mod_exec, warnings_module_exec}, + {0, NULL} +}; -error: - if (st != NULL) { - warnings_clear_state(st); - } - Py_DECREF(m); - return NULL; +static struct PyModuleDef warnings_module = { + PyModuleDef_HEAD_INIT, + .m_name = MODULE_NAME, + .m_doc = warnings__doc__, + .m_size = 0, + .m_methods = warnings_functions, + .m_slots = warnings_slots, +}; + + +PyMODINIT_FUNC +_PyWarnings_Init(void) +{ + return PyModuleDef_Init(&warnings_module); } // We need this to ensure that warnings still work until late in finalization. -- cgit v1.3