all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* bug#55174: python-shell-send-statement in python.el sends malformed code
@ 2022-04-28 19:37 Jin Choi
  2022-04-29 10:21 ` Lars Ingebrigtsen
  0 siblings, 1 reply; 5+ messages in thread
From: Jin Choi @ 2022-04-28 19:37 UTC (permalink / raw)
  To: 55174

[-- Attachment #1: Type: text/plain, Size: 2237 bytes --]

python-shell-send-statement mangles the sent code if the region is inactive and the current statement is indented. Example: with the indented statement

	a = 1

it will send the string “if True:” by itself. With a multi-line statement such as
	a = (1,
		2)

it will send
if True:
	2)

The first line is always truncated.

I’ve looked a little into what is happening, and it looks like python-shell-buffer-substring has seen a number of edits over time that don’t work together properly.

* python-shell-send-statement works properly if the region is active because it calls python-shell-send-region with no-cookie set to nil, but when it is not, it calls python-shell-send-region with no-cookie set to t.
* python-shell-send-region calls python-shell-buffer-substring, passing along no-cookie.
* python-shell-buffer-substring does the following:
	- the start position is adjusted to the beginning of the line if the statement was indented
	- if no-cookie is false, fillstr is set to be a coding cookie (e.g., “# -*- coding: utf-8 -*-“) and a number of newlines to get the statement to the correct line
	- Then, this code appears:

    (with-temp-buffer
      (python-mode)
      (when fillstr
        (insert fillstr)) ; inserts coding cookie and newlines if not no-cookie
      (insert substring) ; inserts code
      (goto-char (point-min))
      (when (not toplevel-p)
        (insert "if True:")
        (delete-region (point) (line-end-position))) ; inserts “if True:” and deletes the rest of the first line

This works when no-cookie is false, because it *deletes the cookie line*, incidentally negating any benefit the coding cookie was supposed to provide. It fails when an indented statement is sent and no-cookie is true, because the first line IS the line to be sent. Either it gets deleted entirely, or if it is a multiline statement, the first line is replaced with the “if True:”.

Note that setting no-cookie has the added effect of removing the newlines for the line number matching.

I don’t know what use case the no-cookie argument was supposed to address, but the current implementation seems incompatible with how it’s being used with indented statements.

[-- Attachment #2: smime.p7s --]
[-- Type: application/pkcs7-signature, Size: 4374 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-04-30 11:35 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-04-28 19:37 bug#55174: python-shell-send-statement in python.el sends malformed code Jin Choi
2022-04-29 10:21 ` Lars Ingebrigtsen
2022-04-29 15:29   ` Jin Choi
2022-04-29 15:34     ` Jin Choi
2022-04-30 11:35       ` Lars Ingebrigtsen

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.