From fd16fcaf6b0cb7f54f8cc659e943dd6578e81e86 Mon Sep 17 00:00:00 2001 From: Vinay Sajip Date: Tue, 9 Aug 2016 14:57:03 +0100 Subject: [PATCH] Issue #21999: Handled empty strings correctly when in POSIX mode. --- Lib/shlex.py | 2 +- Lib/test/test_shlex.py | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/Lib/shlex.py b/Lib/shlex.py index e7c8accd42..7bef0ddc6c 100644 --- a/Lib/shlex.py +++ b/Lib/shlex.py @@ -230,7 +230,7 @@ class shlex: if self.debug >= 2: print "shlex: I see punctuation in word state" self.state = ' ' - if self.token: + if self.token or (self.posix and quoted): break # emit current token else: continue diff --git a/Lib/test/test_shlex.py b/Lib/test/test_shlex.py index 6c35f491b5..ed741722af 100644 --- a/Lib/test/test_shlex.py +++ b/Lib/test/test_shlex.py @@ -178,6 +178,19 @@ class ShlexTest(unittest.TestCase): "%s: %s != %s" % (self.data[i][0], l, self.data[i][1:])) + def testEmptyStringHandling(self): + """Test that parsing of empty strings is correctly handled.""" + # see Issue #21999 + expected = ['', ')', 'abc'] + + s = shlex.shlex("'')abc", posix=True) + slist = list(s) + self.assertEqual(slist, expected) + expected = ["''", ')', 'abc'] + s = shlex.shlex("'')abc") + self.assertEqual(list(s), expected) + + # Allow this test to be used with old shlex.py if not getattr(shlex, "split", None): for methname in dir(ShlexTest): -- 2.50.1