make script execution more robust

This commit is contained in:
Valentin Boettcher 2024-05-16 12:07:14 -04:00
parent fba8ea7e41
commit ce67b62261
No known key found for this signature in database
GPG key ID: E034E12B7AF56ACE

View file

@ -107,6 +107,11 @@ If in doubt, set this to :python.")
:type 'number :type 'number
:group 'python-vterm-repl) :group 'python-vterm-repl)
(defcustom python-vterm-repl-script-timeout 5
"Time in seconds to wait for the Python REPL to execute a script."
:type 'number
:group 'python-vterm-repl)
(defun python-vterm-repl-buffer-name (&optional session-name) (defun python-vterm-repl-buffer-name (&optional session-name)
"Return a Python REPL buffer name whose session name is SESSION-NAME. "Return a Python REPL buffer name whose session name is SESSION-NAME.
If SESSION-NAME is not given, the default session name `main' is assumed." If SESSION-NAME is not given, the default session name `main' is assumed."
@ -267,6 +272,8 @@ Return a corresponding symbol or nil if not ready for input."
(defun python-vterm--execute-script (name &rest args) (defun python-vterm--execute-script (name &rest args)
"Load the script with file NAME and call the eponymous function with ARGS. "Load the script with file NAME and call the eponymous function with ARGS.
Returns either the result of the function or nil if execution
times out after `python-vterm-repl-script-timeout'.
The script file is expected to be in the `scripts' directory of The script file is expected to be in the `scripts' directory of
the package and must contain exactly one function with the same the package and must contain exactly one function with the same
@ -300,6 +307,10 @@ will be cleaned up afterwards."
(python-vterm-paste-string (format "del %s" name)) (python-vterm-paste-string (format "del %s" name))
(python-vterm-send-return-key) (python-vterm-send-return-key)
(with-timeout (python-vterm-repl-script-timeout
(progn (display-warning 'python-vterm "Python script did not finish in time.")
(delete-file tmpfile)
nil))
(while (while
(progn (with-temp-buffer (progn (with-temp-buffer
(insert-file-contents tmpfile) (insert-file-contents tmpfile)
@ -315,7 +326,7 @@ will be cleaned up afterwards."
(let ((inhibit-read-only t)) (let ((inhibit-read-only t))
(python-vterm-repl-clear-buffer) (python-vterm-repl-clear-buffer)
(insert up-to-now)) (insert up-to-now))
result)) result)))
(defun python-vterm--read-script (name) (defun python-vterm--read-script (name)