Commit ef94ca08 by William Tisäter

Fix MEMORY_CACHE and MMAP_CACHE in Python 3 - issue #21

parent a4d3785f
......@@ -42,7 +42,7 @@ from threading import Lock
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
from io import StringIO, BytesIO
import pygeoip.const
from pygeoip import util
......@@ -110,7 +110,8 @@ class GeoIP(GeoIPBase):
elif self._flags & const.MEMORY_CACHE:
f = open(filename, 'rb')
self._memoryBuffer = f.read()
self._filehandle = StringIO(self._memoryBuffer)
iohandle = BytesIO if PY3 else StringIO
self._filehandle = iohandle(self._memoryBuffer)
f.close()
else:
......@@ -150,7 +151,8 @@ class GeoIP(GeoIPBase):
flag = 'unicode_escape'
delim = self._filehandle.read(3)
if (delim == chars) if PY3 else (delim == unicode(chars, flag)):
self._databaseType = ord(self._filehandle.read(1))
byte = self._filehandle.read(1)
self._databaseType = ord(byte)
# Compatibility with databases from April 2003 and earlier
if (self._databaseType >= 106):
......@@ -212,7 +214,9 @@ class GeoIP(GeoIPBase):
x = [0, 0]
for i in range(2):
for j in range(self._recordLength):
x[i] += ord(buf[self._recordLength * i + j]) << (j * 8)
byte = buf[self._recordLength * i + j]
magic = byte if type(byte) is int else ord(byte)
x[i] += magic << (j * 8)
if ipnum & (1 << depth):
if x[1] >= self._databaseSegments:
return x[1]
......
......@@ -53,10 +53,10 @@ def ip2long_v4(ip):
if PY3:
# int and long are unified in py3
return int(ip_array[0]) * 16777216 + int(ip_array[1]) * 65536 + \
int(ip_array[2]) * 256 + int(ip_array[3])
int(ip_array[2]) * 256 + int(ip_array[3])
else:
return long(ip_array[0]) * 16777216 + long(ip_array[1]) * 65536 + \
long(ip_array[2]) * 256 + long(ip_array[3])
long(ip_array[2]) * 256 + long(ip_array[3])
def ip2long_v6(ip):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment