[Python-checkins] cpython: Cleanup code: remove int/long idioms and simplify a while statement.

florent.xicluna python-checkins at python.org
Sun Oct 23 22:21:22 CEST 2011


http://hg.python.org/cpython/rev/67053b135ed9
changeset:   73076:67053b135ed9
user:        Florent Xicluna <florent.xicluna at gmail.com>
date:        Sun Oct 23 22:11:00 2011 +0200
summary:
  Cleanup code: remove int/long idioms and simplify a while statement.

files:
  Lib/ftplib.py        |  17 ++++-------------
  Lib/optparse.py      |   5 +----
  Lib/pickletools.py   |   5 +----
  Lib/xdrlib.py        |   6 +-----
  Lib/xmlrpc/client.py |  12 +++---------
  5 files changed, 10 insertions(+), 35 deletions(-)


diff --git a/Lib/ftplib.py b/Lib/ftplib.py
--- a/Lib/ftplib.py
+++ b/Lib/ftplib.py
@@ -175,10 +175,8 @@
 
     # Internal: "sanitize" a string for printing
     def sanitize(self, s):
-        if s[:5] == 'pass ' or s[:5] == 'PASS ':
-            i = len(s)
-            while i > 5 and s[i-1] in {'\r', '\n'}:
-                i = i-1
+        if s[:5] in {'pass ', 'PASS '}:
+            i = len(s.rstrip('\r\n'))
             s = s[:5] + '*'*(i-5) + s[i:]
         return repr(s)
 
@@ -596,10 +594,7 @@
         resp = self.sendcmd('SIZE ' + filename)
         if resp[:3] == '213':
             s = resp[3:].strip()
-            try:
-                return int(s)
-            except (OverflowError, ValueError):
-                return int(s)
+            return int(s)
 
     def mkd(self, dirname):
         '''Make a directory, return its full pathname.'''
@@ -861,11 +856,7 @@
     m = _150_re.match(resp)
     if not m:
         return None
-    s = m.group(1)
-    try:
-        return int(s)
-    except (OverflowError, ValueError):
-        return int(s)
+    return int(m.group(1))
 
 
 _227_re = None
diff --git a/Lib/optparse.py b/Lib/optparse.py
--- a/Lib/optparse.py
+++ b/Lib/optparse.py
@@ -417,11 +417,8 @@
 def _parse_int(val):
     return _parse_num(val, int)
 
-def _parse_long(val):
-    return _parse_num(val, int)
-
 _builtin_cvt = { "int" : (_parse_int, _("integer")),
-                 "long" : (_parse_long, _("long integer")),
+                 "long" : (_parse_int, _("integer")),
                  "float" : (float, _("floating-point")),
                  "complex" : (complex, _("complex")) }
 
diff --git a/Lib/pickletools.py b/Lib/pickletools.py
--- a/Lib/pickletools.py
+++ b/Lib/pickletools.py
@@ -510,10 +510,7 @@
     elif s == b"01":
         return True
 
-    try:
-        return int(s)
-    except OverflowError:
-        return int(s)
+    return int(s)
 
 def read_decimalnl_long(f):
     r"""
diff --git a/Lib/xdrlib.py b/Lib/xdrlib.py
--- a/Lib/xdrlib.py
+++ b/Lib/xdrlib.py
@@ -141,11 +141,7 @@
         data = self.__buf[i:j]
         if len(data) < 4:
             raise EOFError
-        x = struct.unpack('>L', data)[0]
-        try:
-            return int(x)
-        except OverflowError:
-            return x
+        return struct.unpack('>L', data)[0]
 
     def unpack_int(self):
         i = self.__pos
diff --git a/Lib/xmlrpc/client.py b/Lib/xmlrpc/client.py
--- a/Lib/xmlrpc/client.py
+++ b/Lib/xmlrpc/client.py
@@ -535,15 +535,6 @@
         write("<value><nil/></value>")
     dispatch[type(None)] = dump_nil
 
-    def dump_int(self, value, write):
-        # in case ints are > 32 bits
-        if value > MAXINT or value < MININT:
-            raise OverflowError("int exceeds XML-RPC limits")
-        write("<value><int>")
-        write(str(value))
-        write("</int></value>\n")
-    #dispatch[int] = dump_int
-
     def dump_bool(self, value, write):
         write("<value><boolean>")
         write(value and "1" or "0")
@@ -558,6 +549,9 @@
         write("</int></value>\n")
     dispatch[int] = dump_long
 
+    # backward compatible
+    dump_int = dump_long
+
     def dump_double(self, value, write):
         write("<value><double>")
         write(repr(value))

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list