]> granicus.if.org Git - python/commitdiff
Issue #16341: convert examples to use except ... as ... syntax.
authorAndrew Svetlov <andrew.svetlov@gmail.com>
Tue, 30 Oct 2012 19:56:43 +0000 (21:56 +0200)
committerAndrew Svetlov <andrew.svetlov@gmail.com>
Tue, 30 Oct 2012 19:56:43 +0000 (21:56 +0200)
Doc/howto/urllib2.rst
Doc/includes/email-unpack.py
Doc/includes/sqlite3/complete_statement.py
Doc/library/csv.rst
Doc/library/getopt.rst
Doc/library/shutil.rst
Doc/library/socket.rst
Doc/library/ssl.rst
Doc/library/xdrlib.rst
Doc/library/xmlrpclib.rst

index fe77d13d02b55a15fd4d9442d0dbba8b3a2a3c66..a8553081b57016c23bca23c5455a67667624daeb 100644 (file)
@@ -201,7 +201,7 @@ e.g. ::
 
     >>> req = urllib2.Request('http://www.pretend_server.org')
     >>> try: urllib2.urlopen(req)
-    ... except URLError, e:
+    ... except URLError as e:
     ...    print e.reason   #doctest: +SKIP
     ...
     (4, 'getaddrinfo failed')
@@ -310,7 +310,7 @@ geturl, and info, methods. ::
     >>> req = urllib2.Request('http://www.python.org/fish.html')
     >>> try:
     ...     urllib2.urlopen(req)
-    ... except urllib2.HTTPError, e:
+    ... except urllib2.HTTPError as e:
     ...     print e.code
     ...     print e.read() #doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
     ...
@@ -338,10 +338,10 @@ Number 1
     req = Request(someurl)
     try:
         response = urlopen(req)
-    except HTTPError, e:
+    except HTTPError as e:
         print 'The server couldn\'t fulfill the request.'
         print 'Error code: ', e.code
-    except URLError, e:
+    except URLError as e:
         print 'We failed to reach a server.'
         print 'Reason: ', e.reason
     else:
@@ -362,7 +362,7 @@ Number 2
     req = Request(someurl)
     try:
         response = urlopen(req)
-    except URLError, e:
+    except URLError as e:
         if hasattr(e, 'reason'):
             print 'We failed to reach a server.'
             print 'Reason: ', e.reason
index 8f99ded22c3a67a6d32cc18370dd1c913c61c21b..a8f712d26fa9e2c7bb1f02389d1b82a293669668 100644 (file)
@@ -35,7 +35,7 @@ Usage: %prog [options] msgfile
 
     try:
         os.mkdir(opts.directory)
-    except OSError, e:
+    except OSError as e:
         # Ignore directory exists error
         if e.errno != errno.EEXIST:
             raise
index 22525e310df659b008af0cd96d5a3e17af33586a..76ea7f6a66abc82c20a4c09217059f1971832fe8 100644 (file)
@@ -23,7 +23,7 @@ while True:
 
             if buffer.lstrip().upper().startswith("SELECT"):
                 print cur.fetchall()
-        except sqlite3.Error, e:
+        except sqlite3.Error as e:
             print "An error occurred:", e.args[0]
         buffer = ""
 
index 735a3053ae6ce3cd5f51a15ab44b4adb073873f1..1fbc5f2bb4056a195caaa63d8a6c0b6c3040d2a4 100644 (file)
@@ -480,7 +480,7 @@ A slightly more advanced use of the reader --- catching and reporting errors::
        try:
            for row in reader:
                print row
-       except csv.Error, e:
+       except csv.Error as 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
index b3ba6146255569407ed9c266d18013a19b8d2a7c..b454814b3b600402d4983a1489caf72ba97cecb3 100644 (file)
@@ -126,7 +126,7 @@ In a script, typical usage is something like this::
    def main():
        try:
            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 str(err) # will print something like "option -a not recognized"
            usage()
index a1e1696fcb3c751cc2943c3423d112ced1e4ba1e..e8974839acc85293aa9afe3d65ace3717484d57a 100644 (file)
@@ -219,18 +219,18 @@ provided by this module. ::
                else:
                    copy2(srcname, dstname)
                # XXX What about devices, sockets etc.?
-           except (IOError, os.error), why:
+           except (IOError, os.error) as why:
                errors.append((srcname, dstname, str(why)))
            # catch the Error from the recursive copytree so that we can
            # continue with other files
-           except Error, err:
+           except Error as err:
                errors.extend(err.args[0])
        try:
            copystat(src, dst)
        except WindowsError:
            # can't copy file access times on Windows
            pass
-       except OSError, why:
+       except OSError as why:
            errors.extend((src, dst, str(why)))
        if errors:
            raise Error(errors)
index f6dc4f037901ec4218b33fcc96a0e16e5d98c1b6..0e5dac0b777d1db8fbb2b3ba4e6f088f1ecd6a5e 100644 (file)
@@ -920,13 +920,13 @@ sends traffic to the first one connected successfully. ::
        af, socktype, proto, canonname, sa = res
        try:
            s = socket.socket(af, socktype, proto)
-       except socket.error, msg:
+       except socket.error as msg:
            s = None
            continue
        try:
            s.bind(sa)
            s.listen(1)
-       except socket.error, msg:
+       except socket.error as msg:
            s.close()
            s = None
            continue
@@ -955,12 +955,12 @@ sends traffic to the first one connected successfully. ::
        af, socktype, proto, canonname, sa = res
        try:
            s = socket.socket(af, socktype, proto)
-       except socket.error, msg:
+       except socket.error as msg:
            s = None
            continue
        try:
            s.connect(sa)
-       except socket.error, msg:
+       except socket.error as msg:
            s.close()
            s = None
            continue
index 87824392d9e55e3566269bd59c41a635fb9e7d7f..19b90a15cb7b14d9333f3ffe72361b5a6f87d7ef 100644 (file)
@@ -361,7 +361,7 @@ SSLSocket Objects
             try:
                 s.do_handshake()
                 break
-            except ssl.SSLError, err:
+            except ssl.SSLError as err:
                 if err.args[0] == ssl.SSL_ERROR_WANT_READ:
                     select.select([s], [], [])
                 elif err.args[0] == ssl.SSL_ERROR_WANT_WRITE:
index e56650cd3aff70d806a716c3fc347dabcda938dc..6f05306bb81cf12e40d83d170b5e9588200cd68a 100644 (file)
@@ -274,6 +274,5 @@ Here is an example of how you would catch one of these exceptions::
    p = xdrlib.Packer()
    try:
        p.pack_double(8.01)
-   except xdrlib.ConversionError, instance:
+   except xdrlib.ConversionError as instance:
        print 'packing the double failed:', instance.msg
-
index 64c67ad274d0b99a65747db4dcfaed40ef3e2c42..f50f270819bb132fdc7506752a72a29300a37efc 100644 (file)
@@ -380,7 +380,7 @@ The client code for the preceding server::
    proxy = xmlrpclib.ServerProxy("http://localhost:8000/")
    try:
        proxy.add(2, 5)
-   except xmlrpclib.Fault, err:
+   except xmlrpclib.Fault as err:
        print "A fault occurred"
        print "Fault code: %d" % err.faultCode
        print "Fault string: %s" % err.faultString
@@ -427,7 +427,7 @@ by providing an URI that doesn't point to an XMLRPC server::
 
    try:
        proxy.some_method()
-   except xmlrpclib.ProtocolError, err:
+   except xmlrpclib.ProtocolError as err:
        print "A protocol error occurred"
        print "URL: %s" % err.url
        print "HTTP/HTTPS headers: %s" % err.headers
@@ -545,7 +545,7 @@ Example of Client Usage
 
    try:
        print server.examples.getStateName(41)
-   except Error, v:
+   except Error as v:
        print "ERROR", v
 
 To access an XML-RPC server through a proxy, you need to define  a custom