]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.7] bpo-35168: Make shlex.punctuation_chars read-only (GH-11631) (GH-15926)
authorVinay Sajip <vinay_sajip@yahoo.co.uk>
Wed, 11 Sep 2019 12:39:08 +0000 (13:39 +0100)
committerGitHub <noreply@github.com>
Wed, 11 Sep 2019 12:39:08 +0000 (13:39 +0100)
(cherry picked from commit 972cf5c06a5ba16ad243a442dbb9c15307fbed95)

Co-authored-by: Alex <a.v.shkop@gmail.com>
Doc/library/shlex.rst
Lib/shlex.py
Lib/test/test_shlex.py
Misc/NEWS.d/next/Library/2019-01-22-09-23-20.bpo-35168.UGv2yW.rst [new file with mode: 0644]

index fb335c690068165e1d34b69e0780b193c4c7ecbb..d5b5ec6b14e476add75d9a17cd81b49c6da310e6 100644 (file)
@@ -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.
 
index 2c9786c517a350f2bd766b539e41ac860f9dd435..195dc12bbce95e290eed5667da88ce8de7181f88 100644 (file)
@@ -55,7 +55,7 @@ class shlex:
             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 @@ class shlex:
             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:
index fd35788e81b27203b9a5825f498fff74cf7c79aa..6d4627d7f4eab2ca6aecf6035c57d0291fa4cd5c 100644 (file)
@@ -308,6 +308,14 @@ class ShlexTest(unittest.TestCase):
             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):
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 (file)
index 0000000..10684fd
--- /dev/null
@@ -0,0 +1 @@
+:attr:`shlex.shlex.punctuation_chars` is now a read-only property.