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
3658if __name__ == '__main__' :
3759 print ("running..." )
0 commit comments