Skip to content

Commit 408822f

Browse files
author
ThanhNguyxn
committed
gh-140950: Read pushed shlex source after EOF
1 parent a2495ff commit 408822f

3 files changed

Lines changed: 13 additions & 0 deletions

File tree

Lib/shlex.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ def push_source(self, newstream, newfile=None):
8181
self.infile = newfile
8282
self.instream = newstream
8383
self.lineno = 1
84+
self.state = ' '
8485
if self.debug:
8586
if newfile is not None:
8687
print('shlex: pushing to file %s' % (self.infile,))

Lib/test/test_shlex.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,16 @@ def testCompat(self):
179179
"%s: %s != %s" %
180180
(self.data[i][0], l, self.data[i][1:]))
181181

182+
def test_push_source_after_eof(self):
183+
for posix in (False, True):
184+
with self.subTest(posix=posix):
185+
lexer = shlex.shlex('a', posix=posix)
186+
self.assertEqual(lexer.get_token(), 'a')
187+
self.assertEqual(lexer.get_token(), lexer.eof)
188+
lexer.push_source('b')
189+
self.assertEqual(lexer.get_token(), 'b')
190+
self.assertEqual(lexer.get_token(), lexer.eof)
191+
182192
def testSyntaxSplitAmpersandAndPipe(self):
183193
"""Test handling of syntax splitting of &, |"""
184194
# Could take these forms: &&, &, |&, ;&, ;;&
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix :meth:`shlex.shlex.push_source` so a new source pushed after EOF is read
2+
correctly instead of being skipped.

0 commit comments

Comments
 (0)