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 ...@@ -42,7 +42,7 @@ from threading import Lock
try: try:
from StringIO import StringIO from StringIO import StringIO
except ImportError: except ImportError:
from io import StringIO from io import StringIO, BytesIO
import pygeoip.const import pygeoip.const
from pygeoip import util from pygeoip import util
...@@ -110,7 +110,8 @@ class GeoIP(GeoIPBase): ...@@ -110,7 +110,8 @@ class GeoIP(GeoIPBase):
elif self._flags & const.MEMORY_CACHE: elif self._flags & const.MEMORY_CACHE:
f = open(filename, 'rb') f = open(filename, 'rb')
self._memoryBuffer = f.read() self._memoryBuffer = f.read()
self._filehandle = StringIO(self._memoryBuffer) iohandle = BytesIO if PY3 else StringIO
self._filehandle = iohandle(self._memoryBuffer)
f.close() f.close()
else: else:
...@@ -150,7 +151,8 @@ class GeoIP(GeoIPBase): ...@@ -150,7 +151,8 @@ class GeoIP(GeoIPBase):
flag = 'unicode_escape' flag = 'unicode_escape'
delim = self._filehandle.read(3) delim = self._filehandle.read(3)
if (delim == chars) if PY3 else (delim == unicode(chars, flag)): 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 # Compatibility with databases from April 2003 and earlier
if (self._databaseType >= 106): if (self._databaseType >= 106):
...@@ -212,7 +214,9 @@ class GeoIP(GeoIPBase): ...@@ -212,7 +214,9 @@ class GeoIP(GeoIPBase):
x = [0, 0] x = [0, 0]
for i in range(2): for i in range(2):
for j in range(self._recordLength): 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 ipnum & (1 << depth):
if x[1] >= self._databaseSegments: if x[1] >= self._databaseSegments:
return x[1] return x[1]
......
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