[Patches] [ python-Patches-612627 ] Allow more Unicode on sys.stdout

SourceForge.net noreply@sourceforge.net
Fri, 28 Mar 2003 00:44:42 -0800


Patches item #612627, was opened at 2002-09-21 22:32
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=612627&group_id=5470

Category: Core (C code)
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Martin v. Löwis (loewis)
Assigned to: M.-A. Lemburg (lemburg)
Summary: Allow more Unicode on sys.stdout

Initial Comment:
This patch extends the set of Unicode strings that can
be printed to sys.stdout, to support all strings that
the terminal will likely support. It also adds an
encoding attribute to sys.std{in,out}.

To do that:
- it adds a .encoding attribute to all file objects,
which is normally None
- initializes the encoding of sys.stdin and sys.stdout
if either is a terminal.
- adds a wrapper object around sys.stdout in site.py
that encodes all Unicode objects according to the
detected encoding, if that encoding is known to Python

To find the encoding of the terminal, it
- uses GetConsoleCP and GetConsoleOutputCP on Windows,
- uses nl_langinfo(CODESET) on Unix, if available.

The primary rationale for this change is that people
should be able to print Unicode in an interactive
session. A parallel change needs to be added for IDLE,
so that it adds the .encoding attribute to the emulated
stdout (it already supports printing of Unicode on stdout).

----------------------------------------------------------------------

>Comment By: M.-A. Lemburg (lemburg)
Date: 2003-03-28 09:44

Message:
Logged In: YES 
user_id=38388

Looks ok except for the direct hacking
of f_encoding in the sys module. Please add
either a macro or a new API to make changing
the encoding from C possible without tapping
directly into the implementation.

----------------------------------------------------------------------

Comment By: Martin v. Löwis (loewis)
Date: 2003-03-23 12:59

Message:
Logged In: YES 
user_id=21627

Is the patch now acceptable?

----------------------------------------------------------------------

Comment By: Martin v. Löwis (loewis)
Date: 2002-10-26 19:47

Message:
Logged In: YES 
user_id=21627

I've attached a revised version which implements your
proposal; this version works without modification of site.py.

In its current form, the file encoding is only applied in
print; for sys.stdout.write, it is ignored. For print, it is
applied independent of whether this is a script or
interactive mode.

----------------------------------------------------------------------

Comment By: M.-A. Lemburg (lemburg)
Date: 2002-10-25 14:09

Message:
Logged In: YES 
user_id=38388

I think it could work by adding a special case to 
PyFile_WriteObject() instead of calling PyObject_Print().
You first encode the Unicode object and then let
PyFile_WriteString() take care of the writing to the
FILE* object.

I see no other way, since you can't place the .encoding 
information into the FILE* object.

----------------------------------------------------------------------

Comment By: Martin v. Löwis (loewis)
Date: 2002-09-24 11:02

Message:
Logged In: YES 
user_id=21627

I have considered implementing it in the file object.
However, it becomes quite involved, and heavy C code:
PyFile_WriteObject calls PyObject_Print. Since Unicode does
not implement a tp_print, this calls str/repr, which
converts using the default encoding.

It is not clear at which point the file encoding should be
taking into account.

----------------------------------------------------------------------

Comment By: Nobody/Anonymous (nobody)
Date: 2002-09-24 10:10

Message:
Logged In: NO 

I like the .encoding concept. 

I don't really like the sys.stdout wrapper. Wouldn't it be 
better to add the functionality to the file object .write() and 
.writelines() methods and then only use the wrapper in case 
sys.stdout is not a true file object ?


----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=612627&group_id=5470