Fix Python 3.8+ DeprecationWarning: PY_SSIZE_T_CLEAN will be required for ‘#’ formats

Using python-snappy with Python 3.8 or 3.9 produces this deprecation warning:

DeprecationWarning: PY_SSIZE_T_CLEAN will be required for '#' formats

To fix this issue define PY_SSIZE_T_CLEAN before including python.h:

#define PY_SSIZE_T_CLEAN
#include "Python.h"

You will also need to change the type passed to '#' formats from int to Py_ssize_t, eg:

 snappy__is_valid_compressed_buffer(PyObject *self, PyObject *args)
 {
     const char * compressed;
-    int comp_size;
+    Py_ssize_t comp_size;
     snappy_status status;

 #if PY_MAJOR_VERSION >=3
     if (!PyArg_ParseTuple(args, "y#", &compressed, &comp_size))
 #else
     if (!PyArg_ParseTuple(args, "s#", &compressed, &comp_size))
 #endif
...

References:

Related Posts