2
2
Provides utilities to test output reproducibility.
3
3
"""
4
4
5
+ import six
6
+
5
7
import io
6
8
import os
7
9
import re
10
+ import sys
11
+ from subprocess import check_output
8
12
9
13
from matplotlib import pyplot as plt
10
14
11
15
12
- def _determinism_save (filename , objects = 'mhi' , format = "pdf" ):
16
+ def _determinism_save (objects = 'mhi' , format = "pdf" ):
13
17
# save current value of SOURCE_DATE_EPOCH and set it
14
18
# to a constant value, so that time difference is not
15
19
# taken into account
@@ -51,7 +55,13 @@ def _determinism_save(filename, objects='mhi', format="pdf"):
51
55
x = range (5 )
52
56
fig .add_subplot (1 , 6 , 6 ).plot (x , x )
53
57
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 ())
55
65
56
66
# Restores SOURCE_DATE_EPOCH
57
67
if sde is None :
@@ -77,22 +87,17 @@ def _determinism_check(objects='mhi', format="pdf", uid=""):
77
87
some string to add to the filename used to store the output. Use it to
78
88
allow parallel execution of two tests with the same objects parameter.
79
89
"""
80
- import sys
81
- from subprocess import check_call
82
90
from nose .tools import assert_equal
83
- filename = 'determinism_O%s%s.%s' % (objects , uid , format )
84
91
plots = []
85
92
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 )
96
101
for p in plots [1 :]:
97
102
assert_equal (p , plots [0 ])
98
103
@@ -114,23 +119,17 @@ def _determinism_source_date_epoch(format, string, keyword=b"CreationDate"):
114
119
a string to look at when searching for the timestamp in the document
115
120
(used in case the test fails).
116
121
"""
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 )])
127
129
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