diff options
author | shadchin <shadchin@yandex-team.com> | 2025-06-13 00:05:26 +0300 |
---|---|---|
committer | shadchin <shadchin@yandex-team.com> | 2025-06-13 00:35:30 +0300 |
commit | 796b9088366b10b4cd42885101fc20c0b5709b07 (patch) | |
tree | f287eacb0b95ffd7cabf95b16cafb4788645dc38 /contrib/tools/python3/Python/sysmodule.c | |
parent | c72bca862651e507d2ff4980ef7f4ff7267a7227 (diff) | |
download | ydb-796b9088366b10b4cd42885101fc20c0b5709b07.tar.gz |
Update Python 3 to 3.12.10
commit_hash:dd2398e159fe1d72ea6b12da52fccc933a41a785
Diffstat (limited to 'contrib/tools/python3/Python/sysmodule.c')
-rw-r--r-- | contrib/tools/python3/Python/sysmodule.c | 183 |
1 files changed, 150 insertions, 33 deletions
diff --git a/contrib/tools/python3/Python/sysmodule.c b/contrib/tools/python3/Python/sysmodule.c index 7813048be45..62d53a6820f 100644 --- a/contrib/tools/python3/Python/sysmodule.c +++ b/contrib/tools/python3/Python/sysmodule.c @@ -29,6 +29,7 @@ Data members: #include "pycore_pymem.h" // _PyMem_SetDefaultAllocator() #include "pycore_pystate.h" // _PyThreadState_GET() #include "pycore_structseq.h" // _PyStructSequence_InitBuiltinWithFlags() +#include "pycore_sysmodule.h" #include "pycore_tuple.h" // _PyTuple_FromArray() #include "frameobject.h" // PyFrame_FastToLocalsWithError() @@ -38,8 +39,10 @@ Data members: #include <locale.h> #ifdef MS_WINDOWS -#define WIN32_LEAN_AND_MEAN -#include <windows.h> +# ifndef WIN32_LEAN_AND_MEAN +# define WIN32_LEAN_AND_MEAN +# endif +# include <windows.h> #endif /* MS_WINDOWS */ #ifdef MS_COREDLL @@ -78,23 +81,97 @@ _PySys_GetAttr(PyThreadState *tstate, PyObject *name) return value; } -static PyObject * -_PySys_GetObject(PyInterpreterState *interp, const char *name) + PyObject * +_PySys_GetRequiredAttr(PyObject *name) { - PyObject *sysdict = interp->sysdict; + if (!PyUnicode_Check(name)) { + PyErr_Format(PyExc_TypeError, + "attribute name must be string, not '%.200s'", + Py_TYPE(name)->tp_name); + return NULL; + } + PyThreadState *tstate = _PyThreadState_GET(); + PyObject *sysdict = tstate->interp->sysdict; if (sysdict == NULL) { + PyErr_SetString(PyExc_RuntimeError, "no sys module"); return NULL; } - return _PyDict_GetItemStringWithError(sysdict, name); + PyObject *value = _PyDict_GetItemWithError(sysdict, name); + Py_XINCREF(value); + if (value == NULL && !_PyErr_Occurred(tstate)) { + PyErr_Format(PyExc_RuntimeError, "lost sys.%U", name); + } + return value; } PyObject * -PySys_GetObject(const char *name) +_PySys_GetRequiredAttrString(const char *name) { PyThreadState *tstate = _PyThreadState_GET(); + PyObject *sysdict = tstate->interp->sysdict; + if (sysdict == NULL) { + PyErr_SetString(PyExc_RuntimeError, "no sys module"); + return NULL; + } + PyObject *value = _PyDict_GetItemStringWithError(sysdict, name); + Py_XINCREF(value); + if (value == NULL && !_PyErr_Occurred(tstate)) { + PyErr_Format(PyExc_RuntimeError, "lost sys.%s", name); + } + return value; +} +int +_PySys_GetOptionalAttr(PyObject *name, PyObject **value) +{ + if (!PyUnicode_Check(name)) { + PyErr_Format(PyExc_TypeError, + "attribute name must be string, not '%.200s'", + Py_TYPE(name)->tp_name); + *value = NULL; + return -1; + } + PyThreadState *tstate = _PyThreadState_GET(); + PyObject *sysdict = tstate->interp->sysdict; + if (sysdict == NULL) { + *value = NULL; + return 0; + } + *value = _PyDict_GetItemWithError(sysdict, name); + if (*value) { + Py_INCREF(*value); + return 1; + } + return _PyErr_Occurred(tstate) ? -1 : 0; +} + +int +_PySys_GetOptionalAttrString(const char *name, PyObject **value) +{ + PyThreadState *tstate = _PyThreadState_GET(); + PyObject *sysdict = tstate->interp->sysdict; + if (sysdict == NULL) { + *value = NULL; + return 0; + } + *value = _PyDict_GetItemStringWithError(sysdict, name); + if (*value) { + Py_INCREF(*value); + return 1; + } + return _PyErr_Occurred(tstate) ? -1 : 0; +} + +PyObject * +PySys_GetObject(const char *name) +{ + PyThreadState *tstate = _PyThreadState_GET(); + PyObject *sysdict = tstate->interp->sysdict; + if (sysdict == NULL) { + return NULL; + } PyObject *exc = _PyErr_GetRaisedException(tstate); - PyObject *value = _PySys_GetObject(tstate->interp, name); + PyObject *value = _PyDict_GetItemStringWithError(sysdict, name); /* XXX Suppress a new exception if it was raised and restore * the old one. */ _PyErr_SetRaisedException(tstate, exc); @@ -108,6 +185,10 @@ sys_set_object(PyInterpreterState *interp, PyObject *key, PyObject *v) return -1; } PyObject *sd = interp->sysdict; + if (sd == NULL) { + PyErr_SetString(PyExc_RuntimeError, "no sys module"); + return -1; + } if (v == NULL) { v = _PyDict_Pop(sd, key, Py_None); if (v == NULL) { @@ -724,9 +805,13 @@ sys_displayhook(PyObject *module, PyObject *o) } if (PyObject_SetAttr(builtins, _Py_LATIN1_CHR('_'), Py_None) != 0) return NULL; - outf = _PySys_GetAttr(tstate, &_Py_ID(stdout)); - if (outf == NULL || outf == Py_None) { + outf = _PySys_GetRequiredAttr(&_Py_ID(stdout)); + if (outf == NULL) { + return NULL; + } + if (outf == Py_None) { _PyErr_SetString(tstate, PyExc_RuntimeError, "lost sys.stdout"); + Py_DECREF(outf); return NULL; } if (PyFile_WriteObject(o, outf, 0) != 0) { @@ -737,17 +822,23 @@ sys_displayhook(PyObject *module, PyObject *o) _PyErr_Clear(tstate); err = sys_displayhook_unencodable(outf, o); if (err) { + Py_DECREF(outf); return NULL; } } else { + Py_DECREF(outf); return NULL; } } - if (PyFile_WriteObject(_Py_LATIN1_CHR('\n'), outf, Py_PRINT_RAW) != 0) + if (PyFile_WriteObject(_Py_LATIN1_CHR('\n'), outf, Py_PRINT_RAW) != 0) { + Py_DECREF(outf); return NULL; - if (PyObject_SetAttr(builtins, _Py_LATIN1_CHR('_'), o) != 0) + } + Py_DECREF(outf); + if (PyObject_SetAttr(builtins, _Py_LATIN1_CHR('_'), o) != 0) { return NULL; + } Py_RETURN_NONE; } @@ -2610,7 +2701,10 @@ _PySys_ReadPreinitXOptions(PyConfig *config) static PyObject * get_warnoptions(PyThreadState *tstate) { - PyObject *warnoptions = _PySys_GetAttr(tstate, &_Py_ID(warnoptions)); + PyObject *warnoptions; + if (_PySys_GetOptionalAttr(&_Py_ID(warnoptions), &warnoptions) < 0) { + return NULL; + } if (warnoptions == NULL || !PyList_Check(warnoptions)) { /* PEP432 TODO: we can reach this if warnoptions is NULL in the main * interpreter config. When that happens, we need to properly set @@ -2622,6 +2716,7 @@ get_warnoptions(PyThreadState *tstate) * call optional for embedding applications, thus making this * reachable again. */ + Py_XDECREF(warnoptions); warnoptions = PyList_New(0); if (warnoptions == NULL) { return NULL; @@ -2630,7 +2725,6 @@ get_warnoptions(PyThreadState *tstate) Py_DECREF(warnoptions); return NULL; } - Py_DECREF(warnoptions); } return warnoptions; } @@ -2644,10 +2738,15 @@ PySys_ResetWarnOptions(void) return; } - PyObject *warnoptions = _PySys_GetAttr(tstate, &_Py_ID(warnoptions)); - if (warnoptions == NULL || !PyList_Check(warnoptions)) + PyObject *warnoptions; + if (_PySys_GetOptionalAttr(&_Py_ID(warnoptions), &warnoptions) < 0) { + PyErr_Clear(); return; - PyList_SetSlice(warnoptions, 0, PyList_GET_SIZE(warnoptions), NULL); + } + if (warnoptions != NULL && PyList_Check(warnoptions)) { + PyList_SetSlice(warnoptions, 0, PyList_GET_SIZE(warnoptions), NULL); + } + Py_XDECREF(warnoptions); } static int @@ -2659,8 +2758,10 @@ _PySys_AddWarnOptionWithError(PyThreadState *tstate, PyObject *option) return -1; } if (PyList_Append(warnoptions, option)) { + Py_DECREF(warnoptions); return -1; } + Py_DECREF(warnoptions); return 0; } @@ -2698,16 +2799,24 @@ _Py_COMP_DIAG_POP int PySys_HasWarnOptions(void) { - PyThreadState *tstate = _PyThreadState_GET(); - PyObject *warnoptions = _PySys_GetAttr(tstate, &_Py_ID(warnoptions)); - return (warnoptions != NULL && PyList_Check(warnoptions) - && PyList_GET_SIZE(warnoptions) > 0); + PyObject *warnoptions; + if (_PySys_GetOptionalAttr(&_Py_ID(warnoptions), &warnoptions) < 0) { + PyErr_Clear(); + return 0; + } + int r = (warnoptions != NULL && PyList_Check(warnoptions) && + PyList_GET_SIZE(warnoptions) > 0); + Py_XDECREF(warnoptions); + return r; } static PyObject * get_xoptions(PyThreadState *tstate) { - PyObject *xoptions = _PySys_GetAttr(tstate, &_Py_ID(_xoptions)); + PyObject *xoptions; + if (_PySys_GetOptionalAttr(&_Py_ID(_xoptions), &xoptions) < 0) { + return NULL; + } if (xoptions == NULL || !PyDict_Check(xoptions)) { /* PEP432 TODO: we can reach this if xoptions is NULL in the main * interpreter config. When that happens, we need to properly set @@ -2719,6 +2828,7 @@ get_xoptions(PyThreadState *tstate) * call optional for embedding applications, thus making this * reachable again. */ + Py_XDECREF(xoptions); xoptions = PyDict_New(); if (xoptions == NULL) { return NULL; @@ -2727,7 +2837,6 @@ get_xoptions(PyThreadState *tstate) Py_DECREF(xoptions); return NULL; } - Py_DECREF(xoptions); } return xoptions; } @@ -2766,11 +2875,13 @@ _PySys_AddXOptionWithError(const wchar_t *s) } Py_DECREF(name); Py_DECREF(value); + Py_DECREF(opts); return 0; error: Py_XDECREF(name); Py_XDECREF(value); + Py_XDECREF(opts); return -1; } @@ -2792,7 +2903,9 @@ PyObject * PySys_GetXOptions(void) { PyThreadState *tstate = _PyThreadState_GET(); - return get_xoptions(tstate); + PyObject *opts = get_xoptions(tstate); + Py_XDECREF(opts); + return opts; } /* XXX This doc string is too long to be a single string literal in VC++ 5.0. @@ -3517,16 +3630,15 @@ _PySys_UpdateConfig(PyThreadState *tstate) #undef COPY_WSTR // sys.flags - PyObject *flags = _PySys_GetObject(interp, "flags"); // borrowed ref + PyObject *flags = _PySys_GetRequiredAttrString("flags"); if (flags == NULL) { - if (!_PyErr_Occurred(tstate)) { - _PyErr_SetString(tstate, PyExc_RuntimeError, "lost sys.flags"); - } return -1; } if (set_flags_from_config(interp, flags) < 0) { + Py_DECREF(flags); return -1; } + Py_DECREF(flags); SET_SYS("dont_write_bytecode", PyBool_FromLong(!config->write_bytecode)); @@ -3754,12 +3866,15 @@ PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath) Py_FatalError("can't compute path0 from argv"); } - PyObject *sys_path = _PySys_GetAttr(tstate, &_Py_ID(path)); - if (sys_path != NULL) { + PyObject *sys_path; + if (_PySys_GetOptionalAttr(&_Py_ID(path), &sys_path) < 0) { + Py_FatalError("can't get sys.path"); + } + else if (sys_path != NULL) { if (PyList_Insert(sys_path, 0, path0) < 0) { - Py_DECREF(path0); Py_FatalError("can't prepend path0 to sys.path"); } + Py_DECREF(sys_path); } Py_DECREF(path0); } @@ -3847,8 +3962,8 @@ sys_write(PyObject *key, FILE *fp, const char *format, va_list va) PyThreadState *tstate = _PyThreadState_GET(); PyObject *exc = _PyErr_GetRaisedException(tstate); - file = _PySys_GetAttr(tstate, key); written = PyOS_vsnprintf(buffer, sizeof(buffer), format, va); + file = _PySys_GetRequiredAttr(key); if (sys_pyfile_write(buffer, file) != 0) { _PyErr_Clear(tstate); fputs(buffer, fp); @@ -3858,6 +3973,7 @@ sys_write(PyObject *key, FILE *fp, const char *format, va_list va) if (sys_pyfile_write(truncated, file) != 0) fputs(truncated, fp); } + Py_XDECREF(file); _PyErr_SetRaisedException(tstate, exc); } @@ -3889,15 +4005,16 @@ sys_format(PyObject *key, FILE *fp, const char *format, va_list va) PyThreadState *tstate = _PyThreadState_GET(); PyObject *exc = _PyErr_GetRaisedException(tstate); - file = _PySys_GetAttr(tstate, key); message = PyUnicode_FromFormatV(format, va); if (message != NULL) { + file = _PySys_GetRequiredAttr(key); if (sys_pyfile_write_unicode(message, file) != 0) { _PyErr_Clear(tstate); utf8 = PyUnicode_AsUTF8(message); if (utf8 != NULL) fputs(utf8, fp); } + Py_XDECREF(file); Py_DECREF(message); } _PyErr_SetRaisedException(tstate, exc); |