From d006e4e25783430ad3277ce41059ac42ef27f7bf Mon Sep 17 00:00:00 2001 From: David Novakovic Date: Mon, 28 May 2018 15:07:05 +1000 Subject: [PATCH] Bump the minor version and add python 3 support. --- pyspamsum.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++---- setup.py | 2 +- 2 files changed, 65 insertions(+), 5 deletions(-) diff --git a/pyspamsum.c b/pyspamsum.c index 0e62967..bcc4ad7 100644 --- a/pyspamsum.c +++ b/pyspamsum.c @@ -6,6 +6,7 @@ * * Copyright 2009 Russell Keith-Magee */ + #include #include #include @@ -15,6 +16,17 @@ int edit_distn(char *from, int from_len, char *to, int to_len); char *spamsum(const unsigned char *in, unsigned int length, unsigned int flags, unsigned int bsize); unsigned int spamsum_match(const char *str1, const char *str2); +struct module_state { + PyObject *error; +}; + +#if PY_MAJOR_VERSION >= 3 +#define GETSTATE(m) ((struct module_state*)PyModule_GetState(m)) +#else +#define GETSTATE(m) (&_state) +static struct module_state _state; +#endif + PyObject *py_edit_distance(PyObject *self, PyObject *args) { PyObject *result = NULL; @@ -90,8 +102,56 @@ static PyMethodDef methods[] = { {NULL, NULL, 0, NULL} }; -PyMODINIT_FUNC -initspamsum() +#if PY_MAJOR_VERSION >= 3 +static int spamsum_traverse(PyObject *m, visitproc visit, void *arg) { + Py_VISIT(GETSTATE(m)->error); + return 0; +} + +static int spamsum_clear(PyObject *m) { + Py_CLEAR(GETSTATE(m)->error); + return 0; +} +static struct PyModuleDef ss = { - (void) Py_InitModule("spamsum", methods); -} \ No newline at end of file + PyModuleDef_HEAD_INIT, + "spamsum", + "", + sizeof(struct module_state), + methods, + NULL, + spamsum_traverse, + spamsum_clear, + NULL +}; +#define INITERROR return NULL + +PyMODINIT_FUNC +PyInit_spamsum(void) +#else +#define INITERROR return +void +initspamsum(void) +#endif +{ +#if PY_MAJOR_VERSION >= 3 + PyObject *module = PyModule_Create(&ss); +#else + PyObject *module = Py_InitModule("spamsum", methods); +#endif + + if (module == NULL) + INITERROR; + struct module_state *st = GETSTATE(module); + + st->error = PyErr_NewException("spamsum.Error", NULL, NULL); + if (st->error == NULL) { + Py_DECREF(module); + INITERROR; + } + +#if PY_MAJOR_VERSION >= 3 + return module; +#endif +} + diff --git a/setup.py b/setup.py index ce8f434..ff23316 100644 --- a/setup.py +++ b/setup.py @@ -1,7 +1,7 @@ from distutils.core import setup, Extension setup(name = "pyspamsum", - version = "1.0.3", + version = "1.0.4", author = "Russell Keith-Magee", author_email = "russell@keith-magee.com", url = 'http://github.com/freakboy3742/pyspamsum/',