From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Pierre Mercatoris Newsgroups: gmane.emacs.bugs Subject: bug#60142: 28.1; python.el Incorrect region when python-shell-send-region from indented code Date: Fri, 30 Dec 2022 14:14:58 +0100 Message-ID: References: <83mt7lf12y.fsf@gnu.org> <87zgbjheqt.fsf@gmail.com> Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="000000000000176fab05f10b64e2" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="16014"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Eli Zaretskii , Augusto Stoffel , 60142@debbugs.gnu.org To: kobarity Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Fri Dec 30 14:16:41 2022 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pBFFZ-000435-2x for geb-bug-gnu-emacs@m.gmane-mx.org; Fri, 30 Dec 2022 14:16:41 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pBFFE-00061x-G7; Fri, 30 Dec 2022 08:16:20 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pBFEw-0005yl-KE for bug-gnu-emacs@gnu.org; Fri, 30 Dec 2022 08:16:12 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pBFEw-00031g-67 for bug-gnu-emacs@gnu.org; Fri, 30 Dec 2022 08:16:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pBFEw-0004Cu-1W for bug-gnu-emacs@gnu.org; Fri, 30 Dec 2022 08:16:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Pierre Mercatoris Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Fri, 30 Dec 2022 13:16:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 60142 X-GNU-PR-Package: emacs Original-Received: via spool by 60142-submit@debbugs.gnu.org id=B60142.167240611810893 (code B ref 60142); Fri, 30 Dec 2022 13:16:02 +0000 Original-Received: (at 60142) by debbugs.gnu.org; 30 Dec 2022 13:15:18 +0000 Original-Received: from localhost ([127.0.0.1]:33711 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pBFED-0002nz-Rp for submit@debbugs.gnu.org; Fri, 30 Dec 2022 08:15:18 -0500 Original-Received: from mail-ed1-f43.google.com ([209.85.208.43]:35486) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pBFEB-0002Oc-UJ for 60142@debbugs.gnu.org; Fri, 30 Dec 2022 08:15:16 -0500 Original-Received: by mail-ed1-f43.google.com with SMTP id i15so30493201edf.2 for <60142@debbugs.gnu.org>; Fri, 30 Dec 2022 05:15:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=NEMJov1wPHpI2Rs1JqiGyP3AM6B5W8GGzO1FClBf9YA=; b=InQKpGHvXn7knxkasB+/mljk3WWebAJfop8tg2bDUqx061KnxmtkfhPTXSxnB1thTU x2hik7Jhv8zwUFAeqQlH4q3j2uJh7ORU6Xfm1zyh96rLii8/roOe9YUJoRBdREItgaOO bGEXA2P1ri6lVVzD7uq3enB//B/g7+B+1iLlYIn+nVcc4FRmkY+a14F/s/K6vWe+3g8L ZyRhMHla5tf8kiSKh60b+k69Z2eOkTOAvzP//xfcXshucF6htIn41ksN0OPZ5I6IPKLc 3qAHjQmpJYKkaIghIccqAXttv/nQjxObGvaoyp0OtIG7sF6EBRo5ZfFPkU+R3AkpFkF6 HCeg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=NEMJov1wPHpI2Rs1JqiGyP3AM6B5W8GGzO1FClBf9YA=; b=VYHH29FN+g8cjcS9oR7FvyggK6lcd2mXeVbzRsiqJZYbMsJjLXF8/O4NkSb5M4jou8 +Q9/XmoTFjXj3xlcbalGI+4fbj5/i4XjQjBcHz9b7vSyy3sCSvZhYV5vU5uR9y8eH2uv RFSuPAUZCQvrEEgF4n28olgulz6Oin3XlzzIkqM+SkGScboMfBySe69mu5SFXJcdHK8l n61K+gU8PN2b6HgIZ1HtVi0LUsAaxzMV6O/dY9YD9ZBPyObjf1Cs0AOG4vWD6wY5OMBH uLmaN649ArD5yfFYIe9Brlp9riHsOqDmuFtkQV6kTRFyMacHVa6PxZKYjdQT+adMPQyy U1/w== X-Gm-Message-State: AFqh2krpUL+6boerKImKKo8u6LTDp9DR3aYz60i5OF+Qkwb4aAc5DuQ5 1loFK55rX2TcGwuB1D/ZYXsD6g0dRokVCRQQvA4= X-Google-Smtp-Source: AMrXdXuNJr/VBnV+GL2cymOSXug9teNSFnS7bvEKECiKDBe1hSV3E+3TgHrCq8RUGgY4+ba38112mLMF3iea3nmiGsk= X-Received: by 2002:a05:6402:3809:b0:48a:5f4c:c1a with SMTP id es9-20020a056402380900b0048a5f4c0c1amr341874edb.298.1672406109743; Fri, 30 Dec 2022 05:15:09 -0800 (PST) In-Reply-To: X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:252111 Archived-At: --000000000000176fab05f10b64e2 Content-Type: text/plain; charset="UTF-8" Hello and sorry for the late reply.. Excuse my ignorance, but how would I need to go about to apply those changes to test them? Furthermore, I am not sure the main issue I raised has been diluted. Basically I was wondering why a region, which does not include any indentation (as it is a fragment of a line), is sent to the repl results in indentation error if the life the fragment comes from was indented. In the case Kobarty described above, he is mentioning sending the whole line to the repl, in which case it can be debated how to deal with indentation. However, my issue is that equivalent regions sent to the repl without any indentation within them results in different behaviours depending on where those regions (fragments of line) come from. I hope I could explain myself better. Thank you all for your interest in the matter. On Thu, Dec 22, 2022 at 4:01 PM kobarity wrote: > Augusto Stoffel wrote: > > On Sun, 18 Dec 2022 at 12:39, Eli Zaretskii wrote: > > >> If I select the `a` or `a = "test"` it will correctly send it to the > > >> console, however it won't echo the evaluation of the statement. > > > > I can at least explain why this happens and is expected. > > > > An evaluation result is printed only if you send a bunch of statements, > > the last of which is an expression. OTOH, since whitespace is > > significant in Python, if you evaluate anything that's not a "toplevel > > form" it gets wrapper in a `if True:` statement, so the actually > > evaluated code is not a simple expression anymore. > > > > It seems hard to work around this limitation. > > Sorry for the late reply. > > As Augusto explained, `if True:` is added to the indented region. > This behavior is documented in `python-shell-buffer-substring'. Maybe > it's better to add a reference to `python-shell-buffer-substring' in > the docstring of `python-shell-send-region'. > > What I'm trying to do is to improve the behavior in some use cases. > Specifically, there is no need to add `if True:` if the region > consists of a single statement such as `a` or `a = "test"`. Removing > leading spaces should be enough to avoid an indentation error even if > the single statement spans multiple lines. > > The corner case is the following use case. > > #+begin_src python > if True: > s = """ > a = 1 > b = 2 > """ > #+end_src > > Let's assume we want to send the lines "a = 1" and "b = 1" only. > Although they are part of a single statement (multiline string), `if > True:` should be added to avoid an indentation error. In other words, > they should not be considered as a single statement. To address such > situation, the single statement check should be done after calling > `narrow-to-region'. > > Attached is a patch to achieve this. I appreciate your comments. > > Thanks, > --000000000000176fab05f10b64e2 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hello and sorry for the late reply..

Excuse my ignorance, but how would I need to go about to apply tho= se changes to test them?

Furthermore, I am not sur= e the main issue I raised has been diluted. Basically I was wondering why a= region, which does not include any indentation (as it is a fragment of a l= ine), is sent to the repl results in indentation error if the life the frag= ment comes from was indented. In the case Kobarty described above, he is me= ntioning sending the whole line to the repl, in which case it can be debate= d how to deal with indentation. However, my issue is that equivalent region= s sent to the repl without any indentation within them results in different= behaviours depending on where those regions (fragments of line) come from.=

I hope I could explain myself better.
<= div>
Thank you all for your interest in the matter.

On T= hu, Dec 22, 2022 at 4:01 PM kobarity <kobarity@gmail.com> wrote:
Augusto Stoffel wrote:
> On Sun, 18 Dec 2022 at 12:39, Eli Zaretskii wrote:
> >> If I select the `a` or `a =3D "test"` it will corre= ctly send it to the
> >> console, however it won't echo the evaluation of the stat= ement.
>
> I can at least explain why this happens and is expected.
>
> An evaluation result is printed only if you send a bunch of statements= ,
> the last of which is an expression.=C2=A0 OTOH, since whitespace is > significant in Python, if you evaluate anything that's not a "= ;toplevel
> form" it gets wrapper in a `if True:` statement, so the actually<= br> > evaluated code is not a simple expression anymore.
>
> It seems hard to work around this limitation.

Sorry for the late reply.

As Augusto explained, `if True:` is added to the indented region.
This behavior is documented in `python-shell-buffer-substring'. Maybe it's better to add a reference to `python-shell-buffer-substring' i= n
the docstring of `python-shell-send-region'.

What I'm trying to do is to improve the behavior in some use cases.
Specifically, there is no need to add `if True:` if the region
consists of a single statement such as `a` or `a =3D "test"`.=C2= =A0 Removing
leading spaces should be enough to avoid an indentation error even if
the single statement spans multiple lines.

The corner case is the following use case.

#+begin_src python
if True:
=C2=A0 =C2=A0 s =3D """
=C2=A0 =C2=A0 a =3D 1
=C2=A0 =C2=A0 b =3D 2
"""
#+end_src

Let's assume we want to send the lines "a =3D 1" and "b = =3D 1" only.
Although they are part of a single statement (multiline string), `if
True:` should be added to avoid an indentation error.=C2=A0 In other words,=
they should not be considered as a single statement.=C2=A0 To address such<= br> situation, the single statement check should be done after calling
`narrow-to-region'.

Attached is a patch to achieve this.=C2=A0 I appreciate your comments.

Thanks,
--000000000000176fab05f10b64e2--