aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/tools/python3/Python/pythonrun.c
diff options
context:
space:
mode:
authorshadchin <shadchin@yandex-team.com>2025-06-13 00:05:26 +0300
committershadchin <shadchin@yandex-team.com>2025-06-13 00:35:30 +0300
commit796b9088366b10b4cd42885101fc20c0b5709b07 (patch)
treef287eacb0b95ffd7cabf95b16cafb4788645dc38 /contrib/tools/python3/Python/pythonrun.c
parentc72bca862651e507d2ff4980ef7f4ff7267a7227 (diff)
downloadydb-796b9088366b10b4cd42885101fc20c0b5709b07.tar.gz
Update Python 3 to 3.12.10
commit_hash:dd2398e159fe1d72ea6b12da52fccc933a41a785
Diffstat (limited to 'contrib/tools/python3/Python/pythonrun.c')
-rw-r--r--contrib/tools/python3/Python/pythonrun.c87
1 files changed, 64 insertions, 23 deletions
diff --git a/contrib/tools/python3/Python/pythonrun.c b/contrib/tools/python3/Python/pythonrun.c
index cf84573a8e6..89287bace08 100644
--- a/contrib/tools/python3/Python/pythonrun.c
+++ b/contrib/tools/python3/Python/pythonrun.c
@@ -8,8 +8,6 @@
/* TODO: Cull includes following phase split */
-#include <stdbool.h>
-
#include "Python.h"
#include "pycore_ast.h" // PyAST_mod2obj
@@ -27,6 +25,8 @@
#include "errcode.h" // E_EOF
#include "marshal.h" // PyMarshal_ReadLongFromFile()
+#include <stdbool.h>
+
#ifdef MS_WINDOWS
# include "malloc.h" // alloca()
#endif
@@ -115,17 +115,35 @@ _PyRun_InteractiveLoopObject(FILE *fp, PyObject *filename, PyCompilerFlags *flag
flags = &local_flags;
}
- PyThreadState *tstate = _PyThreadState_GET();
- PyObject *v = _PySys_GetAttr(tstate, &_Py_ID(ps1));
+ PyObject *v;
+ if (_PySys_GetOptionalAttr(&_Py_ID(ps1), &v) < 0) {
+ PyErr_Print();
+ return -1;
+ }
if (v == NULL) {
- _PySys_SetAttr(&_Py_ID(ps1), v = PyUnicode_FromString(">>> "));
- Py_XDECREF(v);
+ v = PyUnicode_FromString(">>> ");
+ if (v == NULL) {
+ PyErr_Clear();
+ }
+ if (_PySys_SetAttr(&_Py_ID(ps1), v) < 0) {
+ PyErr_Clear();
+ }
+ }
+ Py_XDECREF(v);
+ if (_PySys_GetOptionalAttr(&_Py_ID(ps2), &v) < 0) {
+ PyErr_Print();
+ return -1;
}
- v = _PySys_GetAttr(tstate, &_Py_ID(ps2));
if (v == NULL) {
- _PySys_SetAttr(&_Py_ID(ps2), v = PyUnicode_FromString("... "));
- Py_XDECREF(v);
+ v = PyUnicode_FromString("... ");
+ if (v == NULL) {
+ PyErr_Clear();
+ }
+ if (_PySys_SetAttr(&_Py_ID(ps2), v) < 0) {
+ PyErr_Clear();
+ }
}
+ Py_XDECREF(v);
#ifdef Py_REF_DEBUG
int show_ref_count = _Py_GetConfig()->show_ref_count;
@@ -190,11 +208,12 @@ PyRun_InteractiveOneObjectEx(FILE *fp, PyObject *filename,
PyArena *arena;
const char *ps1 = "", *ps2 = "", *enc = NULL;
int errcode = 0;
- PyThreadState *tstate = _PyThreadState_GET();
if (fp == stdin) {
/* Fetch encoding from sys.stdin if possible. */
- v = _PySys_GetAttr(tstate, &_Py_ID(stdin));
+ if (_PySys_GetOptionalAttr(&_Py_ID(stdin), &v) < 0) {
+ return -1;
+ }
if (v && v != Py_None) {
oenc = PyObject_GetAttr(v, &_Py_ID(encoding));
if (oenc)
@@ -202,10 +221,14 @@ PyRun_InteractiveOneObjectEx(FILE *fp, PyObject *filename,
if (!enc)
PyErr_Clear();
}
+ Py_XDECREF(v);
+ }
+ if (_PySys_GetOptionalAttr(&_Py_ID(ps1), &v) < 0) {
+ Py_XDECREF(oenc);
+ return -1;
}
- v = _PySys_GetAttr(tstate, &_Py_ID(ps1));
if (v != NULL) {
- v = PyObject_Str(v);
+ Py_SETREF(v, PyObject_Str(v));
if (v == NULL)
PyErr_Clear();
else if (PyUnicode_Check(v)) {
@@ -216,9 +239,12 @@ PyRun_InteractiveOneObjectEx(FILE *fp, PyObject *filename,
}
}
}
- w = _PySys_GetAttr(tstate, &_Py_ID(ps2));
+ if (_PySys_GetOptionalAttr(&_Py_ID(ps2), &w) < 0) {
+ Py_XDECREF(oenc);
+ return -1;
+ }
if (w != NULL) {
- w = PyObject_Str(w);
+ Py_SETREF(w, PyObject_Str(w));
if (w == NULL)
PyErr_Clear();
else if (PyUnicode_Check(w)) {
@@ -718,8 +744,8 @@ _Py_HandleSystemExit(int *exitcode_p)
exitcode = (int)PyLong_AsLong(exc);
}
else {
- PyThreadState *tstate = _PyThreadState_GET();
- PyObject *sys_stderr = _PySys_GetAttr(tstate, &_Py_ID(stderr));
+ PyObject *sys_stderr;
+ (void) _PySys_GetOptionalAttr(&_Py_ID(stderr), &sys_stderr);
/* We clear the exception here to avoid triggering the assertion
* in PyObject_Str that ensures it won't silently lose exception
* details.
@@ -731,6 +757,7 @@ _Py_HandleSystemExit(int *exitcode_p)
PyObject_Print(exc, stderr, Py_PRINT_RAW);
fflush(stderr);
}
+ Py_XDECREF(sys_stderr);
PySys_WriteStderr("\n");
exitcode = 1;
}
@@ -755,7 +782,7 @@ handle_system_exit(void)
static void
_PyErr_PrintEx(PyThreadState *tstate, int set_sys_last_vars)
{
- PyObject *typ = NULL, *tb = NULL;
+ PyObject *typ = NULL, *tb = NULL, *hook = NULL;
handle_system_exit();
PyObject *exc = _PyErr_GetRaisedException(tstate);
@@ -784,7 +811,9 @@ _PyErr_PrintEx(PyThreadState *tstate, int set_sys_last_vars)
_PyErr_Clear(tstate);
}
}
- PyObject *hook = _PySys_GetAttr(tstate, &_Py_ID(excepthook));
+ if (_PySys_GetOptionalAttr(&_Py_ID(excepthook), &hook) < 0) {
+ PyErr_Clear();
+ }
if (_PySys_Audit(tstate, "sys.excepthook", "OOOO", hook ? hook : Py_None,
typ, exc, tb) < 0) {
if (PyErr_ExceptionMatches(PyExc_RuntimeError)) {
@@ -821,6 +850,7 @@ _PyErr_PrintEx(PyThreadState *tstate, int set_sys_last_vars)
}
done:
+ Py_XDECREF(hook);
Py_XDECREF(typ);
Py_XDECREF(exc);
Py_XDECREF(tb);
@@ -1567,17 +1597,24 @@ _PyErr_Display(PyObject *file, PyObject *unused, PyObject *value, PyObject *tb)
void
PyErr_Display(PyObject *unused, PyObject *value, PyObject *tb)
{
- PyThreadState *tstate = _PyThreadState_GET();
- PyObject *file = _PySys_GetAttr(tstate, &_Py_ID(stderr));
+ PyObject *file;
+ if (_PySys_GetOptionalAttr(&_Py_ID(stderr), &file) < 0) {
+ PyObject *exc = PyErr_GetRaisedException();
+ _PyObject_Dump(value);
+ fprintf(stderr, "lost sys.stderr\n");
+ _PyObject_Dump(exc);
+ Py_DECREF(exc);
+ return;
+ }
if (file == NULL) {
_PyObject_Dump(value);
fprintf(stderr, "lost sys.stderr\n");
return;
}
if (file == Py_None) {
+ Py_DECREF(file);
return;
}
- Py_INCREF(file);
_PyErr_Display(file, NULL, value, tb);
Py_DECREF(file);
}
@@ -1664,7 +1701,10 @@ PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
static void
flush_io_stream(PyThreadState *tstate, PyObject *name)
{
- PyObject *f = _PySys_GetAttr(tstate, name);
+ PyObject *f;
+ if (_PySys_GetOptionalAttr(name, &f) < 0) {
+ PyErr_Clear();
+ }
if (f != NULL) {
PyObject *r = _PyObject_CallMethodNoArgs(f, &_Py_ID(flush));
if (r) {
@@ -1673,6 +1713,7 @@ flush_io_stream(PyThreadState *tstate, PyObject *name)
else {
PyErr_Clear();
}
+ Py_DECREF(f);
}
}