Skip to content

Commit 1662726

Browse files
e-kwsmstefanseefeld
authored andcommitted
fix(test.pickle): fix for change in the return value of object.__reduce__()
https://docs.python.org/3.11/library/pickle.html#object.__reduce__ fix #461
1 parent 303299e commit 1662726

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

test/pickle1.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
1
1010
>>> pickle1_ext.world.__name__
1111
'world'
12-
>>> pickle1_ext.world('Hello').__reduce__()
12+
>>> pickle1_ext.world('Hello').__reduce__() # doctest: +PY310
1313
(<class 'pickle1_ext.world'>, ('Hello',))
14+
>>> pickle1_ext.world('Hello').__reduce__() # doctest: +PY311
15+
(<class 'pickle1_ext.world'>, ('Hello',), None)
1416
>>> wd = pickle1_ext.world('California')
1517
>>> pstr = pickle.dumps(wd)
1618
>>> wl = pickle.loads(pstr)
@@ -31,7 +33,27 @@ def run(args = None):
3133

3234
if args is not None:
3335
sys.argv = args
34-
return doctest.testmod(sys.modules.get(__name__))
36+
37+
# > https://docs.python.org/3.11/library/pickle.html#object.__reduce__
38+
# object.__reduce__() returns
39+
# - python 3.10 or prior: a 2-element tuple
40+
# - python 3.11 or later: a 3-element tuple (object's state added)
41+
PY310 = doctest.register_optionflag("PY310")
42+
PY311 = doctest.register_optionflag("PY311")
43+
44+
class ConditionalChecker(doctest.OutputChecker):
45+
def check_output(self, want, got, optionflags):
46+
if (optionflags & PY311) and (sys.version_info[:2] < (3, 11)):
47+
return True
48+
if (optionflags & PY310) and (sys.version_info[:2] >= (3, 11)):
49+
return True
50+
return doctest.OutputChecker.check_output(self, want, got, optionflags)
51+
52+
runner = doctest.DocTestRunner(ConditionalChecker())
53+
for test in doctest.DocTestFinder().find(sys.modules.get(__name__)):
54+
runner.run(test)
55+
56+
return doctest.TestResults(runner.failures, runner.tries)
3557

3658
if __name__ == '__main__':
3759
print("running...")

test/pickle4.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
1
1313
>>> pickle4_ext.world.__name__
1414
'world'
15-
>>> pickle4_ext.world('Hello').__reduce__()
15+
>>> pickle4_ext.world('Hello').__reduce__() # doctest: +PY310
1616
(<class 'pickle4_ext.world'>, ('Hello',))
17+
>>> pickle4_ext.world('Hello').__reduce__() # doctest: +PY311
18+
(<class 'pickle4_ext.world'>, ('Hello',), None)
1719
>>> wd = pickle4_ext.world('California')
1820
>>> pstr = pickle.dumps(wd)
1921
>>> wl = pickle.loads(pstr)
@@ -29,7 +31,27 @@ def run(args = None):
2931

3032
if args is not None:
3133
sys.argv = args
32-
return doctest.testmod(sys.modules.get(__name__))
34+
35+
# > https://docs.python.org/3.11/library/pickle.html#object.__reduce__
36+
# object.__reduce__() returns
37+
# - python 3.10 or prior: a 2-element tuple
38+
# - python 3.11 or later: a 3-element tuple (object's state added)
39+
PY310 = doctest.register_optionflag("PY310")
40+
PY311 = doctest.register_optionflag("PY311")
41+
42+
class ConditionalChecker(doctest.OutputChecker):
43+
def check_output(self, want, got, optionflags):
44+
if (optionflags & PY311) and (sys.version_info[:2] < (3, 11)):
45+
return True
46+
if (optionflags & PY310) and (sys.version_info[:2] >= (3, 11)):
47+
return True
48+
return doctest.OutputChecker.check_output(self, want, got, optionflags)
49+
50+
runner = doctest.DocTestRunner(ConditionalChecker())
51+
for test in doctest.DocTestFinder().find(sys.modules.get(__name__)):
52+
runner.run(test)
53+
54+
return doctest.TestResults(runner.failures, runner.tries)
3355

3456
if __name__ == '__main__':
3557
print("running...")

0 commit comments

Comments
 (0)