mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Bring old demo up-to-date.
This commit is contained in:
parent
58d2f2689a
commit
18cb949807
1 changed files with 31 additions and 32 deletions
|
@ -22,7 +22,6 @@
|
||||||
# - except for -n/-p, run directly from the file if at all possible
|
# - except for -n/-p, run directly from the file if at all possible
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import string
|
|
||||||
import getopt
|
import getopt
|
||||||
|
|
||||||
FS = ''
|
FS = ''
|
||||||
|
@ -36,7 +35,7 @@ PFLAG = 0
|
||||||
try:
|
try:
|
||||||
optlist, ARGS = getopt.getopt(sys.argv[1:], 'acde:F:np')
|
optlist, ARGS = getopt.getopt(sys.argv[1:], 'acde:F:np')
|
||||||
except getopt.error, msg:
|
except getopt.error, msg:
|
||||||
sys.stderr.write(sys.argv[0] + ': ' + msg + '\n')
|
sys.stderr.write('%s: %s\n' % (sys.argv[0], msg))
|
||||||
sys.exit(2)
|
sys.exit(2)
|
||||||
|
|
||||||
for option, optarg in optlist:
|
for option, optarg in optlist:
|
||||||
|
@ -47,7 +46,7 @@ for option, optarg in optlist:
|
||||||
elif option == '-d':
|
elif option == '-d':
|
||||||
DFLAG = 1
|
DFLAG = 1
|
||||||
elif option == '-e':
|
elif option == '-e':
|
||||||
for line in string.splitfields(optarg, '\n'):
|
for line in optarg.split('\n'):
|
||||||
SCRIPT.append(line)
|
SCRIPT.append(line)
|
||||||
elif option == '-F':
|
elif option == '-F':
|
||||||
FS = optarg
|
FS = optarg
|
||||||
|
@ -81,31 +80,31 @@ if CFLAG:
|
||||||
elif NFLAG:
|
elif NFLAG:
|
||||||
# Note that it is on purpose that AFLAG and PFLAG are
|
# Note that it is on purpose that AFLAG and PFLAG are
|
||||||
# tested dynamically each time through the loop
|
# tested dynamically each time through the loop
|
||||||
prologue = [ \
|
prologue = [
|
||||||
'LINECOUNT = 0', \
|
'LINECOUNT = 0',
|
||||||
'for FILE in ARGS:', \
|
'for FILE in ARGS:',
|
||||||
' \tif FILE == \'-\':', \
|
' \tif FILE == \'-\':',
|
||||||
' \t \tFP = sys.stdin', \
|
' \t \tFP = sys.stdin',
|
||||||
' \telse:', \
|
' \telse:',
|
||||||
' \t \tFP = open(FILE, \'r\')', \
|
' \t \tFP = open(FILE, \'r\')',
|
||||||
' \tLINENO = 0', \
|
' \tLINENO = 0',
|
||||||
' \twhile 1:', \
|
' \twhile 1:',
|
||||||
' \t \tLINE = FP.readline()', \
|
' \t \tLINE = FP.readline()',
|
||||||
' \t \tif not LINE: break', \
|
' \t \tif not LINE: break',
|
||||||
' \t \tLINENO = LINENO + 1', \
|
' \t \tLINENO = LINENO + 1',
|
||||||
' \t \tLINECOUNT = LINECOUNT + 1', \
|
' \t \tLINECOUNT = LINECOUNT + 1',
|
||||||
' \t \tL = LINE[:-1]', \
|
' \t \tL = LINE[:-1]',
|
||||||
' \t \taflag = AFLAG', \
|
' \t \taflag = AFLAG',
|
||||||
' \t \tif aflag:', \
|
' \t \tif aflag:',
|
||||||
' \t \t \tif FS: F = string.splitfields(L, FS)', \
|
' \t \t \tif FS: F = L.split(FS)',
|
||||||
' \t \t \telse: F = string.split(L)' \
|
' \t \t \telse: F = L.split()'
|
||||||
]
|
]
|
||||||
epilogue = [ \
|
epilogue = [
|
||||||
' \t \tif not PFLAG: continue', \
|
' \t \tif not PFLAG: continue',
|
||||||
' \t \tif aflag:', \
|
' \t \tif aflag:',
|
||||||
' \t \t \tif FS: print string.joinfields(F, FS)', \
|
' \t \t \tif FS: print FS.join(F)',
|
||||||
' \t \t \telse: print string.join(F)', \
|
' \t \t \telse: print \' \'.join(F)',
|
||||||
' \t \telse: print L', \
|
' \t \telse: print L',
|
||||||
]
|
]
|
||||||
else:
|
else:
|
||||||
prologue = ['if 1:']
|
prologue = ['if 1:']
|
||||||
|
@ -114,10 +113,10 @@ else:
|
||||||
# Note that we indent using tabs only, so that any indentation style
|
# Note that we indent using tabs only, so that any indentation style
|
||||||
# used in 'command' will come out right after re-indentation.
|
# used in 'command' will come out right after re-indentation.
|
||||||
|
|
||||||
program = string.joinfields(prologue, '\n') + '\n'
|
program = '\n'.join(prologue) + '\n'
|
||||||
for line in SCRIPT:
|
for line in SCRIPT:
|
||||||
program = program + (' \t \t' + line + '\n')
|
program += ' \t \t' + line + '\n'
|
||||||
program = program + (string.joinfields(epilogue, '\n') + '\n')
|
program += '\n'.join(epilogue) + '\n'
|
||||||
|
|
||||||
import tempfile
|
import tempfile
|
||||||
fp = tempfile.NamedTemporaryFile()
|
fp = tempfile.NamedTemporaryFile()
|
||||||
|
@ -125,6 +124,6 @@ fp.write(program)
|
||||||
fp.flush()
|
fp.flush()
|
||||||
if DFLAG:
|
if DFLAG:
|
||||||
import pdb
|
import pdb
|
||||||
pdb.run('execfile(%r)' % (tfn,))
|
pdb.run('execfile(%r)' % (fp.name,))
|
||||||
else:
|
else:
|
||||||
execfile(tfn)
|
execfile(fp.name)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue