Skip to content

Commit 987c956

Browse files
committed
Drop Python 2 support from extension
1 parent 045a622 commit 987c956

File tree

1 file changed

+11
-34
lines changed

1 file changed

+11
-34
lines changed

extension/maxminddb.c

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,6 @@ static PyObject *from_array(MMDB_entry_data_list_s **entry_data_list);
4141
static PyObject *from_uint128(const MMDB_entry_data_list_s *entry_data_list);
4242
static int ip_converter(PyObject *obj, struct sockaddr_storage *ip_address);
4343

44-
#if PY_MAJOR_VERSION >= 3
45-
#define MOD_INIT(name) PyMODINIT_FUNC PyInit_##name(void)
46-
#define RETURN_MOD_INIT(m) return (m)
47-
#define FILE_NOT_FOUND_ERROR PyExc_FileNotFoundError
48-
#else
49-
#define MOD_INIT(name) PyMODINIT_FUNC init##name(void)
50-
#define RETURN_MOD_INIT(m) return
51-
#define PyInt_FromLong PyLong_FromLong
52-
#define FILE_NOT_FOUND_ERROR PyExc_IOError
53-
#endif
54-
5544
#ifdef __GNUC__
5645
#define UNUSED(x) UNUSED_##x __attribute__((__unused__))
5746
#else
@@ -78,8 +67,9 @@ static int Reader_init(PyObject *self, PyObject *args, PyObject *kwds) {
7867
}
7968

8069
if (0 != access(filename, R_OK)) {
81-
PyErr_Format(
82-
FILE_NOT_FOUND_ERROR, "No such file or directory: '%s'", filename);
70+
PyErr_Format(PyExc_FileNotFoundError,
71+
"No such file or directory: '%s'",
72+
filename);
8373
return -1;
8474
}
8575

@@ -213,17 +203,10 @@ static int get_record(PyObject *self, PyObject *args, PyObject **record) {
213203
}
214204

215205
static int ip_converter(PyObject *obj, struct sockaddr_storage *ip_address) {
216-
#if PY_MAJOR_VERSION >= 3
217206
if (PyUnicode_Check(obj)) {
218207
Py_ssize_t len;
219208
const char *ipstr = PyUnicode_AsUTF8AndSize(obj, &len);
220-
#else
221-
if (PyUnicode_Check(obj) || PyString_Check(obj)) {
222-
// Although this should work on Python 3, we will hopefully delete
223-
// this soon and the Python 3 version is cleaner.
224-
const char *ipstr = PyString_AsString(obj);
225-
Py_ssize_t len = PyString_Size(obj);
226-
#endif
209+
227210
if (!ipstr) {
228211
PyErr_SetString(PyExc_TypeError,
229212
"argument 1 contains an invalid string");
@@ -713,57 +696,51 @@ static PyTypeObject Metadata_Type = {
713696

714697
static PyMethodDef MaxMindDB_methods[] = {{NULL, NULL, 0, NULL}};
715698

716-
#if PY_MAJOR_VERSION >= 3
717699
static struct PyModuleDef MaxMindDB_module = {
718700
PyModuleDef_HEAD_INIT,
719701
.m_name = "extension",
720702
.m_doc = "This is a C extension to read MaxMind DB file format",
721703
.m_methods = MaxMindDB_methods,
722704
};
723-
#endif
724705

725-
MOD_INIT(extension) {
706+
PyMODINIT_FUNC PyInit_extension(void) {
726707
PyObject *m;
727708

728-
#if PY_MAJOR_VERSION >= 3
729709
m = PyModule_Create(&MaxMindDB_module);
730-
#else
731-
m = Py_InitModule("extension", MaxMindDB_methods);
732-
#endif
733710

734711
if (!m) {
735-
RETURN_MOD_INIT(NULL);
712+
return NULL;
736713
}
737714

738715
Reader_Type.tp_new = PyType_GenericNew;
739716
if (PyType_Ready(&Reader_Type)) {
740-
RETURN_MOD_INIT(NULL);
717+
return NULL;
741718
}
742719
Py_INCREF(&Reader_Type);
743720
PyModule_AddObject(m, "Reader", (PyObject *)&Reader_Type);
744721

745722
Metadata_Type.tp_new = PyType_GenericNew;
746723
if (PyType_Ready(&Metadata_Type)) {
747-
RETURN_MOD_INIT(NULL);
724+
return NULL;
748725
}
749726
PyModule_AddObject(m, "extension", (PyObject *)&Metadata_Type);
750727

751728
PyObject *error_mod = PyImport_ImportModule("maxminddb.errors");
752729
if (error_mod == NULL) {
753-
RETURN_MOD_INIT(NULL);
730+
return NULL;
754731
}
755732

756733
MaxMindDB_error = PyObject_GetAttrString(error_mod, "InvalidDatabaseError");
757734
Py_DECREF(error_mod);
758735

759736
if (MaxMindDB_error == NULL) {
760-
RETURN_MOD_INIT(NULL);
737+
return NULL;
761738
}
762739

763740
Py_INCREF(MaxMindDB_error);
764741

765742
/* We primarily add it to the module for backwards compatibility */
766743
PyModule_AddObject(m, "InvalidDatabaseError", MaxMindDB_error);
767744

768-
RETURN_MOD_INIT(m);
745+
return m;
769746
}

0 commit comments

Comments
 (0)