Skip to content

Commit fa59402

Browse files
authored
Merge pull request #7067 from youknowone/doctest
Update doctest,test_generators from v3.14.2 and fix generator bugs
2 parents a9995a7 + ad6e965 commit fa59402

File tree

29 files changed

+1836
-1705
lines changed

29 files changed

+1836
-1705
lines changed

Lib/doctest.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ def _test():
109109
from _colorize import ANSIColors, can_colorize
110110

111111

112+
__unittest = True
113+
112114
class TestResults(namedtuple('TestResults', 'failed attempted')):
113115
def __new__(cls, failed, attempted, *, skipped=0):
114116
results = super().__new__(cls, failed, attempted)
@@ -390,11 +392,11 @@ def __init__(self, out):
390392
# still use input() to get user input
391393
self.use_rawinput = 1
392394

393-
def set_trace(self, frame=None):
395+
def set_trace(self, frame=None, *, commands=None):
394396
self.__debugger_used = True
395397
if frame is None:
396398
frame = sys._getframe().f_back
397-
pdb.Pdb.set_trace(self, frame)
399+
pdb.Pdb.set_trace(self, frame, commands=commands)
398400

399401
def set_continue(self):
400402
# Calling set_continue unconditionally would break unit test
@@ -1230,7 +1232,7 @@ class DocTestRunner:
12301232
`OutputChecker` to the constructor.
12311233
12321234
The test runner's display output can be controlled in two ways.
1233-
First, an output function (`out) can be passed to
1235+
First, an output function (`out`) can be passed to
12341236
`TestRunner.run`; this function will be called with strings that
12351237
should be displayed. It defaults to `sys.stdout.write`. If
12361238
capturing the output is not sufficient, then the display output
@@ -1398,11 +1400,11 @@ def __run(self, test, compileflags, out):
13981400
exec(compile(example.source, filename, "single",
13991401
compileflags, True), test.globs)
14001402
self.debugger.set_continue() # ==== Example Finished ====
1401-
exception = None
1403+
exc_info = None
14021404
except KeyboardInterrupt:
14031405
raise
1404-
except:
1405-
exception = sys.exc_info()
1406+
except BaseException as exc:
1407+
exc_info = type(exc), exc, exc.__traceback__.tb_next
14061408
self.debugger.set_continue() # ==== Example Finished ====
14071409

14081410
got = self._fakeout.getvalue() # the actual output
@@ -1411,21 +1413,21 @@ def __run(self, test, compileflags, out):
14111413

14121414
# If the example executed without raising any exceptions,
14131415
# verify its output.
1414-
if exception is None:
1416+
if exc_info is None:
14151417
if check(example.want, got, self.optionflags):
14161418
outcome = SUCCESS
14171419

14181420
# The example raised an exception: check if it was expected.
14191421
else:
1420-
formatted_ex = traceback.format_exception_only(*exception[:2])
1421-
if issubclass(exception[0], SyntaxError):
1422+
formatted_ex = traceback.format_exception_only(*exc_info[:2])
1423+
if issubclass(exc_info[0], SyntaxError):
14221424
# SyntaxError / IndentationError is special:
14231425
# we don't care about the carets / suggestions / etc
14241426
# We only care about the error message and notes.
14251427
# They start with `SyntaxError:` (or any other class name)
14261428
exception_line_prefixes = (
1427-
f"{exception[0].__qualname__}:",
1428-
f"{exception[0].__module__}.{exception[0].__qualname__}:",
1429+
f"{exc_info[0].__qualname__}:",
1430+
f"{exc_info[0].__module__}.{exc_info[0].__qualname__}:",
14291431
)
14301432
exc_msg_index = next(
14311433
index
@@ -1436,7 +1438,7 @@ def __run(self, test, compileflags, out):
14361438

14371439
exc_msg = "".join(formatted_ex)
14381440
if not quiet:
1439-
got += _exception_traceback(exception)
1441+
got += _exception_traceback(exc_info)
14401442

14411443
# If `example.exc_msg` is None, then we weren't expecting
14421444
# an exception.
@@ -1465,7 +1467,7 @@ def __run(self, test, compileflags, out):
14651467
elif outcome is BOOM:
14661468
if not quiet:
14671469
self.report_unexpected_exception(out, test, example,
1468-
exception)
1470+
exc_info)
14691471
failures += 1
14701472
else:
14711473
assert False, ("unknown outcome", outcome)
@@ -2327,7 +2329,7 @@ def runTest(self):
23272329
sys.stdout = old
23282330

23292331
if results.failed:
2330-
raise self.failureException(self.format_failure(new.getvalue()))
2332+
raise self.failureException(self.format_failure(new.getvalue().rstrip('\n')))
23312333

23322334
def format_failure(self, err):
23332335
test = self._dt_test
@@ -2737,7 +2739,7 @@ def testsource(module, name):
27372739
return testsrc
27382740

27392741
def debug_src(src, pm=False, globs=None):
2740-
"""Debug a single doctest docstring, in argument `src`'"""
2742+
"""Debug a single doctest docstring, in argument `src`"""
27412743
testsrc = script_from_examples(src)
27422744
debug_script(testsrc, pm, globs)
27432745

@@ -2873,7 +2875,7 @@ def get(self):
28732875
def _test():
28742876
import argparse
28752877

2876-
parser = argparse.ArgumentParser(description="doctest runner")
2878+
parser = argparse.ArgumentParser(description="doctest runner", color=True)
28772879
parser.add_argument('-v', '--verbose', action='store_true', default=False,
28782880
help='print very verbose output for all tests')
28792881
parser.add_argument('-o', '--option', action='append',

Lib/pdb.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,15 @@ def __init__(self, completekey='tab', stdin=None, stdout=None, skip=None,
185185
self.commands_bnum = None # The breakpoint number for which we are
186186
# defining a list
187187

188+
def set_trace(self, frame=None, *, commands=None):
189+
if frame is None:
190+
frame = sys._getframe().f_back
191+
192+
if commands is not None:
193+
self.rcLines.extend(commands)
194+
195+
super().set_trace(frame)
196+
188197
def sigint_handler(self, signum, frame):
189198
if self.allow_kbdint:
190199
raise KeyboardInterrupt

Lib/test/test_concurrent_futures/test_wait.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ class ProcessPoolForkWaitTest(ProcessPoolForkWaitTest): # TODO: RUSTPYTHON
205205
def test_first_completed(self): super().test_first_completed() # TODO: RUSTPYTHON
206206
@unittest.skipIf(sys.platform == 'linux', "TODO: RUSTPYTHON Fatal Python error: Segmentation fault")
207207
def test_first_completed_some_already_completed(self): super().test_first_completed_some_already_completed() # TODO: RUSTPYTHON
208-
@unittest.skipIf(sys.platform == 'linux', "TODO: RUSTPYTHON flaky")
208+
@unittest.skipIf(sys.platform != 'win32', "TODO: RUSTPYTHON flaky")
209209
def test_first_exception(self): super().test_first_exception() # TODO: RUSTPYTHON
210210
@unittest.skipIf(sys.platform == 'linux', "TODO: RUSTPYTHON flaky")
211211
def test_first_exception_one_already_failed(self): super().test_first_exception_one_already_failed() # TODO: RUSTPYTHON

Lib/test/test_dbm.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Test script for the dbm.open function based on testdumbdbm.py"""
22

3+
import sys
34
import unittest
45
import dbm
56
import os
@@ -252,10 +253,13 @@ def setUp(self):
252253
assert mod.__name__.startswith('dbm.')
253254
suffix = mod.__name__[4:]
254255
testname = f'TestCase_{suffix}'
255-
globals()[testname] = type(testname,
256-
(AnyDBMTestCase, unittest.TestCase),
257-
{'module': mod})
258-
256+
cls = type(testname,
257+
(AnyDBMTestCase, unittest.TestCase),
258+
{'module': mod})
259+
# TODO: RUSTPYTHON; sqlite3 file locking prevents cleanup on Windows
260+
if suffix == 'sqlite3' and sys.platform == 'win32':
261+
cls = unittest.skip("TODO: RUSTPYTHON; sqlite3 file locking on Windows")(cls)
262+
globals()[testname] = cls
259263

260264
if __name__ == "__main__":
261265
unittest.main()

0 commit comments

Comments
 (0)