python_compatibility.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. * python_compatibility.h -- macros for Python 2/3 compatibility
  3. *
  4. * This file is Copyright 2016 by the GPSD project
  5. * SPDX-License-Identifier: BSD-2-clause
  6. *
  7. * Definitions based on examples in "Supporting Python 3 - The Book Site"
  8. * http://python3porting.com/cextensions.html
  9. */
  10. #ifndef _PYTHON_COMPATIBILITY_H_
  11. #define _PYTHON_COMPATIBILITY_H_
  12. #include <Python.h>
  13. #if PY_MAJOR_VERSION >= 3
  14. #define GPSD_PY_MODULE_INIT(name) PyMODINIT_FUNC PyInit_##name(void)
  15. #define GPSD_PY_MODULE_DEF(mod, name, doc, methods) \
  16. static struct PyModuleDef moduledef = { \
  17. PyModuleDef_HEAD_INIT, name, doc, -1, methods, }; \
  18. mod = PyModule_Create(&moduledef);
  19. #define GPSD_PY_MODULE_ERROR_VAL NULL
  20. #define GPSD_PY_MODULE_SUCCESS_VAL(val) val
  21. #define GPSD_PY_BYTE_FORMAT "y#"
  22. #else /* !Python 3 */
  23. #define GPSD_PY_MODULE_INIT(name) PyMODINIT_FUNC init##name(void)
  24. #define GPSD_PY_MODULE_DEF(mod, name, doc, methods) \
  25. mod = Py_InitModule3(name, methods, doc);
  26. #define GPSD_PY_MODULE_ERROR_VAL
  27. #define GPSD_PY_MODULE_SUCCESS_VAL(val)
  28. #define GPSD_PY_BYTE_FORMAT "s#"
  29. #endif /* !Python 3 */
  30. #endif /* _PYTHON_COMPATIBILITY_H_ */
  31. // vim: set expandtab shiftwidth=4