all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Evgenii Klimov via "Bug reports for GNU Emacs, the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
To: Eli Zaretskii <eliz@gnu.org>
Cc: 45938@debbugs.gnu.org, kobarity@gmail.com
Subject: bug#45938: [PATCH] Avoid ANSI escape characters (bug#45938)
Date: Wed, 28 Aug 2024 21:39:27 +0100	[thread overview]
Message-ID: <87le0g8jr4.fsf@lipklim.org> (raw)
In-Reply-To: <86jzg0h6ql.fsf@gnu.org> (Eli Zaretskii's message of "Wed, 28 Aug 2024 20:55:46 +0300")

Eli Zaretskii <eliz@gnu.org> writes:

>> From: Evgenii Klimov <eugene.dev@lipklim.org>
>> Cc: kobarity <kobarity@gmail.com>,  45938@debbugs.gnu.org
>> Date: Wed, 28 Aug 2024 17:28:30 +0100
>> 
>> Eli Zaretskii <eliz@gnu.org> writes:
>> [...]
>> >> diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
>> >> index a00289d6de9..7193cc19425 100644
>> >> --- a/lisp/progmodes/python.el
>> >> +++ b/lisp/progmodes/python.el
>> >> @@ -4549,6 +4549,9 @@ (defun python-shell-completion-native-setup ()
>> >>              readline.parse_and_bind('tab: complete')
>> >>              # Require just one tab to send output.
>> >>              readline.parse_and_bind('set show-all-if-ambiguous on')
>> >> +            # Avoid ANSI escape characters in the output
>> >> +            readline.parse_and_bind('set colored-completion-prefix off')
>> >> +            readline.parse_and_bind('set colored-stats off')
>> >>              # Avoid replacing common prefix with ellipsis.
>> >>              readline.parse_and_bind('set completion-prefix-display-length 0')
>> >>  
>> >
>> > Are these commands available in every version of Python?
>> 
>> These commands are from GNU Readline, not from Python
>
> Yes, I know.  But not every Python is built with GNU Readline, right?

If Python is not built with GNU Readline, then this function
(python-shell-completion-native-setup) will not set up native completion
in any case:

    ;; Shell completion: hitting tab will try to complete the current
    ;; word.  The two built-in mechanisms depend on Python's readline
    ;; module [1]

    (defun python-shell-completion-native-setup ()
      "Try to setup native completion, return non-nil on success."
      (let* ((process (python-shell-get-process))
             (output (python-shell-send-string-no-output "
    def __PYTHON_EL_native_completion_setup():
        try:
            import readline
            ...
        except:
            import sys
            print ('python.el: native completion setup failed, %s: %s'
                   % sys.exc_info()[:2])

[...]
> Which versions of Python or GNU/Linux are likely to have older
> versions of Readline?  And what happens in an older Readline when
> these commands are sent?

I tested what happens if we pass nonexistent variable - it prints to
stdout:

    In [15]: readline.parse_and_bind("set nonexistent-variable off")
    readline: nonexistent-variable: unknown variable name

In the context of `python-shell-completion-native-setup' such output
doesn't hurt, because output string still matches with expected one.
E.g. let's say that I added nonexistent-variable:

    (defun python-shell-completion-native-setup ()
      "Try to setup native completion, return non-nil on success."
      (let* ((process (python-shell-get-process))
             (output (python-shell-send-string-no-output "
    def __PYTHON_EL_native_completion_setup():
            ...
            if readline.__doc__ and 'libedit' in readline.__doc__:
                raise Exception('''libedit based readline is known not to work,
          see etc/PROBLEMS under \"In Inferior Python mode, input is echoed\".''')
                readline.parse_and_bind('bind ^I rl_complete')
            else:
                readline.parse_and_bind('tab: complete')
                # Require just one tab to send output.
                readline.parse_and_bind('set show-all-if-ambiguous on')
->              readline.parse_and_bind('set nonexistent-variable off')
                # Avoid replacing common prefix with ellipsis.
                readline.parse_and_bind('set completion-prefix-display-length 0')

            print ('python.el: native completion setup loaded')
        except:
            import sys
            print ('python.el: native completion setup failed, %s: %s'
                   % sys.exc_info()[:2])

    __PYTHON_EL_native_completion_setup()" process)))
        (when (string-match-p "python\\.el: native completion setup loaded"
                              output)
          (python-shell-completion-native-try))))

Then the `output' would be:

     "readline: nonexistent-variable: unknown variable name\npython.el: native completion setup loaded\n"

and

    (string-match-p "python\\.el: native completion setup loaded"
                              output)

finds the match.

I tried that in CPython and IPython.

Actually, I also tried python built with readline-6.2
(`python-shell-completion-native-setup' already uses
"show-all-if-ambiguous", first appeared in that version) and found out
that that version doesn't even print anything when nonexistent variable
is passed.

    eugene@gx:~/git (gx)
    $ guix shell --with-input=readline=readline@6.2 python 
    eugene@gx:~/git (gx) [env]
    $ which python3
    /gnu/store/hhcay3d42k3k7avahrmlqv3qnjk2l6wp-profile/bin/python3
    eugene@gx:~/git (gx) [env]
    $ echo $GUIX_ENVIRONMENT 
    /gnu/store/hhcay3d42k3k7avahrmlqv3qnjk2l6wp-profile
    eugene@gx:~/git (gx) [env]
    $ python3
    Python 3.10.7 (main, Jan  1 1970, 00:00:01) [GCC 11.3.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import readline
    >>> readline.parse_and_bind("set nonexistent-variable off")
    >>> 

[1] https://git.sv.gnu.org/cgit/emacs.git/tree/lisp/progmodes/python.el?h=master#n119





  reply	other threads:[~2024-08-28 20:39 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-17 16:20 bug#45938: 28.0.50; python native completion fails with fancier readline settings Zoltán Vandrus
2022-06-07 14:05 ` Lars Ingebrigtsen
2024-08-27 21:47 ` bug#45938: [PATCH] Avoid ANSI escape characters (bug#45938) Evgenii Klimov via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-08-28 12:00   ` Eli Zaretskii
2024-08-28 16:28     ` Evgenii Klimov via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-08-28 17:55       ` Eli Zaretskii
2024-08-28 20:39         ` Evgenii Klimov via Bug reports for GNU Emacs, the Swiss army knife of text editors [this message]
2024-08-29  4:47           ` Eli Zaretskii
2024-08-29 12:20             ` kobarity
2024-08-31 10:14               ` Eli Zaretskii

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=87le0g8jr4.fsf@lipklim.org \
    --to=bug-gnu-emacs@gnu.org \
    --cc=45938@debbugs.gnu.org \
    --cc=eliz@gnu.org \
    --cc=eugene.dev@lipklim.org \
    --cc=kobarity@gmail.com \
    /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.