From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Augusto Stoffel Newsgroups: gmane.emacs.bugs Subject: bug#50503: [PATCH] Properly encode all strings sent to the Python shell Date: Fri, 10 Sep 2021 12:40:29 +0200 Message-ID: <87czpgww6a.fsf@gmail.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="7620"; mail-complaints-to="usenet@ciao.gmane.io" To: 50503@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Fri Sep 10 12:41:24 2021 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 1mOdyF-0001oq-PE for geb-bug-gnu-emacs@m.gmane-mx.org; Fri, 10 Sep 2021 12:41:23 +0200 Original-Received: from localhost ([::1]:39034 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mOdyD-0008MO-Ms for geb-bug-gnu-emacs@m.gmane-mx.org; Fri, 10 Sep 2021 06:41:21 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:39112) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mOdxw-0008FV-RY for bug-gnu-emacs@gnu.org; Fri, 10 Sep 2021 06:41:04 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:53649) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mOdxu-0003wj-Jr for bug-gnu-emacs@gnu.org; Fri, 10 Sep 2021 06:41:04 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1mOdxu-0002Cq-Ee for bug-gnu-emacs@gnu.org; Fri, 10 Sep 2021 06:41:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Augusto Stoffel Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Fri, 10 Sep 2021 10:41:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 50503 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.16312704378430 (code B ref -1); Fri, 10 Sep 2021 10:41:02 +0000 Original-Received: (at submit) by debbugs.gnu.org; 10 Sep 2021 10:40:37 +0000 Original-Received: from localhost ([127.0.0.1]:36962 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mOdxU-0002Bt-TN for submit@debbugs.gnu.org; Fri, 10 Sep 2021 06:40:37 -0400 Original-Received: from lists.gnu.org ([209.51.188.17]:41632) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mOdxT-0002Bl-I4 for submit@debbugs.gnu.org; Fri, 10 Sep 2021 06:40:35 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:38956) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mOdxT-0007kx-9a for bug-gnu-emacs@gnu.org; Fri, 10 Sep 2021 06:40:35 -0400 Original-Received: from mail-wr1-x42e.google.com ([2a00:1450:4864:20::42e]:38900) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mOdxR-0003Sp-BJ for bug-gnu-emacs@gnu.org; Fri, 10 Sep 2021 06:40:35 -0400 Original-Received: by mail-wr1-x42e.google.com with SMTP id u16so1950479wrn.5 for ; Fri, 10 Sep 2021 03:40:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:subject:date:message-id:mime-version; bh=t+bknbkto+IG9flOzIJ3OIfzh263bJ3hlYyCciVOsq0=; b=l2qoBoF0tqFMHeJ0rSw1wBO4aNVsNFz2mhVSjbBlSUiJULyQIAy+ywzprIX/gO2d2F jtBtEb9WWhqNXWtJodc8C3/9LV8neT6YCoVLxQPJDsMaVUEV9dO5T6EdN6SD2l65hhoB ErF/T2NfxPrgf8GOX5Lbv6+Qc7T+0YcRUQ+f+J1fdoNzoeiKv95ImbNCMeETMjG3HeBO 6Xj/NYxiU003e1ZN+4cTCdduL78q3GE8+JpdkkDnW6amFUD4UfjKbaexkxxxI8UkAuP2 z0dQbWntyXPQkAnZSHCi0c6esMQR6bCP+HxOMkZpQhmzYo7XQKRYrC8A6EtU9OoUhc7p UeQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:mime-version; bh=t+bknbkto+IG9flOzIJ3OIfzh263bJ3hlYyCciVOsq0=; b=qiLqNawmzy/9t/btNZKhMPx4DJflTNkYvGmVplBUaHY5Eql2T8HucROoE5uuS/HaVr cqsQvK8D+kgGJxS7nAV3lbq2w5pH+mx8BgfmzDr31neyy8vMZR6YhCz0sFNdyZO0LqsQ 5bGS2ISyIG/nF78HSAK63DCfPOJqJkdVrQH8jgIYubKPWsaZut3AxSAvup97V3PtUny/ 9m23/tidwaCoMygDGqfT8lkmLX4imsoLIDZyQ+h3Wilhs5vjkJG+wGJ3jVcctPqhrE87 rwLrQPkSjQP8PaxgpBm5QrdpKty+CkNhmO7yD8kF/wa/camUDzkNFryJfQaIs06zqm7K Yz/g== X-Gm-Message-State: AOAM533SXRTmVbheQjWvcjpA3lnf0TDu2ZuqgSr+YWqKv80X23v0Fdmd W7zcNG38Op0grEec6Izj29OQ00LO9E0= X-Google-Smtp-Source: ABdhPJwkDlodcQ9w9nafdXSESQm6OwaQLKFB0PdodtnOR4whzSa+pA7Nm8MGslCX5We+lSy6aTXYuA== X-Received: by 2002:adf:eb89:: with SMTP id t9mr9172319wrn.66.1631270431476; Fri, 10 Sep 2021 03:40:31 -0700 (PDT) Original-Received: from ars3 ([2a02:8109:8ac0:56d0::b1d]) by smtp.gmail.com with ESMTPSA id q10sm3872522wmq.12.2021.09.10.03.40.30 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Sep 2021 03:40:30 -0700 (PDT) Received-SPF: pass client-ip=2a00:1450:4864:20::42e; envelope-from=arstoffel@gmail.com; helo=mail-wr1-x42e.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, 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: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:213981 Archived-At: --=-=-= Content-Type: text/plain Tags: patch Some Python shell functions naively assume that '''''' is valid Python code, which is not true, say, if the arbitrary text ends with a quote sign. This patch fixes this. Some more remarks: - python.el now depends on Emacs 28 because it uses 'format-prompt' and 'comint-max-line-length' (luckily, only the latter is my fault). Is this a problem? - I've deleted some defcustoms which are not usable in my opinion. The intended customizability is not impossible, but can only be achieved by advising certain functions. - There are some long-obsolete functions (since say Emacs 24) that I would like to delete. One example is 'python-send-string' which is easy to confuse with the 'python-shell-send-string', which is real deal. What are the rules for this? In GNU Emacs 28.0.50 (build 6, x86_64-pc-linux-gnu, GTK+ Version 3.24.29, cairo version 1.17.4) of 2021-09-09 built on toolbox Repository revision: 8c023e5ea159c6756c92fd02643983aa449e0da9 Repository branch: master Windowing system distributor 'The X.Org Foundation', version 11.0.12101002 System Description: Fedora 34 (Workstation Edition) --=-=-= Content-Type: text/patch; charset=utf-8 Content-Disposition: attachment; filename=0001-Properly-encode-all-strings-sent-to-Python-shell.patch Content-Transfer-Encoding: quoted-printable >From 032b81954685ccd0c2d0a34cfc79bda23393d8e2 Mon Sep 17 00:00:00 2001 From: Augusto Stoffel Date: Fri, 10 Sep 2021 11:06:03 +0200 Subject: [PATCH] Properly encode all strings sent to Python shell * lisp/progmodes/python.el: Now depends on Emacs 28. (python-shell-package-enable, python-shell-completion-get-completions, python-ffap-module-path, python-eldoc--get-doc-at-point): Enconde Python strings using 'python-shell--encode-string' instead of triple quotes. (python-shell-completion-string-code, python-eldoc-string-code, python-ffap-string-code): Remove defcustoms. --- lisp/progmodes/python.el | 66 ++++++++++++++-------------------------- 1 file changed, 22 insertions(+), 44 deletions(-) diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index e342ce7f56..2898897755 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el @@ -5,7 +5,7 @@ ;; Author: Fabi=C3=A1n E. Gallina ;; URL: https://github.com/fgallina/python.el ;; Version: 0.27.1 -;; Package-Requires: ((emacs "24.2") (cl-lib "1.0")) +;; Package-Requires: ((emacs "28.1") (cl-lib "1.0")) ;; Maintainer: emacs-devel@gnu.org ;; Created: Jul 2010 ;; Keywords: languages @@ -2571,10 +2571,12 @@ python-shell-package-enable (format (concat "import os.path;import sys;" - "sys.path.append(os.path.dirname(os.path.dirname('''%s''')));" - "__package__ =3D '''%s''';" + "sys.path.append(os.path.dirname(os.path.dirname(%s)));" + "__package__ =3D %s;" "import %s") - directory package package) + (python-shell--encode-string directory) + (python-shell--encode-string package) + package) (python-shell-get-process))) =20 (defun python-shell-accept-process-output (process &optional timeout regex= p) @@ -3532,14 +3534,6 @@ python-shell-completion-setup-code "25.1" "Completion string code must work for (i)pdb.") =20 -(defcustom python-shell-completion-string-code - "';'.join(__PYTHON_EL_get_completions('''%s'''))" - "Python code used to get a string of completions separated by semicolons. -The string passed to the function is the current python name or -the full statement in the case of imports." - :type 'string - :group 'python) - (defcustom python-shell-completion-native-disabled-interpreters ;; PyPy's readline cannot handle some escape sequences yet. Native ;; completion doesn't work on w32 (Bug#28580). @@ -3834,9 +3828,10 @@ python-shell-completion-get-completions (python-util-strip-string (python-shell-send-string-no-output (format - (concat python-shell-completion-setup-code - "\nprint (" python-shell-completion-string-code ")") - input) process)))) + "%s\nprint(';'.join(__PYTHON_EL_get_completions(%s)))" + python-shell-completion-setup-code + (python-shell--encode-string input)) + process)))) (when (> (length completions) 2) (split-string completions "^'\\|^\"\\|;\\|'$\\|\"$" t))))) @@ -4559,28 +4554,16 @@ python-ffap-setup-code :type 'string :group 'python) =20 -(defcustom python-ffap-string-code - "__FFAP_get_module_path('''%s''')" - "Python code used to get a string with the path of a module." - :type 'string - :group 'python) - (defun python-ffap-module-path (module) "Function for `ffap-alist' to return path for MODULE." - (let ((process (or - (and (derived-mode-p 'inferior-python-mode) - (get-buffer-process (current-buffer))) - (python-shell-get-process)))) - (if (not process) - nil - (let ((module-file - (python-shell-send-string-no-output - (concat - python-ffap-setup-code - "\nprint (" (format python-ffap-string-code module) ")") - process))) - (unless (zerop (length module-file)) - (python-util-strip-string module-file)))))) + (when-let ((process (python-shell-get-process)) + (module-file + (python-shell-send-string-no-output + (format "%s\nprint(__FFAP_get_module_path(%s))" + python-ffap-setup-code + (python-shell--encode-string module))))) + (unless (string-empty-p module-file) + (python-util-strip-string module-file)))) =20 (defvar ffap-alist) =20 @@ -4671,12 +4654,6 @@ python-eldoc-setup-code :type 'string :group 'python) =20 -(defcustom python-eldoc-string-code - "__PYDOC_get_help('''%s''')" - "Python code used to get a string with the documentation of an object." - :type 'string - :group 'python) - (defun python-eldoc--get-symbol-at-point () "Get the current symbol for eldoc. Returns the current symbol handling point within arguments." @@ -4706,11 +4683,12 @@ python-eldoc--get-doc-at-point ;; enabled. Bug#18794. (python-util-strip-string (python-shell-send-string-no-output - (concat + (format + "%s\nprint(__PYDOC_get_help(%s))" python-eldoc-setup-code - "\nprint(" (format python-eldoc-string-code input) ")") + (python-shell--encode-string input)) process))))) - (unless (zerop (length docstring)) + (unless (string-empty-p docstring) docstring))))) =20 (defvar-local python-eldoc-get-doc t --=20 2.31.1 --=-=-=--