99
1010import os .path
1111import sys
12+ from collections import deque
1213
1314try :
1415 from cStringIO import StringIO
@@ -45,11 +46,11 @@ def __init__(self, instream=None, infile=None, posix=False):
4546 self .escape = '\\ '
4647 self .escapedquotes = '"'
4748 self .state = ' '
48- self .pushback = []
49+ self .pushback = deque ()
4950 self .lineno = 1
5051 self .debug = 0
5152 self .token = ''
52- self .filestack = []
53+ self .filestack = deque ()
5354 self .source = None
5455 if self .debug :
5556 print 'shlex: reading from %s, line %d' \
@@ -59,13 +60,13 @@ def push_token(self, tok):
5960 "Push a token onto the stack popped by the get_token method"
6061 if self .debug >= 1 :
6162 print "shlex: pushing token " + `tok`
62- self .pushback .insert ( 0 , tok )
63+ self .pushback .appendleft ( tok )
6364
6465 def push_source (self , newstream , newfile = None ):
6566 "Push an input source onto the lexer's input source stack."
6667 if isinstance (newstream , basestring ):
6768 newstream = StringIO (newstream )
68- self .filestack .insert ( 0 , (self .infile , self .instream , self .lineno ))
69+ self .filestack .appendleft ( (self .infile , self .instream , self .lineno ))
6970 self .infile = newfile
7071 self .instream = newstream
7172 self .lineno = 1
@@ -78,8 +79,7 @@ def push_source(self, newstream, newfile=None):
7879 def pop_source (self ):
7980 "Pop the input source stack."
8081 self .instream .close ()
81- (self .infile , self .instream , self .lineno ) = self .filestack [0 ]
82- self .filestack = self .filestack [1 :]
82+ (self .infile , self .instream , self .lineno ) = self .filestack .popleft ()
8383 if self .debug :
8484 print 'shlex: popping to %s, line %d' \
8585 % (self .instream , self .lineno )
@@ -88,7 +88,7 @@ def pop_source(self):
8888 def get_token (self ):
8989 "Get a token from the input stream (or from stack if it's nonempty)"
9090 if self .pushback :
91- tok = self .pushback .pop ( 0 )
91+ tok = self .pushback .popleft ( )
9292 if self .debug >= 1 :
9393 print "shlex: popping token " + `tok`
9494 return tok
@@ -226,7 +226,7 @@ def read_token(self):
226226 or self .whitespace_split :
227227 self .token = self .token + nextchar
228228 else :
229- self .pushback .insert ( 0 , nextchar )
229+ self .pushback .appendleft ( nextchar )
230230 if self .debug >= 2 :
231231 print "shlex: I see punctuation in word state"
232232 self .state = ' '
0 commit comments