master d0087b33461: Sanitize sys.path when executing Python import management

Eli Zaretskii <[email protected]>
Newsgroups gmane.emacs.diffs
Message-ID <[email protected]>
branch: master
commit d0087b33461de703b6f69eefefa5f42821f57a76
Author: kobarity <[email protected]>
Commit: Eli Zaretskii <[email protected]>

    Sanitize sys.path when executing Python import management
    
    * lisp/progmodes/python.el (python--list-imports): Add
    sanitizing sys.path.
    (python--run-module): New constant.
    (python--do-isort, python-fix-imports): Use it.  (Bug#70440)
---
 lisp/progmodes/python.el | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)

diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index 86336979067..962f9c5a031 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -7056,6 +7056,10 @@ REPORT-FN is Flymake's callback function."
 
 ;;; Import management
 (defconst python--list-imports "\
+import sys
+
+sys.path = [p for p in sys.path if p]
+
 from sys import argv, exit, stdin
 
 try:
@@ -7164,6 +7168,17 @@ PROMPT string, is made unless there is a single candidate."
     (unless (string-empty-p statement)
       statement)))
 
+(defconst python--run-module "\
+import runpy, sys
+
+sys.path = [p for p in sys.path if p]
+
+runpy.run_module(sys.argv.pop(1), run_name='__main__', alter_sys=True)
+"
+  "Script to run a Python module.
+It removes the leading empty string from `sys.path' before executing the
+module.")
+
 (defun python--do-isort (&rest args)
   "Edit the current buffer using isort called with ARGS.
 Return non-nil if the buffer was actually modified."
@@ -7178,7 +7193,7 @@ Return non-nil if the buffer was actually modified."
                                (append
                                  (split-string-shell-command
                                   python-interpreter-args)
-                                 '("-m" "isort" "-")
+                                 `("-c" ,python--run-module "isort" "-")
                                  args)))
                 (tick (buffer-chars-modified-tick)))
             (unless (eq 0 status)
@@ -7272,7 +7287,7 @@ and may appear to hang."
                   (append
                    (split-string-shell-command
                     python-interpreter-args)
-                   '("-m" "pyflakes"))))
+                   `("-c" ,python--run-module "pyflakes"))))
         (goto-char (point-min))
         (when (looking-at-p ".* No module named pyflakes$")
           (error "%s couldn't find pyflakes" python-interpreter))
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.