From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Augusto Stoffel Newsgroups: gmane.emacs.bugs Subject: bug#50516: Allow using 'python-shell-send-file' across machines Date: Sat, 11 Sep 2021 09:03:19 +0200 Message-ID: <87ee9vd26g.fsf@gmail.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="3394"; mail-complaints-to="usenet@ciao.gmane.io" To: 50516@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sat Sep 11 09:04:11 2021 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mOx3a-0000lv-JK for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 11 Sep 2021 09:04:10 +0200 Original-Received: from localhost ([::1]:52068 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mOx3Y-0005ed-OI for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 11 Sep 2021 03:04:08 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:36684) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mOx3S-0005dp-IE for bug-gnu-emacs@gnu.org; Sat, 11 Sep 2021 03:04:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:56166) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mOx3S-00006r-Ay for bug-gnu-emacs@gnu.org; Sat, 11 Sep 2021 03:04:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1mOx3S-0006Wa-0Q for bug-gnu-emacs@gnu.org; Sat, 11 Sep 2021 03:04:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Augusto Stoffel Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sat, 11 Sep 2021 07:04:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 50516 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.163134380925035 (code B ref -1); Sat, 11 Sep 2021 07:04:01 +0000 Original-Received: (at submit) by debbugs.gnu.org; 11 Sep 2021 07:03:29 +0000 Original-Received: from localhost ([127.0.0.1]:39479 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mOx2u-0006Vi-Kk for submit@debbugs.gnu.org; Sat, 11 Sep 2021 03:03:28 -0400 Original-Received: from lists.gnu.org ([209.51.188.17]:55464) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mOx2s-0006Vb-TB for submit@debbugs.gnu.org; Sat, 11 Sep 2021 03:03:27 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:36622) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mOx2s-0005Zc-NG for bug-gnu-emacs@gnu.org; Sat, 11 Sep 2021 03:03:26 -0400 Original-Received: from mail-wm1-x334.google.com ([2a00:1450:4864:20::334]:42665) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mOx2q-0007zL-Td for bug-gnu-emacs@gnu.org; Sat, 11 Sep 2021 03:03:26 -0400 Original-Received: by mail-wm1-x334.google.com with SMTP id u19-20020a7bc053000000b002f8d045b2caso2848184wmc.1 for ; Sat, 11 Sep 2021 00:03:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:subject:date:message-id:mime-version; bh=J8wIG7cuAV+88WxuBNScAZ/qt3i6VB1x85vPNAoPK9U=; b=bKcTLWx2AatbfCgz2TknMkQtbRfNAtkd7k1oJDr5lQo32jx4aSDWxrHDlsE/MjqTZI fCHkuxvY73G0jMmEeOfGPCGSgbMLtMNPK9ugaeNQ0iT8iwVazi9fZ+tIvBlAjxYbMLwR wXzGwTLzRmpvR92PyaIyTXyxmpBqSHOdiRtuefvMeGX3FIHiULK7+o7RmsaDUbB8bgD5 LMv1kyLni5MggzHtVPCFr6fd4y/JHDbhTGSmJKXyn8roEhnct4S4d7jMcgv985rKD+tN jC5vkMpBmuzQ6sQ7gWAvT1Om1AtHzHYb/URJXKoTMgfjJLIn3HCOWraqx55aS2t/RFHE aMgw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:mime-version; bh=J8wIG7cuAV+88WxuBNScAZ/qt3i6VB1x85vPNAoPK9U=; b=J+RmkXB8T03F4yi8ErXd3OMaQ3wix/BWveYRsa6Q0I9vs/AqGR8MZdqIXJW1g5uw3c k4ZsPKz+EoU/FR8temgF8qefe/QJVrGD+fvH68+z0I2DC1AyGVS+k/BFXrCwACgMFnS+ JHCP64Jmdwe2qYp/LzoC4UAliJmLGFuRU5sW2ExNd1uj/B27OiKDBSQFW+B7LlPoZH05 ZPiHBKlZpbKChrKu6NSqm1CEqKUNf0fPf4vS+mBAk00TOYVQJgsnki2zOekrvJu/QG3b PoiG9Zh+ejMc4HbLEkYjuVyaeIAJ7heW5fE4iwvIiIi4p/TtO0E1noKN2wQpVeOyqw88 aKPQ== X-Gm-Message-State: AOAM533KCYBxMQW1UC6lorVzGRTFEYolzx/wZcZ0Sng21mKvrv4wxL2Y QhE2HgTUNaOM09VSAlpMu2hYNxlvr8Ld2g== X-Google-Smtp-Source: ABdhPJzOaB09mFCgBFG83S5eJw4it60ugsfyZJJt+m7bpUAoHMD2aJotkgY0oeO3AX8m5/PWBQpx9g== X-Received: by 2002:a05:600c:35c4:: with SMTP id r4mr1463719wmq.194.1631343801766; Sat, 11 Sep 2021 00:03:21 -0700 (PDT) Original-Received: from ars3 ([2a02:8109:8ac0:56d0::b1d]) by smtp.gmail.com with ESMTPSA id k18sm800034wmi.25.2021.09.11.00.03.20 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 11 Sep 2021 00:03:21 -0700 (PDT) Received-SPF: pass client-ip=2a00:1450:4864:20::334; envelope-from=arstoffel@gmail.com; helo=mail-wm1-x334.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:214046 Archived-At: --=-=-= Content-Type: text/plain 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! --=-=-= Content-Type: text/patch Content-Disposition: attachment; filename=0001-Allow-using-python-shell-send-file-across-machines.patch >From c32fd5541f97cc8e527f38c18ce7bc68b81a5513 Mon Sep 17 00:00:00 2001 From: Augusto Stoffel 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 --=-=-=--