From a0d6b1a8b8bcb899781e690034f568b8a3f38a23 Mon Sep 17 00:00:00 2001 From: Tres Seaver Date: Mon, 27 May 2024 15:25:03 -0400 Subject: [PATCH 1/2] chore: remove Py2 straddle --- hypatia/text/okascore.c | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/hypatia/text/okascore.c b/hypatia/text/okascore.c index 8999047..352d060 100644 --- a/hypatia/text/okascore.c +++ b/hypatia/text/okascore.c @@ -35,10 +35,6 @@ #include "Python.h" -#if PY_MAJOR_VERSION >= 3 -#define PY3K -#endif - #define K1 1.2 #define B 0.75 @@ -136,7 +132,6 @@ static PyMethodDef okascore_functions[] = { {NULL} }; -#ifdef PY3K static struct PyModuleDef moduledef = { PyModuleDef_HEAD_INIT, "okascore", /* m_name */ @@ -156,12 +151,3 @@ PyInit_okascore(void) m = PyModule_Create(&moduledef); return m; } -#else -PyMODINIT_FUNC -initokascore(void) -{ - /* XXX: Error checking */ - Py_InitModule3("okascore", okascore_functions, - "inner scoring loop for Okapi rank"); -} -#endif From 47c1c67a18338ab513c274627e3020ac8fc34261 Mon Sep 17 00:00:00 2001 From: Tres Seaver Date: Mon, 27 May 2024 15:29:19 -0400 Subject: [PATCH 2/2] feat: support PEP 489 multi-phase module init --- hypatia/text/okascore.c | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/hypatia/text/okascore.c b/hypatia/text/okascore.c index 352d060..5f758e5 100644 --- a/hypatia/text/okascore.c +++ b/hypatia/text/okascore.c @@ -127,27 +127,33 @@ static char score__doc__[] = "\n" "Do the inner scoring loop for an Okapi index.\n"; -static PyMethodDef okascore_functions[] = { +static PyMethodDef module_functions[] = { {"score", score, METH_VARARGS, score__doc__}, {NULL} }; -static struct PyModuleDef moduledef = { - PyModuleDef_HEAD_INIT, - "okascore", /* m_name */ - "inner scoring loop for Okapi rank", /* m_doc */ - -1, /* m_size */ - okascore_functions, /* m_methods */ - NULL, /* m_reload */ - NULL, /* m_traverse */ - NULL, /* m_clear */ - NULL, /* m_free */ - }; +static char module__name__[] = "okascore"; +static char module__doc__[] = "inner scoring loop for Okapi rank"; + +/* + * No slot definitions needed multi-phase initialization: + * + * we have no state, and initialize / register no types. + */ +static PyModuleDef_Slot module_slots[] = { + {0, NULL} +}; + +static struct PyModuleDef module_def = { + PyModuleDef_HEAD_INIT, + .m_name = module__name__, + .m_doc = module__doc__, + .m_methods = module_functions, + .m_slots = module_slots, +}; PyMODINIT_FUNC PyInit_okascore(void) { - PyObject *m; - m = PyModule_Create(&moduledef); - return m; + return PyModuleDef_Init(&module_def); }