@@ -181,6 +181,7 @@ def collect(self):
181
181
optionflags = get_optionflags (self )
182
182
runner = doctest .DebugRunner (verbose = 0 , optionflags = optionflags ,
183
183
checker = _get_checker ())
184
+ _fix_spoof_python2 (runner , encoding )
184
185
185
186
parser = doctest .DocTestParser ()
186
187
test = parser .get_doctest (text , globs , name , filename , 0 )
@@ -216,6 +217,10 @@ def collect(self):
216
217
optionflags = get_optionflags (self )
217
218
runner = doctest .DebugRunner (verbose = 0 , optionflags = optionflags ,
218
219
checker = _get_checker ())
220
+
221
+ encoding = self .config .getini ("doctest_encoding" )
222
+ _fix_spoof_python2 (runner , encoding )
223
+
219
224
for test in finder .find (module , module .__name__ ):
220
225
if test .examples : # skip empty doctests
221
226
yield DoctestItem (test .name , self , runner , test )
@@ -324,6 +329,30 @@ def _get_report_choice(key):
324
329
DOCTEST_REPORT_CHOICE_NONE : 0 ,
325
330
}[key ]
326
331
332
+
333
+ def _fix_spoof_python2 (runner , encoding ):
334
+ """
335
+ Installs a "SpoofOut" into the given DebugRunner so it properly deals with unicode output.
336
+
337
+ This fixes the problem related in issue #2434.
338
+ """
339
+ from _pytest .compat import _PY2
340
+ if not _PY2 :
341
+ return
342
+
343
+ from doctest import _SpoofOut
344
+
345
+ class UnicodeSpoof (_SpoofOut ):
346
+
347
+ def getvalue (self ):
348
+ result = _SpoofOut .getvalue (self )
349
+ if encoding :
350
+ result = result .decode (encoding )
351
+ return result
352
+
353
+ runner ._fakeout = UnicodeSpoof ()
354
+
355
+
327
356
@pytest .fixture (scope = 'session' )
328
357
def doctest_namespace ():
329
358
"""
0 commit comments