Repository navigation
Closed
Labels
Description
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee = 'https://github.com/serhiy-storchaka'
closed_at = <Date 2016-01-21.09:17:55.091>
created_at = <Date 2012-08-29.12:41:35.907>
labels = ['expert-IDLE', 'type-bug']
title = '2.7 IDLE console uses incorrect encoding.'
updated_at = <Date 2018-10-31.10:59:33.345>
user = 'https://bugs.python.org/alexhartwig'bugs.python.org fields:
activity = <Date 2018-10-31.10:59:33.345>
actor = 'pradyunsg'
assignee = 'serhiy.storchaka'
closed = True
closed_date = <Date 2016-01-21.09:17:55.091>
closer = 'serhiy.storchaka'
components = ['IDLE']
creation = <Date 2012-08-29.12:41:35.907>
creator = 'alex.hartwig'
dependencies = []
files = ['27044', '27045', '29974', '31400', '32671']
hgrepos = []
issue_num = 15809
keywords = ['patch']
message_count = 37.0
messages = ['169369', '169370', '169371', '169385', '175439', '187551', '187558', '187564', '187568', '187607', '187617', '188358', '188399', '188460', '195780', '195783', '195792', '195795', '195858', '195861', '201960', '203193', '203194', '203247', '212616', '218562', '218571', '218599', '218623', '218973', '218987', '219034', '242247', '243393', '258630', '258648', '258746']
nosy_count = 13.0
nosy_names = ['terry.reedy', 'kbk', 'benjamin.peterson', 'ezio.melotti', 'roger.serwy', 'asvetlov', 'THRlWiTi', 'Geraldo.Xexeo', 'python-dev', 'serhiy.storchaka', 'alex.hartwig', 'Tomoki.Imai', 'fireattack']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue15809'
versions = ['Python 2.7']
Activity
alexhartwig commentedon Aug 29, 2012
ezio-melotti commentedon Aug 29, 2012
Looks like your IDLE is set to use latin-1, but s.encode('latin1') should have failed with an UnicodeEncodeError. Are you sure you copied the snippet correctly?
The default source encoding refers to the .py files you open with IDLE, and doesn't affect the encoding used by IDLE itself while printing.
(Please use English to report issues.)
[-]Строки из IDLE поступают в неверной кодировке.[/-][+]IDLE console uses incorrect encoding.[/+]alexhartwig commentedon Aug 29, 2012
Text is correct. See attachment.
loewis commentedon Aug 29, 2012
The problem is that IDLE passes an UTF-8 encoded source string to compile, and compile, in the absence of a source encoding, uses the PEP-263 default source encoding, i.e. Latin-1.
As the consequence, the variable s has the value
u'\\xd0\\xa0\\xd1\\x83\\xd1\\x81\\xd1\\x81\\xd0\\xba\\xd0\\xb8\\xd0\\xb9 \\xd1\\x82\\xd0\\xb5\\xd0\\xba\\xd1\\x81\\xd1\\x82'
IDLE's "Default Source Encoding" is irrelevant - it only applies to editor windows.
One solution for that is the attached patch. However, this patch isn't right, since it will cause all source to be interpreted as UTF-8. This would be wrong when the sys.stdin.encoding is not UTF-8, and byte string objects are created in interactive mode.
Interactive mode manages to get it right by looking up sys.stdin.encoding during compilation, but it does so only when in interactive mode (i.e. when tok->prompt != NULL.
I don't see any way to fix this problem in Python 2. It is fixed in Python 3, basically by always assuming that the source encoding is UTF-8, by making all string objects Unicode objects, and disallowing non-ASCII characters in bytes literals
serhiy-storchaka commentedon Nov 12, 2012
Can you show how to reproduce the error that you're talking about? I have found no issues running the bare Python and IDLE (with your patch, of course) with files in different encodings under different locales.
serhiy-storchaka commentedon Apr 22, 2013
The problem (as I understand it) is that if Martin's patch fixes an unicode literals, it breaks a string literals.
Here is a different patch, which fixes unicode strings and preserve byte strings.
bitdancer commentedon Apr 22, 2013
I've combined the nosy lists and will close the other issue. Serhiy, what if the source already has a coding cookie line?
serhiy-storchaka commentedon Apr 22, 2013
A coding cookie line will be ignored. Note that this affects only very obscure case when you are pasting a multiline code with a coding cookie line into IDLE shell (this is only way to get a coding cookie line in the shell). Running a file does not pass through runsource(). runsource() used only for a code entered in the shell (may be Martin or Roger correct me if I wrong)
serhiy-storchaka commentedon Apr 22, 2013
An interactive Python console ignores a coding cookie line too.
('\xe9\xf6\xf3\xea' is 'йцук' in cp1251 and 'ИЖСЙ' in koi8-r)
serwy commentedon Apr 23, 2013
Here's a tangentially related issue: bpo-14326
IDLE doesn't handle pasting multi-line code properly (bpo-3559), IDLE2 will silently ignore code after the first executable statement. IDLE3 may give an error.
Can't we just make IDLE's shell default to UTF-8?
serhiy-storchaka commentedon Apr 23, 2013
Yes, this issue looks as a prerequisite for it.
Well, then the patch doesn't introduce a significant regression.
This is easy. Just set IOBinding.encoding to "utf-8". But I'm not sure we don't lost something in this case.
14 remaining items
serhiy-storchaka commentedon May 14, 2014
I'm going to commit my patch in few days. This is not perfect solution, but I believe it is better than current state.
terryjreedy commentedon May 14, 2014
It took me awhile to re-recognize Это из консоли: as 'made in konsole'.
I agree that the discrepancy between Idle shell and console is a bug relative to a general goal of having them work as nearly the same as possible and sensible.
This bug, like many others involving non-Ascii chars, has already been fixed in 3.x, though it is not only a 2 versus 3 issue. Given that there are technical limits to imitating the console, we could have closed this issue as already fixed, "wont fix" in 2.7, upgrade to a version that has the fix. However, since this is also a within 2.7, console versus Idle issue, improvement would be good.
The +coding cookie, -1 line hack induces a non-obvious dependency between files that I think should be documented by comments such as
# run.cleanup_traceback() compensates for added line
# compensation for coding cookie added in pyshel..runsource()
I think it ok to push less than perfect patches that make definite improvements and do not preclude possibly better patches in the future. I have not seen any comment from alex whether this fixes his particular example on his machine, but if you are convinced that this fixes some problems and only causes a micro slowdown of working uses, go ahead.
[-]IDLE console uses incorrect encoding.[/-][+]2.7 IDLE console uses incorrect encoding.[/+]serhiy-storchaka commentedon May 15, 2014
Alternative solution is to force UTF-8 in IDLE console. But I think this will be more surprising, especially for Windows users.
terryjreedy commentedon May 15, 2014
Idle 3.x has Martin's patch, except that the 'if' clause is merely commented out rather than removed. (Perhaps someone wanted to test the removal first. It should now be deleted.)
My 2.7 Idle has Default Source Encoding ... [x] None. Since there is nothing for this in either config-main.def, I presume it is the default -- at least for Windows. But Martin says it is not relevant.
'Surprising' suggests that you think or know that doing the same in 2.7 might introduce other discrepancies from console behavior. I am reluctant to do that, though it is possible that it might make Idle *better* than the console.
I would definitely be reluctant to do something more wide-ranging than your patch without extensive tests, and even that patch should have some tests, some of which pass now and continue to pass, and other that fail now and pass with the patch.
Irdb said "As a Windows user, currently I can't print u'йцук' in interactive mode and get an "Unsupported characters in input" error because my default system encoding (cp1256) can't encode Russian.)"
serhiy-storchaka commentedon May 23, 2014
When someone reads file in locale encoding (either as str or unicode in 2.7), he gets printable result. When someone lists directory, he gets printable result (str or unicode). When someone enter string literal (either str or unicode) in interactive mode, he gets printable result. This is expected behavior. And this means that default encoding for text files, filesystem encoding and encoding used in interactive Python (or IDLE console) should be the same, locale encoding.
This is different issue. It would be better if IDLE will filter pasted text and replace unencodable characters or underscore them with wavy red line.
terryjreedy commentedon May 23, 2014
I wonder if we should consider extracting the body of
if isinstance(source, types.UnicodeType):
as a function, call it ufix_latin1; add one or more alterntives, ufix_utf8, ufix_locale, ufix_irdb(?); keep ufix_latin1 as default; but switch to an alternative according to an Idle command line switch (or environmental variable?)?
serhiy-storchaka commentedon May 24, 2014
Only ufix_utf8 (with the utf-8 encoding of stdin/stdout/stderr) and ufix_locale (with hacked tracebacks) make sense.
terryjreedy commentedon Apr 29, 2015
bpo-19625, with a bit of discussion, was closed as a duplicate of this issue.
python-dev commentedon May 17, 2015
New changeset 247f003b42ea by Serhiy Storchaka in branch '2.7':
Issue bpo-15809: IDLE shell now uses locale encoding instead of Latin1 for
https://hg.python.org/cpython/rev/247f003b42ea
fireattack commentedon Jan 19, 2016
Any update on this?
terryjreedy commentedon Jan 20, 2016
Serhiy, unless you are planning to do something more, please close this. The extended extended maintenance period for 2.7 is mainly for build and security issues, and I don't expect to do any more 2.7-only patches.
serhiy-storchaka commentedon Jan 21, 2016
It would be nice to add configuration option for IDLE Shell encoding and highlight non-encodable characters in Shell and Editor. But this feature is needed mainly for 2.7.