[py-dev] stdout encoding is set to None

Sridhar Ratnakumar SridharR at activestate.com
Tue Jul 7 01:20:24 CEST 2009


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



More information about the Pytest-dev mailing list