[Python-checkins] cpython: Issue 14814: %s implies coercion with str() - remove a lot of redundant str()

nick.coghlan python-checkins at python.org
Sat Jul 7 05:35:26 CEST 2012


http://hg.python.org/cpython/rev/d9c98730e2e8
changeset:   77959:d9c98730e2e8
user:        Nick Coghlan <ncoghlan at gmail.com>
date:        Sat Jul 07 13:34:50 2012 +1000
summary:
  Issue 14814: %s implies coercion with str() - remove a lot of redundant str() calls from the ipaddress implementation

files:
  Lib/ipaddress.py |  67 +++++++++++++++++------------------
  1 files changed, 33 insertions(+), 34 deletions(-)


diff --git a/Lib/ipaddress.py b/Lib/ipaddress.py
--- a/Lib/ipaddress.py
+++ b/Lib/ipaddress.py
@@ -242,7 +242,7 @@
         raise TypeError('first and last must be IP addresses, not networks')
     if first.version != last.version:
         raise TypeError("%s and %s are not of the same version" % (
-                str(first), str(last)))
+                         first, last))
     if first > last:
         raise ValueError('last IP address must be greater than first')
 
@@ -266,7 +266,7 @@
             if current <= last_int:
                 break
         prefix = _get_prefix_length(first_int, current, ip_bits)
-        net = ip('%s/%d' % (str(first), prefix))
+        net = ip('%s/%d' % (first, prefix))
         yield net
         if current == ip._ALL_ONES:
             break
@@ -348,12 +348,12 @@
         if isinstance(ip, _BaseAddress):
             if ips and ips[-1]._version != ip._version:
                 raise TypeError("%s and %s are not of the same version" % (
-                        str(ip), str(ips[-1])))
+                                 ip, ips[-1]))
             ips.append(ip)
         elif ip._prefixlen == ip._max_prefixlen:
             if ips and ips[-1]._version != ip._version:
                 raise TypeError("%s and %s are not of the same version" % (
-                        str(ip), str(ips[-1])))
+                                 ip, ips[-1]))
             try:
                 ips.append(ip.ip)
             except AttributeError:
@@ -361,7 +361,7 @@
         else:
             if nets and nets[-1]._version != ip._version:
                 raise TypeError("%s and %s are not of the same version" % (
-                        str(ip), str(nets[-1])))
+                                 ip, nets[-1]))
             nets.append(ip)
 
     # sort and dedup
@@ -517,10 +517,10 @@
     def __lt__(self, other):
         if self._version != other._version:
             raise TypeError('%s and %s are not of the same version' % (
-                    str(self), str(other)))
+                             self, other))
         if not isinstance(other, _BaseAddress):
             raise TypeError('%s and %s are not of the same type' % (
-                    str(self), str(other)))
+                             self, other))
         if self._ip != other._ip:
             return self._ip < other._ip
         return False
@@ -528,10 +528,10 @@
     def __gt__(self, other):
         if self._version != other._version:
             raise TypeError('%s and %s are not of the same version' % (
-                    str(self), str(other)))
+                             self, other))
         if not isinstance(other, _BaseAddress):
             raise TypeError('%s and %s are not of the same type' % (
-                    str(self), str(other)))
+                             self, other))
         if self._ip != other._ip:
             return self._ip > other._ip
         return False
@@ -582,8 +582,7 @@
         return '%s(%r)' % (self.__class__.__name__, str(self))
 
     def __str__(self):
-        return '%s/%d' % (str(self.network_address),
-                          self.prefixlen)
+        return '%s/%d' % (self.network_address, self.prefixlen)
 
     def hosts(self):
         """Generate Iterator over usable hosts in a network.
@@ -621,10 +620,10 @@
     def __lt__(self, other):
         if self._version != other._version:
             raise TypeError('%s and %s are not of the same version' % (
-                    str(self), str(other)))
+                             self, other))
         if not isinstance(other, _BaseNetwork):
             raise TypeError('%s and %s are not of the same type' % (
-                    str(self), str(other)))
+                             self, other))
         if self.network_address != other.network_address:
             return self.network_address < other.network_address
         if self.netmask != other.netmask:
@@ -634,10 +633,10 @@
     def __gt__(self, other):
         if self._version != other._version:
             raise TypeError('%s and %s are not of the same version' % (
-                    str(self), str(other)))
+                             self, other))
         if not isinstance(other, _BaseNetwork):
             raise TypeError('%s and %s are not of the same type' % (
-                    str(self), str(other)))
+                             self, other))
         if self.network_address != other.network_address:
             return self.network_address > other.network_address
         if self.netmask != other.netmask:
@@ -659,7 +658,7 @@
     def __eq__(self, other):
         if not isinstance(other, _BaseNetwork):
             raise TypeError('%s and %s are not of the same type' % (
-                    str(self), str(other)))
+                             self, other))
         return (self._version == other._version and
                 self.network_address == other.network_address and
                 int(self.netmask) == int(other.netmask))
@@ -712,15 +711,15 @@
 
     @property
     def with_prefixlen(self):
-        return '%s/%d' % (str(self.network_address), self._prefixlen)
+        return '%s/%d' % (self.network_address, self._prefixlen)
 
     @property
     def with_netmask(self):
-        return '%s/%s' % (str(self.network_address), str(self.netmask))
+        return '%s/%s' % (self.network_address, self.netmask)
 
     @property
     def with_hostmask(self):
-        return '%s/%s' % (str(self.network_address), str(self.hostmask))
+        return '%s/%s' % (self.network_address, self.hostmask)
 
     @property
     def num_addresses(self):
@@ -777,10 +776,10 @@
         """
         if not self._version == other._version:
             raise TypeError("%s and %s are not of the same version" % (
-                str(self), str(other)))
+                             self, other))
 
         if not isinstance(other, _BaseNetwork):
-            raise TypeError("%s is not a network object" % str(other))
+            raise TypeError("%s is not a network object" % other)
 
         if not (other.network_address >= self.network_address and
                 other.broadcast_address <= self.broadcast_address):
@@ -789,8 +788,8 @@
             raise StopIteration
 
         # Make sure we're comparing the network of other.
-        other = other.__class__('%s/%s' % (str(other.network_address),
-                                           str(other.prefixlen)))
+        other = other.__class__('%s/%s' % (other.network_address,
+                                           other.prefixlen))
 
         s1, s2 = self.subnets()
         while s1 != other and s2 != other:
@@ -806,7 +805,7 @@
                 # If we got here, there's a bug somewhere.
                 raise AssertionError('Error performing exclusion: '
                                      's1: %s s2: %s other: %s' %
-                                     (str(s1), str(s2), str(other)))
+                                     (s1, s2, other))
         if s1 == other:
             yield s2
         elif s2 == other:
@@ -815,7 +814,7 @@
             # If we got here, there's a bug somewhere.
             raise AssertionError('Error performing exclusion: '
                                  's1: %s s2: %s other: %s' %
-                                 (str(s1), str(s2), str(other)))
+                                 (s1, s2, other))
 
     def compare_networks(self, other):
         """Compare two IP objects.
@@ -852,7 +851,7 @@
         # does this need to raise a ValueError?
         if self._version != other._version:
             raise TypeError('%s and %s are not of the same type' % (
-                    str(self), str(other)))
+                             self, other))
         # self._version == other._version below here:
         if self.network_address < other.network_address:
             return -1
@@ -919,11 +918,11 @@
         if not self._is_valid_netmask(str(new_prefixlen)):
             raise ValueError(
                 'prefix length diff %d is invalid for netblock %s' % (
-                    new_prefixlen, str(self)))
+                    new_prefixlen, self))
 
         first = self.__class__('%s/%s' %
-                                 (str(self.network_address),
-                                  str(self._prefixlen + prefixlen_diff)))
+                                 (self.network_address,
+                                  self._prefixlen + prefixlen_diff))
 
         yield first
         current = first
@@ -932,8 +931,8 @@
             if broadcast == self.broadcast_address:
                 return
             new_addr = self._address_class(int(broadcast) + 1)
-            current = self.__class__('%s/%s' % (str(new_addr),
-                                                str(new_prefixlen)))
+            current = self.__class__('%s/%s' % (new_addr,
+                                                new_prefixlen))
 
             yield current
 
@@ -973,10 +972,10 @@
                 'current prefixlen is %d, cannot have a prefixlen_diff of %d' %
                 (self.prefixlen, prefixlen_diff))
         # TODO (pmoody): optimize this.
-        t = self.__class__('%s/%d' % (str(self.network_address),
+        t = self.__class__('%s/%d' % (self.network_address,
                                       self.prefixlen - prefixlen_diff),
                                      strict=False)
-        return t.__class__('%s/%d' % (str(t.network_address), t.prefixlen))
+        return t.__class__('%s/%d' % (t.network_address, t.prefixlen))
 
 
 class _BaseV4:
@@ -2032,7 +2031,7 @@
         if strict:
             if (IPv6Address(int(self.network_address) & int(self.netmask)) !=
                 self.network_address):
-                raise ValueError('%s has host bits set' % str(self))
+                raise ValueError('%s has host bits set' % self)
         self.network_address = IPv6Address(int(self.network_address) &
                                            int(self.netmask))
 

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


More information about the Python-checkins mailing list