22Provides utilities to test output reproducibility.
33"""
44
5+ import six
6+
57import io
68import os
79import re
10+ import sys
11+ from subprocess import check_output
812
913from matplotlib import pyplot as plt
1014
1115
12- def _determinism_save (filename , objects = 'mhi' , format = "pdf" ):
16+ def _determinism_save (objects = 'mhi' , format = "pdf" ):
1317 # save current value of SOURCE_DATE_EPOCH and set it
1418 # to a constant value, so that time difference is not
1519 # taken into account
@@ -51,7 +55,13 @@ def _determinism_save(filename, objects='mhi', format="pdf"):
5155 x = range (5 )
5256 fig .add_subplot (1 , 6 , 6 ).plot (x , x )
5357
54- fig .savefig (filename , format = format )
58+ if six .PY2 and format == 'ps' :
59+ stdout = io .StringIO ()
60+ else :
61+ stdout = getattr (sys .stdout , 'buffer' , sys .stdout )
62+ fig .savefig (stdout , format = format )
63+ if six .PY2 and format == 'ps' :
64+ sys .stdout .write (stdout .getvalue ())
5565
5666 # Restores SOURCE_DATE_EPOCH
5767 if sde is None :
@@ -77,22 +87,17 @@ def _determinism_check(objects='mhi', format="pdf", uid=""):
7787 some string to add to the filename used to store the output. Use it to
7888 allow parallel execution of two tests with the same objects parameter.
7989 """
80- import sys
81- from subprocess import check_call
8290 from nose .tools import assert_equal
83- filename = 'determinism_O%s%s.%s' % (objects , uid , format )
8491 plots = []
8592 for i in range (3 ):
86- check_call ([sys .executable , '-R' , '-c' ,
87- 'import matplotlib; '
88- 'matplotlib.use(%r); '
89- 'from matplotlib.testing.determinism '
90- 'import _determinism_save;'
91- '_determinism_save(%r,%r,%r)'
92- % (format , filename , objects , format )])
93- with open (filename , 'rb' ) as fd :
94- plots .append (fd .read ())
95- os .unlink (filename )
93+ result = check_output ([sys .executable , '-R' , '-c' ,
94+ 'import matplotlib; '
95+ 'matplotlib.use(%r); '
96+ 'from matplotlib.testing.determinism '
97+ 'import _determinism_save;'
98+ '_determinism_save(%r,%r)'
99+ % (format , objects , format )])
100+ plots .append (result )
96101 for p in plots [1 :]:
97102 assert_equal (p , plots [0 ])
98103
@@ -114,23 +119,17 @@ def _determinism_source_date_epoch(format, string, keyword=b"CreationDate"):
114119 a string to look at when searching for the timestamp in the document
115120 (used in case the test fails).
116121 """
117- import sys
118- from subprocess import check_call
119- filename = 'test_SDE_on.%s' % format
120- check_call ([sys .executable , '-R' , '-c' ,
121- 'import matplotlib; '
122- 'matplotlib.use(%r); '
123- 'from matplotlib.testing.determinism '
124- 'import _determinism_save;'
125- '_determinism_save(%r,%r,%r)'
126- % (format , filename , "" , format )])
122+ buff = check_output ([sys .executable , '-R' , '-c' ,
123+ 'import matplotlib; '
124+ 'matplotlib.use(%r); '
125+ 'from matplotlib.testing.determinism '
126+ 'import _determinism_save;'
127+ '_determinism_save(%r,%r)'
128+ % (format , "" , format )])
127129 find_keyword = re .compile (b".*" + keyword + b".*" )
128- with open (filename , 'rb' ) as fd :
129- buff = fd .read ()
130- key = find_keyword .search (buff )
131- if key :
132- print (key .group ())
133- else :
134- print ("Timestamp keyword (%s) not found!" % keyword )
135- assert string in buff
136- os .unlink (filename )
130+ key = find_keyword .search (buff )
131+ if key :
132+ print (key .group ())
133+ else :
134+ print ("Timestamp keyword (%s) not found!" % keyword )
135+ assert string in buff
0 commit comments