SF patch 1631942 by Collin Winter:

(a) "except E, V" -> "except E as V"
(b) V is now limited to a simple name (local variable)
(c) V is now deleted at the end of the except block
This commit is contained in:
Guido van Rossum
2007-01-10 16:19:56 +00:00
parent 893523e80a
commit b940e113bf
295 changed files with 817 additions and 743 deletions

View File

@@ -119,5 +119,5 @@ class WikiPage:
f.write('\n')
f.close()
return ""
except IOError, err:
except IOError as err:
return "IOError: %s" % str(err)

View File

@@ -28,7 +28,7 @@ def main():
for file in sys.argv[1:]:
try:
fp = open(file, 'r')
except IOError, msg:
except IOError as msg:
print "%s: %s" % (file, msg)
continue
lineno = 0

View File

@@ -41,7 +41,7 @@ def main():
def reportboguslinks(prefix):
try:
names = os.listdir('.')
except os.error, msg:
except os.error as msg:
print "%s%s: can't list: %s" % (prefix, '.', msg)
return
names.sort()
@@ -62,7 +62,7 @@ def reportboguslinks(prefix):
elif S_ISDIR(mode):
try:
os.chdir(name)
except os.error, msg:
except os.error as msg:
print "%s%s: can't chdir: %s" % \
(prefix, name, msg)
continue

View File

@@ -17,7 +17,7 @@ def testChunk(t, fileName):
# against a large source file like Tkinter.py.
ast = None
new = parser.tuple2ast(tup)
except parser.ParserError, err:
except parser.ParserError as err:
print
print 'parser module raised exception on input file', fileName + ':'
traceback.print_exc()

View File

@@ -492,7 +492,7 @@ def testdir(a):
print 'Testing %s' % fullname
try:
roundtrip(fullname, output)
except Exception, e:
except Exception as e:
print ' Failed to compile, exception is %s' % repr(e)
elif os.path.isdir(fullname):
testdir(fullname)

View File

@@ -87,7 +87,7 @@ class FSProxyLocal:
fs = macfs.FSSpec(name)
c, t = fs.GetCreatorType()
if t != 'TEXT': return 0
except macfs.error, msg:
except macfs.error as msg:
print "***", name, msg
return 0
else:

View File

@@ -42,7 +42,7 @@ class CommandFrameWork:
if args is None: args = sys.argv[1:]
try:
opts, args = getopt.getopt(args, self.GlobalFlags)
except getopt.error, msg:
except getopt.error as msg:
return self.usage(msg)
self.options(opts)
if not args:
@@ -62,7 +62,7 @@ class CommandFrameWork:
flags = ''
try:
opts, args = getopt.getopt(args[1:], flags)
except getopt.error, msg:
except getopt.error as msg:
return self.usage(
"subcommand %s: " % cmd + str(msg))
self.ready()

View File

@@ -135,7 +135,7 @@ def compare(local, remote, mode):
def sendfile(local, remote, name):
try:
remote.create(name)
except (IOError, os.error), msg:
except (IOError, os.error) as msg:
print "cannot create:", msg
return
@@ -171,7 +171,7 @@ def recvfile(local, remote, name):
def recvfile_real(local, remote, name):
try:
local.create(name)
except (IOError, os.error), msg:
except (IOError, os.error) as msg:
print "cannot create:", msg
return

View File

@@ -129,7 +129,7 @@ class Lock:
self.lockdir = self.cvslck
os.mkdir(self.cvslck, 0777)
return
except os.error, msg:
except os.error as msg:
self.lockdir = None
if msg[0] == EEXIST:
try:
@@ -234,7 +234,7 @@ def MultipleWriteLock(repositories, delay = DELAY):
for r in repositories:
try:
locks.append(WriteLock(r, 0))
except Locked, instance:
except Locked as instance:
del locks
break
else:

View File

@@ -22,7 +22,7 @@ def main():
raise getopt.error, "unknown command"
coptset, func = commands[cmd]
copts, files = getopt.getopt(rest, coptset)
except getopt.error, msg:
except getopt.error as msg:
print msg
print "usage: rrcs [options] command [options] [file] ..."
print "where command can be:"
@@ -41,7 +41,7 @@ def main():
for fn in files:
try:
func(x, copts, fn)
except (IOError, os.error), msg:
except (IOError, os.error) as msg:
print "%s: %s" % (fn, msg)
def checkin(x, copts, fn):

View File

@@ -21,14 +21,14 @@ def main():
opts, args = getopt.getopt(sys.argv[1:], "")
if len(args) > 1:
raise getopt.error, "Too many arguments."
except getopt.error, msg:
except getopt.error as msg:
usage(msg)
for o, a in opts:
pass
if args:
try:
port = string.atoi(args[0])
except ValueError, msg:
except ValueError as msg:
usage(msg)
else:
port = PORT
@@ -83,7 +83,7 @@ def run_interpreter(stdin, stdout):
source = source + line
try:
code = compile_command(source)
except SyntaxError, err:
except SyntaxError as err:
source = ""
traceback.print_exception(SyntaxError, err, None, file=stdout)
continue
@@ -92,7 +92,7 @@ def run_interpreter(stdin, stdout):
source = ""
try:
run_command(code, stdin, stdout, globals)
except SystemExit, how:
except SystemExit as how:
if how:
try:
how = str(how)
@@ -109,7 +109,7 @@ def run_command(code, stdin, stdout, globals):
sys.stdin = stdin
try:
exec(code, globals)
except SystemExit, how:
except SystemExit as how:
raise SystemExit, how, sys.exc_info()[2]
except:
type, value, tb = sys.exc_info()

View File

@@ -194,8 +194,7 @@ def test():
fh = sf[1]
if fh:
ncl = NFSClient(host)
as = ncl.Getattr(fh)
print as
print ncl.Getattr(fh)
list = ncl.Listdir(fh)
for item in list: print item
mcl.Umnt(filesys)

View File

@@ -330,7 +330,8 @@ def bindresvport(sock, host):
try:
sock.bind((host, i))
return last_resv_port_tried
except socket.error, (errno, msg):
except socket.error as e:
(errno, msg) = e
if errno != 114:
raise socket.error, (errno, msg)
raise RuntimeError, 'can\'t assign reserved port'
@@ -765,7 +766,7 @@ class TCPServer(Server):
call = recvrecord(sock)
except EOFError:
break
except socket.error, msg:
except socket.error as msg:
print 'socket error:', msg
break
reply = self.handle(call)
@@ -866,7 +867,7 @@ def testsvr():
s = S('', 0x20000000, 1, 0)
try:
s.unregister()
except RuntimeError, msg:
except RuntimeError as msg:
print 'RuntimeError:', msg, '(ignored)'
s.register()
print 'Service started...'

View File

@@ -62,7 +62,7 @@ def recursedown(dirname):
bad = 0
try:
names = os.listdir(dirname)
except os.error, msg:
except os.error as msg:
err('%s: cannot list directory: %r\n' % (dirname, msg))
return 1
names.sort()
@@ -83,7 +83,7 @@ def fix(filename):
## dbg('fix(%r)\n' % (dirname,))
try:
f = open(filename, 'r')
except IOError, msg:
except IOError as msg:
err('%s: cannot open: %r\n' % (filename, msg))
return 1
head, tail = os.path.split(filename)
@@ -120,7 +120,7 @@ def fix(filename):
if g is None:
try:
g = open(tempname, 'w')
except IOError, msg:
except IOError as msg:
f.close()
err('%s: cannot create: %r\n' % (tempname, msg))
return 1
@@ -144,17 +144,17 @@ def fix(filename):
try:
statbuf = os.stat(filename)
os.chmod(tempname, statbuf[ST_MODE] & 07777)
except os.error, msg:
except os.error as msg:
err('%s: warning: chmod failed (%r)\n' % (tempname, msg))
# Then make a backup of the original file as filename~
try:
os.rename(filename, filename + '~')
except os.error, msg:
except os.error as msg:
err('%s: warning: backup failed (%r)\n' % (filename, msg))
# Now move the temp file to the original file
try:
os.rename(tempname, filename)
except os.error, msg:
except os.error as msg:
err('%s: rename failed (%r)\n' % (filename, msg))
return 1
# Return succes

View File

@@ -25,7 +25,7 @@ def main():
search = None
try:
opts, args = getopt.getopt(sys.argv[1:], 'm:s:')
except getopt.error, msg:
except getopt.error as msg:
print msg
print 'usage: ftpstats [-m maxitems] [file]'
sys.exit(2)
@@ -41,7 +41,7 @@ def main():
else:
try:
f = open(file, 'r')
except IOError, msg:
except IOError as msg:
print file, ':', msg
sys.exit(1)
bydate = {}

View File

@@ -16,7 +16,7 @@ def main():
dofile = mmdf
try:
opts, args = getopt.getopt(sys.argv[1:], 'f')
except getopt.error, msg:
except getopt.error as msg:
sys.stderr.write('%s\n' % msg)
sys.exit(2)
for o, a in opts:
@@ -33,7 +33,7 @@ def main():
elif os.path.isfile(arg):
try:
f = open(arg)
except IOError, msg:
except IOError as msg:
sys.stderr.write('%s: %s\n' % (arg, msg))
sts = 1
continue
@@ -56,7 +56,7 @@ def mh(dir):
fn = os.path.join(dir, msg)
try:
f = open(fn)
except IOError, msg:
except IOError as msg:
sys.stderr.write('%s: %s\n' % (fn, msg))
sts = 1
continue

View File

@@ -330,7 +330,7 @@ def main():
else:
s = NNTP(newshost)
connected = 1
except (nntplib.error_temp, nntplib.error_perm), x:
except (nntplib.error_temp, nntplib.error_perm) as x:
print 'Error connecting to host:', x
print 'I\'ll try to use just the local list.'
connected = 0

View File

@@ -35,7 +35,7 @@ PFLAG = 0
try:
optlist, ARGS = getopt.getopt(sys.argv[1:], 'acde:F:np')
except getopt.error, msg:
except getopt.error as msg:
sys.stderr.write(sys.argv[0] + ': ' + msg + '\n')
sys.exit(2)

View File

@@ -19,7 +19,7 @@ class FileObj:
self.changed = 0
try:
self.lines = open(filename, 'r').readlines()
except IOError, msg:
except IOError as msg:
print '*** Can\'t open "%s":' % filename, msg
self.lines = None
return
@@ -32,7 +32,7 @@ class FileObj:
try:
os.rename(self.filename, self.filename + '~')
fp = open(self.filename, 'w')
except (os.error, IOError), msg:
except (os.error, IOError) as msg:
print '*** Can\'t rewrite "%s":' % self.filename, msg
return
print 'writing', self.filename
@@ -67,7 +67,7 @@ def main():
if sys.argv[1:]:
try:
fp = open(sys.argv[1], 'r')
except IOError, msg:
except IOError as msg:
print 'Can\'t open "%s":' % sys.argv[1], msg
sys.exit(1)
else:

View File

@@ -142,7 +142,7 @@ def browser(*args):
raise RuntimeError, 'too many args'
try:
browse_menu(selector, host, port)
except socket.error, msg:
except socket.error as msg:
print 'Socket error:', msg
sys.exit(1)
except KeyboardInterrupt:
@@ -202,7 +202,7 @@ def browse_textfile(selector, host, port):
p = os.popen('${PAGER-more}', 'w')
x = SaveLines(p)
get_alt_textfile(selector, host, port, x.writeln)
except IOError, msg:
except IOError as msg:
print 'IOError:', msg
if x:
x.close()
@@ -213,7 +213,7 @@ def browse_textfile(selector, host, port):
try:
get_alt_textfile(selector, host, port, x.writeln)
print 'Done.'
except IOError, msg:
except IOError as msg:
print 'IOError:', msg
x.close()
@@ -311,7 +311,7 @@ def open_savefile():
cmd = savefile[1:].strip()
try:
p = os.popen(cmd, 'w')
except IOError, msg:
except IOError as msg:
print repr(cmd), ':', msg
return None
print 'Piping through', repr(cmd), '...'
@@ -320,7 +320,7 @@ def open_savefile():
savefile = os.path.expanduser(savefile)
try:
f = open(savefile, 'w')
except IOError, msg:
except IOError as msg:
print repr(savefile), ':', msg
return None
print 'Saving to', repr(savefile), '...'

Some files were not shown because too many files have changed in this diff Show More