@@ -48,16 +48,27 @@ static int ip_converter(PyObject *obj, struct sockaddr_storage *ip_address);
4848#endif
4949
5050static int Reader_init (PyObject * self , PyObject * args , PyObject * kwds ) {
51- char * filename ;
51+ PyObject * filepath = NULL ;
5252 int mode = 0 ;
5353
5454 static char * kwlist [] = {"database" , "mode" , NULL };
55- if (!PyArg_ParseTupleAndKeywords (
56- args , kwds , "s|i" , kwlist , & filename , & mode )) {
55+ if (!PyArg_ParseTupleAndKeywords (args ,
56+ kwds ,
57+ "O&|i" ,
58+ kwlist ,
59+ PyUnicode_FSConverter ,
60+ & filepath ,
61+ & mode )) {
62+ return -1 ;
63+ }
64+
65+ char * filename = PyBytes_AS_STRING (filepath );
66+ if (filename == NULL ) {
5767 return -1 ;
5868 }
5969
6070 if (mode != 0 && mode != 1 ) {
71+ Py_XDECREF (filepath );
6172 PyErr_Format (
6273 PyExc_ValueError ,
6374 "Unsupported open mode (%i). Only "
@@ -67,6 +78,7 @@ static int Reader_init(PyObject *self, PyObject *args, PyObject *kwds) {
6778 }
6879
6980 if (0 != access (filename , R_OK )) {
81+ Py_XDECREF (filepath );
7082 PyErr_Format (PyExc_FileNotFoundError ,
7183 "No such file or directory: '%s'" ,
7284 filename );
@@ -75,18 +87,21 @@ static int Reader_init(PyObject *self, PyObject *args, PyObject *kwds) {
7587
7688 MMDB_s * mmdb = (MMDB_s * )malloc (sizeof (MMDB_s ));
7789 if (NULL == mmdb ) {
90+ Py_XDECREF (filepath );
7891 PyErr_NoMemory ();
7992 return -1 ;
8093 }
8194
8295 Reader_obj * mmdb_obj = (Reader_obj * )self ;
8396 if (!mmdb_obj ) {
97+ Py_XDECREF (filepath );
8498 free (mmdb );
8599 PyErr_NoMemory ();
86100 return -1 ;
87101 }
88102
89103 uint16_t status = MMDB_open (filename , MMDB_MODE_MMAP , mmdb );
104+ Py_XDECREF (filepath );
90105
91106 if (MMDB_SUCCESS != status ) {
92107 free (mmdb );
0 commit comments