Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0fbab7f

Browse files
committed
Removed another occurrence of PyInt_ExactCheck()
I've modified the semantic of PyTraceBack_Print and sys.tracebacklimit slightly. Overfloats or values <= 0 are replaced with a default value to avoid infinite recursion and other issues.
1 parent 3267f87 commit 0fbab7f

1 file changed

Lines changed: 23 additions & 4 deletions

File tree

Python/traceback.c

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,23 +242,42 @@ tb_printinternal(PyTracebackObject *tb, PyObject *f, int limit)
242242
return err;
243243
}
244244

245+
#define PyTraceBack_LIMIT 1000
246+
245247
int
246248
PyTraceBack_Print(PyObject *v, PyObject *f)
247249
{
248250
int err;
249251
PyObject *limitv;
250-
int limit = 1000;
252+
int limit = PyTraceBack_LIMIT;
253+
251254
if (v == NULL)
252255
return 0;
253256
if (!PyTraceBack_Check(v)) {
254257
PyErr_BadInternalCall();
255258
return -1;
256259
}
257260
limitv = PySys_GetObject("tracebacklimit");
258-
if (limitv && PyInt_CheckExact(limitv)) {
261+
if (limitv) {
262+
PyObject *exc_type, *exc_value, *exc_tb;
263+
264+
PyErr_Fetch(&exc_type, &exc_value, &exc_tb);
259265
limit = PyLong_AsLong(limitv);
260-
if (limit <= 0)
261-
return 0;
266+
if (limit == -1 && PyErr_Occurred()) {
267+
if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
268+
limit = PyTraceBack_LIMIT;
269+
}
270+
else {
271+
Py_XDECREF(exc_type);
272+
Py_XDECREF(exc_value);
273+
Py_XDECREF(exc_tb);
274+
return 0;
275+
}
276+
}
277+
else if (limit <= 0) {
278+
limit = PyTraceBack_LIMIT;
279+
}
280+
PyErr_Restore(exc_type, exc_value, exc_tb);
262281
}
263282
err = PyFile_WriteString("Traceback (most recent call last):\n", f);
264283
if (!err)

0 commit comments

Comments
 (0)