Skip to content

Commit ba7f44f

Browse files
committed
Initialize class properly
1 parent 2636738 commit ba7f44f

File tree

3 files changed

+25
-16
lines changed

3 files changed

+25
-16
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ before_install:
2020
- if [[ $TRAVIS_PYTHON_VERSION == '2.6' ]]; then pip install unittest2; fi
2121

2222
script:
23-
- CFLAGS="-Werror -Wall -Wextra" python setup.py test
23+
- MAXMINDDB_PURE_PYTHON=0 CFLAGS="-Werror -Wall -Wextra" python setup.py test
2424
- MAXMINDDB_PURE_PYTHON=1 coverage run --source=maxminddb setup.py test
2525
- if [[ $TRAVIS_PYTHON_VERSION == '2.7' ]]; then pylint --rcfile .pylintrc maxminddb/*.py; fi
2626

maxminddb/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
import os
33

44
try:
5-
if os.environ.get('MAXMINDDB_PURE_PYTHON'):
5+
if os.environ.get('MAXMINDDB_PURE_PYTHON') == '1':
66
raise ImportError()
77
from maxminddb.extension import Reader, InvalidDatabaseError
8-
except ImportError:
8+
except ImportError as e:
9+
if os.environ.get('MAXMINDDB_PURE_PYTHON') == '0':
10+
raise e
911
from maxminddb.decoder import InvalidDatabaseError
1012
from maxminddb.reader import Reader
1113

maxminddb/extension/maxminddb.c

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,47 +49,47 @@ static PyObject *from_uint128(const MMDB_entry_data_list_s *entry_data_list);
4949
# define UNUSED(x) UNUSED_ ## x
5050
#endif
5151

52-
static PyObject *Reader_constructor(PyObject *UNUSED(self), PyObject *args)
52+
static int Reader_constructor(PyObject *self, PyObject *args, PyObject *UNUSED(kwds))
5353
{
5454
char *filename;
5555

5656
if (!PyArg_ParseTuple(args, "s", &filename)) {
57-
return NULL;
57+
return -1;
5858
}
5959

6060
if (0 != access(filename, R_OK)) {
6161
PyErr_Format(FILE_NOT_FOUND_ERROR,
6262
"No such file or directory: '%s'",
6363
filename);
64-
return NULL;
64+
return -1;
6565
}
6666

6767
MMDB_s *mmdb = (MMDB_s *)malloc(sizeof(MMDB_s));
6868
if (NULL == mmdb) {
6969
PyErr_NoMemory();
70-
return NULL;
70+
return -1;
7171
}
7272

73-
Reader_obj *obj = PyObject_New(Reader_obj, &Reader_Type);
74-
if (!obj) {
73+
Reader_obj *mmdb_obj = (Reader_obj *)self;
74+
if (!mmdb_obj) {
7575
PyErr_NoMemory();
76-
return NULL;
76+
return -1;
7777
}
7878

7979
uint16_t status = MMDB_open(filename, MMDB_MODE_MMAP, mmdb);
8080

8181
if (MMDB_SUCCESS != status) {
8282
free(mmdb);
83-
PyObject_Del(obj);
84-
return PyErr_Format(
83+
PyErr_Format(
8584
MaxMindDB_error,
8685
"Error opening database file (%s). Is this a valid MaxMind DB file?",
8786
filename
8887
);
88+
return -1;
8989
}
9090

91-
obj->mmdb = mmdb;
92-
return (PyObject *)obj;
91+
mmdb_obj->mmdb = mmdb;
92+
return 0;
9393
}
9494

9595
static PyObject *Reader_get(PyObject *self, PyObject *args)
@@ -418,6 +418,7 @@ static PyTypeObject Reader_Type = {
418418
.tp_flags = Py_TPFLAGS_DEFAULT,
419419
.tp_methods = Reader_methods,
420420
.tp_name = "Reader",
421+
.tp_init = Reader_constructor,
421422
};
422423

423424
static PyMethodDef Metadata_methods[] = {
@@ -460,11 +461,10 @@ static PyTypeObject Metadata_Type = {
460461
};
461462

462463
static PyMethodDef MaxMindDB_methods[] = {
463-
{ "Reader", Reader_constructor, METH_VARARGS,
464-
"Creates a new maxminddb.extension.Reader object" },
465464
{ NULL, NULL, 0, NULL}
466465
};
467466

467+
468468
#if PY_MAJOR_VERSION >= 3
469469
static struct PyModuleDef MaxMindDB_module = {
470470
PyModuleDef_HEAD_INIT,
@@ -506,6 +506,13 @@ MOD_INIT(extension){
506506
RETURN_MOD_INIT(NULL);
507507
}
508508

509+
Reader_Type.tp_new = PyType_GenericNew;
510+
if (PyType_Ready(&Reader_Type)) {
511+
RETURN_MOD_INIT(NULL);
512+
}
513+
Py_INCREF(&Reader_Type);
514+
PyModule_AddObject(m, "Reader", (PyObject *)&Reader_Type);
515+
509516
Py_INCREF(MaxMindDB_error);
510517
PyModule_AddObject(m, "InvalidDatabaseError", MaxMindDB_error);
511518

0 commit comments

Comments
 (0)