Skip to content

Commit aca878e

Browse files
vsajipashkop
andauthored
[3.7] bpo-35168: Make shlex.punctuation_chars read-only (GH-11631) (GH-15926)
(cherry picked from commit 972cf5c) Co-authored-by: Alex <a.v.shkop@gmail.com>
1 parent 99f0e81 commit aca878e

File tree

4 files changed

+19
-4
lines changed

4 files changed

+19
-4
lines changed

Doc/library/shlex.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ The :mod:`shlex` module defines the following class:
9898
characters, those characters will be used as the punctuation characters. Any
9999
characters in the :attr:`wordchars` attribute that appear in
100100
*punctuation_chars* will be removed from :attr:`wordchars`. See
101-
:ref:`improved-shell-compatibility` for more information.
101+
:ref:`improved-shell-compatibility` for more information. *punctuation_chars*
102+
can be set only upon :class:`~shlex.shlex` instance creation and can't be
103+
modified later.
102104

103105
.. versionchanged:: 3.6
104106
The *punctuation_chars* parameter was added.
@@ -299,8 +301,8 @@ variables which either control lexical analysis or can be used for debugging:
299301

300302
.. attribute:: shlex.punctuation_chars
301303

302-
Characters that will be considered punctuation. Runs of punctuation
303-
characters will be returned as a single token. However, note that no
304+
A read-only property. Characters that will be considered punctuation. Runs of
305+
punctuation characters will be returned as a single token. However, note that no
304306
semantic validity checking will be performed: for example, '>>>' could be
305307
returned as a token, even though it may not be recognised as such by shells.
306308

Lib/shlex.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def __init__(self, instream=None, infile=None, posix=False,
5555
punctuation_chars = ''
5656
elif punctuation_chars is True:
5757
punctuation_chars = '();<>|&'
58-
self.punctuation_chars = punctuation_chars
58+
self._punctuation_chars = punctuation_chars
5959
if punctuation_chars:
6060
# _pushback_chars is a push back queue used by lookahead logic
6161
self._pushback_chars = deque()
@@ -65,6 +65,10 @@ def __init__(self, instream=None, infile=None, posix=False,
6565
t = self.wordchars.maketrans(dict.fromkeys(punctuation_chars))
6666
self.wordchars = self.wordchars.translate(t)
6767

68+
@property
69+
def punctuation_chars(self):
70+
return self._punctuation_chars
71+
6872
def push_token(self, tok):
6973
"Push a token onto the stack popped by the get_token method"
7074
if self.debug >= 1:

Lib/test/test_shlex.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,14 @@ def testQuote(self):
308308
self.assertEqual(shlex.quote("test%s'name'" % u),
309309
"'test%s'\"'\"'name'\"'\"''" % u)
310310

311+
def testPunctuationCharsReadOnly(self):
312+
punctuation_chars = "/|$%^"
313+
shlex_instance = shlex.shlex(punctuation_chars=punctuation_chars)
314+
self.assertEqual(shlex_instance.punctuation_chars, punctuation_chars)
315+
with self.assertRaises(AttributeError):
316+
shlex_instance.punctuation_chars = False
317+
318+
311319
# Allow this test to be used with old shlex.py
312320
if not getattr(shlex, "split", None):
313321
for methname in dir(ShlexTest):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:attr:`shlex.shlex.punctuation_chars` is now a read-only property.

0 commit comments

Comments
 (0)