Ema has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/355229 )

Change subject: bgp.ip: do not crash on unicode strings
......................................................................

bgp.ip: do not crash on unicode strings

The proper way to support both str and unicode strings is to use
isinstance(x, basestring) rather than type(x) == str.

Change-Id: I7a9ec001254ed2b05e38b068324088c56edc529b
---
M pybal/bgp/ip.py
M pybal/bgp/test_ip.py
2 files changed, 14 insertions(+), 3 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/operations/debs/pybal 
refs/changes/29/355229/1

diff --git a/pybal/bgp/ip.py b/pybal/bgp/ip.py
index 444da9c..a9f207e 100644
--- a/pybal/bgp/ip.py
+++ b/pybal/bgp/ip.py
@@ -37,7 +37,7 @@
             self.addressfamily = addressfamily
 
             prefix, self.prefixlen = ipprefix
-            if type(prefix) is str:
+            if isinstance(prefix, basestring):
                 # tuple (ipstr, prefixlen)
                 self.prefix = prefix
             elif type(prefix) is int:
@@ -49,7 +49,7 @@
             else:
                 # Assume prefix is a sequence of octets
                 self.prefix = b"".join(map(chr, prefix))
-        elif type(ipprefix) is str:
+        elif isinstance(ipprefix, basestring):
             # textual form
             prefix, prefixlen = ipprefix.split('/')
             self.addressfamily = addressfamily or (':' in prefix and AFI_INET6 
or AFI_INET)
@@ -143,7 +143,7 @@
     """Class that represents a single non-prefix IPv4 IP."""
 
     def __init__(self, ip):
-        if type(ip) is str and len(ip) > 4:
+        if isinstance(ip, basestring) and len(ip) > 4:
             super(IPv4IP, self).__init__(ip + '/32', AFI_INET)
         else:
             super(IPv4IP, self).__init__((ip, 32), AFI_INET)
diff --git a/pybal/bgp/test_ip.py b/pybal/bgp/test_ip.py
index bbbc429..c062f46 100644
--- a/pybal/bgp/test_ip.py
+++ b/pybal/bgp/test_ip.py
@@ -98,6 +98,17 @@
         self.assertEquals(len(prefix), 46)
         self.assertEquals(str(prefix), '2620:0:863:0:0:0:0:0/46')
 
+    def testPrefixUnicode(self):
+        prefix = ip.IPPrefix(u'192.168.1.2/24')
+        self.assertEquals(len(prefix), 24)
+        prefix.mask(len(prefix))
+        self.assertEquals(prefix, ip.IPPrefix('192.168.1.0/24'))
+
+    def testIPUnicode(self):
+        prefix = ip.IPv4IP(u'192.168.1.2')
+        self.assertEquals(len(prefix), 32)
+        prefix.mask(len(prefix))
+
     def testComparisons(self):
         p1, p2 = ip.IPPrefix('1.2.3.4/8'), ip.IPPrefix('1.2.3.4/16')
         self.assertNotEqual(p1, p2)

-- 
To view, visit https://gerrit.wikimedia.org/r/355229
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I7a9ec001254ed2b05e38b068324088c56edc529b
Gerrit-PatchSet: 1
Gerrit-Project: operations/debs/pybal
Gerrit-Branch: master
Gerrit-Owner: Ema <e...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to