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

Skip to content

Commit 1543a22

Browse files
author
Hirokazu Yamamoto
committed
Blocked revisions 67002 via svnmerge
........ r67002 | hirokazu.yamamoto | 2008-10-23 09:37:33 +0900 | 1 line Issue #4183: Some tests didn't run with pickle.HIGHEST_PROTOCOL. ........
1 parent c1de4cc commit 1543a22

2 files changed

Lines changed: 21 additions & 8 deletions

File tree

Lib/pickle.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,9 @@ def save_pers(self, pid):
345345
else:
346346
self.write(PERSID + str(pid).encode("ascii") + b'\n')
347347

348+
def _isiter(self, obj):
349+
return hasattr(obj, '__next__') and hasattr(obj, '__iter__')
350+
348351
def save_reduce(self, func, args, state=None,
349352
listitems=None, dictitems=None, obj=None):
350353
# This API is called by some subclasses
@@ -357,6 +360,16 @@ def save_reduce(self, func, args, state=None,
357360
if not hasattr(func, '__call__'):
358361
raise PicklingError("func from save_reduce() should be callable")
359362

363+
# Assert that listitems is an iterator
364+
if listitems is not None and not self._isiter(listitems):
365+
raise PicklingError("listitems from save_reduce() should be an "
366+
"iterator")
367+
368+
# Assert that dictitems is an iterator
369+
if dictitems is not None and not self._isiter(dictitems):
370+
raise PicklingError("dictitems from save_reduce() should be an "
371+
"iterator")
372+
360373
save = self.save
361374
write = self.write
362375

Modules/_pickle.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1963,21 +1963,13 @@ save_reduce(PicklerObject *self, PyObject *args, PyObject *obj)
19631963
PyObject *state = NULL;
19641964
PyObject *listitems = Py_None;
19651965
PyObject *dictitems = Py_None;
1966-
Py_ssize_t size;
19671966

19681967
int use_newobj = self->proto >= 2;
19691968

19701969
const char reduce_op = REDUCE;
19711970
const char build_op = BUILD;
19721971
const char newobj_op = NEWOBJ;
19731972

1974-
size = PyTuple_Size(args);
1975-
if (size < 2 || size > 5) {
1976-
PyErr_SetString(PicklingError, "tuple returned by "
1977-
"__reduce__ must contain 2 through 5 elements");
1978-
return -1;
1979-
}
1980-
19811973
if (!PyArg_UnpackTuple(args, "save_reduce", 2, 5,
19821974
&callable, &argtup, &state, &listitems, &dictitems))
19831975
return -1;
@@ -2154,6 +2146,7 @@ save(PicklerObject *self, PyObject *obj, int pers_save)
21542146
PyObject *reduce_value = NULL;
21552147
PyObject *memo_key = NULL;
21562148
int status = 0;
2149+
Py_ssize_t size;
21572150

21582151
if (Py_EnterRecursiveCall(" while pickling an object") < 0)
21592152
return -1;
@@ -2332,6 +2325,13 @@ save(PicklerObject *self, PyObject *obj, int pers_save)
23322325
goto error;
23332326
}
23342327

2328+
size = PyTuple_Size(reduce_value);
2329+
if (size < 2 || size > 5) {
2330+
PyErr_SetString(PicklingError, "tuple returned by "
2331+
"__reduce__ must contain 2 through 5 elements");
2332+
goto error;
2333+
}
2334+
23352335
status = save_reduce(self, reduce_value, obj);
23362336

23372337
if (0) {

0 commit comments

Comments
 (0)