Your message dated Mon, 15 Feb 2010 18:23:14 +0100
with message-id <[email protected]>
and subject line Re: Bug#415968: Not a bug
has caused the Debian Bug report #415968,
regarding Cannot output unicode to sys.stdout when redirected to a file
to be marked as done.
This means that you claim that the problem has been dealt with.
If this is not the case it is now your responsibility to reopen the
Bug report if necessary, and/or fix the problem forthwith.
(NB: If you are a system administrator and have no idea what this
message is talking about, this may indicate a serious mail system
misconfiguration somewhere. Please contact [email protected]
immediately.)
--
415968: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=415968
Debian Bug Tracking System
Contact [email protected] with problems
--- Begin Message ---
Package: python2.4
Version: 2.4.4-2
Severity: normal
Hello,
Thanks for maintaining Python.
While I can output unicode properly on a terminal, the same script does
not work when redirected to a file:
$ cat test.py
#! /usr/bin/python
# vim:set encoding=utf-8:
import sys
def output(s):
print s.decode('utf-8')
print sys.stdout.encoding
s = "ééé"
output("%s" % (s))
$ ./test.py
UTF-8
ééé
$ ./test.py > test
Traceback (most recent call last):
File "./test.py", line 12, in ?
output("%s" % (s))
File "./test.py", line 7, in output
print s.decode('utf-8')
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2:
ordinal not in range(128)
$ cat test
None
A work-around for this is (courtesy of MadCoder):
import sys, codecs
if sys.stdout.encoding is None:
sys.stdout = codecs.open("/dev/stdout", "w", 'utf-8')
Ciao,
Enrico
-- System Information:
Debian Release: 4.0
APT prefers testing
APT policy: (500, 'testing')
Architecture: amd64 (x86_64)
Shell: /bin/sh linked to /bin/bash
Kernel: Linux 2.6.20.1enrico
Locale: LANG=it_IT.UTF-8, LC_CTYPE=it_IT.UTF-8 (charmap=UTF-8)
Versions of packages python2.4 depends on:
ii libbz2-1.0 1.0.3-6 high-quality block-sorting file co
ii libc6 2.3.6.ds1-13 GNU C Library: Shared libraries
ii libdb4.4 4.4.20-8 Berkeley v4.4 Database Libraries [
ii libncursesw5 5.5-5 Shared libraries for terminal hand
ii libreadline5 5.2-2 GNU readline and history libraries
ii libssl0.9.8 0.9.8c-4 SSL shared libraries
ii mime-support 3.39-1 MIME files 'mime.types' & 'mailcap
ii python2.4-minimal 2.4.4-2 A minimal subset of the Python lan
python2.4 recommends no packages.
-- no debconf information
--- End Message ---
--- Begin Message ---
tags 415968 + wontfix
thanks
On 13.02.2010 22:20, Martin v. Löwis wrote:
This is not a bug in Python, but in the individual scripts. Scripts
should not rely on being able to write Unicode strings to stdout.
Instead, they need to encode strings explicitly, according to whatever
protocol is defined for whatever file they send the data to. In absence
of a more specific guidance from the user (or the protocol that they
follow), they should use locale.getpreferredencoding().
I propose to close this as "won't fix". Python 3 has resolved this issue
in a different way, explicitly distinguishing between text files (which
get encoded in the locale's encoding unless specified differently), and
binary files (which don't support writing Unicode to them).
--- End Message ---