Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
pygeoip
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
pygeoip
Commits
545855cf
Commit
545855cf
authored
Jul 22, 2013
by
William Tisäter
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix codecs.open() for Python 3.X
parent
2abe41a3
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
30 additions
and
21 deletions
+30
-21
pygeoip/__init__.py
+30
-21
No files found.
pygeoip/__init__.py
View file @
545855cf
...
...
@@ -103,22 +103,31 @@ class GeoIP(object):
if
self
.
_flags
&
const
.
MMAP_CACHE
:
f
=
codecs
.
open
(
filename
,
'rb'
,
ENCODING
)
access
=
mmap
.
ACCESS_READ
self
.
_f
ilehandle
=
mmap
.
mmap
(
f
.
fileno
(),
0
,
access
=
access
)
self
.
_f
p
=
mmap
.
mmap
(
f
.
fileno
(),
0
,
access
=
access
)
f
.
close
()
elif
self
.
_flags
&
const
.
MEMORY_CACHE
:
f
=
codecs
.
open
(
filename
,
'rb'
,
ENCODING
)
self
.
_memoryBuffer
=
f
.
read
()
iohandle
=
BytesIO
if
PY3
else
StringIO
self
.
_filehandle
=
iohandle
(
self
.
_memoryBuffer
)
self
.
_memory
=
f
.
read
()
self
.
_fp
=
self
.
_str_to_fp
(
self
.
_memory
)
f
.
close
()
else
:
self
.
_f
ilehandle
=
codecs
.
open
(
filename
,
'rb'
,
ENCODING
)
self
.
_f
p
=
codecs
.
open
(
filename
,
'rb'
,
ENCODING
)
self
.
_lock
=
Lock
()
self
.
_setup_segments
()
@classmethod
def
_str_to_fp
(
cls
,
data
):
"""
Convert bytes data to file handle object
@param data: string data
@type data: str
@return: file handle object
@rtype: StringIO or BytesIO
"""
return
BytesIO
(
bytearray
(
data
,
ENCODING
))
if
PY3
else
StringIO
(
data
)
def
_setup_segments
(
self
):
"""
Parses the database file to determine what kind of database is
...
...
@@ -145,12 +154,12 @@ class GeoIP(object):
self
.
_databaseSegments
=
const
.
COUNTRY_BEGIN
self
.
_lock
.
acquire
()
filepos
=
self
.
_f
ilehandle
.
tell
()
self
.
_f
ilehandle
.
seek
(
-
3
,
os
.
SEEK_END
)
filepos
=
self
.
_f
p
.
tell
()
self
.
_f
p
.
seek
(
-
3
,
os
.
SEEK_END
)
for
i
in
range
(
const
.
STRUCTURE_INFO_MAX_SIZE
):
chars
=
chr
(
255
)
*
3
delim
=
self
.
_f
ilehandle
.
read
(
3
)
delim
=
self
.
_f
p
.
read
(
3
)
if
PY3
and
type
(
delim
)
is
bytes
:
delim
=
delim
.
decode
(
ENCODING
)
...
...
@@ -161,7 +170,7 @@ class GeoIP(object):
delim
=
delim
.
decode
(
ENCODING
)
if
delim
==
chars
:
byte
=
self
.
_f
ilehandle
.
read
(
1
)
byte
=
self
.
_f
p
.
read
(
1
)
self
.
_databaseType
=
ord
(
byte
)
# Compatibility with databases from April 2003 and earlier
...
...
@@ -182,7 +191,7 @@ class GeoIP(object):
const
.
ASNUM_EDITION
,
const
.
ASNUM_EDITION_V6
):
self
.
_databaseSegments
=
0
buf
=
self
.
_f
ilehandle
.
read
(
const
.
SEGMENT_RECORD_LENGTH
)
buf
=
self
.
_f
p
.
read
(
const
.
SEGMENT_RECORD_LENGTH
)
if
PY3
and
type
(
buf
)
is
bytes
:
buf
=
buf
.
decode
(
ENCODING
)
...
...
@@ -195,9 +204,9 @@ class GeoIP(object):
self
.
_recordLength
=
const
.
ORG_RECORD_LENGTH
break
else
:
self
.
_f
ilehandle
.
seek
(
-
4
,
os
.
SEEK_CUR
)
self
.
_f
p
.
seek
(
-
4
,
os
.
SEEK_CUR
)
self
.
_f
ilehandle
.
seek
(
filepos
,
os
.
SEEK_SET
)
self
.
_f
p
.
seek
(
filepos
,
os
.
SEEK_SET
)
self
.
_lock
.
release
()
def
_seek_country
(
self
,
ipnum
):
...
...
@@ -218,13 +227,13 @@ class GeoIP(object):
if
self
.
_flags
&
const
.
MEMORY_CACHE
:
startIndex
=
2
*
self
.
_recordLength
*
offset
endIndex
=
startIndex
+
(
2
*
self
.
_recordLength
)
buf
=
self
.
_memory
Buffer
[
startIndex
:
endIndex
]
buf
=
self
.
_memory
[
startIndex
:
endIndex
]
else
:
startIndex
=
2
*
self
.
_recordLength
*
offset
readLength
=
2
*
self
.
_recordLength
self
.
_lock
.
acquire
()
self
.
_f
ilehandle
.
seek
(
startIndex
,
os
.
SEEK_SET
)
buf
=
self
.
_f
ilehandle
.
read
(
readLength
)
self
.
_f
p
.
seek
(
startIndex
,
os
.
SEEK_SET
)
buf
=
self
.
_f
p
.
read
(
readLength
)
self
.
_lock
.
release
()
if
PY3
and
type
(
buf
)
is
bytes
:
...
...
@@ -262,8 +271,8 @@ class GeoIP(object):
read_length
=
(
2
*
self
.
_recordLength
-
1
)
*
self
.
_databaseSegments
self
.
_lock
.
acquire
()
self
.
_f
ilehandle
.
seek
(
seek_org
+
read_length
,
os
.
SEEK_SET
)
buf
=
self
.
_f
ilehandle
.
read
(
const
.
MAX_ORG_RECORD_LENGTH
)
self
.
_f
p
.
seek
(
seek_org
+
read_length
,
os
.
SEEK_SET
)
buf
=
self
.
_f
p
.
read
(
const
.
MAX_ORG_RECORD_LENGTH
)
self
.
_lock
.
release
()
if
PY3
and
type
(
buf
)
is
bytes
:
...
...
@@ -335,8 +344,8 @@ class GeoIP(object):
read_length
=
(
2
*
self
.
_recordLength
-
1
)
*
self
.
_databaseSegments
self
.
_lock
.
acquire
()
self
.
_f
ilehandle
.
seek
(
seek_country
+
read_length
,
os
.
SEEK_SET
)
buf
=
self
.
_f
ilehandle
.
read
(
const
.
FULL_RECORD_LENGTH
)
self
.
_f
p
.
seek
(
seek_country
+
read_length
,
os
.
SEEK_SET
)
buf
=
self
.
_f
p
.
read
(
const
.
FULL_RECORD_LENGTH
)
self
.
_lock
.
release
()
if
PY3
and
type
(
buf
)
is
bytes
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment