Revision 4b5dfe6ed6e3067d14f2e762d768a82229681b18 authored by EXTERNAL-Ewall-Wice on 02 November 2018, 05:08:50 UTC, committed by EXTERNAL-Ewall-Wice on 02 November 2018, 05:08:50 UTC
1 parent f94fb9e
Raw File
aipy_compat.h
/* Helpers for compatibility in the C modules between Python 2 and Python 3. */

#ifndef __AIPY_COMPAT_H
#define __AIPY_COMPAT_H

#if PY_MAJOR_VERSION >= 3
# define PyCapsule_Type PyCObject_Type
# define PyInt_AsLong PyLong_AsLong
# define PyInt_FromLong PyLong_FromLong
# define PyInt_Check PyLong_Check
# define PyString_FromString PyUnicode_FromString
# define PyString_Check PyUnicode_Check
# define PyString_Size PyUnicode_GET_LENGTH
# define PyString_FromStringAndSize PyUnicode_FromStringAndSize

static char *
PyString_AsString(PyObject *ob)
{
    PyObject *enc;
    char *cstr;

    enc = PyUnicode_AsEncodedString(ob, "utf-8", "Error");
    if (enc == NULL) {
        PyErr_Format(PyExc_ValueError, "Cannot encode string");
        return NULL;
    }

    cstr = PyBytes_AsString(enc);
    Py_XDECREF(enc);
    return cstr;
}
#endif

#ifndef PyMODINIT_FUNC
#define PyMODINIT_FUNC void
#endif

#if PY_MAJOR_VERSION >= 3
# define MOD_ERROR_VAL NULL
# define MOD_SUCCESS_VAL(val) val
# define MOD_INIT(name) PyMODINIT_FUNC PyInit_##name(void)
# define MOD_DEF(ob, name, methods, doc) \
    static struct PyModuleDef moduledef = { PyModuleDef_HEAD_INIT, name, doc, -1, methods, }; \
    ob = PyModule_Create(&moduledef);
#else
# define MOD_ERROR_VAL
# define MOD_SUCCESS_VAL(val)
# define MOD_INIT(name) PyMODINIT_FUNC init##name(void)
# define MOD_DEF(ob, name, methods, doc) \
    ob = Py_InitModule3(name, methods, doc);
#endif

#endif
back to top