[tor-commits] [bridgedb/develop] Make `isValidIP()` backwards compatible with deprecated `is_valid_ip()`.

isis at torproject.org isis at torproject.org
Thu Aug 28 04:02:17 UTC 2014


commit ee0ba04f6345b22c9adea0a7eb7761cfb5558fb1
Author: Isis Lovecruft <isis at torproject.org>
Date:   Wed Jun 11 00:10:21 2014 +0000

    Make `isValidIP()` backwards compatible with deprecated `is_valid_ip()`.
    
    The old `bridgedb.Bridges.is_valid_ip()` function expected a single
    string parameter, and would return a boolean. The new
    `bridgedb.parse.addr.isValidIP()` function expected an
    `ipaddr.IPAddress` as a parameter.
    
     * CHANGE `bridgedb.parse.addr.isValidIP()` to convert any strings which
       it receives as arguments into `ipaddr.IPAddress`s, which makes it
       backwards compatible with the (deprecated)
       `bridgedb.Bridges.is_valid_ip()` function.
---
 lib/bridgedb/parse/addr.py |   47 +++++++++++++++++++++++++-------------------
 1 file changed, 27 insertions(+), 20 deletions(-)

diff --git a/lib/bridgedb/parse/addr.py b/lib/bridgedb/parse/addr.py
index 01acf58..750ed78 100644
--- a/lib/bridgedb/parse/addr.py
+++ b/lib/bridgedb/parse/addr.py
@@ -396,34 +396,41 @@ def isValidIP(ip):
     >>> isValidIP('xyzzy')
     False
 
-    :type ip: An :class:`ipaddr.IPAddress`, :class:`ipaddr.IPv4Address`, or
-        :class:`ipaddr.IPv6Address`.
-    :param ip: An ``ipaddr.IPAddress`` class.
+    :type ip: An :class:`ipaddr.IPAddress`, :class:`ipaddr.IPv4Address`,
+        :class:`ipaddr.IPv6Address`, or str
+    :param ip: An IP address. If it is a string, it will be converted to a
+        :class:`ipaddr.IPAddress`.
     :rtype: boolean
     :returns: ``True``, if **ip** passes the checks; False otherwise.
     """
     reasons  = []
 
-    if ip.is_link_local:
-        reasons.append('link local')
-    if ip.is_loopback:
-        reasons.append('loopback')
-    if ip.is_multicast:
-        reasons.append('multicast')
-    if ip.is_private:
-        reasons.append('private')
-    if ip.is_unspecified:
-        reasons.append('unspecified')
-
-    if (ip.version == 6) and ip.is_site_local:
-        reasons.append('site local')
-    elif (ip.version == 4) and ip.is_reserved:
-        reasons.append('reserved')
+    try:
+        if isinstance(ip, basestring):
+            ip = ipaddr.IPAddress(ip)
+
+        if ip.is_link_local:
+            reasons.append('link local')
+        if ip.is_loopback:
+            reasons.append('loopback')
+        if ip.is_multicast:
+            reasons.append('multicast')
+        if ip.is_private:
+            reasons.append('private')
+        if ip.is_unspecified:
+            reasons.append('unspecified')
+
+        if (ip.version == 6) and ip.is_site_local:
+            reasons.append('site local')
+        elif (ip.version == 4) and ip.is_reserved:
+            reasons.append('reserved')
+    except ValueError:
+        reasons.append('cannot convert to ip')
 
     if reasons:
         explain = ', '.join([r for r in reasons]).strip(', ')
-        logging.debug("IPv%d address %s is invalid! Reason(s): %s"
-                      % (ip.version, ip, explain))
+        logging.debug("IP address %r is invalid! Reason(s): %s"
+                      % (ip, explain))
         return False
     return True
 





More information about the tor-commits mailing list