Skip to content

Commit 70928f4

Browse files
committed
Clean up metadata object creation
1 parent ed7ec3b commit 70928f4

File tree

1 file changed

+93
-69
lines changed

1 file changed

+93
-69
lines changed

maxminddb/extension/maxminddb.c

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

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

@@ -81,10 +81,10 @@ static int Reader_constructor(PyObject *self, PyObject *args, PyObject *UNUSED(k
8181
if (MMDB_SUCCESS != status) {
8282
free(mmdb);
8383
PyErr_Format(
84-
MaxMindDB_error,
85-
"Error opening database file (%s). Is this a valid MaxMind DB file?",
86-
filename
87-
);
84+
MaxMindDB_error,
85+
"Error opening database file (%s). Is this a valid MaxMind DB file?",
86+
filename
87+
);
8888
return -1;
8989
}
9090

@@ -170,11 +170,6 @@ static PyObject *Reader_metadata(PyObject *self, PyObject *UNUSED(args))
170170
return NULL;
171171
}
172172

173-
Metadata_obj *obj = PyObject_New(Metadata_obj, &Metadata_Type);
174-
if (!obj) {
175-
return NULL;
176-
}
177-
178173
MMDB_entry_data_list_s *entry_data_list;
179174
MMDB_get_metadata_as_entry_data_list(mmdb_obj->mmdb, &entry_data_list);
180175
MMDB_entry_data_list_s *original_entry_data_list = entry_data_list;
@@ -184,50 +179,20 @@ static PyObject *Reader_metadata(PyObject *self, PyObject *UNUSED(args))
184179
if (NULL == metadata_dict || !PyDict_Check(metadata_dict)) {
185180
PyErr_SetString(MaxMindDB_error,
186181
"Error decoding metadata.");
187-
PyObject_Del(obj);
188182
return NULL;
189183
}
190184

191-
obj->binary_format_major_version = PyDict_GetItemString(
192-
metadata_dict, "binary_format_major_version");
193-
obj->binary_format_minor_version = PyDict_GetItemString(
194-
metadata_dict, "binary_format_minor_version");
195-
obj->build_epoch = PyDict_GetItemString(metadata_dict, "build_epoch");
196-
obj->database_type = PyDict_GetItemString(metadata_dict, "database_type");
197-
obj->description = PyDict_GetItemString(metadata_dict, "description");
198-
obj->ip_version = PyDict_GetItemString(metadata_dict, "ip_version");
199-
obj->languages = PyDict_GetItemString(metadata_dict, "languages");
200-
obj->node_count = PyDict_GetItemString(metadata_dict, "node_count");
201-
obj->record_size = PyDict_GetItemString(metadata_dict, "record_size");
202-
203-
if (NULL == obj->binary_format_major_version ||
204-
NULL == obj->binary_format_minor_version ||
205-
NULL == obj->build_epoch ||
206-
NULL == obj->database_type ||
207-
NULL == obj->description ||
208-
NULL == obj->ip_version ||
209-
NULL == obj->languages ||
210-
NULL == obj->node_count ||
211-
NULL == obj->record_size) {
212-
PyErr_SetString(MaxMindDB_error,
213-
"Error decoding metadata.");
214-
PyObject_Del(obj);
185+
PyObject *args = PyTuple_New(0);
186+
if (NULL == args) {
187+
Py_DECREF(metadata_dict);
215188
return NULL;
216189
}
217190

218-
Py_INCREF(obj->binary_format_major_version);
219-
Py_INCREF(obj->binary_format_minor_version);
220-
Py_INCREF(obj->build_epoch);
221-
Py_INCREF(obj->database_type);
222-
Py_INCREF(obj->description);
223-
Py_INCREF(obj->ip_version);
224-
Py_INCREF(obj->languages);
225-
Py_INCREF(obj->node_count);
226-
Py_INCREF(obj->record_size);
191+
PyObject *metadata = PyObject_Call((PyObject *)&Metadata_Type, args,
192+
metadata_dict);
227193

228194
Py_DECREF(metadata_dict);
229-
230-
return (PyObject *)obj;
195+
return metadata;
231196
}
232197

233198
static PyObject *Reader_close(PyObject *self, PyObject *UNUSED(args))
@@ -253,6 +218,71 @@ static void Reader_dealloc(PyObject *self)
253218
PyObject_Del(self);
254219
}
255220

221+
static int Metadata_init(PyObject *self, PyObject *args, PyObject *kwds)
222+
{
223+
224+
PyObject
225+
*binary_format_major_version,
226+
*binary_format_minor_version,
227+
*build_epoch,
228+
*database_type,
229+
*description,
230+
*ip_version,
231+
*languages,
232+
*node_count,
233+
*record_size;
234+
235+
static char *kwlist[] = {
236+
"binary_format_major_version",
237+
"binary_format_minor_version",
238+
"build_epoch",
239+
"database_type",
240+
"description",
241+
"ip_version",
242+
"languages",
243+
"node_count",
244+
"record_size",
245+
NULL
246+
};
247+
248+
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOOOOOOOO", kwlist,
249+
&binary_format_major_version,
250+
&binary_format_minor_version,
251+
&build_epoch,
252+
&database_type,
253+
&description,
254+
&ip_version,
255+
&languages,
256+
&node_count,
257+
&record_size)) {
258+
return -1;
259+
}
260+
261+
Metadata_obj *obj = (Metadata_obj *)self;
262+
263+
obj->binary_format_major_version = binary_format_major_version;
264+
obj->binary_format_minor_version = binary_format_minor_version;
265+
obj->build_epoch = build_epoch;
266+
obj->database_type = database_type;
267+
obj->description = description;
268+
obj->ip_version = ip_version;
269+
obj->languages = languages;
270+
obj->node_count = node_count;
271+
obj->record_size = record_size;
272+
273+
Py_INCREF(obj->binary_format_major_version);
274+
Py_INCREF(obj->binary_format_minor_version);
275+
Py_INCREF(obj->build_epoch);
276+
Py_INCREF(obj->database_type);
277+
Py_INCREF(obj->description);
278+
Py_INCREF(obj->ip_version);
279+
Py_INCREF(obj->languages);
280+
Py_INCREF(obj->node_count);
281+
Py_INCREF(obj->record_size);
282+
283+
return 0;
284+
}
285+
256286
static void Metadata_dealloc(PyObject *self)
257287
{
258288
Metadata_obj *obj = (Metadata_obj *)self;
@@ -406,8 +436,8 @@ static PyMethodDef Reader_methods[] = {
406436
"Get record for IP address" },
407437
{ "metadata", Reader_metadata, METH_NOARGS,
408438
"Returns metadata object for database" },
409-
{ "close", Reader_close, METH_NOARGS, "Closes database"},
410-
{ NULL, NULL, 0, NULL }
439+
{ "close", Reader_close, METH_NOARGS, "Closes database" },
440+
{ NULL, NULL, 0, NULL }
411441
};
412442

413443
static PyTypeObject Reader_Type = {
@@ -418,7 +448,7 @@ static PyTypeObject Reader_Type = {
418448
.tp_flags = Py_TPFLAGS_DEFAULT,
419449
.tp_methods = Reader_methods,
420450
.tp_name = "Reader",
421-
.tp_init = Reader_constructor,
451+
.tp_init = Reader_init,
422452
};
423453

424454
static PyMethodDef Metadata_methods[] = {
@@ -458,10 +488,11 @@ static PyTypeObject Metadata_Type = {
458488
.tp_members = Metadata_members,
459489
.tp_methods = Metadata_methods,
460490
.tp_name = "Metadata",
491+
.tp_init = Metadata_init
461492
};
462493

463494
static PyMethodDef MaxMindDB_methods[] = {
464-
{ NULL, NULL, 0, NULL}
495+
{ NULL, NULL, 0, NULL }
465496
};
466497

467498

@@ -474,16 +505,6 @@ static struct PyModuleDef MaxMindDB_module = {
474505
};
475506
#endif
476507

477-
static void init_type(PyObject *m, PyTypeObject *type)
478-
{
479-
Metadata_Type.tp_new = PyType_GenericNew;
480-
481-
if (PyType_Ready(type) == 0) {
482-
Py_INCREF(type);
483-
PyModule_AddObject(m, "extension", (PyObject *)type);
484-
}
485-
}
486-
487508
MOD_INIT(extension){
488509
PyObject *m;
489510

@@ -497,22 +518,25 @@ MOD_INIT(extension){
497518
RETURN_MOD_INIT(NULL);
498519
}
499520

500-
init_type(m, &Reader_Type);
501-
init_type(m, &Metadata_Type);
502-
503-
MaxMindDB_error = PyErr_NewException("extension.InvalidDatabaseError", NULL,
504-
NULL);
505-
if (MaxMindDB_error == NULL) {
506-
RETURN_MOD_INIT(NULL);
507-
}
508-
509521
Reader_Type.tp_new = PyType_GenericNew;
510522
if (PyType_Ready(&Reader_Type)) {
511523
RETURN_MOD_INIT(NULL);
512524
}
513525
Py_INCREF(&Reader_Type);
514526
PyModule_AddObject(m, "Reader", (PyObject *)&Reader_Type);
515527

528+
Metadata_Type.tp_new = PyType_GenericNew;
529+
if (PyType_Ready(&Metadata_Type)) {
530+
RETURN_MOD_INIT(NULL);
531+
}
532+
PyModule_AddObject(m, "extension", (PyObject *)&Metadata_Type);
533+
534+
MaxMindDB_error = PyErr_NewException("extension.InvalidDatabaseError", NULL,
535+
NULL);
536+
if (MaxMindDB_error == NULL) {
537+
RETURN_MOD_INIT(NULL);
538+
}
539+
516540
Py_INCREF(MaxMindDB_error);
517541
PyModule_AddObject(m, "InvalidDatabaseError", MaxMindDB_error);
518542

0 commit comments

Comments
 (0)