summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorFred Drake <fdrake@acm.org>2000-07-03 09:56:23 (GMT)
committerFred Drake <fdrake@acm.org>2000-07-03 09:56:23 (GMT)
commit52dc76c81fffa709fae35af92538723d23ad18d6 (patch)
treeca026ec14dda2005023bafa48784151841c50ec5 /Lib
parent353aaadd9be62e2fa075a1da5771b31d1f0e7046 (diff)
downloadcpython-52dc76c81fffa709fae35af92538723d23ad18d6.zip
cpython-52dc76c81fffa709fae35af92538723d23ad18d6.tar.gz
cpython-52dc76c81fffa709fae35af92538723d23ad18d6.tar.bz2
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.]
Diffstat (limited to 'Lib')
-rw-r--r--Lib/shlex.py30
1 files changed, 21 insertions, 9 deletions
diff --git a/Lib/shlex.py b/Lib/shlex.py
index 575fc35..876dcdf 100644
--- a/Lib/shlex.py
+++ b/Lib/shlex.py
@@ -3,8 +3,10 @@
# Module and documentation by Eric S. Raymond, 21 Dec 1998
# 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."
def __init__(self, instream=None, infile=None):
@@ -26,7 +28,8 @@ class shlex:
self.filestack = []
self.source = None
if self.debug:
- print 'shlex: reading from %s, line %d' % (self.instream,self.lineno)
+ print 'shlex: reading from %s, line %d' \
+ % (self.instream, self.lineno)
def push_token(self, tok):
"Push a token onto the stack popped by the get_token method"
@@ -47,7 +50,7 @@ class shlex:
# Handle inclusions
while raw == self.source:
(newfile, newstream) = self.sourcehook(self.read_token())
- self.filestack = [(self.infile,self.instream,self.lineno)] + self.filestack
+ self.filestack.insert(0, (self.infile, self.instream, self.lineno))
self.infile = newfile
self.instream = newstream
self.lineno = 1
@@ -63,7 +66,8 @@ class shlex:
(self.infile, self.instream, self.lineno) = self.filestack[0]
self.filestack = self.filestack[1:]
if self.debug:
- print 'shlex: popping to %s, line %d' % (self.instream, self.lineno)
+ print 'shlex: popping to %s, line %d' \
+ % (self.instream, self.lineno)
self.state = ' '
raw = self.get_token()
# Neither inclusion nor EOF
@@ -82,7 +86,8 @@ class shlex:
if nextchar == '\n':
self.lineno = self.lineno + 1
if self.debug >= 3:
- print "shlex: in state " + repr(self.state) + " I see character: " + repr(nextchar)
+ print "shlex: in state", repr(self.state), \
+ "I see character:", repr(nextchar)
if self.state == None:
self.token = ''; # past end of file
break
@@ -156,6 +161,9 @@ class shlex:
"Hook called on a filename to be sourced."
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"))
def error_leader(self, infile=None, lineno=None):
@@ -166,12 +174,16 @@ class shlex:
lineno = self.lineno
return "\"%s\", line %d: " % (infile, lineno)
-if __name__ == '__main__':
- lexer = shlex()
+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()
- print "Token: " + repr(tt)
- if not tt:
+ if tt:
+ print "Token: " + repr(tt)
+ else:
break
-