From 9965f1b8e01a662d6980a299dfac8c2ed25df37f Mon Sep 17 00:00:00 2001 From: Alex Shkop Date: Mon, 21 Jan 2019 10:45:16 +0200 Subject: [PATCH 1/3] bpo-35168: Documentation about shlex.punctuation_chars now states that it should be set in __init__.py --- Doc/library/shlex.rst | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Doc/library/shlex.rst b/Doc/library/shlex.rst index fb335c69006816..d5b5ec6b14e476 100644 --- a/Doc/library/shlex.rst +++ b/Doc/library/shlex.rst @@ -98,7 +98,9 @@ The :mod:`shlex` module defines the following class: characters, those characters will be used as the punctuation characters. Any characters in the :attr:`wordchars` attribute that appear in *punctuation_chars* will be removed from :attr:`wordchars`. See - :ref:`improved-shell-compatibility` for more information. + :ref:`improved-shell-compatibility` for more information. *punctuation_chars* + can be set only upon :class:`~shlex.shlex` instance creation and can't be + modified later. .. versionchanged:: 3.6 The *punctuation_chars* parameter was added. @@ -299,8 +301,8 @@ variables which either control lexical analysis or can be used for debugging: .. attribute:: shlex.punctuation_chars - Characters that will be considered punctuation. Runs of punctuation - characters will be returned as a single token. However, note that no + A read-only property. Characters that will be considered punctuation. Runs of + punctuation characters will be returned as a single token. However, note that no semantic validity checking will be performed: for example, '>>>' could be returned as a token, even though it may not be recognised as such by shells. From a65944d31b05f46eb22e86da7dc4c2590ae3a0ea Mon Sep 17 00:00:00 2001 From: Alex Shkop Date: Mon, 21 Jan 2019 11:35:12 +0200 Subject: [PATCH 2/3] bpo-35168: Convert shlex.punctuation_chars to read-only property --- Lib/shlex.py | 6 +++++- Lib/test/test_shlex.py | 7 +++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/Lib/shlex.py b/Lib/shlex.py index 2c9786c517a350..195dc12bbce95e 100644 --- a/Lib/shlex.py +++ b/Lib/shlex.py @@ -55,7 +55,7 @@ def __init__(self, instream=None, infile=None, posix=False, punctuation_chars = '' elif punctuation_chars is True: punctuation_chars = '();<>|&' - self.punctuation_chars = punctuation_chars + self._punctuation_chars = punctuation_chars if punctuation_chars: # _pushback_chars is a push back queue used by lookahead logic self._pushback_chars = deque() @@ -65,6 +65,10 @@ def __init__(self, instream=None, infile=None, posix=False, t = self.wordchars.maketrans(dict.fromkeys(punctuation_chars)) self.wordchars = self.wordchars.translate(t) + @property + def punctuation_chars(self): + return self._punctuation_chars + def push_token(self, tok): "Push a token onto the stack popped by the get_token method" if self.debug >= 1: diff --git a/Lib/test/test_shlex.py b/Lib/test/test_shlex.py index fd35788e81b272..8594a6db30b5dc 100644 --- a/Lib/test/test_shlex.py +++ b/Lib/test/test_shlex.py @@ -308,6 +308,13 @@ def testQuote(self): self.assertEqual(shlex.quote("test%s'name'" % u), "'test%s'\"'\"'name'\"'\"''" % u) + def testPunctuationCharsReadOnly(self): + punctuation_chars = "/|$%^" + shlex_instance = shlex.shlex(punctuation_chars=punctuation_chars) + self.assertEqual(shlex_instance.punctuation_chars, punctuation_chars) + with self.assertRaises(AttributeError): + shlex_instance.punctuation_chars = False + # Allow this test to be used with old shlex.py if not getattr(shlex, "split", None): for methname in dir(ShlexTest): From 34b1823bad99ef873c607c7d759624cf3b00d033 Mon Sep 17 00:00:00 2001 From: Alex Shkop Date: Tue, 22 Jan 2019 09:37:35 +0200 Subject: [PATCH 3/3] Add NEWS.d entry --- .../NEWS.d/next/Library/2019-01-22-09-23-20.bpo-35168.UGv2yW.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2019-01-22-09-23-20.bpo-35168.UGv2yW.rst diff --git a/Misc/NEWS.d/next/Library/2019-01-22-09-23-20.bpo-35168.UGv2yW.rst b/Misc/NEWS.d/next/Library/2019-01-22-09-23-20.bpo-35168.UGv2yW.rst new file mode 100644 index 00000000000000..10684fdbde2f19 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-01-22-09-23-20.bpo-35168.UGv2yW.rst @@ -0,0 +1 @@ +:attr:`shlex.shlex.punctuation_chars` is now a read-only property.