Skip to content

Commit 85be00a

Browse files
authoredFeb 3, 2020
Merge pull request numpy#15430 from sethtroisi/refguide_warning
MAINT: Use contextmanager in _run_doctests
2 parents 74d8c0c + a71e8a8 commit 85be00a

File tree

1 file changed

+32
-46
lines changed

1 file changed

+32
-46
lines changed
 

‎tools/refguide_check.py

+32-46
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,23 @@
2525
2626
$ python refguide_check.py --rst docs
2727
"""
28-
import sys
29-
import os
30-
import re
3128
import copy
32-
import inspect
33-
import warnings
3429
import doctest
35-
import tempfile
30+
import glob
31+
import inspect
3632
import io
37-
import docutils.core
38-
from docutils.parsers.rst import directives
33+
import os
34+
import re
3935
import shutil
40-
import glob
41-
from doctest import NORMALIZE_WHITESPACE, ELLIPSIS, IGNORE_EXCEPTION_DETAIL
36+
import sys
37+
import tempfile
38+
import warnings
39+
import docutils.core
4240
from argparse import ArgumentParser
41+
from contextlib import contextmanager, redirect_stderr
42+
from doctest import NORMALIZE_WHITESPACE, ELLIPSIS, IGNORE_EXCEPTION_DETAIL
43+
44+
from docutils.parsers.rst import directives
4345
from pkg_resources import parse_version
4446

4547
import sphinx
@@ -784,37 +786,27 @@ def _run_doctests(tests, full_name, verbose, doctest_warnings):
784786
runner = DTRunner(full_name, checker=Checker(), optionflags=flags,
785787
verbose=verbose)
786788

787-
output = []
789+
output = io.StringIO(newline='')
788790
success = True
789-
def out(msg):
790-
output.append(msg)
791-
792-
class MyStderr:
793-
"""
794-
Redirect stderr to the current stdout
795-
"""
796-
def write(self, msg):
797-
if doctest_warnings:
798-
sys.stdout.write(msg)
799-
else:
800-
out(msg)
801791

802-
# a flush method is required when a doctest uses multiprocessing
803-
# multiprocessing/popen_fork.py flushes sys.stderr
804-
def flush(self):
805-
if doctest_warnings:
806-
sys.stdout.flush()
792+
# Redirect stderr to the stdout or output
793+
tmp_stderr = sys.stdout if doctest_warnings else output
794+
795+
@contextmanager
796+
def temp_cwd():
797+
cwd = os.getcwd()
798+
tmpdir = tempfile.mkdtemp()
799+
try:
800+
os.chdir(tmpdir)
801+
yield tmpdir
802+
finally:
803+
os.chdir(cwd)
804+
shutil.rmtree(tmpdir)
807805

808806
# Run tests, trying to restore global state afterward
809-
old_printoptions = np.get_printoptions()
810-
old_errstate = np.seterr()
811-
old_stderr = sys.stderr
812807
cwd = os.getcwd()
813-
tmpdir = tempfile.mkdtemp()
814-
sys.stderr = MyStderr()
815-
try:
816-
os.chdir(tmpdir)
817-
808+
with np.errstate(), np.printoptions(), temp_cwd() as tmpdir, \
809+
redirect_stderr(tmp_stderr):
818810
# try to ensure random seed is NOT reproducible
819811
np.random.seed(None)
820812

@@ -829,18 +821,12 @@ def flush(self):
829821
# Process our options
830822
if any([SKIPBLOCK in ex.options for ex in t.examples]):
831823
continue
832-
fails, successes = runner.run(t, out=out, clear_globs=False)
824+
fails, successes = runner.run(t, out=output.write, clear_globs=False)
833825
if fails > 0:
834826
success = False
835827
ns = t.globs
836-
finally:
837-
sys.stderr = old_stderr
838-
os.chdir(cwd)
839-
shutil.rmtree(tmpdir)
840-
np.set_printoptions(**old_printoptions)
841-
np.seterr(**old_errstate)
842828

843-
return success, output
829+
return success, output.read()
844830

845831

846832
def check_doctests(module, verbose, ns=None,
@@ -902,7 +888,7 @@ def check_doctests(module, verbose, ns=None,
902888
if dots:
903889
output_dot('.' if success else 'F')
904890

905-
results.append((full_name, success, "".join(output)))
891+
results.append((full_name, success, output))
906892

907893
if HAVE_MATPLOTLIB:
908894
import matplotlib.pyplot as plt
@@ -1022,7 +1008,7 @@ def check_doctests_testfile(fname, verbose, ns=None,
10221008
if dots:
10231009
output_dot('.' if success else 'F')
10241010

1025-
results.append((full_name, success, "".join(output)))
1011+
results.append((full_name, success, output))
10261012

10271013
if HAVE_MATPLOTLIB:
10281014
import matplotlib.pyplot as plt

0 commit comments

Comments
 (0)
Please sign in to comment.