Allow escape sequences in Python prompts

* lisp/progmodes/python.el (python-shell-prompt-detect): Use
Python's json package if available, and remove escape sequences
in prompts.
* test/lisp/progmodes/python-tests.el
(python-tests-interpreter-2-6-higher-p): New predicate
function.
(python-shell-prompt-detect-7): New test.  (Bug#71440)
This commit is contained in:
kobarity 2024-06-12 01:09:21 +09:00 committed by Eli Zaretskii
parent ffa349f983
commit af6e7ed4c1
2 changed files with 35 additions and 2 deletions

View file

@ -3104,8 +3104,13 @@ detection and just returns nil."
(let* ((code (concat
"import sys\n"
"ps = [getattr(sys, 'ps%s' % i, '') for i in range(1,4)]\n"
"try:\n"
" import json\n"
" ps_json = '\\n' + json.dumps(ps)\n"
"except ImportError:\n"
;; JSON is built manually for compatibility
"ps_json = '\\n[\"%s\", \"%s\", \"%s\"]\\n' % tuple(ps)\n"
" ps_json = '\\n[\"%s\", \"%s\", \"%s\"]\\n' % tuple(ps)\n"
"\n"
"print (ps_json)\n"
"sys.exit(0)\n"))
(interpreter python-shell-interpreter)
@ -3168,7 +3173,7 @@ detection and just returns nil."
"Or alternatively in:\n"
" + `python-shell-prompt-input-regexps'\n"
" + `python-shell-prompt-output-regexps'")))
prompts))))
(mapcar #'ansi-color-filter-apply prompts)))))
(defun python-shell-prompt-validate-regexps ()
"Validate all user provided regexps for prompts.

View file

@ -3820,6 +3820,17 @@ This function is intended to be used as the PRED argument of
(when (string= (car (split-string (cdr info) "\\.")) "3")
(car info)))
(defun python-tests-interpreter-2-6-higher-p (info)
"Check if the interpreter major version in INFO is 2.6 or higher.
This function is intended to be used as the PRED argument of
`python-tests-get-shell-interpreter'."
(let* ((version (split-string (cdr info) "\\."))
(major (string-to-number (car version)))
(minor (string-to-number (cadr version))))
(when (or (>= major 3)
(and (= major 2) (>= minor 6)))
(car info))))
(ert-deftest python-shell-get-process-name-1 ()
"Check process name calculation sans `buffer-file-name'."
(python-tests-with-temp-buffer
@ -4353,6 +4364,23 @@ and `python-shell-interpreter-args' in the new shell buffer."
(should (not (get-buffer "*Warnings*"))))
(ignore-errors (delete-file startup-file))))))
(ert-deftest python-shell-prompt-detect-7 ()
"Check prompt autodetection with escape sequences. Bug#71440."
(python-tests-with-shell-interpreter
#'python-tests-interpreter-2-6-higher-p
(let* ((process-environment process-environment)
(startup-code (concat "import sys\n"
"sys.ps1 = '\033[32mpy> \033[0m'\n"
"sys.ps2 = '\033[32m..> \033[0m'\n"
"sys.ps3 = '\033[32mout \033[0m'\n"))
(startup-file (python-shell--save-temp-file startup-code)))
(unwind-protect
(progn
(setenv "PYTHONSTARTUP" startup-file)
(should python-shell-prompt-detect-enabled)
(should (equal (python-shell-prompt-detect) '("py> " "..> " "out "))))
(ignore-errors (delete-file startup-file))))))
(ert-deftest python-shell-prompt-validate-regexps-1 ()
"Check `python-shell-prompt-input-regexps' are validated."
(let* ((python-shell-prompt-input-regexps '("\\("))