Skip to content

Commit 91449f7

Browse files
author
Matthias Koeppe
committed
Refactor through new function DocTestController.source_baseline
1 parent e944a54 commit 91449f7

File tree

3 files changed

+48
-32
lines changed

3 files changed

+48
-32
lines changed

src/sage/doctest/control.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,6 +1097,34 @@ def sort_key(source):
10971097
return -self.stats.get(basename, default).get('walltime', 0), basename
10981098
self.sources = sorted(self.sources, key=sort_key)
10991099

1100+
def source_baseline(self, source):
1101+
r"""
1102+
Return the ``baseline_stats`` value of ``source``.
1103+
1104+
INPUT:
1105+
1106+
- ``source`` -- a :class:`DocTestSource` instance
1107+
1108+
OUTPUT:
1109+
1110+
A dictionary.
1111+
1112+
EXAMPLES::
1113+
1114+
sage: from sage.doctest.control import DocTestDefaults, DocTestController
1115+
sage: from sage.env import SAGE_SRC
1116+
sage: import os
1117+
sage: filename = os.path.join(SAGE_SRC,'sage','doctest','util.py')
1118+
sage: DD = DocTestDefaults()
1119+
sage: DC = DocTestController(DD, [filename])
1120+
sage: DC.source_baseline(DC.sources[0])
1121+
{}
1122+
"""
1123+
if self.baseline_stats:
1124+
basename = source.basename
1125+
return self.baseline_stats.get(basename, {})
1126+
return {}
1127+
11001128
def run_doctests(self):
11011129
"""
11021130
Actually runs the doctests.

src/sage/doctest/forker.py

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -522,8 +522,7 @@ def __init__(self, *args, **kwds):
522522
- ``optionflags`` -- Controls the comparison with the expected
523523
output. See :mod:`testmod` for more information.
524524
525-
- ``baseline`` -- ``None`` or a dictionary, the ``baseline_stats``
526-
value
525+
- ``baseline`` -- dictionary, the ``baseline_stats`` value
527526
528527
EXAMPLES::
529528
@@ -538,9 +537,7 @@ def __init__(self, *args, **kwds):
538537
O = kwds.pop('outtmpfile', None)
539538
self.msgfile = kwds.pop('msgfile', None)
540539
self.options = kwds.pop('sage_options')
541-
self.baseline = kwds.pop('baseline', None)
542-
if self.baseline is None:
543-
self.baseline = {}
540+
self.baseline = kwds.pop('baseline', {})
544541
doctest.DocTestRunner.__init__(self, *args, **kwds)
545542
self._fakeout = SageSpoofInOut(O)
546543
if self.msgfile is None:
@@ -1727,20 +1724,16 @@ def serial_dispatch(self):
17271724
"""
17281725
for source in self.controller.sources:
17291726
heading = self.controller.reporter.report_head(source)
1730-
basename = source.basename
1731-
if self.controller.baseline_stats:
1732-
the_baseline_stats = self.controller.baseline_stats.get(basename, {})
1733-
else:
1734-
the_baseline_stats = {}
1735-
if the_baseline_stats.get('failed', False):
1727+
baseline = self.controller.source_baseline(source)
1728+
if baseline.get('failed', False):
17361729
heading += " # [failed in baseline]"
17371730
if not self.controller.options.only_errors:
17381731
self.controller.log(heading)
17391732

17401733
with tempfile.TemporaryFile() as outtmpfile:
17411734
result = DocTestTask(source)(self.controller.options,
17421735
outtmpfile, self.controller.logger,
1743-
baseline=the_baseline_stats)
1736+
baseline=baseline)
17441737
outtmpfile.seek(0)
17451738
output = bytes_to_str(outtmpfile.read())
17461739

@@ -1999,16 +1992,12 @@ def sel_exit():
19991992
# Start a new worker.
20001993
import copy
20011994
worker_options = copy.copy(opt)
2002-
basename = source.basename
2003-
if self.controller.baseline_stats:
2004-
the_baseline_stats = self.controller.baseline_stats.get(basename, {})
2005-
else:
2006-
the_baseline_stats = {}
1995+
baseline = self.controller.source_baseline(source)
20071996
if target_endtime is not None:
20081997
worker_options.target_walltime = (target_endtime - now) / (max(1, pending_tests / opt.nthreads))
2009-
w = DocTestWorker(source, options=worker_options, baseline=the_baseline_stats, funclist=[sel_exit])
1998+
w = DocTestWorker(source, options=worker_options, funclist=[sel_exit], baseline=baseline)
20101999
heading = self.controller.reporter.report_head(w.source)
2011-
if the_baseline_stats.get('failed', False):
2000+
if baseline.get('failed', False):
20122001
heading += " # [failed in baseline]"
20132002
if not self.controller.options.only_errors:
20142003
w.messages = heading + "\n"
@@ -2149,6 +2138,8 @@ class should be accessed by the child process.
21492138
- ``funclist`` -- a list of callables to be called at the start of
21502139
the child process.
21512140
2141+
- ``baseline`` -- dictionary, the ``baseline_stats`` value
2142+
21522143
EXAMPLES::
21532144
21542145
sage: from sage.doctest.forker import DocTestWorker, DocTestTask
@@ -2264,7 +2255,8 @@ def run(self):
22642255
os.close(self.rmessages)
22652256
msgpipe = os.fdopen(self.wmessages, "w")
22662257
try:
2267-
task(self.options, self.outtmpfile, msgpipe, self.result_queue, baseline=self.baseline)
2258+
task(self.options, self.outtmpfile, msgpipe, self.result_queue,
2259+
baseline=self.baseline)
22682260
finally:
22692261
msgpipe.close()
22702262
self.outtmpfile.close()
@@ -2548,8 +2540,7 @@ def __call__(self, options, outtmpfile=None, msgfile=None, result_queue=None, *,
25482540
- ``result_queue`` -- an instance of :class:`multiprocessing.Queue`
25492541
to store the doctest result. For testing, this can also be None.
25502542
2551-
- ``baseline`` -- ``None`` or a dictionary, the ``baseline_stats``
2552-
value.
2543+
- ``baseline`` -- a dictionary, the ``baseline_stats`` value.
25532544
25542545
OUTPUT:
25552546

src/sage/doctest/reporting.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -399,10 +399,7 @@ def report(self, source, timeout, return_code, results, output, pid=None):
399399
postscript = self.postscript
400400
stats = self.stats
401401
basename = source.basename
402-
if self.controller.baseline_stats:
403-
the_baseline_stats = self.controller.baseline_stats.get(basename, {})
404-
else:
405-
the_baseline_stats = {}
402+
baseline = self.controller.source_baseline(source)
406403
cmd = self.report_head(source)
407404
try:
408405
ntests, result_dict = results
@@ -423,14 +420,14 @@ def report(self, source, timeout, return_code, results, output, pid=None):
423420
fail_msg += " (and interrupt failed)"
424421
else:
425422
fail_msg += " (with %s after interrupt)" % signal_name(sig)
426-
if the_baseline_stats.get('failed', False):
423+
if baseline.get('failed', False):
427424
fail_msg += " [failed in baseline]"
428425
log(" %s\n%s\nTests run before %s timed out:" % (fail_msg, "*"*70, process_name))
429426
log(output)
430427
log("*"*70)
431428
postscript['lines'].append(cmd + " # %s" % fail_msg)
432429
stats[basename] = {"failed": True, "walltime": 1e6, "ntests": ntests}
433-
if not the_baseline_stats.get('failed', False):
430+
if not baseline.get('failed', False):
434431
self.error_status |= 4
435432
elif return_code:
436433
if return_code > 0:
@@ -439,14 +436,14 @@ def report(self, source, timeout, return_code, results, output, pid=None):
439436
fail_msg = "Killed due to %s" % signal_name(-return_code)
440437
if ntests > 0:
441438
fail_msg += " after testing finished"
442-
if the_baseline_stats.get('failed', False):
439+
if baseline.get('failed', False):
443440
fail_msg += " [failed in baseline]"
444441
log(" %s\n%s\nTests run before %s failed:" % (fail_msg,"*"*70, process_name))
445442
log(output)
446443
log("*"*70)
447444
postscript['lines'].append(cmd + " # %s" % fail_msg)
448445
stats[basename] = {"failed": True, "walltime": 1e6, "ntests": ntests}
449-
if not the_baseline_stats.get('failed', False):
446+
if not baseline.get('failed', False):
450447
self.error_status |= (8 if return_code > 0 else 16)
451448
else:
452449
if hasattr(result_dict, 'walltime') and hasattr(result_dict.walltime, '__len__') and len(result_dict.walltime) > 0:
@@ -509,10 +506,10 @@ def report(self, source, timeout, return_code, results, output, pid=None):
509506
f = result_dict.failures
510507
if f:
511508
fail_msg = "%s failed" % (count_noun(f, "doctest"))
512-
if the_baseline_stats.get('failed', False):
509+
if baseline.get('failed', False):
513510
fail_msg += " [failed in baseline]"
514511
postscript['lines'].append(cmd + " # %s" % fail_msg)
515-
if not the_baseline_stats.get('failed', False):
512+
if not baseline.get('failed', False):
516513
self.error_status |= 1
517514
if f or result_dict.err == 'tab':
518515
stats[basename] = {"failed": True, "walltime": wall, "ntests": ntests}

0 commit comments

Comments
 (0)