[Python-checkins] gh-92345: Import rlcompleter before sys.path is extended (#92346)

tiran webhook-mailer at python.org
Thu May 5 15:24:26 EDT 2022


https://github.com/python/cpython/commit/8122e8d5017be9f0683a49bc20d3c82e8b5398d6
commit: 8122e8d5017be9f0683a49bc20d3c82e8b5398d6
branch: main
author: Christian Heimes <christian at python.org>
committer: tiran <christian at python.org>
date: 2022-05-05T21:24:16+02:00
summary:

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``.

files:
A Misc/NEWS.d/next/Core and Builtins/2022-05-05-20-05-41.gh-issue-92345.lnN_RA.rst
M Lib/rlcompleter.py
M Modules/main.c

diff --git a/Lib/rlcompleter.py b/Lib/rlcompleter.py
index 4ede6dcce3fea..206d6fb511cdf 100644
--- a/Lib/rlcompleter.py
+++ b/Lib/rlcompleter.py
@@ -32,6 +32,8 @@
 import atexit
 import builtins
 import inspect
+import keyword
+import re
 import __main__
 
 __all__ = ["Completer"]
@@ -113,7 +115,6 @@ def global_matches(self, text):
         defined in self.namespace that match.
 
         """
-        import keyword
         matches = []
         seen = {"__builtins__"}
         n = len(text)
@@ -146,7 +147,6 @@ def attr_matches(self, text):
         with a __getattr__ hook is evaluated.
 
         """
-        import re
         m = re.match(r"(\w+(\.\w+)*)\.(\w*)", text)
         if not m:
             return []
diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-05-05-20-05-41.gh-issue-92345.lnN_RA.rst b/Misc/NEWS.d/next/Core and Builtins/2022-05-05-20-05-41.gh-issue-92345.lnN_RA.rst
new file mode 100644
index 0000000000000..2e2a6084f492c
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2022-05-05-20-05-41.gh-issue-92345.lnN_RA.rst	
@@ -0,0 +1,3 @@
+``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.
diff --git a/Modules/main.c b/Modules/main.c
index 2443f5631b94b..624c0f3946085 100644
--- a/Modules/main.c
+++ b/Modules/main.c
@@ -219,6 +219,13 @@ pymain_import_readline(const PyConfig *config)
     else {
         Py_DECREF(mod);
     }
+    mod = PyImport_ImportModule("rlcompleter");
+    if (mod == NULL) {
+        PyErr_Clear();
+    }
+    else {
+        Py_DECREF(mod);
+    }
 }
 
 
@@ -555,6 +562,9 @@ pymain_run_python(int *exitcode)
         }
     }
 
+    // import readline and rlcompleter before script dir is added to sys.path
+    pymain_import_readline(config);
+
     if (main_importer_path != NULL) {
         if (pymain_sys_path_add_path0(interp, main_importer_path) < 0) {
             goto error;
@@ -577,7 +587,6 @@ pymain_run_python(int *exitcode)
     }
 
     pymain_header(config);
-    pymain_import_readline(config);
 
     if (config->run_command) {
         *exitcode = pymain_run_command(config->run_command);



More information about the Python-checkins mailing list