From 872e3385ab843fa778d930a8d614d3e750a4a06e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Delfino?= Date: Thu, 3 May 2018 20:31:47 -0300 Subject: [PATCH 1/4] Consider the special case of string/byte literals, and update the list while at it --- Lib/pydoc.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Lib/pydoc.py b/Lib/pydoc.py index e7a1655b228f892..82f8a898209325e 100644 --- a/Lib/pydoc.py +++ b/Lib/pydoc.py @@ -1748,7 +1748,9 @@ class Helper: # Either add symbols to this dictionary or to the symbols dictionary # directly: Whichever is easier. They are merged later. _symbols_inverse = { - 'STRINGS' : ("'", "'''", "r'", "b'", '"""', '"', 'r"', 'b"'), + 'STRINGS' : ("'", "'''", '"', '"""', "b'", 'b"', "B'", 'B"', "f'", + 'f"', "F'", 'F"', "r'", 'r"', "R'", 'R"', "u'", 'u"', + "U'", 'U"'), 'OPERATORS' : ('+', '-', '*', '**', '/', '//', '%', '<<', '>>', '&', '|', '^', '~', '<', '>', '<=', '>=', '==', '!=', '<>'), 'COMPARISON' : ('<', '>', '<=', '>=', '==', '!=', '<>'), @@ -1910,7 +1912,9 @@ def interact(self): if not request: break except (KeyboardInterrupt, EOFError): break - request = replace(request, '"', '', "'", '').strip() + request = request.strip() + if not (request[0].lower() in 'bfru' and request[1] in ("'", '"')): + request = replace(request, '"', '', "'", '') if request.lower() in ('q', 'quit'): break if request == 'help': self.intro() From a6c34582091b49e0475b0970c0b5ef60c478a60e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Delfino?= Date: Fri, 4 May 2018 14:53:10 -0300 Subject: [PATCH 2/4] Address comments --- Lib/pydoc.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Lib/pydoc.py b/Lib/pydoc.py index 82f8a898209325e..efd0acbb9c1b387 100644 --- a/Lib/pydoc.py +++ b/Lib/pydoc.py @@ -1747,10 +1747,9 @@ class Helper: } # Either add symbols to this dictionary or to the symbols dictionary # directly: Whichever is easier. They are merged later. + _strprefixes = [p + q for p in ('b', 'f', 'r', 'u') for q in ("'", '"')] _symbols_inverse = { - 'STRINGS' : ("'", "'''", '"', '"""', "b'", 'b"', "B'", 'B"', "f'", - 'f"', "F'", 'F"', "r'", 'r"', "R'", 'R"', "u'", 'u"', - "U'", 'U"'), + 'STRINGS' : ("'", "'''", '"', '"""', *_strprefixes), 'OPERATORS' : ('+', '-', '*', '**', '/', '//', '%', '<<', '>>', '&', '|', '^', '~', '<', '>', '<=', '>=', '==', '!=', '<>'), 'COMPARISON' : ('<', '>', '<=', '>=', '==', '!=', '<>'), @@ -1913,7 +1912,9 @@ def interact(self): except (KeyboardInterrupt, EOFError): break request = request.strip() - if not (request[0].lower() in 'bfru' and request[1] in ("'", '"')): + if not (len(request) == 2 and + request[0].lower() in ('b', 'f', 'r', 'u') and + request[1] in ("'", '"')): request = replace(request, '"', '', "'", '') if request.lower() in ('q', 'quit'): break if request == 'help': From d93d6e45edb0a609f95bf1eebd37851ffe52713f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Delfino?= Date: Sat, 5 May 2018 09:54:05 -0300 Subject: [PATCH 3/4] Simplify literal prefix check and add NEWS entry --- Lib/pydoc.py | 7 ++++--- .../next/Library/2018-05-05-09-53-05.bpo-33422.4FtQ0q.rst | 2 ++ 2 files changed, 6 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2018-05-05-09-53-05.bpo-33422.4FtQ0q.rst diff --git a/Lib/pydoc.py b/Lib/pydoc.py index efd0acbb9c1b387..f132edd91588d93 100644 --- a/Lib/pydoc.py +++ b/Lib/pydoc.py @@ -1912,9 +1912,10 @@ def interact(self): except (KeyboardInterrupt, EOFError): break request = request.strip() - if not (len(request) == 2 and - request[0].lower() in ('b', 'f', 'r', 'u') and - request[1] in ("'", '"')): + + # Make sure significant trailing quoting marks of literals don't + # get deleted while cleaning input + if request.lower() not in self._strprefixes: request = replace(request, '"', '', "'", '') if request.lower() in ('q', 'quit'): break if request == 'help': diff --git a/Misc/NEWS.d/next/Library/2018-05-05-09-53-05.bpo-33422.4FtQ0q.rst b/Misc/NEWS.d/next/Library/2018-05-05-09-53-05.bpo-33422.4FtQ0q.rst new file mode 100644 index 000000000000000..57a5d9aae6bac32 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-05-05-09-53-05.bpo-33422.4FtQ0q.rst @@ -0,0 +1,2 @@ +Fix trailing quotation marks getting deleted when looking up byte/string +literals on pydoc From 8041fcf4e673d459e96e780448f0e2360949565d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Delfino?= Date: Sat, 5 May 2018 12:10:26 -0300 Subject: [PATCH 4/4] Address comments --- Lib/pydoc.py | 5 +++-- .../next/Library/2018-05-05-09-53-05.bpo-33422.4FtQ0q.rst | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Lib/pydoc.py b/Lib/pydoc.py index f132edd91588d93..199745ca6fa8b0b 100644 --- a/Lib/pydoc.py +++ b/Lib/pydoc.py @@ -1915,8 +1915,9 @@ def interact(self): # Make sure significant trailing quoting marks of literals don't # get deleted while cleaning input - if request.lower() not in self._strprefixes: - request = replace(request, '"', '', "'", '') + if (len(request) > 2 and request[0] == request[-1] in ("'", '"') + and request[0] not in request[1:-1]): + request = request[1:-1] if request.lower() in ('q', 'quit'): break if request == 'help': self.intro() diff --git a/Misc/NEWS.d/next/Library/2018-05-05-09-53-05.bpo-33422.4FtQ0q.rst b/Misc/NEWS.d/next/Library/2018-05-05-09-53-05.bpo-33422.4FtQ0q.rst index 57a5d9aae6bac32..0d284d508f1061f 100644 --- a/Misc/NEWS.d/next/Library/2018-05-05-09-53-05.bpo-33422.4FtQ0q.rst +++ b/Misc/NEWS.d/next/Library/2018-05-05-09-53-05.bpo-33422.4FtQ0q.rst @@ -1,2 +1,2 @@ Fix trailing quotation marks getting deleted when looking up byte/string -literals on pydoc +literals on pydoc. Patch by Andrés Delfino.