[Python-checkins] CVS: python/dist/src/Lib shlex.py,1.7,1.8

Fred L. Drake python-dev@python.org
Mon, 3 Jul 2000 02:56:26 -0700


Update of /cvsroot/python/python/dist/src/Lib
In directory slayer.i.sourceforge.net:/tmp/cvs-serv24985/Lib
Modified Files:
	shlex.py 
Log Message:
Eric S. Raymond <esr@thyrsus.com>:
This patch implements relative-path semantics for the "source" facility resembling
those of cpp(1), documents the change, and improves the shlex test main to
make it easier to test this feature. Along the way, it fixes a name error
in the existing docs.
[Additional documentation markup changes for consistency by FLD.]
Index: shlex.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/shlex.py,v
retrieving revision 1.7
retrieving revision 1.8
diff -C2 -r1.7 -r1.8
*** shlex.py	2000年05月01日 20:14:12	1.7
--- shlex.py	2000年07月03日 09:56:23	1.8
***************
*** 4,9 ****
--- 4,11 ----
 # Input stacking and error message cleanup added by ESR, March 2000
 
+ import os.path
 import sys
 
+ 
 class shlex:
 "A lexical analyzer class for simple shell-like syntaxes." 
***************
*** 27,31 ****
 self.source = None
 if self.debug:
! print 'shlex: reading from %s, line %d' % (self.instream,self.lineno)
 
 def push_token(self, tok):
--- 29,34 ----
 self.source = None
 if self.debug:
! print 'shlex: reading from %s, line %d' \
! % (self.instream, self.lineno)
 
 def push_token(self, tok):
***************
*** 48,52 ****
 while raw == self.source:
 (newfile, newstream) = self.sourcehook(self.read_token())
! self.filestack = [(self.infile,self.instream,self.lineno)] + self.filestack
 self.infile = newfile
 self.instream = newstream
--- 51,55 ----
 while raw == self.source:
 (newfile, newstream) = self.sourcehook(self.read_token())
! self.filestack.insert(0, (self.infile, self.instream, self.lineno))
 self.infile = newfile
 self.instream = newstream
***************
*** 64,68 ****
 self.filestack = self.filestack[1:]
 if self.debug:
! print 'shlex: popping to %s, line %d' % (self.instream, self.lineno)
 self.state = ' '
 raw = self.get_token()
--- 67,72 ----
 self.filestack = self.filestack[1:]
 if self.debug:
! print 'shlex: popping to %s, line %d' \
! % (self.instream, self.lineno)
 self.state = ' '
 raw = self.get_token()
***************
*** 83,87 ****
 self.lineno = self.lineno + 1
 if self.debug >= 3:
! print "shlex: in state " + repr(self.state) + " I see character: " + repr(nextchar) 
 if self.state == None:
 self.token = '';	# past end of file
--- 87,92 ----
 self.lineno = self.lineno + 1
 if self.debug >= 3:
! print "shlex: in state", repr(self.state), \
! "I see character:", repr(nextchar) 
 if self.state == None:
 self.token = '';	# past end of file
***************
*** 157,160 ****
--- 162,168 ----
 if newfile[0] == '"':
 newfile = newfile[1:-1]
+ # This implements cpp-like semantics for relative-path inclusion.
+ if type(self.infile) == type("") and not os.path.isabs(newfile):
+ newfile = os.path.join(os.path.dirname(self.infile), newfile)
 return (newfile, open(newfile, "r"))
 
***************
*** 167,177 ****
 return "\"%s\", line %d: " % (infile, lineno)
 
- if __name__ == '__main__': 
 
! lexer = shlex()
 while 1:
 tt = lexer.get_token()
! print "Token: " + repr(tt)
! if not tt:
 break
- 
--- 175,189 ----
 return "\"%s\", line %d: " % (infile, lineno)
 
 
! if __name__ == '__main__': 
! if len(sys.argv) == 1:
! lexer = shlex()
! else:
! file = sys.argv[1]
! lexer = shlex(open(file), file)
 while 1:
 tt = lexer.get_token()
! if tt:
! print "Token: " + repr(tt)
! else:
 break

AltStyle によって変換されたページ (->オリジナル) /