From c97b8e6650fa28f44e36e200f966046ffe99fe0f Mon Sep 17 00:00:00 2001 From: kobarity Date: Sun, 11 Jan 2026 17:49:29 +0900 Subject: [PATCH] Improve non-native completion in Python mode Previously, both the definition of __PYTHON_EL_get_completions and the call to __PYTHON_EL_get_completions were sent to the inferior Python each time 'python-shell-completion-get-completions' was executed. However, there is no need to send the definition every time as long as the definition remains unchanged. We improved this so that the definition of __PYTHON_EL_get_completions is only sent during the inferior Python initialization; it is no longer sent during 'python-shell-completion-get-completions' execution. * lisp/progmodes/python.el (python-shell-completion-send-setup-code): New function. (python-shell-first-prompt-hook): Add the above new function. (python-shell-completion-get-completions): Omit sending 'python-shell-completion-setup-code'. (Bug#80182) --- lisp/progmodes/python.el | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index 9c5e1e5ee6c..5a820f05d77 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el @@ -4514,6 +4514,13 @@ def __PYTHON_EL_get_completions(text): "Code used to setup completion in inferior Python processes." :type 'string) +(defun python-shell-completion-send-setup-code () + "Send `python-shell-completion-setup-code' to inferior Python process." + (python-shell-send-string-no-output python-shell-completion-setup-code)) + +(add-hook 'python-shell-first-prompt-hook + #'python-shell-completion-send-setup-code) + (define-obsolete-variable-alias 'python-shell-completion-module-string-code 'python-shell-completion-string-code @@ -4844,8 +4851,7 @@ With argument MSG show activation/deactivation message." (with-current-buffer (process-buffer process) (let ((completions (python-shell-send-string-no-output - (format "%s\nprint(__PYTHON_EL_get_completions(%s))" - python-shell-completion-setup-code + (format "print(__PYTHON_EL_get_completions(%s))" (python-shell--encode-string input)) process))) (condition-case nil