summaryrefslogtreecommitdiffstats
path: root/contrib/tools/python3/Modules/faulthandler.c
diff options
context:
space:
mode:
authorshadchin <[email protected]>2025-06-13 00:05:26 +0300
committershadchin <[email protected]>2025-06-13 00:35:30 +0300
commit796b9088366b10b4cd42885101fc20c0b5709b07 (patch)
treef287eacb0b95ffd7cabf95b16cafb4788645dc38 /contrib/tools/python3/Modules/faulthandler.c
parentc72bca862651e507d2ff4980ef7f4ff7267a7227 (diff)
Update Python 3 to 3.12.10
commit_hash:dd2398e159fe1d72ea6b12da52fccc933a41a785
Diffstat (limited to 'contrib/tools/python3/Modules/faulthandler.c')
-rw-r--r--contrib/tools/python3/Modules/faulthandler.c45
1 files changed, 31 insertions, 14 deletions
diff --git a/contrib/tools/python3/Modules/faulthandler.c b/contrib/tools/python3/Modules/faulthandler.c
index 96cc6d8ca11..2f2b00787ed 100644
--- a/contrib/tools/python3/Modules/faulthandler.c
+++ b/contrib/tools/python3/Modules/faulthandler.c
@@ -3,6 +3,7 @@
#include "pycore_pyerrors.h" // _Py_DumpExtensionModules
#include "pycore_pystate.h" // _PyThreadState_GET()
#include "pycore_signal.h" // Py_NSIG
+#include "pycore_sysmodule.h" // _PySys_GetRequiredAttr()
#include "pycore_traceback.h" // _Py_DumpTracebackThreads
#include <object.h>
@@ -29,14 +30,15 @@
#define PUTS(fd, str) _Py_write_noraise(fd, str, strlen(str))
-// clang uses __attribute__((no_sanitize("undefined")))
-// GCC 4.9+ uses __attribute__((no_sanitize_undefined))
-#if defined(__has_feature) // Clang
+// Clang and GCC 9.0+ use __attribute__((no_sanitize("undefined")))
+#if defined(__has_feature)
# if __has_feature(undefined_behavior_sanitizer)
# define _Py_NO_SANITIZE_UNDEFINED __attribute__((no_sanitize("undefined")))
# endif
#endif
-#if defined(__GNUC__) \
+
+// GCC 4.9+ uses __attribute__((no_sanitize_undefined))
+#if !defined(_Py_NO_SANITIZE_UNDEFINED) && defined(__GNUC__) \
&& ((__GNUC__ >= 5) || (__GNUC__ == 4) && (__GNUC_MINOR__ >= 9))
# define _Py_NO_SANITIZE_UNDEFINED __attribute__((no_sanitize_undefined))
#endif
@@ -103,14 +105,13 @@ faulthandler_get_fileno(PyObject **file_ptr)
PyObject *file = *file_ptr;
if (file == NULL || file == Py_None) {
- PyThreadState *tstate = _PyThreadState_GET();
- file = _PySys_GetAttr(tstate, &_Py_ID(stderr));
+ file = _PySys_GetRequiredAttr(&_Py_ID(stderr));
if (file == NULL) {
- PyErr_SetString(PyExc_RuntimeError, "unable to get sys.stderr");
return -1;
}
if (file == Py_None) {
PyErr_SetString(PyExc_RuntimeError, "sys.stderr is None");
+ Py_DECREF(file);
return -1;
}
}
@@ -126,10 +127,15 @@ faulthandler_get_fileno(PyObject **file_ptr)
*file_ptr = NULL;
return fd;
}
+ else {
+ Py_INCREF(file);
+ }
result = PyObject_CallMethodNoArgs(file, &_Py_ID(fileno));
- if (result == NULL)
+ if (result == NULL) {
+ Py_DECREF(file);
return -1;
+ }
fd = -1;
if (PyLong_Check(result)) {
@@ -142,6 +148,7 @@ faulthandler_get_fileno(PyObject **file_ptr)
if (fd == -1) {
PyErr_SetString(PyExc_RuntimeError,
"file.fileno() is not a valid file descriptor");
+ Py_DECREF(file);
return -1;
}
@@ -224,19 +231,23 @@ faulthandler_dump_traceback_py(PyObject *self,
return NULL;
tstate = get_thread_state();
- if (tstate == NULL)
+ if (tstate == NULL) {
+ Py_XDECREF(file);
return NULL;
+ }
if (all_threads) {
errmsg = _Py_DumpTracebackThreads(fd, NULL, tstate);
if (errmsg != NULL) {
PyErr_SetString(PyExc_RuntimeError, errmsg);
+ Py_XDECREF(file);
return NULL;
}
}
else {
_Py_DumpTraceback(fd, tstate);
}
+ Py_XDECREF(file);
if (PyErr_CheckSignals())
return NULL;
@@ -498,10 +509,11 @@ faulthandler_py_enable(PyObject *self, PyObject *args, PyObject *kwargs)
return NULL;
tstate = get_thread_state();
- if (tstate == NULL)
+ if (tstate == NULL) {
+ Py_XDECREF(file);
return NULL;
+ }
- Py_XINCREF(file);
Py_XSETREF(fatal_error.file, file);
fatal_error.fd = fd;
fatal_error.all_threads = all_threads;
@@ -691,12 +703,14 @@ faulthandler_dump_traceback_later(PyObject *self,
if (!thread.running) {
thread.running = PyThread_allocate_lock();
if (!thread.running) {
+ Py_XDECREF(file);
return PyErr_NoMemory();
}
}
if (!thread.cancel_event) {
thread.cancel_event = PyThread_allocate_lock();
if (!thread.cancel_event || !thread.running) {
+ Py_XDECREF(file);
return PyErr_NoMemory();
}
@@ -708,6 +722,7 @@ faulthandler_dump_traceback_later(PyObject *self,
/* format the timeout */
header = format_timeout(timeout_us);
if (header == NULL) {
+ Py_XDECREF(file);
return PyErr_NoMemory();
}
header_len = strlen(header);
@@ -715,7 +730,6 @@ faulthandler_dump_traceback_later(PyObject *self,
/* Cancel previous thread, if running */
cancel_dump_traceback_later();
- Py_XINCREF(file);
Py_XSETREF(thread.file, file);
thread.fd = fd;
/* the downcast is safe: we check that 0 < timeout_us < PY_TIMEOUT_MAX */
@@ -877,14 +891,17 @@ faulthandler_register_py(PyObject *self,
if (user_signals == NULL) {
user_signals = PyMem_Calloc(Py_NSIG, sizeof(user_signal_t));
- if (user_signals == NULL)
+ if (user_signals == NULL) {
+ Py_XDECREF(file);
return PyErr_NoMemory();
+ }
}
user = &user_signals[signum];
if (!user->enabled) {
#ifdef FAULTHANDLER_USE_ALT_STACK
if (faulthandler_allocate_stack() < 0) {
+ Py_XDECREF(file);
return NULL;
}
#endif
@@ -892,13 +909,13 @@ faulthandler_register_py(PyObject *self,
err = faulthandler_register(signum, chain, &previous);
if (err) {
PyErr_SetFromErrno(PyExc_OSError);
+ Py_XDECREF(file);
return NULL;
}
user->previous = previous;
}
- Py_XINCREF(file);
Py_XSETREF(user->file, file);
user->fd = fd;
user->all_threads = all_threads;