Improve Python setup codes to avoid leaving global names

* lisp/progmodes/python.el (python-shell-setup-code)
(python-shell-readline-detect): Improve Python code.  (Bug#80551)
This commit is contained in:
kobarity 2026-03-06 16:27:44 +09:00 committed by Eli Zaretskii
parent cb583c737c
commit b9b59447a1

View file

@ -3652,14 +3652,18 @@ eventually provide a shell."
(defconst python-shell-setup-code (defconst python-shell-setup-code
"\ "\
try: def _PYTHON_EL_setup():
try:
import termios import termios
except ImportError: except ImportError:
pass pass
else: else:
attr = termios.tcgetattr(0) attr = termios.tcgetattr(0)
attr[3] &= ~termios.ECHO attr[3] &= ~termios.ECHO
termios.tcsetattr(0, termios.TCSADRAIN, attr)" termios.tcsetattr(0, termios.TCSADRAIN, attr)
_PYTHON_EL_setup()
del _PYTHON_EL_setup"
"Code used to setup the inferior Python processes.") "Code used to setup the inferior Python processes.")
(defconst python-shell-eval-setup-code (defconst python-shell-eval-setup-code
@ -4639,11 +4643,15 @@ shell has no readline support.")
"Detect the readline support for Python shell completion." "Detect the readline support for Python shell completion."
(let* ((process (python-shell-get-process)) (let* ((process (python-shell-get-process))
(output (python-shell-send-string-no-output " (output (python-shell-send-string-no-output "
try: def _PYTHON_EL_detect_readline():
try:
import readline import readline
print(readline.get_completer_delims()) print(readline.get_completer_delims())
except: except:
print('No readline support')" process))) print('No readline support')
_PYTHON_EL_detect_readline()
del _PYTHON_EL_detect_readline" process)))
(setq-local python-shell-readline-completer-delims (setq-local python-shell-readline-completer-delims
(unless (string-search "No readline support" output) (unless (string-search "No readline support" output)
(string-trim-right output))))) (string-trim-right output)))))