all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Jin Choi <jsc@alum.mit.edu>
To: 55174@debbugs.gnu.org
Subject: bug#55174: python-shell-send-statement in python.el sends malformed code
Date: Thu, 28 Apr 2022 15:37:50 -0400	[thread overview]
Message-ID: <7A511431-4FC0-443F-904B-86FBAC274297@alum.mit.edu> (raw)

[-- 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 --]

             reply	other threads:[~2022-04-28 19:37 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-28 19:37 Jin Choi [this message]
2022-04-29 10:21 ` bug#55174: python-shell-send-statement in python.el sends malformed code Lars Ingebrigtsen
2022-04-29 15:29   ` Jin Choi
2022-04-29 15:34     ` Jin Choi
2022-04-30 11:35       ` Lars Ingebrigtsen

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=7A511431-4FC0-443F-904B-86FBAC274297@alum.mit.edu \
    --to=jsc@alum.mit.edu \
    --cc=55174@debbugs.gnu.org \
    /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.