From: kobarity <kobarity@gmail.com>
To: Eli Zaretskii <eliz@gnu.org>
Cc: one.last.kiss@outlook.com, 71440@debbugs.gnu.org
Subject: bug#71440: Python Inferior Mode Can’t Recognize My Prompt
Date: Wed, 12 Jun 2024 22:09:45 +0900 [thread overview]
Message-ID: <eke7sexi489y.wl-kobarity@gmail.com> (raw)
In-Reply-To: <86ikye1swv.fsf@gnu.org>
Eli Zaretskii wrote:
>
> > Date: Wed, 12 Jun 2024 01:24:39 +0900
> > From: kobarity <kobarity@gmail.com>
> > Cc: 71440@debbugs.gnu.org
> >
> >
> > > Eli Zaretskii wrote:
> > > > > > I wonder how PS1 and PS2 are at all relevant when using
> > > > > > python-shell-send-buffer? That function sends the buffer
> > > > > > text to Python, so where do PS1 and PS2 come into play,
> > > > > > and why does Python say "__PYTHON_EL_eval is not defined"
> > > > > > just because you have PS1 and PS2 customized?
> >
> > PS1 and PS2 are set to `comint-prompt-regexp', and used to identify
> > execution completion. The prompts must also be identified to
> > determine if the command can be sent.
> >
> > Python REPL cannot accept the Python code as is. For example, try
> > pasting the following code to the REPL:
> >
> > if True:
> > print("True")
> > print("Hi")
> >
> > You should see the message "SyntaxError: invalid syntax". This is
> > because the REPL requires an empty line to recognize the completion of
> > a block. For such reasons, `python-shell-send-*' sends a string as
> > the argument to __PYTHON_EL_eval instead of sending the code as is.
> >
> > __PYTHON_EL_eval is defined during the initialization of inferior
> > Python, but if the prompt is not recognized, its definition cannot be
> > done either.
> >
> > The prompts are recognized by `python-shell-prompt-detect'. A small
> > Python code sends PS1, etc. to Emacs as an array of JSON strings.
> > However, the JSON strings are hand-crafted for compatibility, as noted
> > in the comments below.
> >
> > ;; JSON is built manually for compatibility
> >
> > The json package was added in Python 2.6, so I assume it is to support
> > Python 2.5 and earlier. This is fine for prompts consisting only of
> > ordinary characters, but will not result in a correct JSON string if
> > it contains escape sequences.
> >
> > The attached improved patch uses the json package if available, so it
> > can handle escape sequences; without the json package, it works as
> > before. It means that prompts containing escape sequences can be
> > recognized in Python 2.6 or later. I have also added an ERT to check
> > this.
>
> Thanks, but what do you mean by "json package" here? Emacs nowadays
> has JSON capabilities built in, so no optional features should be
> required. I did I misunderstand what you say above?
I meant the json package of the Python standard library. A small
Python code is embedded in python.el. I changed this Python code to
use the json package of the Python standard library if available.
It may be easier to see the actual code:
(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"
"\n"
"print (ps_json)\n"
"sys.exit(0)\n"))
next prev parent reply other threads:[~2024-06-12 13:09 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-06-08 16:29 bug#71440: Python Inferior Mode Can’t Recognize My Prompt shynur .
2024-06-08 16:53 ` Eli Zaretskii
2024-06-08 17:06 ` shynur .
2024-06-08 17:29 ` bug#71440: " Eli Zaretskii
2024-06-08 17:46 ` shynur .
2024-06-08 18:41 ` bug#71440: " Eli Zaretskii
2024-06-08 19:50 ` shynur .
2024-06-09 4:31 ` bug#71440: " Eli Zaretskii
2024-06-09 10:11 ` shynur .
2024-06-09 10:25 ` bug#71440: " Eli Zaretskii
2024-06-09 10:49 ` shynur .
2024-06-09 11:02 ` bug#71440: " Eli Zaretskii
2024-06-09 15:26 ` kobarity
2024-06-09 15:40 ` bug#71440: Python Inferior Mode Canʼt " shynur .
2024-06-11 16:24 ` bug#71440: Python Inferior Mode Can’t " kobarity
2024-06-12 8:12 ` Eli Zaretskii
2024-06-12 13:09 ` kobarity [this message]
2024-06-12 16:57 ` shynur .
2024-06-13 14:24 ` kobarity
2024-06-15 10:45 ` Eli Zaretskii
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=eke7sexi489y.wl-kobarity@gmail.com \
--to=kobarity@gmail.com \
--cc=71440@debbugs.gnu.org \
--cc=eliz@gnu.org \
--cc=one.last.kiss@outlook.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.