[py-dev] stdout encoding is set to None

holger krekel holger at merlinux.eu
Tue Jul 7 12:11:39 CEST 2009


Hi Sridhar, 

indeed, py.test by default redirects the stdout/stderr file
descriptors to a file, see py/io/fdcapture.py.  This leads to
the encoding error.  I guess the redirection file should
better be opened with the "preferredencoding" similar to what
you do.  Can you imagine submitting an according test and patch? 

Your context manager causes problems, btw, because it closes 
the redirection-stream which it shouldn't do.  

best,

holger

On Mon, Jul 06, 2009 at 16:20 -0700, Sridhar Ratnakumar wrote:
> I noticed that from within py.test .. `sys.stdout.encoding` is set to  
> None. This makes it impossible to print non-ascii unicode strings. To  
> workaround this error, I wrapped the stdout stream using codecs.getwriter  
> as explained in the PrintFails[1] python wiki page. Here's my snippet:
> 
> 	def _message(self, msg, *args):
> 	    with _good_terminal_stream() as stream:
> 	        msg = u'[ui] {0}'.format(msg)
> 	        print >>stream, msg % args
> 
> 	@contextmanager
> 	def _good_terminal_stream():
> 	    """Return a proper terminal stream"""
> 	    preferredencoding = locale.getpreferredencoding()
> 	    assert preferredencoding not in (None, 'ascii'), \
> 		'improper locale encoding: {0}'.format(
> 		preferredencoding)
> 	    stream = codecs.getwriter(preferredencoding)(sys.stdout)
> 	    try:
> 		yield stream
> 	    finally:
> 		stream.close()
> 
> However, this lead to py.test internal error:
> 
> 	    def done(self):
> 		""" unpatch and clean up, returns the self.tmpfile (file object)
> 		    """
> 		os.dup2(self._savefd, self.targetfd)
> 		self.unsetfiles()
> 		os.close(self._savefd)
> 	>       self.tmpfile.seek(0)
> 	E       ValueError: I/O operation on closed file
> 
> 	/home/sridharr/as/pypm-txtui/eggs/py-1.0.0b7-py2.6.egg/py/io/fdcapture.py:39:  
> ValueError
> 
> I've made available the entire log file here:  
> http://files.getdropbox.com/u/87045/tmp/pytesterror
> 
> -srid
> 
> *****
> [1] http://wiki.python.org/moin/PrintFails
> _______________________________________________
> py-dev mailing list
> py-dev at codespeak.net
> http://codespeak.net/mailman/listinfo/py-dev
> 

-- 
Metaprogramming, Python, Testing: http://tetamap.wordpress.com
Python, PyPy, pytest contracting: http://merlinux.eu 



More information about the Pytest-dev mailing list