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 Py_TPFLAGS_HAVE_CLASS 66 #define Py_TPFLAGS_HAVE_CLASS (0) 67 #endif 68 69 #ifndef Py_TPFLAGS_HAVE_WEAKREFS 70 #define Py_TPFLAGS_HAVE_WEAKREFS (0) 71 #endif 72 73 #ifndef Py_TPFLAGS_HAVE_ITER 74 #define Py_TPFLAGS_HAVE_ITER (0) 75 #endif 76 77 #ifndef PyMODINIT_FUNC 78 #define EXT_INIT_FUNC PyObject *CONCATENATE(PyInit_, EXT_MODULE)(void) 79 #else 80 #define EXT_INIT_FUNC PyMODINIT_FUNC CONCATENATE(PyInit_, EXT_MODULE)(void) 81 #endif 82 83 #define EXT_DEFINE(name, methods, doc) \ 84 static struct PyModuleDef EXT_DEFINE_VAR = { \ 85 PyModuleDef_HEAD_INIT, \ 86 name, \ 87 doc, \ 88 -1, \ 89 methods, \ 90 NULL, \ 91 NULL, \ 92 NULL, \ 93 NULL \ 94 } 95 96 #define EXT_CREATE(def) (PyModule_Create(def)) 97 #define EXT_INIT_ERROR(module) do {Py_XDECREF(module); return NULL;} while(0) 98 #define EXT_INIT_RETURN(module) return module 99 100 #define EXT_DOC_UNICODE(m) 101 102 #else /* end py3k */ 103 104 #define EXT2 105 106 #ifndef PyVarObject_HEAD_INIT 107 #define PyVarObject_HEAD_INIT(type, size) \ 108 PyObject_HEAD_INIT(type) size, 109 #endif 110 111 #ifndef PyMODINIT_FUNC 112 #define EXT_INIT_FUNC void CONCATENATE(init, EXT_MODULE)(void) 113 #else 114 #define EXT_INIT_FUNC PyMODINIT_FUNC CONCATENATE(init, EXT_MODULE)(void) 115 #endif 116 117 #define EXT_DEFINE__STRUCT \ 118 CONCATENATE(struct, CONCATENATE(EXT_MODULE, _module)) 119 120 struct EXT_DEFINE__STRUCT { 121 char *m_name; 122 char *m_doc; 123 PyMethodDef *m_methods; 124 }; 125 #define EXT_DEFINE(name, methods, doc) \ 126 static struct EXT_DEFINE__STRUCT EXT_DEFINE_VAR = { \ 127 name, \ 128 doc, \ 129 methods \ 130 } 131 132 #define EXT_CREATE(def) ((def)->m_doc \ 133 ? Py_InitModule3((def)->m_name, (def)->m_methods, (def)->m_doc) \ 134 : Py_InitModule((def)->m_name, (def)->m_methods) \ 135 ) 136 #define EXT_INIT_ERROR(module) return 137 #define EXT_INIT_RETURN(module) return 138 139 #define EXT_DOC_UNICODE(m) do { \ 140 PyObject *doc__, *uni__; \ 141 int res__; \ 142 \ 143 if ((doc__ = PyObject_GetAttrString(m, "__doc__"))) { \ 144 uni__ = PyUnicode_FromEncodedObject(doc__, "utf-8", "strict"); \ 145 Py_DECREF(doc__); \ 146 if (!uni__) \ 147 EXT_INIT_ERROR(m); \ 148 res__ = PyObject_SetAttrString(m, "__doc__", uni__); \ 149 Py_DECREF(uni__); \ 150 if (res__ == -1) \ 151 EXT_INIT_ERROR(m); \ 152 } \ 153 else if (!(PyErr_Occurred() \ 154 && PyErr_ExceptionMatches(PyExc_AttributeError))) \ 155 EXT_INIT_ERROR(m); \ 156 } while(0) 157 158 #endif /* end py2K */ 159 160 #define EXT_INIT_TYPE(module, type) do { \ 161 if (PyType_Ready(type) < 0) \ 162 EXT_INIT_ERROR(module); \ 163 } while (0) 164 165 #define EXT_ADD_TYPE(module, name, type) do { \ 166 Py_INCREF(type); \ 167 if (PyModule_AddObject(module, name, (PyObject *)(type)) < 0) \ 168 EXT_INIT_ERROR(module); \ 169 } while (0) 170 171 #define EXT_ADD_UNICODE(module, name, string, encoding) do { \ 172 if (PyModule_AddObject( \ 173 module, \ 174 name, \ 175 PyUnicode_Decode( \ 176 string, \ 177 sizeof(string) - 1, \ 178 encoding, \ 179 "strict" \ 180 )) < 0) \ 181 EXT_INIT_ERROR(module); \ 182 } while (0) 183 184 #define EXT_ADD_STRING(module, name, string) do { \ 185 if (PyModule_AddStringConstant(module, name, string) < 0) \ 186 EXT_INIT_ERROR(module); \ 187 } while (0) 188 189 #define EXT_ADD_INT(module, name, number) do { \ 190 if (PyModule_AddIntConstant(module, name, number) < 0) \ 191 EXT_INIT_ERROR(module); \ 192 } while (0) 193 194 195 /* PEP 353 support, implemented as of python 2.5 */ 196 #if PY_VERSION_HEX < 0x02050000 197 typedef int Py_ssize_t; 198 #define PyInt_FromSsize_t(arg) PyInt_FromLong((long)arg) 199 #define PyInt_AsSsize_t(arg) (int)PyInt_AsLong(arg) 200 #define PY_SSIZE_T_MAX ((Py_ssize_t)INT_MAX) 201 #endif 202 203 /* 204 * some helper macros (Python 2.4) 205 */ 206 #ifndef Py_VISIT 207 #define Py_VISIT(op) do { \ 208 if (op) { \ 209 int vret = visit((op), arg); \ 210 if (vret) return vret; \ 211 } \ 212 } while (0) 213 #endif 214 215 #ifdef Py_CLEAR 216 #undef Py_CLEAR 217 #endif 218 #define Py_CLEAR(op) do { \ 219 if (op) { \ 220 PyObject *tmp__ = (PyObject *)(op); \ 221 (op) = NULL; \ 222 Py_DECREF(tmp__); \ 223 } \ 224 } while (0) 225 226 #ifndef Py_RETURN_NONE 227 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None 228 #endif 229 230 #ifndef Py_RETURN_FALSE 231 #define Py_RETURN_FALSE return Py_INCREF(Py_False), Py_False 232 #endif 233 234 #ifndef Py_RETURN_TRUE 235 #define Py_RETURN_TRUE return Py_INCREF(Py_True), Py_True 236 #endif 237 238 /* Macros for inline documentation. (Python 2.3) */ 239 #ifndef PyDoc_VAR 240 #define PyDoc_VAR(name) static char name[] 241 #endif 242 243 #ifndef PyDoc_STRVAR 244 #define PyDoc_STRVAR(name,str) PyDoc_VAR(name) = PyDoc_STR(str) 245 #endif 246 247 #ifndef PyDoc_STR 248 #ifdef WITH_DOC_STRINGS 249 #define PyDoc_STR(str) str 250 #else 251 #define PyDoc_STR(str) "" 252 #endif 253 #endif 254 255 /* Basestring check (basestring introduced in Python 2.3) */ 256 #if PY_VERSION_HEX < 0x02030000 257 #define BaseString_Check(type) ( \ 258 PyObject_TypeCheck((type), &PyString_Type) \ 259 || PyObject_TypeCheck((type), &PyUnicode_Type) \ 260 ) 261 #else 262 #define BaseString_Check(type) PyObject_TypeCheck((type), &PyBaseString_Type) 263 #endif 264 265 #define GENERIC_ALLOC(type) \ 266 ((void *)((PyTypeObject *)type)->tp_alloc(type, (Py_ssize_t)0)) 267 268 /* PyPy doesn't define it */ 269 #ifndef PyType_IS_GC 270 #define PyType_IS_GC(t) PyType_HasFeature((t), Py_TPFLAGS_HAVE_GC) 271 #endif 272 273 #ifndef Py_TYPE 274 #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type) 275 #endif 276 277 #define DEFINE_GENERIC_DEALLOC(prefix) \ 278 static void prefix##_dealloc(void *self) \ 279 { \ 280 if (PyType_IS_GC(Py_TYPE(self))) \ 281 PyObject_GC_UnTrack(self); \ 282 (void)prefix##_clear(self); \ 283 (Py_TYPE(self))->tp_free((PyObject *)self); \ 284 } 285 286 #endif /* SETUP_CEXT_H */ 287