25
25
26
26
$ python refguide_check.py --rst docs
27
27
"""
28
- import sys
29
- import os
30
- import re
31
28
import copy
32
- import inspect
33
- import warnings
34
29
import doctest
35
- import tempfile
30
+ import glob
31
+ import inspect
36
32
import io
37
- import docutils . core
38
- from docutils . parsers . rst import directives
33
+ import os
34
+ import re
39
35
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
42
40
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
43
45
from pkg_resources import parse_version
44
46
45
47
import sphinx
@@ -784,37 +786,27 @@ def _run_doctests(tests, full_name, verbose, doctest_warnings):
784
786
runner = DTRunner (full_name , checker = Checker (), optionflags = flags ,
785
787
verbose = verbose )
786
788
787
- output = []
789
+ output = io . StringIO ( newline = '' )
788
790
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 )
801
791
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 )
807
805
808
806
# Run tests, trying to restore global state afterward
809
- old_printoptions = np .get_printoptions ()
810
- old_errstate = np .seterr ()
811
- old_stderr = sys .stderr
812
807
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 ):
818
810
# try to ensure random seed is NOT reproducible
819
811
np .random .seed (None )
820
812
@@ -829,18 +821,12 @@ def flush(self):
829
821
# Process our options
830
822
if any ([SKIPBLOCK in ex .options for ex in t .examples ]):
831
823
continue
832
- fails , successes = runner .run (t , out = out , clear_globs = False )
824
+ fails , successes = runner .run (t , out = output . write , clear_globs = False )
833
825
if fails > 0 :
834
826
success = False
835
827
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 )
842
828
843
- return success , output
829
+ return success , output . read ()
844
830
845
831
846
832
def check_doctests (module , verbose , ns = None ,
@@ -902,7 +888,7 @@ def check_doctests(module, verbose, ns=None,
902
888
if dots :
903
889
output_dot ('.' if success else 'F' )
904
890
905
- results .append ((full_name , success , "" . join ( output ) ))
891
+ results .append ((full_name , success , output ))
906
892
907
893
if HAVE_MATPLOTLIB :
908
894
import matplotlib .pyplot as plt
@@ -1022,7 +1008,7 @@ def check_doctests_testfile(fname, verbose, ns=None,
1022
1008
if dots :
1023
1009
output_dot ('.' if success else 'F' )
1024
1010
1025
- results .append ((full_name , success , "" . join ( output ) ))
1011
+ results .append ((full_name , success , output ))
1026
1012
1027
1013
if HAVE_MATPLOTLIB :
1028
1014
import matplotlib .pyplot as plt
0 commit comments