Mailing List Archive

1 2  View All
Re: Baffled by readline module [ In reply to ]
On 2023-03-09 at 19:11:56 -0800,
Grant Edwards <grant.b.edwards@gmail.com> wrote:

> On 2023-03-10, 2QdxY4RzWzUUiLuE@potatochowder.com <2QdxY4RzWzUUiLuE@potatochowder.com> wrote:

> If you run this application from the command line, you get command
> recall and editing when entering strings at the "cmd:" prompt?
>
> #!/usr/bin/python
> while True:
> try:
> line = input('cmd: ')
> except EOFError:
> print()
> break
> print('You entered "%s"' % line)

In my case, no.
--
https://mail.python.org/mailman/listinfo/python-list
Re: Baffled by readline module [ In reply to ]
On 10/03/23 2:57 pm, Chris Angelico wrote:
> import sys; "readline" in sys.modules
>
> Is it?

Yes, it is -- but only when using the repl!
If I put that in a script, I get False.

My current theory is that it gets pre-imported when using
Python interactively because the repl itself uses it.

--
Greg
--
https://mail.python.org/mailman/listinfo/python-list
Re: Baffled by readline module [ In reply to ]
On 10/03/23 4:00 pm, 2QdxY4RzWzUUiLuE@potatochowder.com wrote:
> My ~/.pythonrc contains the following:
>
> import readline
> import rlcompleter
> readline.parse_and_bind( 'tab: complete' )

I don't have a ~/.pythonrc, so that's not what's doing it
for me.

--
Greg
--
https://mail.python.org/mailman/listinfo/python-list
Re: Baffled by readline module [ In reply to ]
On 3/9/23 20:31, 2QdxY4RzWzUUiLuE@potatochowder.com wrote:

>
> I was with you until that part of the small wooden box. :-)
>
So was I, but we all put that off as long as we can. I retired 22 years ago.

>> We're not really an industry that has a concept of retirement.
>
> Which is why I'm still here (on this mailing list, and a handful of
> others like it).
>
More echo. And doing things, albeit at a slower pace, that I always
wanted to do.

Take care and stay well all.

Cheers, Gene Heskett.
--
"There are four boxes to be used in defense of liberty:
soap, ballot, jury, and ammo. Please use in that order."
-Ed Howdershelt (Author, 1940)
If we desire respect for the law, we must first make the law respectable.
- Louis D. Brandeis
Genes Web page <http://geneslinuxbox.net:6309/>

--
https://mail.python.org/mailman/listinfo/python-list
Re: Baffled by readline module [ In reply to ]
I would say, ?No, readline is not the right tool.?

cmd.Cmd is: https://docs.python.org/3/library/cmd.html. I have a couple of cmd.Cmd modules, one of which I use daily and the other weekly.

From: Python-list <python-list-bounces+gweatherby=uchc.edu@python.org> on behalf of Grant Edwards <grant.b.edwards@gmail.com>
Date: Thursday, March 9, 2023 at 2:29 PM
To: python-list@python.org <python-list@python.org>
Subject: Baffled by readline module
*** Attention: This is an external email. Use caution responding, opening attachments or clicking on links. ***

In an interactive command-line Python program on Linux, I want to be
able to read a line at a time from stdin, providing command line
history and editing to the user. In C, I would use GNU readline to do
that.

Python has the readline module, which appears to be a wrapper for GNU
readline. However, I've read and re-read the documenation for that
module, but I'm completely baffled. There's all sorts of stuff about
history files, but I neither need nor want a history file. Likewise
tab-completion: don't want it. All the examples not only involve
history files and tab-completion but they're somehow connected to the
interactive Python REPL, which is also completely off-base for my use
case.

Is the readline module not the right tool for an interactive Linux
command-line application that needs to provide command line recall and
editing to the user who's entering stuff on stdin from a tty?

--
https://urldefense.com/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!iMrZQmCvzTBhFnzmb4Px34UNZrxSWMjU1Hxvg9CTc_-3tFWtjxB3-OrQUrXChOoNP8vm3em2V1XxihY0742KUNLDJnrV$<https://urldefense.com/v3/__https:/mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!iMrZQmCvzTBhFnzmb4Px34UNZrxSWMjU1Hxvg9CTc_-3tFWtjxB3-OrQUrXChOoNP8vm3em2V1XxihY0742KUNLDJnrV$>
--
https://mail.python.org/mailman/listinfo/python-list
Re: Baffled by readline module [ In reply to ]
On 2023-03-10, Weatherby,Gerard <gweatherby@uchc.edu> wrote:

> I would say, “No, readline is not the right tool.”
>
> cmd.Cmd is: https://docs.python.org/3/library/cmd.html. I have a
> couple of cmd.Cmd modules, one of which I use daily and the other
> weekly.

I'll have to remember that one. It doesn't really fit my current use
case, but there are others where it would work nicely.

However, cmd.Cmd does not provide command recall and
editing. According to the page above, that's provided by the readline
module:

If the readline module is loaded, input will automatically
inherit bash-like history-list editing (e.g. Control-P scrolls
back to the last command, Control-N forward to the next one,
Control-F moves the cursor to the right non-destructively,
Control-B moves the cursor to the left non-destructively, etc.).

--
Grant
--
https://mail.python.org/mailman/listinfo/python-list
Re: Baffled by readline module [ In reply to ]
On our Linux systems, I can up-arrow to go back to prior commands and use the left and right arrows to navigate a line. The functionality may be provided internally by readline. I?ve never had to dig into it because it meets my needs out of the box.

From: Python-list <python-list-bounces+gweatherby=uchc.edu@python.org> on behalf of Grant Edwards <grant.b.edwards@gmail.com>
Date: Friday, March 10, 2023 at 9:39 AM
To: python-list@python.org <python-list@python.org>
Subject: Re: Baffled by readline module
*** Attention: This is an external email. Use caution responding, opening attachments or clicking on links. ***

On 2023-03-10, Weatherby,Gerard <gweatherby@uchc.edu> wrote:

> I would say, ?No, readline is not the right tool.?
>
> cmd.Cmd is: https://urldefense.com/v3/__https://docs.python.org/3/library/cmd.html__;!!Cn_UX_p3!iH9PMR4wNfv3oBLWR_W46RY8KUzLVxsA_XRv2RJk6Td_ND59EaBvcJ58-Crx2PReX-ELMx7IEMAQ1YfpIt7xmHBGs8a1$<https://urldefense.com/v3/__https:/docs.python.org/3/library/cmd.html__;!!Cn_UX_p3!iH9PMR4wNfv3oBLWR_W46RY8KUzLVxsA_XRv2RJk6Td_ND59EaBvcJ58-Crx2PReX-ELMx7IEMAQ1YfpIt7xmHBGs8a1$> . I have a
> couple of cmd.Cmd modules, one of which I use daily and the other
> weekly.

I'll have to remember that one. It doesn't really fit my current use
case, but there are others where it would work nicely.

However, cmd.Cmd does not provide command recall and
editing. According to the page above, that's provided by the readline
module:

If the readline module is loaded, input will automatically
inherit bash-like history-list editing (e.g. Control-P scrolls
back to the last command, Control-N forward to the next one,
Control-F moves the cursor to the right non-destructively,
Control-B moves the cursor to the left non-destructively, etc.).

--
Grant
--
https://urldefense.com/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!iH9PMR4wNfv3oBLWR_W46RY8KUzLVxsA_XRv2RJk6Td_ND59EaBvcJ58-Crx2PReX-ELMx7IEMAQ1YfpIt7xmEU_EBpr$<https://urldefense.com/v3/__https:/mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!iH9PMR4wNfv3oBLWR_W46RY8KUzLVxsA_XRv2RJk6Td_ND59EaBvcJ58-Crx2PReX-ELMx7IEMAQ1YfpIt7xmEU_EBpr$>
--
https://mail.python.org/mailman/listinfo/python-list
Re: Baffled by readline module [ In reply to ]
On 2023-03-10, Weatherby,Gerard <gweatherby@uchc.edu> wrote:

> On our Linux systems, I can up-arrow to go back to prior commands
> and use the left and right arrows to navigate a line. The
> functionality may be provided internally by readline. I’ve never had
> to dig into it because it meets my needs out of the box.

Apparently the cmd.Cmd docs are wrong. It says:

If the readline module is loaded, input will automatically
inherit bash-like history-list editing (e.g. Control-P scrolls
back to the last command, Control-N forward to the next one,
Control-F moves the cursor to the right non-destructively,
Control-B moves the cursor to the left non-destructively, etc.).

On my Python 3.10.10 Linux system, cmd.Com itself is importing the
readline module unconditionally when I call cmdloop(). There's no 'if'
about it.

[.It's moot for my current usage, because my application allows
multiple commands per input line, and I don't see how cmd.Cmd can
support that.]

Here's my application that seem to show that cmd.Cmd.cmdloop() is
importing the readline module:

-------------------------------testit.py--------------------------------
import sys
print('readline' in sys.modules)
import cmd
print('readline' in sys.modules)

class MyShell(cmd.Cmd):
intro = 'Welcome to my shell. Type help or ? to list commands.\n'
prompt = '(what) '
file = None

def do_what(self,arg):
print('readline' in sys.modules)
def do_bye(self, arg):
print('Good bye')
return True

print('readline' in sys.modules)

if __name__ == '__main__':
print('readline' in sys.modules)
MyShell().cmdloop()
------------------------------------------------------------------------

And here's what I see when I run it:

------------------------------------------------------------------------
$ python testit.py
False
False
False
False
Welcome to my shell. Type help or ? to list commands.

(what) what
True
(what) bye
Good bye
$
------------------------------------------------------------------------


--
https://mail.python.org/mailman/listinfo/python-list
Re: Baffled by readline module [ In reply to ]
On Fri, Mar 10, 2023 at 06:37:56AM -0800, Grant Edwards wrote:
>On 2023-03-10, Weatherby,Gerard <gweatherby@uchc.edu> wrote:
>I'll have to remember that one. It doesn't really fit my current use
>case, but there are others where it would work nicely.
>
>However, cmd.Cmd does not provide command recall and
>editing. According to the page above, that's provided by the readline
>module

There is also prompt_toolkit[1] for even more fancy command?line
handling. The documentation even states it “can be a very advanced pure
Python replacement for GNU readline, but it can also be used for
building full screen applications.” (It doesn’t depend on readline or
libedit.) It’s used by IPython for its history, editing, and completion
features. If cmd with readline is overkill for your use case then this
is even more so, but I thought it worth a mention.

Simon
--
https://mail.python.org/mailman/listinfo/python-list
Re: Baffled by readline module [ In reply to ]
On 3/9/23, Greg Ewing via Python-list <python-list@python.org> wrote:
> On 10/03/23 4:00 pm, 2QdxY4RzWzUUiLuE@potatochowder.com wrote:
>> My ~/.pythonrc contains the following:
>>
>> import readline
>> import rlcompleter
>> readline.parse_and_bind( 'tab: complete' )
>
> I don't have a ~/.pythonrc, so that's not what's doing it
> for me.

If it's available, the readline and rlcompleter modules are imported
if stdin is a tty, [I]solated mode isn't enabled, and either [i]nspect
is enabled or the REPL is being run. For example:

$ python -c "import sys;print('readline' in sys.modules)"
False

$ python -ic "import sys;print('readline' in sys.modules)"
True
>>>

$ python -ic "import sys;print('readline' in sys.modules)" 0</dev/null
False
>>>

$ python -Iic "import sys;print('readline' in sys.modules)"
False
>>>

This is determined by the following function in "Modules/main.c:"

pymain_import_readline(const PyConfig *config)
{
if (config->isolated) {
return;
}
if (!config->inspect && config_run_code(config)) {
return;
}
if (!isatty(fileno(stdin))) {
return;
}

PyObject *mod = PyImport_ImportModule("readline");
if (mod == NULL) {
PyErr_Clear();
}
else {
Py_DECREF(mod);
}
mod = PyImport_ImportModule("rlcompleter");
if (mod == NULL) {
PyErr_Clear();
}
else {
Py_DECREF(mod);
}
}

The hook for input() is handled by PyOS_Readline() in
"Parser/myreadline.c". It depends on the exported function pointer
PyOS_ReadlineFunctionPointer. When imported, the readline extension
module in "Modules/readline.c" sets PyOS_ReadlineFunctionPointer to
its call_readline() function.

---

On Windows, Python is not distributed with a readline module. Instead,
Python relies on the console host's builtin support for command-line
editing, history, and aliases.

The Windows console API function ReadConsoleW() has a parameter that
supports implementing custom command-line completion. This is how the
CMD shell implements tab completion for file paths. However, no one
has developed a version of rlcompleter for the Windows console.
Instead, there's a third-party "pyreadline3" package (note the "3";
don't install "pyreadline" by mistake) that implements readline for
the Windows console, which supports rlcompleter.
--
https://mail.python.org/mailman/listinfo/python-list
Re: Baffled by readline module [ In reply to ]
On 10Mar2023 09:12, Grant Edwards <grant.b.edwards@gmail.com> wrote:
>On 2023-03-10, Weatherby,Gerard <gweatherby@uchc.edu> wrote:
>> On our Linux systems, I can up-arrow to go back to prior commands
>> and use the left and right arrows to navigate a line. The
>> functionality may be provided internally by readline. I’ve never had
>> to dig into it because it meets my needs out of the box.
>
>Apparently the cmd.Cmd docs are wrong. It says:
>
> If the readline module is loaded, input will automatically
> inherit bash-like history-list editing (e.g. Control-P scrolls
> back to the last command, Control-N forward to the next one,
> Control-F moves the cursor to the right non-destructively,
> Control-B moves the cursor to the left non-destructively, etc.).
>
>On my Python 3.10.10 Linux system, cmd.Com itself is importing the
>readline module unconditionally when I call cmdloop(). There's no 'if'
>about it.

I was wondering about that myself, whether this is an accident of
phrasing. It doesn't say "is imported", so maybe the author was thinking
"if readline's part of the install" here.

Anyway, I've got a try/import-readline/except-importerror/pass in my
cmd.Cmd wrapper, because I read this the way you read it.

Cheers,
Cameron Simpson <cs@cskk.id.au>
--
https://mail.python.org/mailman/listinfo/python-list
Re: Baffled by readline module [ In reply to ]
On 2023-03-10, Cameron Simpson <cs@cskk.id.au> wrote:
> On 10Mar2023 09:12, Grant Edwards <grant.b.edwards@gmail.com> wrote:
>>On 2023-03-10, Weatherby,Gerard <gweatherby@uchc.edu> wrote:
>>> On our Linux systems, I can up-arrow to go back to prior commands
>>> and use the left and right arrows to navigate a line. The
>>> functionality may be provided internally by readline. I’ve never had
>>> to dig into it because it meets my needs out of the box.
>>
>>Apparently the cmd.Cmd docs are wrong. It says:
>>
>> If the readline module is loaded, input will automatically
>> inherit bash-like history-list editing (e.g. Control-P scrolls
>> back to the last command, Control-N forward to the next one,
>> Control-F moves the cursor to the right non-destructively,
>> Control-B moves the cursor to the left non-destructively, etc.).
>>
>>On my Python 3.10.10 Linux system, cmd.Com itself is importing the
>>readline module unconditionally when I call cmdloop(). There's no 'if'
>>about it.
>
> I was wondering about that myself, whether this is an accident of
> phrasing. It doesn't say "is imported", so maybe the author was thinking
> "if readline's part of the install" here.

Ah, that never ocurred to me. I understood "loaded" to mean "imported"
without giving it a second thought. It probably does mean supported.



--
https://mail.python.org/mailman/listinfo/python-list
Re: Baffled by readline module [ In reply to ]
This is the implementation of cmd.Cmd (Python 3.9). As you can see in cmdloop(), the import is already wrapped, and the readline feature can be turned off explicitly by passing None to the completekey in the constructor.

def __init__(self, completekey='tab', stdin=None, stdout=None):
"""Instantiate a line-oriented interpreter framework.

The optional argument 'completekey' is the readline name of a
completion key; it defaults to the Tab key. If completekey is
not None and the readline module is available, command completion
is done automatically. The optional arguments stdin and stdout
specify alternate input and output file objects; if not specified,
sys.stdin and sys.stdout are used.

"""
if stdin is not None:
self.stdin = stdin
else:
self.stdin = sys.stdin
if stdout is not None:
self.stdout = stdout
else:
self.stdout = sys.stdout
self.cmdqueue = []
self.completekey = completekey

def cmdloop(self, intro=None):
"""Repeatedly issue a prompt, accept input, parse an initial prefix
off the received input, and dispatch to action methods, passing them
the remainder of the line as argument.

"""

self.preloop()
if self.use_rawinput and self.completekey:
try:
import readline
self.old_completer = readline.get_completer()
readline.set_completer(self.complete)
readline.parse_and_bind(self.completekey+": complete")
except ImportError:
pass

From: Python-list <python-list-bounces+gweatherby=uchc.edu@python.org> on behalf of Cameron Simpson <cs@cskk.id.au>
Date: Friday, March 10, 2023 at 5:15 PM
To: python-list@python.org <python-list@python.org>
Subject: Re: Baffled by readline module
*** Attention: This is an external email. Use caution responding, opening attachments or clicking on links. ***

On 10Mar2023 09:12, Grant Edwards <grant.b.edwards@gmail.com> wrote:
>On 2023-03-10, Weatherby,Gerard <gweatherby@uchc.edu> wrote:
>> On our Linux systems, I can up-arrow to go back to prior commands
>> and use the left and right arrows to navigate a line. The
>> functionality may be provided internally by readline. I?ve never had
>> to dig into it because it meets my needs out of the box.
>
>Apparently the cmd.Cmd docs are wrong. It says:
>
> If the readline module is loaded, input will automatically
> inherit bash-like history-list editing (e.g. Control-P scrolls
> back to the last command, Control-N forward to the next one,
> Control-F moves the cursor to the right non-destructively,
> Control-B moves the cursor to the left non-destructively, etc.).
>
>On my Python 3.10.10 Linux system, cmd.Com itself is importing the
>readline module unconditionally when I call cmdloop(). There's no 'if'
>about it.

I was wondering about that myself, whether this is an accident of
phrasing. It doesn't say "is imported", so maybe the author was thinking
"if readline's part of the install" here.

Anyway, I've got a try/import-readline/except-importerror/pass in my
cmd.Cmd wrapper, because I read this the way you read it.

Cheers,
Cameron Simpson <cs@cskk.id.au>
--
https://urldefense.com/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!jjJFSxKkzPwZf_NLr44bZNCgMKRu0CPwvbh-jItjkqH_B2mJzTCW4ijlNHg8ysJpyMcCbaaa0Prf5SI$<https://urldefense.com/v3/__https:/mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!jjJFSxKkzPwZf_NLr44bZNCgMKRu0CPwvbh-jItjkqH_B2mJzTCW4ijlNHg8ysJpyMcCbaaa0Prf5SI$>
--
https://mail.python.org/mailman/listinfo/python-list
Re: Baffled by readline module [ In reply to ]
On 10Mar2023 23:11, Weatherby,Gerard <gweatherby@uchc.edu> wrote:
>This is the implementation of cmd.Cmd (Python 3.9). As you can see in cmdloop(), the import is already wrapped, and the readline feature can be turned off explicitly by passing None to the completekey in the constructor.

This isn't strictly true, as I read it.

This says that if you supply a `completekey`, _then_ `cmdloop` will try
to import `readline` and set up working completion for that key.

It _doesn't_ say that readline is or is not automatically active for its
other features (command line editing, history etc).

Having a gander further down the `cmdloop` function we see:

while not stop:
if self.cmdqueue:
line = self.cmdqueue.pop(0)
else:
if self.use_rawinput:
try:
line = input(self.prompt)
except EOFError:
line = 'EOF'

and on a Python 3.10 here we see:

>>> help(input)
Help on built-in function input in module builtins:

input(prompt=None, /)
Read a string from standard input. The trailing newline is stripped.

The prompt string, if given, is printed to standard output without a
trailing newline before reading input.

If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError.
On *nix systems, readline is used if available.

which says to me that readline is used if available, regardless of
whether it is already imported.

Cheers,
Cameron Simpson <cs@cskk.id.au>
--
https://mail.python.org/mailman/listinfo/python-list

1 2  View All