unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Augusto Stoffel <arstoffel@gmail.com>
To: 50516@debbugs.gnu.org
Subject: bug#50516: Allow using 'python-shell-send-file' across machines
Date: Sat, 11 Sep 2021 09:03:19 +0200	[thread overview]
Message-ID: <87ee9vd26g.fsf@gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 426 bytes --]

Tags: patch

This addresses the case where you do 'C-c C-l' (python-shell-send-file)
and the selected file is in a different machine than the Python process.
(This is different from a patch I sent a while back, which concerned
non-interactive use and ensured that the temp files were created in the
right machine.)

And I think this would be the last change I have to propose in the
Python shell for the time being.  Thanks!


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Allow-using-python-shell-send-file-across-machines.patch --]
[-- Type: text/patch, Size: 4968 bytes --]

From c32fd5541f97cc8e527f38c18ce7bc68b81a5513 Mon Sep 17 00:00:00 2001
From: Augusto Stoffel <arstoffel@gmail.com>
Date: Sat, 11 Sep 2021 08:27:25 +0200
Subject: [PATCH] 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.
---
 lisp/progmodes/python.el | 45 ++++++++++++++++++++++++++--------------
 1 file changed, 30 insertions(+), 15 deletions(-)

diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index 2eef52de0c..22c4dfb375 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -2842,14 +2842,20 @@ python-shell-eval-setup-code
 
 (defconst python-shell-eval-file-setup-code
   "\
-def __PYTHON_EL_eval_file(filename, tempname, encoding, delete):
-    import codecs, os
+def __PYTHON_EL_eval_file(filename, tempname, delete):
+    import codecs, os, re
+    pattern = r'^[ \t\f]*#.*?coding[:=][ \t]*([-_.a-zA-Z0-9]+)'
+    with codecs.open(tempname or filename, encoding='latin-1') as file:
+        match = re.match(pattern, file.readline())
+        match = match or re.match(pattern, file.readline())
+        encoding = match.group(1) if match else 'utf-8'
     with codecs.open(tempname or filename, encoding=encoding) as file:
         source = file.read().encode(encoding)
     if delete and tempname:
         os.remove(tempname)
     return __PYTHON_EL_eval(source, filename)"
-  "Code used to evaluate files in inferior Python processes.")
+  "Code used to evaluate files in inferior Python processes.
+The coding cookie regexp is specified in PEP 263.")
 
 (defun python-shell-comint-watch-for-first-prompt-output-filter (output)
   "Run `python-shell-first-prompt-hook' when first prompt is found in OUTPUT."
@@ -3126,7 +3132,9 @@ python-shell--save-temp-file
          (temp-file-name (make-temp-file "py"))
          (coding-system-for-write (python-info-encoding)))
     (with-temp-file temp-file-name
-      (insert string)
+      (if (bufferp string)
+          (insert-buffer-substring string)
+        (insert string))
       (delete-trailing-whitespace))
     temp-file-name))
 
@@ -3402,11 +3410,15 @@ python-shell-send-defun
 (defun python-shell-send-file (file-name &optional process temp-file-name
                                          delete msg)
   "Send FILE-NAME to inferior Python PROCESS.
+
 If TEMP-FILE-NAME is passed then that file is used for processing
 instead, while internally the shell will continue to use
-FILE-NAME.  If TEMP-FILE-NAME and DELETE are non-nil, then
-TEMP-FILE-NAME is deleted after evaluation is performed.  When
-optional argument MSG is non-nil, forces display of a
+FILE-NAME.  FILE-NAME can be remote, but TEMP-FILE-NAME must be
+in the same host as PROCESS.  If TEMP-FILE-NAME and DELETE are
+non-nil, then TEMP-FILE-NAME is deleted after evaluation is
+performed.
+
+When optional argument MSG is non-nil, forces display of a
 user-friendly message if there's no process running; defaults to
 t when called interactively."
   (interactive
@@ -3416,22 +3428,25 @@ python-shell-send-file
     nil                                 ; temp-file-name
     nil                                 ; delete
     t))                                 ; msg
-  (let* ((process (or process (python-shell-get-process-or-error msg)))
-         (encoding (with-temp-buffer
-                     (insert-file-contents
-                      (or temp-file-name file-name))
-                     (python-info-encoding)))
-         (file-name (file-local-name (expand-file-name file-name)))
+  (setq process (or process (python-shell-get-process-or-error msg)))
+  (with-current-buffer (process-buffer process)
+    (unless (or temp-file-name
+                (string= (file-remote-p file-name)
+                         (file-remote-p default-directory)))
+      (setq delete t
+            temp-file-name (with-temp-buffer
+                             (insert-file-contents file-name)
+                             (python-shell--save-temp-file (current-buffer))))))
+  (let* ((file-name (file-local-name (expand-file-name file-name)))
          (temp-file-name (when temp-file-name
                            (file-local-name (expand-file-name
                                              temp-file-name)))))
     (comint-send-string
      process
      (format
-      "__PYTHON_EL_eval_file(%s, %s, %s, %s)\n"
+      "__PYTHON_EL_eval_file(%s, %s, %s)\n"
       (python-shell--encode-string file-name)
       (python-shell--encode-string (or temp-file-name ""))
-      (python-shell--encode-string (symbol-name encoding))
       (if delete "True" "False")))))
 
 (defun python-shell-switch-to-shell (&optional msg)
-- 
2.31.1


             reply	other threads:[~2021-09-11  7:03 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-11  7:03 Augusto Stoffel [this message]
2021-09-11 13:02 ` bug#50516: Allow using 'python-shell-send-file' across machines Lars Ingebrigtsen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87ee9vd26g.fsf@gmail.com \
    --to=arstoffel@gmail.com \
    --cc=50516@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).