Skip to content

Commit 8122e8d

Browse files
authored
gh-92345: Import rlcompleter before sys.path is extended (#92346)
``pymain_run_python()`` now imports ``readline`` and ``rlcompleter`` before sys.path is extended to include the current working directory of an interactive interpreter. Non-interactive interpreters are not affected. Also move imports of ``re`` and ``keyword`` module to top level so they are materialized early, too. The ``keyword`` module is trivial and the ``re`` is already imported via ``inspect`` -> ``linecache``.
1 parent 1ed8d03 commit 8122e8d

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

Lib/rlcompleter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import atexit
3333
import builtins
3434
import inspect
35+
import keyword
36+
import re
3537
import __main__
3638

3739
__all__ = ["Completer"]
@@ -113,7 +115,6 @@ def global_matches(self, text):
113115
defined in self.namespace that match.
114116
115117
"""
116-
import keyword
117118
matches = []
118119
seen = {"__builtins__"}
119120
n = len(text)
@@ -146,7 +147,6 @@ def attr_matches(self, text):
146147
with a __getattr__ hook is evaluated.
147148
148149
"""
149-
import re
150150
m = re.match(r"(\w+(\.\w+)*)\.(\w*)", text)
151151
if not m:
152152
return []
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
``pymain_run_python()`` now imports ``readline`` and ``rlcompleter`` before
2+
sys.path is extended to include the current working directory of an
3+
interactive interpreter. Non-interactive interpreters are not affected.

Modules/main.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,13 @@ pymain_import_readline(const PyConfig *config)
219219
else {
220220
Py_DECREF(mod);
221221
}
222+
mod = PyImport_ImportModule("rlcompleter");
223+
if (mod == NULL) {
224+
PyErr_Clear();
225+
}
226+
else {
227+
Py_DECREF(mod);
228+
}
222229
}
223230

224231

@@ -555,6 +562,9 @@ pymain_run_python(int *exitcode)
555562
}
556563
}
557564

565+
// import readline and rlcompleter before script dir is added to sys.path
566+
pymain_import_readline(config);
567+
558568
if (main_importer_path != NULL) {
559569
if (pymain_sys_path_add_path0(interp, main_importer_path) < 0) {
560570
goto error;
@@ -577,7 +587,6 @@ pymain_run_python(int *exitcode)
577587
}
578588

579589
pymain_header(config);
580-
pymain_import_readline(config);
581590

582591
if (config->run_command) {
583592
*exitcode = pymain_run_command(config->run_command);

0 commit comments

Comments
 (0)