Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

readline: Wrong tab completion scope indices in Unicode terminals #60386

Closed
kunkku mannequin opened this issue Oct 9, 2012 · 18 comments
Closed

readline: Wrong tab completion scope indices in Unicode terminals #60386

kunkku mannequin opened this issue Oct 9, 2012 · 18 comments
Labels
stdlib Python modules in the Lib dir topic-unicode type-bug An unexpected behavior, bug, or error

Comments

@kunkku
Copy link
Mannequin

kunkku mannequin commented Oct 9, 2012

BPO 16182
Nosy @malemburg, @loewis, @vstinner, @ezio-melotti, @vadmium, @serhiy-storchaka
Files
  • readline-wide-char-index.patch: Patch for adjusting the scope indices
  • readline_locale.patch
  • readline_locale.v2.patch
  • readline_locale.v3.patch
  • readline_locale.v4.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 = None
    closed_at = <Date 2016-06-15.01:08:50.327>
    created_at = <Date 2012-10-09.20:09:44.031>
    labels = ['type-bug', 'library', 'expert-unicode']
    title = 'readline: Wrong tab completion scope indices in Unicode terminals'
    updated_at = <Date 2016-06-15.01:08:50.326>
    user = 'https://bugs.python.org/kunkku'

    bugs.python.org fields:

    activity = <Date 2016-06-15.01:08:50.326>
    actor = 'martin.panter'
    assignee = 'none'
    closed = True
    closed_date = <Date 2016-06-15.01:08:50.327>
    closer = 'martin.panter'
    components = ['Library (Lib)', 'Unicode']
    creation = <Date 2012-10-09.20:09:44.031>
    creator = 'kunkku'
    dependencies = []
    files = ['27508', '42952', '43068', '43098', '43359']
    hgrepos = []
    issue_num = 16182
    keywords = ['patch']
    message_count = 18.0
    messages = ['172517', '223105', '266124', '266318', '266752', '266881', '267070', '268230', '268361', '268374', '268499', '268506', '268522', '268523', '268541', '268545', '268549', '268598']
    nosy_count = 8.0
    nosy_names = ['lemburg', 'loewis', 'vstinner', 'ezio.melotti', 'python-dev', 'martin.panter', 'serhiy.storchaka', 'kunkku']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue16182'
    versions = ['Python 3.5', 'Python 3.6']

    @kunkku
    Copy link
    Mannequin Author

    kunkku mannequin commented Oct 9, 2012

    Tab completion in the readline module does not seem to work well with Unicode terminals. The get_line_buffer function converts the line buffer to the str type (which are Unicode strings in Python 3), but the indices returned by get_begidx and get_endidx are not adjusted with respect to possible wide characters in the buffer, and hence are not very useful. The documentation is a bit vague on the index functions, but I think they should be relative to code points, regardless of the encoding used by the C library. The suggested correction is attached.

    My second point of complaint is related to the use of PyUnicode_FromString in the module. The strings returned by the readline library use the current locale encoding, which is not necessarily UTF-8. I wonder if PyUnicode_DecodeLocale should be used instead for more portable code.

    @kunkku kunkku mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Oct 9, 2012
    @BreamoreBoy
    Copy link
    Mannequin

    BreamoreBoy mannequin commented Jul 15, 2014

    @Kaarle please accept our apologies for the delay in getting back to you. Can one of our unicode gurus comment please.

    @BreamoreBoy BreamoreBoy mannequin added the topic-unicode label Jul 15, 2014
    @serhiy-storchaka
    Copy link
    Member

    Yes, the readline module is broken in Python 3. Underlying C library operates C strings and use locale-depended C functions to split it on Unicode characters. The Python wrapper always uses the UTF-8 encoding for converting between Python strings and C strings. It works only on UTF-8 locales. get_begidx() and get_endidx() don't correctly work at all for non-ASCII data. We should use locale encoding for converting.

    Proposed patch makes the readline module to use locale depending coding functions instead of default UTF-8. It also corrects indices for get_begidx() and get_endidx().

    @vadmium
    Copy link
    Member

    vadmium commented May 25, 2016

    I’m a bit worried about flex_complete() covering up errors. If I add calls to PyErr_WriteUnraisable(), I can see both the s1 and s2 decodes failing. Input the following line:

    >> "©"; import x;

    and then move the cursor back one place, so it is directly after the “x”, but not after the semicolon (;). Then press Tab. Both errors are:

    ValueError: embedded null byte

    It looks like the reason is that PyUnicode_DecodeLocaleAndSize() requires that str[len] is a null character (the error message is misleading). It seems the len parameter is mainly there to verify that there are no embedded null characters, i.e. you cannot use it to give a truncated string.

    It looks like Py_DecodeLocale() is used underneath; maybe it is simpler to call that directly. But it does not solve the string truncating problem.

    A test case (perhaps using a pseudo-terminal) might also help pick this kind of thing up, if we can’t report errors any other way.

    @vadmium
    Copy link
    Member

    vadmium commented May 31, 2016

    Attached patch adds a possible test, and fixes the truncation problem I mentioned above.

    I tried testing set_completer_delims() with a UTF-8 locale, but I suspect Gnu Readline does not support it. I called set_completer_delims("\xF6"), which encodes as C3 B6, but it seems to be breaking any UTF-8 sequence in half at a C3 byte. In other words, it is treating the delimiter list as a list of bytes, not code points. So I changed to an ASCII delimiter.

    @vadmium
    Copy link
    Member

    vadmium commented Jun 2, 2016

    V3 finishes what I started in v2:

    • Changed unchecked PyBytes_AsString() → PyBytes_AS_STRING()
    • Testing more functions for non-ASCII characters

    I tried to test it with Editline on Linux (using my patch for bpo-13501). There seem to be many quirks with my version of Editline, some of which are not easy to work around:

    • Initial CR swallowed when entering non-ASCII
    • set_completion_display_matches_hook(), set_pre_input_hook(), set_completer_delims() all do nothing useful
    • get_history_item() not updated straight after read_history_file()

    I suspect Apple has patched their version of Editline, but if these quirks exist on Apple as well, it might be simplest to skip the test for Editline.

    @serhiy-storchaka
    Copy link
    Member

    Thank you for your comments and updated patches, and especially for tests Martin. Added some comments on Rietveld.

    @serhiy-storchaka
    Copy link
    Member

    Martin?

    @vadmium
    Copy link
    Member

    vadmium commented Jun 12, 2016

    I updated the patch to fix the error handling and memory leak. it also now skips the test in case the locale cannot encode the test data.

    @serhiy-storchaka
    Copy link
    Member

    LGTM.

    But the test fails with PYTHONIOENCODING=ascii.

    $ PYTHONIOENCODING=ascii ./python -m test test_readline
    Run tests sequentially
    0:00:00 [1/1] test_readline
    test test_readline failed -- Traceback (most recent call last):
      File "/home/serhiy/py/cpython/Lib/test/test_readline.py", line 194, in test_nonascii
        self.assertIn(b"result " + expected + b"\r\n", output)
    AssertionError: b"result '[\\xefnserted]|t\\xebxt[after]'\r\n" not found in bytearray(b'^A^B^B^B^B^B^B^B\t\tx\t\r\n[\xc3\xafnserted]|t\xc3\xab[after]\x08\x08\x08\x08\x08\x08\x08text \'t\\xeb\'\r\nline \'[\\xefnserted]|t\\xeb[after]\'\r\nindexes 11 13\r\n\x07text \'t\\xeb\'\r\nline \'[\\xefnserted]|t\\xeb[after]\'\r\nindexes 11 13\r\nsubstitution \'t\\xeb\'\r\nmatches [\'t\\xebnt\', \'t\\xebxt\']\r\nx[after]\x08\x08\x08\x08\x08\x08\x08t[after]\x08\x08\x08\x08\x08\x08\x08\r\nTraceback (most recent call last):\r\n  File "<string>", line 39, in <module>\r\nUnicodeDecodeError: \'ascii\' codec can\'t decode byte 0xc3 in position 1: ordinal not in range(128)\r\n')

    This is minor problem, since buildbots rarely configured with PYTHONIOENCODING=ascii.

    @vadmium
    Copy link
    Member

    vadmium commented Jun 14, 2016

    I get two other test suite failures if I set PYTHONIOENCODING, so I am not going to bother addressing this in test_readline :)

    FAIL: test_forced_io_encoding (test.test_capi.EmbeddingTests)
    FAIL: test_7 (test.test_pkg.TestPkg)

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Jun 14, 2016

    New changeset 5122b3465a38 by Martin Panter in branch '3.5':
    Issue bpo-16182: Fix readline begidx, endidx, and use locale encoding
    https://hg.python.org/cpython/rev/5122b3465a38

    New changeset 2ae2657d87a6 by Martin Panter in branch 'default':
    Issue bpo-16182: Merge readline locale fix from 3.5
    https://hg.python.org/cpython/rev/2ae2657d87a6

    @vadmium
    Copy link
    Member

    vadmium commented Jun 14, 2016

    Failures from AMD64 Snow Leop buildbots:

    ======================================================================
    FAIL: test_nonascii_history (test.test_readline.TestHistoryManipulation)
    ----------------------------------------------------------------------

    Traceback (most recent call last):
      File "/Users/buildbot/buildarea/3.5.murray-snowleopard/build/Lib/test/test_readline.py", line 102, in test_nonascii_history
        self.assertEqual(readline.get_history_item(1), "entrée 1")
    AssertionError: None != 'entrée 1'

    ======================================================================
    FAIL: test_nonascii (test.test_readline.TestReadline)
    ----------------------------------------------------------------------

    Traceback (most recent call last):
      File "/Users/buildbot/buildarea/3.5.murray-snowleopard/build/Lib/test/test_readline.py", line 174, in test_nonascii
        self.assertIn(b"line '[\\xefnserted]|t\\xeb[after]'\r\n", output)
    AssertionError: b"line '[\\xefnserted]|t\\xeb[after]'\r\n" not found in bytearray(b"^A^B^B^B^B^B^B^B\t\tx\t\r\n|t\xc3\xab[after]\x08\x08\x08\x08\x08\x08\x08text \'t\\xeb\'\r\nline \'|t\\xeb[after]\'\r\nindexes 1 3\r\n\x07text \'t\\xeb\'\r\nline \'|t\\xeb[after]\'\r\nindexes 1 3\r\n\r\nt\xc3\xabxt  t\xc3\xabnt  \r\n\r\r\n|t\xc3\xab[after]\r|t\xc3\xabx[after]\r|t\xc3\xabxt[after]\r|t\xc3\xabxt\r\nresult \'|t\\xebxt[after]\'\r\nhistory \'|t\\xebxt[after]\'\r\n")

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Jun 14, 2016

    New changeset 0794bbfceec6 by Martin Panter in branch '3.5':
    Issue bpo-16182: Attempted workarounds for Apple Editline
    https://hg.python.org/cpython/rev/0794bbfceec6

    New changeset a1ca9c0ebc05 by Martin Panter in branch 'default':
    Issue bpo-16182: Merge test_readline from 3.5
    https://hg.python.org/cpython/rev/a1ca9c0ebc05

    @vadmium
    Copy link
    Member

    vadmium commented Jun 14, 2016

    Also need a fix for missing set_pre_input_hook() on the AIX buildbot:

    ======================================================================
    FAIL: test_nonascii (test.test_readline.TestReadline)
    ----------------------------------------------------------------------

    Traceback (most recent call last):
      File "/home/shager/cpython-buildarea/3.5.edelsohn-aix-ppc64/build/Lib/test/test_readline.py", line 173, in test_nonascii
        self.assertIn(b"text 't\\xeb'\r\n", output)
    AssertionError: b"text 't\\xeb'\r\n" not found in bytearray(b'\x01\x02\x02\x02\x02\x02\x02\x02                x       \r\n\x1b[?1034hTraceback (most recent call last):\r\n  File "<string>", line 18, in <module>\r\nAttributeError: module \'readline\' has no attribute \'set_pre_input_hook\'\r\n')

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Jun 14, 2016

    New changeset 005cab4f5629 by Martin Panter in branch '3.5':
    Issue bpo-16182: set_pre_input_hook() may not exist; document, and update test
    https://hg.python.org/cpython/rev/005cab4f5629

    New changeset c4dd384ee3fa by Martin Panter in branch 'default':
    Issue bpo-16182: Merge readline update from 3.5
    https://hg.python.org/cpython/rev/c4dd384ee3fa

    New changeset cff695a0b449 by Martin Panter in branch '2.7':
    Issue bpo-16182: Backport documentation of set_pre_input_hook() availability
    https://hg.python.org/cpython/rev/cff695a0b449

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Jun 14, 2016

    New changeset ef234a5c5817 by Martin Panter in branch '3.5':
    Issue bpo-16182: One more check for set_pre_input_hook()
    https://hg.python.org/cpython/rev/ef234a5c5817

    New changeset 241bae60cef8 by Martin Panter in branch 'default':
    Issue bpo-16182: Merge test_readline from 3.5
    https://hg.python.org/cpython/rev/241bae60cef8

    @vadmium
    Copy link
    Member

    vadmium commented Jun 15, 2016

    Think I got all the bugs fixed here.

    @vadmium vadmium closed this as completed Jun 15, 2016
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir topic-unicode type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants