bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#55174: python-shell-send-statement in python.el sends malformed code


From: Jin Choi
Subject: bug#55174: python-shell-send-statement in python.el sends malformed code
Date: Thu, 28 Apr 2022 15:37:50 -0400

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: smime.p7s
Description: S/MIME cryptographic signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]