1/* 2 * Copyright 2006 - 2014 3 * Andr\xe9 Malo or his licensors, as applicable 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18/* 19 * central naming stuff 20 */ 21 22#ifndef SETUP_CEXT_H 23#define SETUP_CEXT_H 24 25#ifndef EXT_MODULE 26#error EXT_MODULE must be defined outside of this file (-DEXT_MODULE=...) 27#endif 28 29/* 30 * include core header files 31 */ 32#define PY_SSIZE_T_CLEAN 33 34#include "Python.h" 35#include "structmember.h" 36 37/* 38 * define our helper macros depending on the stuff above 39 */ 40#define STRINGIFY(n) STRINGIFY_HELPER(n) 41#define STRINGIFY_HELPER(n) #n 42#define CONCATENATE(first, second) CONCATENATE_HELPER(first, second) 43#define CONCATENATE_HELPER(first, second) first##second 44 45#define EXT_MODULE_NAME STRINGIFY(EXT_MODULE) 46#ifdef EXT_PACKAGE 47#define EXT_PACKAGE_NAME STRINGIFY(EXT_PACKAGE) 48#define EXT_MODULE_PATH EXT_PACKAGE_NAME "." EXT_MODULE_NAME 49#else 50#define EXT_PACKAGE_NAME "" 51#define EXT_MODULE_PATH EXT_MODULE_NAME 52#endif 53 54#define EXT_DOCS_VAR CONCATENATE(var, CONCATENATE(EXT_MODULE, __doc__)) 55#define EXT_METHODS_VAR CONCATENATE(var, CONCATENATE(EXT_MODULE, _methods)) 56#define EXT_METHODS static PyMethodDef EXT_METHODS_VAR[] 57 58#define EXT_DEFINE_VAR CONCATENATE(var, CONCATENATE(EXT_MODULE, _module)) 59 60/* Py3K Support */ 61#if PY_MAJOR_VERSION >= 3 62 63#define EXT3 64 65#ifndef PyMODINIT_FUNC 66#define EXT_INIT_FUNC PyObject *CONCATENATE(PyInit_, EXT_MODULE)(void) 67#else 68#define EXT_INIT_FUNC PyMODINIT_FUNC CONCATENATE(PyInit_, EXT_MODULE)(void) 69#endif 70 71#define EXT_DEFINE(name, methods, doc) \ 72static struct PyModuleDef EXT_DEFINE_VAR = { \ 73 PyModuleDef_HEAD_INIT, \ 74 name, \ 75 doc, \ 76 -1, \ 77 methods, \ 78 NULL, \ 79 NULL, \ 80 NULL, \ 81 NULL \ 82} 83 84#define EXT_CREATE(def) (PyModule_Create(def)) 85#define EXT_INIT_ERROR(module) do {Py_XDECREF(module); return NULL;} while(0) 86#define EXT_INIT_RETURN(module) return module 87 88#else /* end py3k */ 89 90#define EXT2 91 92#ifndef PyMODINIT_FUNC 93#define EXT_INIT_FUNC void CONCATENATE(init, EXT_MODULE)(void) 94#else 95#define EXT_INIT_FUNC PyMODINIT_FUNC CONCATENATE(init, EXT_MODULE)(void) 96#endif 97 98#define EXT_DEFINE__STRUCT \ 99 CONCATENATE(struct, CONCATENATE(EXT_MODULE, _module)) 100 101struct EXT_DEFINE__STRUCT { 102 char *m_name; 103 char *m_doc; 104 PyMethodDef *m_methods; 105}; 106#define EXT_DEFINE(name, methods, doc) \ 107static struct EXT_DEFINE__STRUCT EXT_DEFINE_VAR = { \ 108 name, \ 109 doc, \ 110 methods \ 111} 112 113#define EXT_CREATE(def) ((def)->m_doc \ 114 ? Py_InitModule3((def)->m_name, (def)->m_methods, (def)->m_doc) \ 115 : Py_InitModule((def)->m_name, (def)->m_methods) \ 116) 117#define EXT_INIT_ERROR(module) return 118#define EXT_INIT_RETURN(module) return 119 120#endif /* end py2K */ 121 122#define EXT_INIT_TYPE(module, type) do { \ 123 if (PyType_Ready(type) < 0) \ 124 EXT_INIT_ERROR(module); \ 125} while (0) 126 127#define EXT_ADD_TYPE(module, name, type) do { \ 128 Py_INCREF(type); \ 129 if (PyModule_AddObject(module, name, (PyObject *)(type)) < 0) \ 130 EXT_INIT_ERROR(module); \ 131} while (0) 132 133#define EXT_ADD_UNICODE(module, name, string, encoding) do { \ 134 if (PyModule_AddObject( \ 135 module, \ 136 name, \ 137 PyUnicode_Decode( \ 138 string, \ 139 sizeof(string) - 1, \ 140 encoding, \ 141 "strict" \ 142 )) < 0) \ 143 EXT_INIT_ERROR(module); \ 144} while (0) 145 146#define EXT_ADD_STRING(module, name, string) do { \ 147 if (PyModule_AddStringConstant(module, name, string) < 0) \ 148 EXT_INIT_ERROR(module); \ 149} while (0) 150 151#define EXT_ADD_INT(module, name, number) do { \ 152 if (PyModule_AddIntConstant(module, name, number) < 0) \ 153 EXT_INIT_ERROR(module); \ 154} while (0) 155 156 157/* PEP 353 support, implemented as of python 2.5 */ 158#if PY_VERSION_HEX < 0x02050000 159typedef int Py_ssize_t; 160#define PyInt_FromSsize_t(arg) PyInt_FromLong((long)arg) 161#define PyInt_AsSsize_t(arg) (int)PyInt_AsLong(arg) 162#define PY_SSIZE_T_MAX ((Py_ssize_t)INT_MAX) 163#endif 164 165/* 166 * some helper macros (Python 2.4) 167 */ 168#ifndef Py_VISIT 169#define Py_VISIT(op) do { \ 170 if (op) { \ 171 int vret = visit((op), arg); \ 172 if (vret) return vret; \ 173 } \ 174} while (0) 175#endif 176 177#ifdef Py_CLEAR 178#undef Py_CLEAR 179#endif 180#define Py_CLEAR(op) do { \ 181 if (op) { \ 182 PyObject *tmp__ = (PyObject *)(op); \ 183 (op) = NULL; \ 184 Py_DECREF(tmp__); \ 185 } \ 186} while (0) 187 188#ifndef Py_RETURN_NONE 189#define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None 190#endif 191 192#ifndef Py_RETURN_FALSE 193#define Py_RETURN_FALSE return Py_INCREF(Py_False), Py_False 194#endif 195 196#ifndef Py_RETURN_TRUE 197#define Py_RETURN_TRUE return Py_INCREF(Py_True), Py_True 198#endif 199 200/* Macros for inline documentation. (Python 2.3) */ 201#ifndef PyDoc_VAR 202#define PyDoc_VAR(name) static char name[] 203#endif 204 205#ifndef PyDoc_STRVAR 206#define PyDoc_STRVAR(name,str) PyDoc_VAR(name) = PyDoc_STR(str) 207#endif 208 209#ifndef PyDoc_STR 210#ifdef WITH_DOC_STRINGS 211#define PyDoc_STR(str) str 212#else 213#define PyDoc_STR(str) "" 214#endif 215#endif 216 217/* Basestring check (basestring introduced in Python 2.3) */ 218#if PY_VERSION_HEX < 0x02030000 219#define BaseString_Check(type) ( \ 220 PyObject_TypeCheck((type), &PyString_Type) \ 221 || PyObject_TypeCheck((type), &PyUnicode_Type) \ 222) 223#else 224#define BaseString_Check(type) PyObject_TypeCheck((type), &PyBaseString_Type) 225#endif 226 227#define GENERIC_ALLOC(type) \ 228 ((void *)((PyTypeObject *)type)->tp_alloc(type, (Py_ssize_t)0)) 229 230/* PyPy doesn't define it */ 231#ifndef PyType_IS_GC 232#define PyType_IS_GC(t) PyType_HasFeature((t), Py_TPFLAGS_HAVE_GC) 233#endif 234 235#define DEFINE_GENERIC_DEALLOC(prefix) \ 236static void prefix##_dealloc(void *self) \ 237{ \ 238 if (PyType_IS_GC(((PyObject *)self)->ob_type)) \ 239 PyObject_GC_UnTrack(self); \ 240 (void)prefix##_clear(self); \ 241 ((PyObject *)self)->ob_type->tp_free((PyObject *)self); \ 242} 243 244#endif /* SETUP_CEXT_H */ 245