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

Skip to content

Commit dff1834

Browse files
committed
Fixed _pickle to use Py_EnterRecursiveCall().
1 parent 2bc91df commit dff1834

1 file changed

Lines changed: 3 additions & 11 deletions

File tree

Modules/_pickle.c

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -304,9 +304,6 @@ typedef struct PicklerObject {
304304
PyObject *arg;
305305
int proto; /* Pickle protocol number, >= 0 */
306306
int bin; /* Boolean, true if proto > 0 */
307-
int nesting; /* Current nesting level, this is to guard
308-
save() from going into infinite recursion
309-
and segfaulting. */
310307
int buf_size; /* Size of the current buffered pickle data */
311308
char *write_buf; /* Write buffer, this is to avoid calling the
312309
write() method of the output stream too
@@ -2075,12 +2072,8 @@ save(PicklerObject *self, PyObject *obj, int pers_save)
20752072
PyObject *memo_key = NULL;
20762073
int status = 0;
20772074

2078-
/* XXX: Use Py_EnterRecursiveCall()? */
2079-
if (++self->nesting > Py_GetRecursionLimit()) {
2080-
PyErr_SetString(PyExc_RuntimeError,
2081-
"maximum recursion depth exceeded");
2082-
goto error;
2083-
}
2075+
if (Py_EnterRecursiveCall(" while pickling an object") < 0)
2076+
return -1;
20842077

20852078
/* The extra pers_save argument is necessary to avoid calling save_pers()
20862079
on its returned object. */
@@ -2273,7 +2266,7 @@ save(PicklerObject *self, PyObject *obj, int pers_save)
22732266
status = -1;
22742267
}
22752268
done:
2276-
self->nesting--;
2269+
Py_LeaveRecursiveCall();
22772270
Py_XDECREF(memo_key);
22782271
Py_XDECREF(reduce_func);
22792272
Py_XDECREF(reduce_value);
@@ -2440,7 +2433,6 @@ Pickler_init(PicklerObject *self, PyObject *args, PyObject *kwds)
24402433
self->proto = proto;
24412434
self->bin = proto > 0;
24422435
self->arg = NULL;
2443-
self->nesting = 0;
24442436
self->fast = 0;
24452437
self->fast_nesting = 0;
24462438
self->fast_memo = NULL;

0 commit comments

Comments
 (0)