1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
https://github.com/boostorg/python/pull/482
From: Eisuke Kawashima <e-kwsm@users.noreply.github.com>
Date: Mon, 31 Mar 2025 19:49:28 +0900
Subject: [PATCH] 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
---
test/pickle1.py | 26 ++++++++++++++++++++++++--
test/pickle4.py | 26 ++++++++++++++++++++++++--
2 files changed, 48 insertions(+), 4 deletions(-)
diff --git a/libs/python/test/pickle1.py b/libs/python/test/pickle1.py
index b8f4efd9b0..0df59a4b3a 100644
--- a/libs/python/test/pickle1.py
+++ b/libs/python/test/pickle1.py
@@ -9,8 +9,10 @@
1
>>> pickle1_ext.world.__name__
'world'
- >>> pickle1_ext.world('Hello').__reduce__()
+ >>> pickle1_ext.world('Hello').__reduce__() # doctest: +PY310
(<class 'pickle1_ext.world'>, ('Hello',))
+ >>> pickle1_ext.world('Hello').__reduce__() # doctest: +PY311
+ (<class 'pickle1_ext.world'>, ('Hello',), None)
>>> wd = pickle1_ext.world('California')
>>> pstr = pickle.dumps(wd)
>>> wl = pickle.loads(pstr)
@@ -31,7 +33,27 @@ def run(args = None):
if args is not None:
sys.argv = args
- return doctest.testmod(sys.modules.get(__name__))
+
+ # > https://docs.python.org/3.11/library/pickle.html#object.__reduce__
+ # object.__reduce__() returns
+ # - python 3.10 or prior: a 2-element tuple
+ # - python 3.11 or later: a 3-element tuple (object's state added)
+ PY310 = doctest.register_optionflag("PY310")
+ PY311 = doctest.register_optionflag("PY311")
+
+ class ConditionalChecker(doctest.OutputChecker):
+ def check_output(self, want, got, optionflags):
+ if (optionflags & PY311) and (sys.version_info[:2] < (3, 11)):
+ return True
+ if (optionflags & PY310) and (sys.version_info[:2] >= (3, 11)):
+ return True
+ return doctest.OutputChecker.check_output(self, want, got, optionflags)
+
+ runner = doctest.DocTestRunner(ConditionalChecker())
+ for test in doctest.DocTestFinder().find(sys.modules.get(__name__)):
+ runner.run(test)
+
+ return doctest.TestResults(runner.failures, runner.tries)
if __name__ == '__main__':
print("running...")
diff --git a/libs/python/test/pickle4.py b/libs/python/test/pickle4.py
index be813bbb13..3cf4d7241f 100644
--- a/libs/python/test/pickle4.py
+++ b/libs/python/test/pickle4.py
@@ -12,8 +12,10 @@
1
>>> pickle4_ext.world.__name__
'world'
- >>> pickle4_ext.world('Hello').__reduce__()
+ >>> pickle4_ext.world('Hello').__reduce__() # doctest: +PY310
(<class 'pickle4_ext.world'>, ('Hello',))
+ >>> pickle4_ext.world('Hello').__reduce__() # doctest: +PY311
+ (<class 'pickle4_ext.world'>, ('Hello',), None)
>>> wd = pickle4_ext.world('California')
>>> pstr = pickle.dumps(wd)
>>> wl = pickle.loads(pstr)
@@ -29,7 +31,27 @@ def run(args = None):
if args is not None:
sys.argv = args
- return doctest.testmod(sys.modules.get(__name__))
+
+ # > https://docs.python.org/3.11/library/pickle.html#object.__reduce__
+ # object.__reduce__() returns
+ # - python 3.10 or prior: a 2-element tuple
+ # - python 3.11 or later: a 3-element tuple (object's state added)
+ PY310 = doctest.register_optionflag("PY310")
+ PY311 = doctest.register_optionflag("PY311")
+
+ class ConditionalChecker(doctest.OutputChecker):
+ def check_output(self, want, got, optionflags):
+ if (optionflags & PY311) and (sys.version_info[:2] < (3, 11)):
+ return True
+ if (optionflags & PY310) and (sys.version_info[:2] >= (3, 11)):
+ return True
+ return doctest.OutputChecker.check_output(self, want, got, optionflags)
+
+ runner = doctest.DocTestRunner(ConditionalChecker())
+ for test in doctest.DocTestFinder().find(sys.modules.get(__name__)):
+ runner.run(test)
+
+ return doctest.TestResults(runner.failures, runner.tries)
if __name__ == '__main__':
print("running...")
|