1414except ImportError :
1515 HAS_PEP8 = False
1616else :
17- HAS_PEP8 = True
17+ HAS_PEP8 = pep8 . __version__ > '1.4.5'
1818
1919import matplotlib
2020
@@ -127,17 +127,21 @@ class StandardReportWithExclusions(pep8.StandardReport):
127127 '*/matplotlib/projections/geo.py' ,
128128 '*/matplotlib/projections/polar.py' ]
129129
130+ #: A class attribute to store the lines of failing tests.
131+ _global_deferred_print = []
132+
130133 #: A class attribute to store patterns which have seen exceptions.
131134 matched_exclusions = set ()
132135
133136 def get_file_results (self ):
134- # If the file had no errors, return self.file_errors (which will be 0)
137+ # If the file had no errors, return self.file_errors
138+ # (which will be 0).
135139 if not self ._deferred_print :
136140 return self .file_errors
137141
138- # Iterate over all of the patterns, to find a possible exclusion. If we
139- # the filename is to be excluded, go ahead and remove the counts that
140- # self.error added.
142+ # Iterate over all of the patterns, to find a possible exclusion.
143+ # If the filename is to be excluded, go ahead and remove the
144+ # counts that self.error added.
141145 for pattern in self .expected_bad_files :
142146 if fnmatch (self .filename , pattern ):
143147 self .matched_exclusions .add (pattern )
@@ -151,12 +155,20 @@ def get_file_results(self):
151155 self .total_errors -= 1
152156 return self .file_errors
153157
154- # Otherwise call the superclass' method to print the bad results.
155- return super (StandardReportWithExclusions ,
156- self ).get_file_results ()
158+ # mirror the content of StandardReport, only storing the output to
159+ # file rather than printing. This could be a feature request for
160+ # the PEP8 tool.
161+ self ._deferred_print .sort ()
162+ for line_number , offset , code , text , _ in self ._deferred_print :
163+ self ._global_deferred_print .append (
164+ self ._fmt % {'path' : self .filename ,
165+ 'row' : self .line_offset + line_number ,
166+ 'col' : offset + 1 , 'code' : code ,
167+ 'text' : text })
168+ return self .file_errors
157169
158170
159- def _test_pep8_conformance ():
171+ def test_pep8_conformance ():
160172# Tests the matplotlib codebase against the "pep8" tool.
161173#
162174# Users can add their own excluded files (should files exist in the
@@ -172,11 +184,12 @@ def _test_pep8_conformance():
172184 # "reporter=pep8.FileReport" to the StyleGuide constructor.
173185 pep8style = pep8 .StyleGuide (quiet = False ,
174186 reporter = StandardReportWithExclusions )
187+ reporter = pep8style .options .reporter
175188
176189 # Extend the number of PEP8 guidelines which are not checked.
177- pep8style .options .ignore = pep8style .options .ignore + ( 'E121' , 'E122' ,
178- 'E123 ' , 'E124 ' , 'E125 ' , 'E126 ' , 'E127 ' ,
179- 'E128' )
190+ pep8style .options .ignore = ( pep8style .options .ignore +
191+ ( 'E121 ' , 'E122 ' , 'E123 ' , 'E124 ' , 'E125 ' ,
192+ 'E126' , 'E127' , 'E128' ) )
180193
181194 # Support for egg shared object wrappers, which are not PEP8 compliant,
182195 # nor part of the matplotlib repository.
@@ -205,10 +218,15 @@ def _test_pep8_conformance():
205218 pep8style .options .exclude .extend (extra_exclude )
206219
207220 result = pep8style .check_files ([os .path .dirname (matplotlib .__file__ )])
208- assert_equal (result .total_errors , 0 , "Found code syntax "
221+ if reporter is StandardReportWithExclusions :
222+ assert_equal (result .total_errors , 0 ,
223+ ("Found code syntax errors (and warnings):\n "
224+ "{0}" .format (
225+ '\n ' .join (reporter ._global_deferred_print ))))
226+ else :
227+ assert_equal (result .total_errors , 0 , "Found code syntax "
209228 "errors (and warnings)." )
210229
211- reporter = pep8style .options .reporter
212230 # If we've been using the exclusions reporter, check that we didn't
213231 # exclude files unnecessarily.
214232 if reporter is StandardReportWithExclusions :
0 commit comments