Skip to content

Commit

Permalink
Remove Python 2 support from C code
Browse files Browse the repository at this point in the history
  • Loading branch information
taleinat committed Jun 25, 2024
1 parent fcf0c33 commit aadb376
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 91 deletions.
4 changes: 0 additions & 4 deletions src/fuzzysearch/_c_ext_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
#define PY_SSIZE_T_CLEAN
#include <Python.h>

#if PY_MAJOR_VERSION >= 3
#define IS_PY3K
#endif

#ifndef unlikely
#ifdef __GNUC__
/* Test for GCC > 2.95 */
Expand Down
30 changes: 2 additions & 28 deletions src/fuzzysearch/_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,7 @@ search_exact_byteslike(PyObject *self, PyObject *args, PyObject *kwdict) {
int subseq_sum;
char *next_match_ptr;

const char* argspec =
#ifdef IS_PY3K
"y*y*|ll:search_exact_byteslike";
#else
"s*s*|ll:search_exact_byteslike";
#endif
const char* argspec = "y*y*|ll:search_exact_byteslike";

if (unlikely(!PyArg_ParseTupleAndKeywords(
args, kwdict,
Expand Down Expand Up @@ -88,11 +83,7 @@ search_exact_byteslike(PyObject *self, PyObject *args, PyObject *kwdict) {

while (next_match_ptr != NULL) {
next_match_index = (const char *)next_match_ptr - seq;
#ifdef IS_PY3K
next_result = PyLong_FromLong(next_match_index + start_index);
#else
next_result = PyInt_FromLong(next_match_index + start_index);
#endif
if (unlikely(next_result == NULL)) {
Py_DECREF(results);
goto error;
Expand Down Expand Up @@ -133,12 +124,7 @@ count_differences_with_maximum_byteslike(PyObject *self, PyObject *args)
Py_ssize_t i;
int n_differences;

const char* argspec =
#ifdef IS_PY3K
"y*y*i";
#else
"s*s*i";
#endif
const char* argspec = "y*y*i";

if (!PyArg_ParseTuple(
args,
Expand Down Expand Up @@ -197,8 +183,6 @@ static PyMethodDef _common_methods[] = {
};


#ifdef IS_PY3K

static struct PyModuleDef _common_module = {
PyModuleDef_HEAD_INIT,
"_common", /* name of module */
Expand All @@ -213,13 +197,3 @@ PyInit__common(void)
{
return PyModule_Create(&_common_module);
}

#else

PyMODINIT_FUNC
init_common(void)
{
(void) Py_InitModule("_common", _common_methods);
}

#endif
35 changes: 4 additions & 31 deletions src/fuzzysearch/_pymemmem.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@
#include "src/fuzzysearch/memmem.h"
#include "src/fuzzysearch/wordlen_memmem.h"

#if PY_MAJOR_VERSION >= 3
#define IS_PY3K
#define PyInt_FromLong PyLong_FromLong
#endif


#ifdef __GNUC__
/* Test for GCC > 2.95 */
Expand All @@ -23,16 +18,6 @@
#endif /* __GNUC__ */


#ifdef IS_PY3K
#define ARG_TYPES_DEF "y#y#"
#else
#if PY_HEX_VERSION >= 0x02070000
#define ARG_TYPES_DEF "t#t#"
#else
#define ARG_TYPES_DEF "s#s#"
#endif
#endif

static PyObject *
py_simple_memmem(PyObject *self, PyObject *args) {
/* input params */
Expand All @@ -44,7 +29,7 @@ py_simple_memmem(PyObject *self, PyObject *args) {

if (unlikely(!PyArg_ParseTuple(
args,
ARG_TYPES_DEF,
"y#y#",
&needle, &needle_len,
&haystack, &haystack_len
))) {
Expand All @@ -57,7 +42,7 @@ py_simple_memmem(PyObject *self, PyObject *args) {
Py_RETURN_NONE;
}
else {
py_result = PyInt_FromLong(result - haystack);
py_result = PyLong_FromLong(result - haystack);
if (unlikely(py_result == NULL)) {
return NULL;
}
Expand All @@ -76,7 +61,7 @@ py_wordlen_memmem(PyObject *self, PyObject *args) {

if (unlikely(!PyArg_ParseTuple(
args,
ARG_TYPES_DEF,
"y#y#",
&needle, &needle_len,
&haystack, &haystack_len
))) {
Expand All @@ -89,7 +74,7 @@ py_wordlen_memmem(PyObject *self, PyObject *args) {
Py_RETURN_NONE;
}
else {
py_result = PyInt_FromLong(result - haystack);
py_result = PyLong_FromLong(result - haystack);
if (unlikely(py_result == NULL)) {
return NULL;
}
Expand All @@ -109,8 +94,6 @@ static PyMethodDef _pymemmem_methods[] = {
};


#ifdef IS_PY3K

static struct PyModuleDef _pymemmem_module = {
PyModuleDef_HEAD_INIT,
"_pymemmem", /* name of module */
Expand All @@ -125,13 +108,3 @@ PyInit__pymemmem(void)
{
return PyModule_Create(&_pymemmem_module);
}

#else

PyMODINIT_FUNC
init_pymemmem(void)
{
(void) Py_InitModule("_pymemmem", _pymemmem_methods);
}

#endif
17 changes: 1 addition & 16 deletions src/fuzzysearch/_substitutions_only.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
#undef DECLARE_VARS


#ifdef IS_PY3K
#define PyInt_FromSsize_t(x) PyLong_FromSsize_t(x)
#endif
#define DECLARE_VARS \
PyObject *results; \
PyObject *next_result
Expand All @@ -28,7 +25,7 @@
if (unlikely(!results)) \
goto error;
#define OUTPUT_VALUE(x) do { \
next_result = PyInt_FromSsize_t((x)); \
next_result = PyLong_FromSsize_t((x)); \
if (unlikely(next_result == NULL)) { \
Py_DECREF(results); \
goto error; \
Expand Down Expand Up @@ -74,8 +71,6 @@ static PyMethodDef substitutions_only_methods[] = {
};


#ifdef IS_PY3K

static struct PyModuleDef substitutions_only_module = {
PyModuleDef_HEAD_INIT,
"_substitutions_only", /* name of module */
Expand All @@ -90,13 +85,3 @@ PyInit__substitutions_only(void)
{
return PyModule_Create(&substitutions_only_module);
}

#else

PyMODINIT_FUNC
init_substitutions_only(void)
{
(void) Py_InitModule("_substitutions_only", substitutions_only_methods);
}

#endif
7 changes: 1 addition & 6 deletions src/fuzzysearch/_substitutions_only_lp_template.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,7 @@ FUNCTION_NAME(PyObject *self, PyObject *args)

DECLARE_VARS;

const char* argspec =
#ifdef IS_PY3K
"y*y*i";
#else
"s*s*i";
#endif
const char* argspec = "y*y*i";

if (unlikely(!PyArg_ParseTuple(
args,
Expand Down
7 changes: 1 addition & 6 deletions src/fuzzysearch/_substitutions_only_ngrams_template.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,7 @@ FUNCTION_NAME(PyObject *self, PyObject *args)

DECLARE_VARS;

const char* argspec =
#ifdef IS_PY3K
"y*y*i";
#else
"s*s*i";
#endif
const char* argspec = "y*y*i";

if (unlikely(!PyArg_ParseTuple(
args,
Expand Down

0 comments on commit aadb376

Please sign in to comment.