aboutsummaryrefslogtreecommitdiffstats
path: root/lisp/progmodes/python.el
diff options
context:
space:
mode:
authorAugusto Stoffel2021-09-04 11:16:11 +0200
committerLars Ingebrigtsen2021-09-05 09:43:13 +0200
commit1fdd898704629de550e2374cf0a06b7c519d6022 (patch)
tree554518b4175b46526f1a06e096e0710eb1ed3cff /lisp/progmodes/python.el
parentba84ec8bd93b931be975ab8a8a7f0d7a2df7054a (diff)
downloademacs-1fdd898704629de550e2374cf0a06b7c519d6022.tar.gz
emacs-1fdd898704629de550e2374cf0a06b7c519d6022.zip
Fixes for 'python-shell-send-string' and 'python-shell-send-file'
* lisp/progmodes/python.el (python-shell-send-string): use a temporary file for sufficiently long strings. (python-shell-send-file, python-shell-eval-file-setup-code): Avoid showing "plumbing code" in the traceback (bug#32042).
Diffstat (limited to 'lisp/progmodes/python.el')
-rw-r--r--lisp/progmodes/python.el46
1 files changed, 31 insertions, 15 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index 306cc3a5428..d8ec032402e 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -3127,12 +3127,18 @@ user-friendly message if there's no process running; defaults to
3127t when called interactively." 3127t when called interactively."
3128 (interactive 3128 (interactive
3129 (list (read-string "Python command: ") nil t)) 3129 (list (read-string "Python command: ") nil t))
3130 (comint-send-string 3130 (let ((process (or process (python-shell-get-process-or-error msg)))
3131 (or process (python-shell-get-process-or-error msg)) 3131 (code (format "exec(%s);__PYTHON_EL_eval(%s, %s)\n"
3132 (format "exec(%s);__PYTHON_EL_eval(%s, %s)\n" 3132 (python-shell--encode-string python-shell-eval-setup-code)
3133 (python-shell--encode-string python-shell-eval-setup-code) 3133 (python-shell--encode-string string)
3134 (python-shell--encode-string string) 3134 (python-shell--encode-string (or (buffer-file-name)
3135 (python-shell--encode-string (or (buffer-file-name) "<string>"))))) 3135 "<string>")))))
3136 (if (<= (string-bytes code) 4096)
3137 (comint-send-string process code)
3138 (let* ((temp-file-name (with-current-buffer (process-buffer process)
3139 (python-shell--save-temp-file string)))
3140 (file-name (or (buffer-file-name) temp-file-name)))
3141 (python-shell-send-file file-name process temp-file-name t)))))
3136 3142
3137(defvar python-shell-output-filter-in-progress nil) 3143(defvar python-shell-output-filter-in-progress nil)
3138(defvar python-shell-output-filter-buffer nil) 3144(defvar python-shell-output-filter-buffer nil)
@@ -3372,6 +3378,18 @@ t when called interactively."
3372 nil ;; noop 3378 nil ;; noop
3373 msg)))) 3379 msg))))
3374 3380
3381
3382(defconst python-shell-eval-file-setup-code
3383 "\
3384def __PYTHON_EL_eval_file(filename, tempname, encoding, delete):
3385 import codecs, os
3386 with codecs.open(tempname or filename, encoding=encoding) as file:
3387 source = file.read().encode(encoding)
3388 if delete and tempname:
3389 os.remove(tempname)
3390 return __PYTHON_EL_eval(source, filename)"
3391 "Code used to evaluate files in inferior Python processes.")
3392
3375(defun python-shell-send-file (file-name &optional process temp-file-name 3393(defun python-shell-send-file (file-name &optional process temp-file-name
3376 delete msg) 3394 delete msg)
3377 "Send FILE-NAME to inferior Python PROCESS. 3395 "Send FILE-NAME to inferior Python PROCESS.
@@ -3401,15 +3419,13 @@ t when called interactively."
3401 (comint-send-string 3419 (comint-send-string
3402 process 3420 process
3403 (format 3421 (format
3404 (concat 3422 "exec(%s);exec(%s);__PYTHON_EL_eval_file(%s, %s, %s, %s)\n"
3405 "import codecs, os;" 3423 (python-shell--encode-string python-shell-eval-setup-code)
3406 "__pyfile = codecs.open('''%s''', encoding='''%s''');" 3424 (python-shell--encode-string python-shell-eval-file-setup-code)
3407 "__code = __pyfile.read().encode('''%s''');" 3425 (python-shell--encode-string file-name)
3408 "__pyfile.close();" 3426 (python-shell--encode-string (or temp-file-name ""))
3409 (when (and delete temp-file-name) 3427 (python-shell--encode-string (symbol-name encoding))
3410 (format "os.remove('''%s''');" temp-file-name)) 3428 (if delete "True" "False")))))
3411 "exec(compile(__code, '''%s''', 'exec'));")
3412 (or temp-file-name file-name) encoding encoding file-name))))
3413 3429
3414(defun python-shell-switch-to-shell (&optional msg) 3430(defun python-shell-switch-to-shell (&optional msg)
3415 "Switch to inferior Python process buffer. 3431 "Switch to inferior Python process buffer.