Skip to content

2.7 IDLE console uses incorrect encoding. #60013

Closed

Description

@alexhartwig
BPO 15809
Nosy @terryjreedy, @kbkaiser, @benjaminp, @ezio-melotti, @serwy, @asvetlov, @serhiy-storchaka, @fireattack
Files
  • Снимок-2012-08-21 10:37:23.png
  • compile_unicode.diff
  • idle_compile_coding_2.patch
  • PyShell.py.patch
  • idle_compile_coding_3.patch
  • 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

    1. alexhartwig commented on Aug 29, 2012

      alexhartwigmannequin
      MannequinAuthor
      Это из IDLE:
      Python 2.7.2+ (default, Oct  4 2011, 20:03:08) 
      [GCC 4.6.1] on linux2
      Type "copyright", "credits" or "license()" for more information.
      >>> import sys
      >>> import locale
      >>> sys.getdefaultencoding()
      'ascii'
      >>> locale.getpreferredencoding()
      'UTF-8'
      >>> s = u'Русский текст'
      >>> print s.encode('utf_8')
      Р���кий �ек��
      >>> print s.encode('latin1')
      Русский текст
      
      Это из консоли:
      >>> import sys
      >>> import locale
      >>> sys.getdefaultencoding()
      'ascii'
      >>> locale.getpreferredencoding()
      'UTF-8'
      >>> s = u'Русский текст'
      >>> print s.encode('utf_8')
      Русский текст
    2. added
      type-bug
      on Aug 29, 2012
    3. ezio-melotti commented on Aug 29, 2012

      @ezio-melotti
      Member

      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.)

    4. changed the title [-]Строки из IDLE поступают в неверной кодировке.[/-] [+]IDLE console uses incorrect encoding.[/+] on Aug 29, 2012
    5. alexhartwig commented on Aug 29, 2012

      alexhartwigmannequin
      MannequinAuthor

      Text is correct. See attachment.

    6. loewis commented on Aug 29, 2012

      loewismannequin
      Mannequin

      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

    7. serhiy-storchaka commented on Nov 12, 2012

      @serhiy-storchaka
      Member

      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.

      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.

    8. serhiy-storchaka commented on Apr 22, 2013

      @serhiy-storchaka
      Member

      The problem (as I understand it) is that if Martin's patch fixes an unicode literals, it breaks a string literals.

      LC_ALL=ru_RU.cp1251 LANG=ru_RU.cp1251 ./python Lib/idlelib/idle.py
      >>> print u'йцук'
      йцук
      >>> print 'йцук'
      йцук

      Here is a different patch, which fixes unicode strings and preserve byte strings.

      >>> print u'йцук'
      йцук
      >>> print 'йцук'
      йцук
    9. bitdancer commented on Apr 22, 2013

      @bitdancer
      Member

      I've combined the nosy lists and will close the other issue. Serhiy, what if the source already has a coding cookie line?

    10. serhiy-storchaka commented on Apr 22, 2013

      @serhiy-storchaka
      Member

      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)

    11. serhiy-storchaka commented on Apr 22, 2013

      @serhiy-storchaka
      Member

      An interactive Python console ignores a coding cookie line too.

      $ cat test.py
      # -*- coding: koi8-r -*-
      print repr('йцук'), 'йцук', repr(u'йцук'), u'йцук'
      $ LC_ALL=ru_RU.cp1251 LANG=ru_RU.cp1251 ./python test.py
      '\xe9\xf6\xf3\xea' йцук u'\u0418\u0416\u0421\u0419' ИЖСЙ
      $ LC_ALL=ru_RU.cp1251 LANG=ru_RU.cp1251 ./python
      Python 2.7.4+ (2.7:0f31f38e8a17+, Apr 13 2013, 21:06:36) 
      [GCC 4.4.3] on linux2
      Type "help", "copyright", "credits" or "license" for more information.
      >>> # -*- coding: koi8-r -*-
      ... print repr('йцук'), 'йцук', repr(u'йцук'), u'йцук'
      '\xe9\xf6\xf3\xea' йцук u'\u0439\u0446\u0443\u043a' йцук

      ('\xe9\xf6\xf3\xea' is 'йцук' in cp1251 and 'ИЖСЙ' in koi8-r)

    12. serwy commented on Apr 23, 2013

      serwymannequin
      Mannequin

      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?

    13. serhiy-storchaka commented on Apr 23, 2013

      @serhiy-storchaka
      Member

      Here's a tangentially related issue: bpo-14326

      Yes, this issue looks as a prerequisite for it.

      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.

      Well, then the patch doesn't introduce a significant regression.

      Can't we just make IDLE's shell default to UTF-8?

      This is easy. Just set IOBinding.encoding to "utf-8". But I'm not sure we don't lost something in this case.

    14. 14 remaining items

    15. serhiy-storchaka commented on May 14, 2014

      @serhiy-storchaka
      Member

      I'm going to commit my patch in few days. This is not perfect solution, but I believe it is better than current state.

    16. terryjreedy commented on May 14, 2014

      @terryjreedy
      Member

      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 is critical for non-ASCII-only users of IDLE.

      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.

    17. changed the title [-]IDLE console uses incorrect encoding.[/-] [+]2.7 IDLE console uses incorrect encoding.[/+] on May 14, 2014
    18. serhiy-storchaka commented on May 15, 2014

      @serhiy-storchaka
      Member

      Alternative solution is to force UTF-8 in IDLE console. But I think this will be more surprising, especially for Windows users.

    19. terryjreedy commented on May 15, 2014

      @terryjreedy
      Member

      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.)"

      Idle 2.7
      >>> u'йцук'
      Unsupported characters in input
      
      Console 2.7
      >>> u'????'
      u'????'
      >>> u'????'[0] == '?'
      True
    20. serhiy-storchaka commented on May 23, 2014

      @serhiy-storchaka
      Member

      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.

      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.)"

      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.

    21. terryjreedy commented on May 23, 2014

      @terryjreedy
      Member

      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?)?

    22. serhiy-storchaka commented on May 24, 2014

      @serhiy-storchaka
      Member

      Only ufix_utf8 (with the utf-8 encoding of stdin/stdout/stderr) and ufix_locale (with hacked tracebacks) make sense.

    23. terryjreedy commented on Apr 29, 2015

      @terryjreedy
      Member

      bpo-19625, with a bit of discussion, was closed as a duplicate of this issue.

    24. python-dev commented on May 17, 2015

      python-devmannequin
      Mannequin

      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

    25. fireattack commented on Jan 19, 2016

      fireattackmannequin
      Mannequin

      Any update on this?

    26. terryjreedy commented on Jan 20, 2016

      @terryjreedy
      Member

      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.

    27. serhiy-storchaka commented on Jan 21, 2016

      @serhiy-storchaka
      Member

      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.

    28. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Labels

    topic-IDLEtype-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions