mirror of https://github.com/python/cpython
gh-60203: Revert changes in cycle.__setstate__ (#99982)
In case if only True/False be supported as boolean arguments in future, we should continue to support 1/0 here.
This commit is contained in:
parent
2488c1e1b6
commit
922a6cf6c2
|
@ -675,6 +675,7 @@ class TestBasicOps(unittest.TestCase):
|
|||
self.assertRaises(TypeError, cycle, 5)
|
||||
self.assertEqual(list(islice(cycle(gen3()),10)), [0,1,2,0,1,2,0,1,2,0])
|
||||
|
||||
def test_cycle_copy_pickle(self):
|
||||
# check copy, deepcopy, pickle
|
||||
c = cycle('abc')
|
||||
self.assertEqual(next(c), 'a')
|
||||
|
@ -710,6 +711,37 @@ class TestBasicOps(unittest.TestCase):
|
|||
d = pickle.loads(p) # rebuild the cycle object
|
||||
self.assertEqual(take(20, d), list('cdeabcdeabcdeabcdeab'))
|
||||
|
||||
def test_cycle_unpickle_compat(self):
|
||||
testcases = [
|
||||
b'citertools\ncycle\n(c__builtin__\niter\n((lI1\naI2\naI3\natRI1\nbtR((lI1\naI0\ntb.',
|
||||
b'citertools\ncycle\n(c__builtin__\niter\n(](K\x01K\x02K\x03etRK\x01btR(]K\x01aK\x00tb.',
|
||||
b'\x80\x02citertools\ncycle\nc__builtin__\niter\n](K\x01K\x02K\x03e\x85RK\x01b\x85R]K\x01aK\x00\x86b.',
|
||||
b'\x80\x03citertools\ncycle\ncbuiltins\niter\n](K\x01K\x02K\x03e\x85RK\x01b\x85R]K\x01aK\x00\x86b.',
|
||||
b'\x80\x04\x95=\x00\x00\x00\x00\x00\x00\x00\x8c\titertools\x8c\x05cycle\x93\x8c\x08builtins\x8c\x04iter\x93](K\x01K\x02K\x03e\x85RK\x01b\x85R]K\x01aK\x00\x86b.',
|
||||
|
||||
b'citertools\ncycle\n(c__builtin__\niter\n((lp0\nI1\naI2\naI3\natRI1\nbtR(g0\nI1\ntb.',
|
||||
b'citertools\ncycle\n(c__builtin__\niter\n(]q\x00(K\x01K\x02K\x03etRK\x01btR(h\x00K\x01tb.',
|
||||
b'\x80\x02citertools\ncycle\nc__builtin__\niter\n]q\x00(K\x01K\x02K\x03e\x85RK\x01b\x85Rh\x00K\x01\x86b.',
|
||||
b'\x80\x03citertools\ncycle\ncbuiltins\niter\n]q\x00(K\x01K\x02K\x03e\x85RK\x01b\x85Rh\x00K\x01\x86b.',
|
||||
b'\x80\x04\x95<\x00\x00\x00\x00\x00\x00\x00\x8c\titertools\x8c\x05cycle\x93\x8c\x08builtins\x8c\x04iter\x93]\x94(K\x01K\x02K\x03e\x85RK\x01b\x85Rh\x00K\x01\x86b.',
|
||||
|
||||
b'citertools\ncycle\n(c__builtin__\niter\n((lI1\naI2\naI3\natRI1\nbtR((lI1\naI00\ntb.',
|
||||
b'citertools\ncycle\n(c__builtin__\niter\n(](K\x01K\x02K\x03etRK\x01btR(]K\x01aI00\ntb.',
|
||||
b'\x80\x02citertools\ncycle\nc__builtin__\niter\n](K\x01K\x02K\x03e\x85RK\x01b\x85R]K\x01a\x89\x86b.',
|
||||
b'\x80\x03citertools\ncycle\ncbuiltins\niter\n](K\x01K\x02K\x03e\x85RK\x01b\x85R]K\x01a\x89\x86b.',
|
||||
b'\x80\x04\x95<\x00\x00\x00\x00\x00\x00\x00\x8c\titertools\x8c\x05cycle\x93\x8c\x08builtins\x8c\x04iter\x93](K\x01K\x02K\x03e\x85RK\x01b\x85R]K\x01a\x89\x86b.',
|
||||
|
||||
b'citertools\ncycle\n(c__builtin__\niter\n((lp0\nI1\naI2\naI3\natRI1\nbtR(g0\nI01\ntb.',
|
||||
b'citertools\ncycle\n(c__builtin__\niter\n(]q\x00(K\x01K\x02K\x03etRK\x01btR(h\x00I01\ntb.',
|
||||
b'\x80\x02citertools\ncycle\nc__builtin__\niter\n]q\x00(K\x01K\x02K\x03e\x85RK\x01b\x85Rh\x00\x88\x86b.',
|
||||
b'\x80\x03citertools\ncycle\ncbuiltins\niter\n]q\x00(K\x01K\x02K\x03e\x85RK\x01b\x85Rh\x00\x88\x86b.',
|
||||
b'\x80\x04\x95;\x00\x00\x00\x00\x00\x00\x00\x8c\titertools\x8c\x05cycle\x93\x8c\x08builtins\x8c\x04iter\x93]\x94(K\x01K\x02K\x03e\x85RK\x01b\x85Rh\x00\x88\x86b.',
|
||||
]
|
||||
assert len(testcases) == 20
|
||||
for t in testcases:
|
||||
it = pickle.loads(t)
|
||||
self.assertEqual(take(10, it), [2, 3, 1, 2, 3, 1, 2, 3, 1, 2])
|
||||
|
||||
def test_cycle_setstate(self):
|
||||
# Verify both modes for restoring state
|
||||
|
||||
|
@ -739,6 +771,10 @@ class TestBasicOps(unittest.TestCase):
|
|||
c.__setstate__((tuple('defg'), 0))
|
||||
take(20, c)
|
||||
|
||||
# The second argument in the setstate tuple must be an int
|
||||
with self.assertRaises(TypeError):
|
||||
cycle('defg').__setstate__((list('abcdefg'), 'x'))
|
||||
|
||||
self.assertRaises(TypeError, cycle('').__setstate__, ())
|
||||
self.assertRaises(TypeError, cycle('').__setstate__, ([],))
|
||||
|
||||
|
|
|
@ -1368,12 +1368,13 @@ cycle_setstate(cycleobject *lz, PyObject *state)
|
|||
PyErr_SetString(PyExc_TypeError, "state is not a tuple");
|
||||
return NULL;
|
||||
}
|
||||
if (!PyArg_ParseTuple(state, "O!p", &PyList_Type, &saved, &firstpass)) {
|
||||
// The second item can be 1/0 in old pickles and True/False in new pickles
|
||||
if (!PyArg_ParseTuple(state, "O!i", &PyList_Type, &saved, &firstpass)) {
|
||||
return NULL;
|
||||
}
|
||||
Py_INCREF(saved);
|
||||
Py_XSETREF(lz->saved, saved);
|
||||
lz->firstpass = firstpass;
|
||||
lz->firstpass = firstpass != 0;
|
||||
lz->index = 0;
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue