Baffled by readline module
Eryk Sun
eryksun at gmail.com
Fri Mar 10 13:11:34 EST 2023
On 3/9/23, Greg Ewing via Python-list <python-list at python.org> wrote:
> On 10/03/23 4:00 pm, 2QdxY4RzWzUUiLuE at 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.
More information about the Python-list
mailing list