aboutsummaryrefslogtreecommitdiffstats
path: root/lisp/progmodes/python.el
diff options
context:
space:
mode:
authorAugusto Stoffel2021-09-11 15:02:04 +0200
committerLars Ingebrigtsen2021-09-11 15:02:04 +0200
commit6343384348180b18be191be64d7106c4866c0675 (patch)
tree7ca35105f67736caedcf643fb61f27e574537e24 /lisp/progmodes/python.el
parentafa82b3f7f864467ebf8807d0d3b95376383d5af (diff)
downloademacs-6343384348180b18be191be64d7106c4866c0675.tar.gz
emacs-6343384348180b18be191be64d7106c4866c0675.zip
Allow using 'python-shell-send-file' across machines
* progmodes/python.el (python-shell-eval-file-setup-code): Look for a file coding cookie on the Python rather than on the Emacs side, to avoid additional file transfers. (python-shell--save-temp-file): Allow argument to be a buffer. (python-shell-send-file): Address the case where the selected file and the inferior process are on different machines (bug#50516).
Diffstat (limited to 'lisp/progmodes/python.el')
-rw-r--r--lisp/progmodes/python.el45
1 files changed, 30 insertions, 15 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index e71a8102dfe..9cf88ea656b 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -2842,14 +2842,20 @@ def __PYTHON_EL_eval(source, filename):
2842 2842
2843(defconst python-shell-eval-file-setup-code 2843(defconst python-shell-eval-file-setup-code
2844 "\ 2844 "\
2845def __PYTHON_EL_eval_file(filename, tempname, encoding, delete): 2845def __PYTHON_EL_eval_file(filename, tempname, delete):
2846 import codecs, os 2846 import codecs, os, re
2847 pattern = r'^[ \t\f]*#.*?coding[:=][ \t]*([-_.a-zA-Z0-9]+)'
2848 with codecs.open(tempname or filename, encoding='latin-1') as file:
2849 match = re.match(pattern, file.readline())
2850 match = match or re.match(pattern, file.readline())
2851 encoding = match.group(1) if match else 'utf-8'
2847 with codecs.open(tempname or filename, encoding=encoding) as file: 2852 with codecs.open(tempname or filename, encoding=encoding) as file:
2848 source = file.read().encode(encoding) 2853 source = file.read().encode(encoding)
2849 if delete and tempname: 2854 if delete and tempname:
2850 os.remove(tempname) 2855 os.remove(tempname)
2851 return __PYTHON_EL_eval(source, filename)" 2856 return __PYTHON_EL_eval(source, filename)"
2852 "Code used to evaluate files in inferior Python processes.") 2857 "Code used to evaluate files in inferior Python processes.
2858The coding cookie regexp is specified in PEP 263.")
2853 2859
2854(defun python-shell-comint-watch-for-first-prompt-output-filter (output) 2860(defun python-shell-comint-watch-for-first-prompt-output-filter (output)
2855 "Run `python-shell-first-prompt-hook' when first prompt is found in OUTPUT." 2861 "Run `python-shell-first-prompt-hook' when first prompt is found in OUTPUT."
@@ -3126,7 +3132,9 @@ there for compatibility with CEDET.")
3126 (temp-file-name (make-temp-file "py")) 3132 (temp-file-name (make-temp-file "py"))
3127 (coding-system-for-write (python-info-encoding))) 3133 (coding-system-for-write (python-info-encoding)))
3128 (with-temp-file temp-file-name 3134 (with-temp-file temp-file-name
3129 (insert string) 3135 (if (bufferp string)
3136 (insert-buffer-substring string)
3137 (insert string))
3130 (delete-trailing-whitespace)) 3138 (delete-trailing-whitespace))
3131 temp-file-name)) 3139 temp-file-name))
3132 3140
@@ -3402,11 +3410,15 @@ t when called interactively."
3402(defun python-shell-send-file (file-name &optional process temp-file-name 3410(defun python-shell-send-file (file-name &optional process temp-file-name
3403 delete msg) 3411 delete msg)
3404 "Send FILE-NAME to inferior Python PROCESS. 3412 "Send FILE-NAME to inferior Python PROCESS.
3413
3405If TEMP-FILE-NAME is passed then that file is used for processing 3414If TEMP-FILE-NAME is passed then that file is used for processing
3406instead, while internally the shell will continue to use 3415instead, while internally the shell will continue to use
3407FILE-NAME. If TEMP-FILE-NAME and DELETE are non-nil, then 3416FILE-NAME. FILE-NAME can be remote, but TEMP-FILE-NAME must be
3408TEMP-FILE-NAME is deleted after evaluation is performed. When 3417in the same host as PROCESS. If TEMP-FILE-NAME and DELETE are
3409optional argument MSG is non-nil, forces display of a 3418non-nil, then TEMP-FILE-NAME is deleted after evaluation is
3419performed.
3420
3421When optional argument MSG is non-nil, forces display of a
3410user-friendly message if there's no process running; defaults to 3422user-friendly message if there's no process running; defaults to
3411t when called interactively." 3423t when called interactively."
3412 (interactive 3424 (interactive
@@ -3416,22 +3428,25 @@ t when called interactively."
3416 nil ; temp-file-name 3428 nil ; temp-file-name
3417 nil ; delete 3429 nil ; delete
3418 t)) ; msg 3430 t)) ; msg
3419 (let* ((process (or process (python-shell-get-process-or-error msg))) 3431 (setq process (or process (python-shell-get-process-or-error msg)))
3420 (encoding (with-temp-buffer 3432 (with-current-buffer (process-buffer process)
3421 (insert-file-contents 3433 (unless (or temp-file-name
3422 (or temp-file-name file-name)) 3434 (string= (file-remote-p file-name)
3423 (python-info-encoding))) 3435 (file-remote-p default-directory)))
3424 (file-name (file-local-name (expand-file-name file-name))) 3436 (setq delete t
3437 temp-file-name (with-temp-buffer
3438 (insert-file-contents file-name)
3439 (python-shell--save-temp-file (current-buffer))))))
3440 (let* ((file-name (file-local-name (expand-file-name file-name)))
3425 (temp-file-name (when temp-file-name 3441 (temp-file-name (when temp-file-name
3426 (file-local-name (expand-file-name 3442 (file-local-name (expand-file-name
3427 temp-file-name))))) 3443 temp-file-name)))))
3428 (comint-send-string 3444 (comint-send-string
3429 process 3445 process
3430 (format 3446 (format
3431 "__PYTHON_EL_eval_file(%s, %s, %s, %s)\n" 3447 "__PYTHON_EL_eval_file(%s, %s, %s)\n"
3432 (python-shell--encode-string file-name) 3448 (python-shell--encode-string file-name)
3433 (python-shell--encode-string (or temp-file-name "")) 3449 (python-shell--encode-string (or temp-file-name ""))
3434 (python-shell--encode-string (symbol-name encoding))
3435 (if delete "True" "False"))))) 3450 (if delete "True" "False")))))
3436 3451
3437(defun python-shell-switch-to-shell (&optional msg) 3452(defun python-shell-switch-to-shell (&optional msg)