mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Issue #16341: convert examples to use except ... as ... syntax.
This commit is contained in:
parent
5c89c19eae
commit
1625d88709
10 changed files with 21 additions and 22 deletions
|
@ -201,7 +201,7 @@ e.g. ::
|
||||||
|
|
||||||
>>> req = urllib2.Request('http://www.pretend_server.org')
|
>>> req = urllib2.Request('http://www.pretend_server.org')
|
||||||
>>> try: urllib2.urlopen(req)
|
>>> try: urllib2.urlopen(req)
|
||||||
... except URLError, e:
|
... except URLError as e:
|
||||||
... print e.reason #doctest: +SKIP
|
... print e.reason #doctest: +SKIP
|
||||||
...
|
...
|
||||||
(4, 'getaddrinfo failed')
|
(4, 'getaddrinfo failed')
|
||||||
|
@ -310,7 +310,7 @@ geturl, and info, methods. ::
|
||||||
>>> req = urllib2.Request('http://www.python.org/fish.html')
|
>>> req = urllib2.Request('http://www.python.org/fish.html')
|
||||||
>>> try:
|
>>> try:
|
||||||
... urllib2.urlopen(req)
|
... urllib2.urlopen(req)
|
||||||
... except urllib2.HTTPError, e:
|
... except urllib2.HTTPError as e:
|
||||||
... print e.code
|
... print e.code
|
||||||
... print e.read() #doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
|
... print e.read() #doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
|
||||||
...
|
...
|
||||||
|
@ -338,10 +338,10 @@ Number 1
|
||||||
req = Request(someurl)
|
req = Request(someurl)
|
||||||
try:
|
try:
|
||||||
response = urlopen(req)
|
response = urlopen(req)
|
||||||
except HTTPError, e:
|
except HTTPError as e:
|
||||||
print 'The server couldn\'t fulfill the request.'
|
print 'The server couldn\'t fulfill the request.'
|
||||||
print 'Error code: ', e.code
|
print 'Error code: ', e.code
|
||||||
except URLError, e:
|
except URLError as e:
|
||||||
print 'We failed to reach a server.'
|
print 'We failed to reach a server.'
|
||||||
print 'Reason: ', e.reason
|
print 'Reason: ', e.reason
|
||||||
else:
|
else:
|
||||||
|
@ -362,7 +362,7 @@ Number 2
|
||||||
req = Request(someurl)
|
req = Request(someurl)
|
||||||
try:
|
try:
|
||||||
response = urlopen(req)
|
response = urlopen(req)
|
||||||
except URLError, e:
|
except URLError as e:
|
||||||
if hasattr(e, 'reason'):
|
if hasattr(e, 'reason'):
|
||||||
print 'We failed to reach a server.'
|
print 'We failed to reach a server.'
|
||||||
print 'Reason: ', e.reason
|
print 'Reason: ', e.reason
|
||||||
|
|
|
@ -35,7 +35,7 @@ Usage: %prog [options] msgfile
|
||||||
|
|
||||||
try:
|
try:
|
||||||
os.mkdir(opts.directory)
|
os.mkdir(opts.directory)
|
||||||
except OSError, e:
|
except OSError as e:
|
||||||
# Ignore directory exists error
|
# Ignore directory exists error
|
||||||
if e.errno != errno.EEXIST:
|
if e.errno != errno.EEXIST:
|
||||||
raise
|
raise
|
||||||
|
|
|
@ -23,7 +23,7 @@ while True:
|
||||||
|
|
||||||
if buffer.lstrip().upper().startswith("SELECT"):
|
if buffer.lstrip().upper().startswith("SELECT"):
|
||||||
print cur.fetchall()
|
print cur.fetchall()
|
||||||
except sqlite3.Error, e:
|
except sqlite3.Error as e:
|
||||||
print "An error occurred:", e.args[0]
|
print "An error occurred:", e.args[0]
|
||||||
buffer = ""
|
buffer = ""
|
||||||
|
|
||||||
|
|
|
@ -480,7 +480,7 @@ A slightly more advanced use of the reader --- catching and reporting errors::
|
||||||
try:
|
try:
|
||||||
for row in reader:
|
for row in reader:
|
||||||
print row
|
print row
|
||||||
except csv.Error, e:
|
except csv.Error as e:
|
||||||
sys.exit('file %s, line %d: %s' % (filename, reader.line_num, e))
|
sys.exit('file %s, line %d: %s' % (filename, reader.line_num, e))
|
||||||
|
|
||||||
And while the module doesn't directly support parsing strings, it can easily be
|
And while the module doesn't directly support parsing strings, it can easily be
|
||||||
|
|
|
@ -126,7 +126,7 @@ In a script, typical usage is something like this::
|
||||||
def main():
|
def main():
|
||||||
try:
|
try:
|
||||||
opts, args = getopt.getopt(sys.argv[1:], "ho:v", ["help", "output="])
|
opts, args = getopt.getopt(sys.argv[1:], "ho:v", ["help", "output="])
|
||||||
except getopt.GetoptError, err:
|
except getopt.GetoptError as err:
|
||||||
# print help information and exit:
|
# print help information and exit:
|
||||||
print str(err) # will print something like "option -a not recognized"
|
print str(err) # will print something like "option -a not recognized"
|
||||||
usage()
|
usage()
|
||||||
|
|
|
@ -219,18 +219,18 @@ provided by this module. ::
|
||||||
else:
|
else:
|
||||||
copy2(srcname, dstname)
|
copy2(srcname, dstname)
|
||||||
# XXX What about devices, sockets etc.?
|
# XXX What about devices, sockets etc.?
|
||||||
except (IOError, os.error), why:
|
except (IOError, os.error) as why:
|
||||||
errors.append((srcname, dstname, str(why)))
|
errors.append((srcname, dstname, str(why)))
|
||||||
# catch the Error from the recursive copytree so that we can
|
# catch the Error from the recursive copytree so that we can
|
||||||
# continue with other files
|
# continue with other files
|
||||||
except Error, err:
|
except Error as err:
|
||||||
errors.extend(err.args[0])
|
errors.extend(err.args[0])
|
||||||
try:
|
try:
|
||||||
copystat(src, dst)
|
copystat(src, dst)
|
||||||
except WindowsError:
|
except WindowsError:
|
||||||
# can't copy file access times on Windows
|
# can't copy file access times on Windows
|
||||||
pass
|
pass
|
||||||
except OSError, why:
|
except OSError as why:
|
||||||
errors.extend((src, dst, str(why)))
|
errors.extend((src, dst, str(why)))
|
||||||
if errors:
|
if errors:
|
||||||
raise Error(errors)
|
raise Error(errors)
|
||||||
|
|
|
@ -920,13 +920,13 @@ sends traffic to the first one connected successfully. ::
|
||||||
af, socktype, proto, canonname, sa = res
|
af, socktype, proto, canonname, sa = res
|
||||||
try:
|
try:
|
||||||
s = socket.socket(af, socktype, proto)
|
s = socket.socket(af, socktype, proto)
|
||||||
except socket.error, msg:
|
except socket.error as msg:
|
||||||
s = None
|
s = None
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
s.bind(sa)
|
s.bind(sa)
|
||||||
s.listen(1)
|
s.listen(1)
|
||||||
except socket.error, msg:
|
except socket.error as msg:
|
||||||
s.close()
|
s.close()
|
||||||
s = None
|
s = None
|
||||||
continue
|
continue
|
||||||
|
@ -955,12 +955,12 @@ sends traffic to the first one connected successfully. ::
|
||||||
af, socktype, proto, canonname, sa = res
|
af, socktype, proto, canonname, sa = res
|
||||||
try:
|
try:
|
||||||
s = socket.socket(af, socktype, proto)
|
s = socket.socket(af, socktype, proto)
|
||||||
except socket.error, msg:
|
except socket.error as msg:
|
||||||
s = None
|
s = None
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
s.connect(sa)
|
s.connect(sa)
|
||||||
except socket.error, msg:
|
except socket.error as msg:
|
||||||
s.close()
|
s.close()
|
||||||
s = None
|
s = None
|
||||||
continue
|
continue
|
||||||
|
|
|
@ -361,7 +361,7 @@ SSLSocket Objects
|
||||||
try:
|
try:
|
||||||
s.do_handshake()
|
s.do_handshake()
|
||||||
break
|
break
|
||||||
except ssl.SSLError, err:
|
except ssl.SSLError as err:
|
||||||
if err.args[0] == ssl.SSL_ERROR_WANT_READ:
|
if err.args[0] == ssl.SSL_ERROR_WANT_READ:
|
||||||
select.select([s], [], [])
|
select.select([s], [], [])
|
||||||
elif err.args[0] == ssl.SSL_ERROR_WANT_WRITE:
|
elif err.args[0] == ssl.SSL_ERROR_WANT_WRITE:
|
||||||
|
|
|
@ -274,6 +274,5 @@ Here is an example of how you would catch one of these exceptions::
|
||||||
p = xdrlib.Packer()
|
p = xdrlib.Packer()
|
||||||
try:
|
try:
|
||||||
p.pack_double(8.01)
|
p.pack_double(8.01)
|
||||||
except xdrlib.ConversionError, instance:
|
except xdrlib.ConversionError as instance:
|
||||||
print 'packing the double failed:', instance.msg
|
print 'packing the double failed:', instance.msg
|
||||||
|
|
||||||
|
|
|
@ -380,7 +380,7 @@ The client code for the preceding server::
|
||||||
proxy = xmlrpclib.ServerProxy("http://localhost:8000/")
|
proxy = xmlrpclib.ServerProxy("http://localhost:8000/")
|
||||||
try:
|
try:
|
||||||
proxy.add(2, 5)
|
proxy.add(2, 5)
|
||||||
except xmlrpclib.Fault, err:
|
except xmlrpclib.Fault as err:
|
||||||
print "A fault occurred"
|
print "A fault occurred"
|
||||||
print "Fault code: %d" % err.faultCode
|
print "Fault code: %d" % err.faultCode
|
||||||
print "Fault string: %s" % err.faultString
|
print "Fault string: %s" % err.faultString
|
||||||
|
@ -427,7 +427,7 @@ by providing an URI that doesn't point to an XMLRPC server::
|
||||||
|
|
||||||
try:
|
try:
|
||||||
proxy.some_method()
|
proxy.some_method()
|
||||||
except xmlrpclib.ProtocolError, err:
|
except xmlrpclib.ProtocolError as err:
|
||||||
print "A protocol error occurred"
|
print "A protocol error occurred"
|
||||||
print "URL: %s" % err.url
|
print "URL: %s" % err.url
|
||||||
print "HTTP/HTTPS headers: %s" % err.headers
|
print "HTTP/HTTPS headers: %s" % err.headers
|
||||||
|
@ -545,7 +545,7 @@ Example of Client Usage
|
||||||
|
|
||||||
try:
|
try:
|
||||||
print server.examples.getStateName(41)
|
print server.examples.getStateName(41)
|
||||||
except Error, v:
|
except Error as v:
|
||||||
print "ERROR", v
|
print "ERROR", v
|
||||||
|
|
||||||
To access an XML-RPC server through a proxy, you need to define a custom
|
To access an XML-RPC server through a proxy, you need to define a custom
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue