jsommers / pytricia

A library for fast IP address lookup in Python.
GNU Lesser General Public License v3.0
214 stars 22 forks source link

Look up by bytes fails #35

Open rjschwei opened 3 years ago

rjschwei commented 3 years ago

The byte lookup is coded for Little Endian systems and breaks on Big Endian machines, yes S390 is alive and kicking.

 self = <test.PyTriciaTests testMethod=testNonStringKey>

    def testNonStringKey(self):
         pyt = pytricia.PyTricia()

        # insert as string
        pyt['10.1.2.3/24'] = 'abc'

        # lookup as string
        for i in range(256):
             self.assertEqual(pyt['10.1.2.{}'.format(i)], 'abc')

        # lookup as bytes (or, ugh, another str in python2)
        b = socket.inet_aton('10.1.2.3')
        self.assertEqual(pyt[b], 'abc')

        # bytes in py3k.  python2 stinks.
        if sys.version_info.major == 3 and sys.version_info.minor >= 4:
            i = b[0] * 2**24 + b[1] * 2**16 + b[2] * 2**8
            for j in range(256):
>               self.assertEqual(pyt[i+j], 'abc')
E               KeyError: 'Prefix not found.'