From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: JD Smith Newsgroups: gmane.emacs.bugs Subject: bug#58780: python.el infinite loop in info-current-defun Date: Tue, 25 Oct 2022 15:06:36 -0400 Message-ID: <4C66FAFB-EB0E-464D-9D26-51A251FF80CC@gmail.com> Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3696.120.41.1.1\)) Content-Type: multipart/alternative; boundary="Apple-Mail=_3636E09C-4CCF-46BC-A583-CB48DF1BEB5C" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="629"; mail-complaints-to="usenet@ciao.gmane.io" To: 58780@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Tue Oct 25 21:08:37 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 1onPHw-000AW7-CV for geb-bug-gnu-emacs@m.gmane-mx.org; Tue, 25 Oct 2022 21:08:36 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1onPGR-0005tP-T7; Tue, 25 Oct 2022 15:07:03 -0400 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 1onPGQ-0005rt-B2 for bug-gnu-emacs@gnu.org; Tue, 25 Oct 2022 15:07:02 -0400 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 1onPGQ-0007Qk-3H for bug-gnu-emacs@gnu.org; Tue, 25 Oct 2022 15:07:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1onPGP-0003XE-UJ for bug-gnu-emacs@gnu.org; Tue, 25 Oct 2022 15:07:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: JD Smith Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Tue, 25 Oct 2022 19:07:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 58780 X-GNU-PR-Package: emacs X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.166672480413563 (code B ref -1); Tue, 25 Oct 2022 19:07:01 +0000 Original-Received: (at submit) by debbugs.gnu.org; 25 Oct 2022 19:06:44 +0000 Original-Received: from localhost ([127.0.0.1]:52291 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1onPG8-0003Wh-59 for submit@debbugs.gnu.org; Tue, 25 Oct 2022 15:06:44 -0400 Original-Received: from lists.gnu.org ([209.51.188.17]:38572) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1onPG6-0003WZ-Ch for submit@debbugs.gnu.org; Tue, 25 Oct 2022 15:06:42 -0400 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 1onPG6-0005e2-7H for bug-gnu-emacs@gnu.org; Tue, 25 Oct 2022 15:06:42 -0400 Original-Received: from mail-io1-xd33.google.com ([2607:f8b0:4864:20::d33]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1onPG4-0007Jz-EU for bug-gnu-emacs@gnu.org; Tue, 25 Oct 2022 15:06:41 -0400 Original-Received: by mail-io1-xd33.google.com with SMTP id 63so1650107iov.8 for ; Tue, 25 Oct 2022 12:06:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=to:date:message-id:subject:mime-version:from:from:to:cc:subject :date:message-id:reply-to; bh=1Uznq9hhn5jAijhxpl03D9IN5cHnAV/glCsAqBc5RiY=; b=jTkqS8gcUz3r7tXVC48K5yIPD+mG4MRXJv7bek2NrBn0e7sKY9Kf+WAI1Vcdgyc2zY eYgmWNDGpK99ygrpRUXTLNyb7T7TRvZSw0vqJxhzHRyRVgLEC22tYIAc/+4FUBgyzt7n ZVvPFB17CuW3f93IebwybI5eZLRKvR7jGqULmVZt1KOH4T8RFqm7ElZz125JkMlJ7hb7 nzGcU+0+KtQYQXT9+emE93LPPf0wcyKGzjCg/1/wQ3mfLhU7mZlf3XRA6tOsQEQh42pi rayZN4v+WtZvhS+5oPsxgIO7gwo8sTkiyR7K6Kf4JuLifrTbhPBVCcCnx2XUdbypKDUJ wLOA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=to:date:message-id:subject:mime-version:from:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=1Uznq9hhn5jAijhxpl03D9IN5cHnAV/glCsAqBc5RiY=; b=BsTljfIJQOAKqniyCb7T9wlfX9iOTDIA2wyi/InA+ELaeRjebml/ID4YThFjagv098 +4XVmDqTr2F7hEDynpOJXSZRXbibE7YicIRbkmsDe//k+V+EE+6kP2NT/khSKlKSaDXR 0c1adxuIy/FmAJMyylS//U7uJvdTUabXTHpoGVLciYFCwTYL/XXCgRVOiQpm4Wucz4tV 1vXN+cYVIfuONvBaSj/GtMByFTcVkY4bURL6Xy/JSItQR0hVRkNHO9qORZIvNeTbwOsw Rt25oEIYwygXnCw5n7gAwhY4ZzpMBx2a5+ixBX555wOI7lMOm+13n/xyDGiVm/LgkHkc pcQA== X-Gm-Message-State: ACrzQf2zp22VJeDchVAaE1O5nggWxo+xHZ6JKa9mnyNSEDgjTblNYidm O+wiKQWFRztSuj0Dmvw4zM38Fb4b4Hw= X-Google-Smtp-Source: AMsMyM764Jrgc9qlXrb7wI6q70th79fzc6EmZrmDGxOPhE6X7LrLOtCJKYHqqHcpr0o2eRPY4TIwHQ== X-Received: by 2002:a02:8804:0:b0:35b:7425:82af with SMTP id r4-20020a028804000000b0035b742582afmr25020059jai.21.1666724798384; Tue, 25 Oct 2022 12:06:38 -0700 (PDT) Original-Received: from smtpclient.apple ([198.30.180.98]) by smtp.gmail.com with ESMTPSA id b18-20020a026f52000000b003725d3b06a0sm1203253jae.45.2022.10.25.12.06.37 for (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 25 Oct 2022 12:06:37 -0700 (PDT) X-Mailer: Apple Mail (2.3696.120.41.1.1) Received-SPF: pass client-ip=2607:f8b0:4864:20::d33; envelope-from=jdtsmith@gmail.com; helo=mail-io1-xd33.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action 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: , Original-Sender: "bug-gnu-emacs" Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:246165 Archived-At: --Apple-Mail=_3636E09C-4CCF-46BC-A583-CB48DF1BEB5C Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 I noticed python.el hangs hard on some of my files when = `python-info-current-defun` is set up for use with which-function, and I = open (but haven=E2=80=99t yet closed) a triple string: =E2=80=9C=E2=80=9D"= . I tracked this down to a bug in `python-nav-end-of-statement` when an = unclosed string is included in a file: =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D def try(): """Do the Foo def a(): """Do A's stuff""" a =3D True =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D (Note: the final newline is important). (python-info-current-defun) = hangs on the unclosed docsig string of try(). =20 The reason why can be demonstrated by placing the cursor before a =3D = True on the final line and (python-nav-end-of-statement). Point moves = to the end of the previous line! Since `python-nav-end-of-defun` calls = end-of-statement repeatedly looking for (eobp), this results in an = infinite loop. The problem is this call in end-of-statement: (re-search-forward (rx (syntax string-delimiter)) nil t) Search starts at the single apostrophe in Do A=E2=80=99s stuff (the = beginning of the apparent-but-incorrect string ppss has found), then = searches forward to the triple quote at the end of the (prior) line. =20 To reproduce this you need: - an unclosed triple string above - a triple string with another type of quote mark enclosed - something after the final =E2=80=9C=E2=80=9D=E2=80=9D (to prevent = eobp).=20 These are surprisingly common conditions to encounter given python = docstring format. A fix might be to insist that the = `python-nav-end-of-statement` occurs at least at the end of the current = line, or perhaps to improve the regex search for the end of string to = match the opening string delimiter (although this could also be fooled I = think). This is Emacs 28, though aside from some additional commentary about = such issues, end-of-statement hasn=E2=80=99t changed in the latest.=20 As an aside, having stepped through this code, it seems python=E2=80=99s = structural navigation and inspection are _very_ heavy, commonly = traversing entire files one statement at a time to find the local = function name, for example. Due to their complexity, they are also = susceptible to these types of infinite loops when syntax is in a = temporarily broken state. Good arguments for the inclusion of = tree-sitter! --Apple-Mail=_3636E09C-4CCF-46BC-A583-CB48DF1BEB5C Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8 I = noticed python.el hangs hard on some of my files when = `python-info-current-defun` is set up for use with which-function, and I = open (but haven=E2=80=99t yet closed) a triple string: =E2=80=9C=E2=80=9D"= .  I tracked this down to a bug in `python-nav-end-of-statement` = when an unclosed string is included in a file:

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
def try():
    = """Do the Foo

def a():
    """Do A's = stuff"""
    a =3D True

=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D

(Note: = the final newline is important).  (python-info-current-defun)= hangs on the unclosed docsig string of try().  

The = reason why can be demonstrated by placing the cursor before a =3D True = on the final line and (python-nav-end-of-statement).  Point moves to the end = of the previous line!   Since= `python-nav-end-of-defun` calls end-of-statement repeatedly looking for = (eobp), this results in an infinite loop.  The problem is this call = in end-of-statement:

  (re-search-forward (rx (syntax string-delimiter)) nil = t)

Search = starts at the single apostrophe in Do A=E2=80=99s stuff (the beginning = of the apparent-but-incorrect string ppss has found), then searches = forward to the triple quote at the end of the (prior) line. =  

To reproduce this you = need:

- an = unclosed triple string above
- a triple string with another type of quote mark = enclosed
- something after the final =E2=80=9C=E2=80=9D=E2=80=9D = (to prevent eobp). 

These are surprisingly common = conditions to encounter given python docstring format.  A fix might = be to insist that the `python-nav-end-of-statement` occurs at least = at the end of the current line, or perhaps to = improve the regex search for the end of string to match = the opening string delimiter (although this could also be = fooled I think).

This is Emacs 28, though aside from some additional = commentary about such issues, end-of-statement hasn=E2=80=99t changed in = the latest. 

As an aside, having = stepped through this code, it seems python=E2=80=99s structural = navigation and inspection are _very_ heavy, commonly traversing entire = files one statement at a time to find the local function name, for = example.  Due to their complexity, they are also susceptible to these = types of infinite loops when syntax is in a temporarily broken state. =  Good arguments for the inclusion of = tree-sitter!

= --Apple-Mail=_3636E09C-4CCF-46BC-A583-CB48DF1BEB5C--