@@ -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
9595static 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
423424static PyMethodDef Metadata_methods [] = {
@@ -460,11 +461,10 @@ static PyTypeObject Metadata_Type = {
460461};
461462
462463static 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
469469static 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