diff --git a/Include/internal/pycore_traceback.h b/Include/internal/pycore_traceback.h index 10922bff98bd4b..82529ffec3c113 100644 --- a/Include/internal/pycore_traceback.h +++ b/Include/internal/pycore_traceback.h @@ -8,6 +8,9 @@ extern "C" { # error "this header requires Py_BUILD_CORE define" #endif +/* Maximum number of error tracebacks to print */ +#define PyTraceBack_LIMIT 1000 + // Export for '_ctypes' shared extension PyAPI_FUNC(int) _Py_DisplaySourceLine(PyObject *, PyObject *, int, int, int *, PyObject **); diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py index 9689ef8e96e072..bdb3c1db287168 100644 --- a/Lib/test/test_sys.py +++ b/Lib/test/test_sys.py @@ -1187,6 +1187,9 @@ def check(tracebacklimit, expected): b' ~~^~~', b'ZeroDivisionError: division by zero' ] + self.assertTrue(hasattr(sys, "tracebacklimit"), + 'tracebacklimit attribute does not exist') + self.assertEqual(sys.tracebacklimit, 1_000) check(10, traceback) check(3, traceback) check(2, traceback[:1] + traceback[4:]) diff --git a/Misc/NEWS.d/next/Library/2024-10-20-11-45-50.gh-issue-123596.HONVYS.rst b/Misc/NEWS.d/next/Library/2024-10-20-11-45-50.gh-issue-123596.HONVYS.rst new file mode 100644 index 00000000000000..1d4444e6038a14 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-10-20-11-45-50.gh-issue-123596.HONVYS.rst @@ -0,0 +1 @@ +Fix missing :attr:`sys.tracebacklimit` in :mod:`sys`. Patch by RUANG. diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 8b9209324002ce..3d0953480a6c2c 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -35,6 +35,7 @@ Data members: #include "pycore_structseq.h" // _PyStructSequence_InitBuiltinWithFlags() #include "pycore_sysmodule.h" // export _PySys_GetSizeOf() #include "pycore_tuple.h" // _PyTuple_FromArray() +#include "pycore_traceback.h" // PyTraceBack_LIMIT #include "pydtrace.h" // PyDTrace_AUDIT() #include "osdefs.h" // DELIM @@ -3666,6 +3667,8 @@ _PySys_UpdateConfig(PyThreadState *tstate) COPY_LIST("path", config->module_search_paths); } + SET_SYS("tracebacklimit", PyLong_FromLong(PyTraceBack_LIMIT)); + COPY_WSTR("executable", config->executable); COPY_WSTR("_base_executable", config->base_executable); COPY_WSTR("prefix", config->prefix); diff --git a/Python/traceback.c b/Python/traceback.c index 47b77c9108dd9a..69836d0a5bb379 100644 --- a/Python/traceback.c +++ b/Python/traceback.c @@ -704,8 +704,6 @@ tb_printinternal(PyTracebackObject *tb, PyObject *f, long limit) return -1; } -#define PyTraceBack_LIMIT 1000 - int _PyTraceBack_Print(PyObject *v, const char *header, PyObject *f) {