diff --git a/Lib/shlex.py b/Lib/shlex.py index e87266f..daaf4e2 100644 --- a/Lib/shlex.py +++ b/Lib/shlex.py @@ -59,11 +59,7 @@ class shlex: if punctuation_chars: # _pushback_chars is a push back queue used by lookahead logic self._pushback_chars = deque() - # these chars added because allowed in file names, args, wildcards - self.wordchars += '~-./*?=' - #remove any punctuation chars from wordchars - t = self.wordchars.maketrans(dict.fromkeys(punctuation_chars)) - self.wordchars = self.wordchars.translate(t) + self.whitespace_split = True def push_token(self, tok): "Push a token onto the stack popped by the get_token method" @@ -246,7 +242,7 @@ class shlex: self.state = ' ' break elif (nextchar in self.wordchars or nextchar in self.quotes - or self.whitespace_split): + or (self.whitespace_split and nextchar not in self.punctuation_chars)): self.token += nextchar else: if self.punctuation_chars: diff --git a/Lib/test/test_shlex.py b/Lib/test/test_shlex.py index 3936c97..fb2b6df 100644 --- a/Lib/test/test_shlex.py +++ b/Lib/test/test_shlex.py @@ -229,8 +229,8 @@ class ShlexTest(unittest.TestCase): def testSyntaxSplitCustom(self): """Test handling of syntax splitting with custom chars""" - ref = ['~/a', '&', '&', 'b-c', '--color=auto', '||', 'd', '*.py?'] - ss = "~/a && b-c --color=auto || d *.py?" + ref = ['~/a&&b-c', '--color=auto', '||', 'd', '*.py?'] + ss = "~/a&&b-c --color=auto||d *.py?" s = shlex.shlex(ss, punctuation_chars="|") result = list(s) self.assertEqual(ref, result, "While splitting '%s'" % ss) @@ -255,20 +255,14 @@ class ShlexTest(unittest.TestCase): observed.append((t, tt)) self.assertEqual(observed, expected) - def testPunctuationInWordChars(self): - """Test that any punctuation chars are removed from wordchars""" - s = shlex.shlex('a_b__c', punctuation_chars='_') - self.assertNotIn('_', s.wordchars) - self.assertEqual(list(s), ['a', '_', 'b', '__', 'c']) - def testPunctuationWithWhitespaceSplit(self): """Test that with whitespace_split, behaviour is as expected""" s = shlex.shlex('a && b || c', punctuation_chars='&') + s.whitespace_split = False # whitespace_split is False, so splitting will be based on # punctuation_chars self.assertEqual(list(s), ['a', '&&', 'b', '|', '|', 'c']) s = shlex.shlex('a && b || c', punctuation_chars='&') - s.whitespace_split = True # whitespace_split is True, so splitting will be based on # white space self.assertEqual(list(s), ['a', '&&', 'b', '||', 'c'])