sss_python.h revision c6278b2fa4a7ea389ed4086b2def16e0e6cbb184
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek#ifndef __SSS_PYTHON_H__
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek#define __SSS_PYTHON_H__
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek#include <Python.h>
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek#include <stdbool.h>
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek#include "util/util.h"
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek#if PY_VERSION_HEX < 0x02050000
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek#define sss_py_const_p(type, value) discard_const_p(type, (value))
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek#else
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek#define sss_py_const_p(type, value) (value)
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek#endif
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda#if PY_MAJOR_VERSION >= 3
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda#define IS_PY3K
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda#define MODINITERROR return NULL
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda#define PYNUMBER_CHECK(what) PyLong_Check(what)
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda#define PYNUMBER_FROMLONG(what) PyLong_FromLong(what)
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda#define PYNUMBER_ASLONG(what) PyLong_AsLong(what)
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda#else
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda#include <bytesobject.h>
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda#define MODINITERROR return
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda#define PYNUMBER_CHECK(what) PyInt_Check(what)
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda#define PYNUMBER_FROMLONG(what) PyInt_FromLong(what)
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda#define PYNUMBER_ASLONG(what) PyInt_AsLong(what)
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda#endif
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek/* Exceptions compatibility */
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub HrozekPyObject *
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozeksss_exception_with_doc(char *name, char *doc, PyObject *base, PyObject *dict);
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek/* Convenience macros */
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek#define TYPE_READY(module, type, name) do { \
c6278b2fa4a7ea389ed4086b2def16e0e6cbb184Lukas Slebodnik if (PyType_Ready(&type) < 0) { \
c6278b2fa4a7ea389ed4086b2def16e0e6cbb184Lukas Slebodnik MODINITERROR; \
c6278b2fa4a7ea389ed4086b2def16e0e6cbb184Lukas Slebodnik } \
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek Py_INCREF(&type); \
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek PyModule_AddObject(module, \
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek discard_const_p(char, name), \
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek (PyObject *) &type); \
4a6a5421113ab662a665c62ed6a24b61a5a36950Jakub Hrozek} while(0) \
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek#define SAFE_SET(old, new) do { \
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek PyObject *__simple_set_tmp = NULL; \
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek __simple_set_tmp = old; \
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek Py_INCREF(new); \
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek old = new; \
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek Py_XDECREF(__simple_set_tmp); \
4a6a5421113ab662a665c62ed6a24b61a5a36950Jakub Hrozek} while(0)
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek#endif /* __SSS_PYTHON_H__ */