all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "Clément Pit--Claudel" <clement.pitclaudel@live.com>
To: Lukas Juhrich <lukasjuhrich@wh2.tu-dresden.de>,
	Noam Postavsky <npostavs@users.sourceforge.net>
Cc: 24401@debbugs.gnu.org
Subject: bug#24401: python-shell-completion-native-try returns incorrect results with python 3.5.2
Date: Wed, 26 Oct 2016 23:35:57 -0400	[thread overview]
Message-ID: <220cf6b0-de5c-7d4b-410e-97c0a37d768c@live.com> (raw)
In-Reply-To: <3d4d75b1-3de1-4edb-abf3-2dd5b1f75e75@wh2.tu-dresden.de>


[-- Attachment #1.1.1: Type: text/plain, Size: 1131 bytes --]

Hi Lukas,

Thanks for the patch!

I prepared a similar one, but I didn't end up pushing it, because the bug has a more complex root cause.  Essentially, the issue is that completion doesn't work for empty strings in when python-shell-interpreter points to Python 3 (see also the original bug report, #22897).

A good way to experiment with this is to copy the completion code to a separate file (I attached it) and run python -i completion.py.  In python 2.7, pressing TAB shows a bunch of completions.  In 3.5, it just inserts a tab.

Can you help figure out why this happen?  Then we could fix the root of this bug.

Cheers,
Clément.

On 2016-10-26 21:34, Lukas Juhrich wrote:
> Hi,
> 
> On 10.09.2016 06:00, Clément Pit--Claudel wrote:
>> Does anyone object to the proposed fix?
>>
>> Clément.
>>
> 
> May I as a complete outsider to emacs development ask what the state
> on this is?  To me it looks like this just hasn't been done yet.
> 
> If having a patch file makes things easier, there is one attached; Let
> me know if I can help otherwise.
> 
> 
> --
> Sincerely,
> Lukas Juhrich
> 

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1.1.2: completion.py --]
[-- Type: text/x-python-script; name="completion.py", Size: 4659 bytes --]

def __PYTHON_EL_native_completion_setup():
    try:
        import readline

        try:
            import __builtin__
        except ImportError:
            # Python 3
            import builtins as __builtin__

        builtins = dir(__builtin__)
        is_ipython = ('__IPYTHON__' in builtins or
                      '__IPYTHON__active' in builtins)

        class __PYTHON_EL_Completer:
            '''Completer wrapper that prints candidates to stdout.

            It wraps an existing completer function and changes its behavior so
            that the user input is unchanged and real candidates are printed to
            stdout.

            Returned candidates are '0__dummy_completion__' and
            '1__dummy_completion__' in that order ('0__dummy_completion__' is
            returned repeatedly until all possible candidates are consumed).

            The real candidates are printed to stdout so that they can be
            easily retrieved through comint output redirect trickery.
            '''

            PYTHON_EL_WRAPPED = True

            def __init__(self, completer):
                self.completer = completer
                self.last_completion = None
                self.print_mode = True

            def __call__(self, text, state):
                if state == 0:
                    # Set the first dummy completion.
                    self.last_completion = None
                    completion = '0__dummy_completion__'
                else:
                    completion = self.completer(text, state - 1)

                if not completion:
                    if self.last_completion != '1__dummy_completion__':
                        # When no more completions are available, returning a
                        # dummy with non-sharing prefix allow ensuring output
                        # while preventing changes to current input.
                        # Coincidentally it's also the end of output.
                        completion = '1__dummy_completion__'
                elif completion.endswith('('):
                    # Remove parens on callables as it breaks completion on
                    # arguments (e.g. str(Ari<tab>)).
                    completion = completion[:-1]
                self.last_completion = completion

                if completion in (
                        '0__dummy_completion__', '1__dummy_completion__'):
                    return completion
                elif completion:
                    # For every non-dummy completion, return a repeated dummy
                    # one and print the real candidate so it can be retrieved
                    # by comint output filters.
                    if self.print_mode:
                        print (completion)
                        return '0__dummy_completion__'
                    else:
                        return completion
                else:
                    return completion

        completer = readline.get_completer()

        if not completer:
            # Used as last resort to avoid breaking customizations.
            import rlcompleter
            completer = readline.get_completer()

        if completer and not getattr(completer, 'PYTHON_EL_WRAPPED', False):
            # Wrap the existing completer function only once.
            new_completer = __PYTHON_EL_Completer(completer)
            if not is_ipython:
                readline.set_completer(new_completer)
            else:
                # Try both initializations to cope with all IPython versions.
                # This works fine for IPython 3.x but not for earlier:
                readline.set_completer(new_completer)
                # IPython<3 hacks readline such that `readline.set_completer`
                # won't work.  This workaround injects the new completer
                # function into the existing instance directly:
                instance = getattr(completer, 'im_self', completer.__self__)
                instance.rlcomplete = new_completer

        if readline.__doc__ and 'libedit' in readline.__doc__:
            print("AAA")
            readline.parse_and_bind('bind ^I rl_complete')
        else:
            print("BBB")
            readline.parse_and_bind('tab: complete')
            # Require just one tab to send output.
            readline.parse_and_bind('set show-all-if-ambiguous on')

        print ('python.el: native completion setup loaded')
    except:
        print ('python.el: native completion setup failed')

__PYTHON_EL_native_completion_setup()

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 819 bytes --]

  reply	other threads:[~2016-10-27  3:35 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-09-09 23:07 bug#24401: python-shell-completion-native-try returns incorrect results with python 3.5.2 Clément Pit--Claudel
2016-09-09 23:30 ` Noam Postavsky
2016-09-10  4:00   ` Clément Pit--Claudel
2016-10-27  1:34     ` Lukas Juhrich
2016-10-27  3:35       ` Clément Pit--Claudel [this message]
2016-10-27  3:55       ` Clément Pit--Claudel
2016-10-29 14:12     ` Clément Pit--Claudel
2016-10-29 14:34       ` Eli Zaretskii
2016-10-29 15:03         ` Clément Pit--Claudel
2016-10-29 15:08           ` Eli Zaretskii
2016-10-31 12:38             ` Clément Pit--Claudel
2016-11-01  0:30               ` npostavs
2016-11-05  1:59                 ` npostavs
2016-11-05  3:28                   ` Clément Pit--Claudel
2016-11-29 16:38 ` bug#24401: Davor Rotim
2016-11-29 17:21   ` bug#24401: Clément Pit--Claudel
2016-11-29 20:27     ` bug#24401: Clément Pit--Claudel
2016-11-29 20:40 ` bug#24401: Davor Rotim
2016-11-29 22:35   ` bug#24401: Clément Pit--Claudel
2016-11-30  9:39 ` bug#24401: Davor Rotim
2016-11-30 14:22   ` bug#24401: Noam Postavsky
2016-11-30 15:09 ` bug#24401: Davor Rotim
2016-12-01  2:57   ` bug#24401: Clément Pit--Claudel
2016-12-01  8:47 ` bug#24401: Davor Rotim
2016-12-01 14:28   ` bug#24401: Clément Pit--Claudel
2016-12-02 16:31   ` bug#24401: Clément Pit--Claudel
2016-12-01 20:23 ` bug#24401: Davor Rotim
2016-12-01 20:40   ` bug#24401: Clément Pit--Claudel
2016-12-01 21:29 ` bug#24401: Davor Rotim
2016-12-01 21:45   ` bug#24401: Clément Pit--Claudel
2016-12-02  7:22     ` bug#24401: Eli Zaretskii
2016-12-02 17:13 ` bug#24401: Davor Rotim
2016-12-02 21:24   ` bug#24401: Clément Pit--Claudel
2016-12-02 21:51 ` bug#24401: Davor Rotim

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=220cf6b0-de5c-7d4b-410e-97c0a37d768c@live.com \
    --to=clement.pitclaudel@live.com \
    --cc=24401@debbugs.gnu.org \
    --cc=lukasjuhrich@wh2.tu-dresden.de \
    --cc=npostavs@users.sourceforge.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.