HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PY_CPythonAPI.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * COMMENTS:
7  * This file provides a set of functions, types, and macros that wrap
8  * Python's C API. This way, we can choose at runtime which version
9  * of the Python library we load, and we do not add a compile/link-time
10  * dependency on a particular version of Python. Use this wrapper API
11  * as follows:
12  *
13  * - Instead of including <python/Python.h>, instead include
14  * PY_CPythonAPI.h. Still include this file first, just like you would
15  * with Python.h.
16  * (It's even in the Python docs that you need to include Python.h
17  * before anything else. Otherwise you get strange gcc warnings.)
18  *
19  * - Add a "PY_" prefix to all Python functions, types, and macros. For
20  * example,
21  * PyObject *dict = PyDict_New();
22  * becomes
23  * PY_PyObject *dict = PY_PyDict_New();
24  *
25  * - Access to Python constants, such as PyExc_SystemExit, become function
26  * calls. For example,
27  * PyErr_SetObject(PyExc_IndexError, error_message);
28  * becomes
29  * PY_PyErr_SetObject(PY_PyExc_IndexError(), error_message);
30  *
31  * - When directly accessing members of Python structures, change the
32  * data member accesses into method calls. For example, when accessing
33  * the ob_type member of a [PY_]PyObject,
34  * op->ob_type
35  * becomes
36  * ob->ob_type()
37  *
38  * - Constructing instances of Python objects like PySequenceMethods,
39  * or creating subclasses of PyObject, is different.
40  */
41 
42 #ifndef __PY_CPythonAPI_h__
43 #define __PY_CPythonAPI_h__
44 
45 #include "PY_API.h"
46 #include <UT/UT_WorkBuffer.h>
47 #include <SYS/SYS_Types.h>
48 #include <hboost/preprocessor/seq/for_each.hpp>
49 #include <hboost/preprocessor/seq/for_each_i.hpp>
50 #include <hboost/preprocessor/seq/seq.hpp>
51 #include <hboost/preprocessor/punctuation/comma_if.hpp>
52 #include <hboost/preprocessor/cat.hpp>
53 #include <hboost/preprocessor/stringize.hpp>
54 #include <string.h>
55 #include <stdarg.h>
56 
57 #ifdef WIN32
58 // Disable MSVC warnings about not enough actual parameters for macros.
59 // Otherwise, wrapped functions that take no parameters will generate
60 // warnings.
61 #pragma warning(disable:4003)
62 #endif
63 
64 using PY_AtExitFunc = void (*)();
65 
66 // These are the functions we'll automatically wrap, of the form
67 // (function_name)(return_type)(parm_types). parm_types is itself a sequence
68 // of (T1)(T2)(T3). While the wrapped functions don't have a "PY_" prefix,
69 // any non-native data types do.
70 #define PY_WRAPPED_FUNCTIONS \
71  ((Py_AtExit)(int)((PY_AtExitFunc))) \
72  ((Py_InitializeEx)(void)((int))) \
73  ((Py_IsInitialized)(int)()) \
74  ((Py_SetPythonHome)(void)((char*))) \
75  ((Py_SetProgramName)(void)((char*))) \
76  ((PyGILState_Ensure)(PY_PyGILState_STATE)()) \
77  ((PyGILState_Release)(void)((PY_PyGILState_STATE))) \
78  ((PyGILState_GetThisThreadState)(PY_PyThreadState *)()) \
79  ((PyRun_SimpleStringFlags)(int)((const char *)(PY_PyCompilerFlags *))) \
80  ((Py_CompileStringFlags)(PY_PyObject *) \
81  ((const char *)(const char *)(int)(PY_PyCompilerFlags *))) \
82  ((PyEval_InitThreads)(void)()) \
83  ((PyEval_SaveThread)(PY_PyThreadState *)()) \
84  ((PyEval_RestoreThread)(void)((PY_PyThreadState *))) \
85  ((PyEval_GetFrame)(PY_PyFrameObject *)()) \
86  ((PyEval_EvalCode)(PY_PyObject *) \
87  ((PY_PyCodeObject *)(PY_PyObject *)(PY_PyObject *))) \
88  ((PyThread_get_thread_ident)(long)()) \
89  ((PyThreadState_SetAsyncExc)(int)((long)(PY_PyObject *))) \
90  ((PyMem_Malloc)(void *)((size_t))) \
91  ((Py_Main)(int)((int)(char **))) \
92  ((Py_IncRef)(void)((PY_PyObject *))) \
93  ((Py_DecRef)(void)((PY_PyObject *))) \
94  ((PyObject_GetAttr)(PY_PyObject *)((PY_PyObject *)(PY_PyObject *))) \
95  ((PyObject_GetAttrString)(PY_PyObject *)((PY_PyObject *)(const char *))) \
96  ((PyObject_SetAttr)(int)((PY_PyObject *)(PY_PyObject *)(PY_PyObject *))) \
97  ((PyObject_Repr)(PY_PyObject *)((PY_PyObject*))) \
98  ((PyObject_Str)(PY_PyObject *)((PY_PyObject *))) \
99  ((PyObject_IsTrue)(int)((PY_PyObject *))) \
100  ((PyObject_CallObject)(PY_PyObject *)((PY_PyObject *)(PY_PyObject *))) \
101  ((PyObject_Call)(PY_PyObject *) \
102  ((PY_PyObject *)(PY_PyObject *)(PY_PyObject *))) \
103  ((PyObject_GetIter)(PY_PyObject *)((PY_PyObject *))) \
104  ((PyObject_RichCompareBool)(int)((PY_PyObject *)(PY_PyObject *)(int))) \
105  ((PyObject_Print)(int)((PY_PyObject *)(FILE *)(int))) \
106  ((Py_VaBuildValue)(PY_PyObject *)((const char *)(va_list))) \
107  ((PyArg_VaParse)(int)((PY_PyObject *)(const char *)(va_list))) \
108  ((PyType_IsSubtype)(int)((PY_PyTypeObject *)(PY_PyTypeObject *))) \
109  ((PyType_Ready)(int)((PY_PyTypeObject *))) \
110  ((PyErr_Occurred)(PY_PyObject *)()) \
111  ((PyErr_ExceptionMatches)(int)((PY_PyObject *))) \
112  ((PyErr_Fetch)(void)((PY_PyObject **)(PY_PyObject **)(PY_PyObject **))) \
113  ((PyErr_Clear)(void)()) \
114  ((PyErr_SetString)(void)((PY_PyObject *)(const char *))) \
115  ((PyErr_SetNone)(void)((PY_PyObject *))) \
116  ((PyErr_SetObject)(void)((PY_PyObject *)(PY_PyObject *))) \
117  ((PyErr_CheckSignals)(int)()) \
118  ((PyCObject_GetDesc)(void *)((PY_PyObject *))) \
119  ((PyCObject_AsVoidPtr)(void *)((PY_PyObject *))) \
120  ((PyLong_FromLongLong)(PY_PyObject *)((int64))) \
121  ((PyLong_FromLong)(PY_PyObject *)((long))) \
122  ((PyLong_AsLong)(long)((PY_PyObject *))) \
123  ((PyLong_AsVoidPtr)(void*)((PY_PyObject *))) \
124  ((PyFloat_AsDouble)(double)((PY_PyObject *))) \
125  ((PyFloat_FromDouble)(PY_PyObject *)((double))) \
126  ((PySequence_Check)(int)((PY_PyObject *))) \
127  ((PySequence_Size)(PY_Py_ssize_t)((PY_PyObject *))) \
128  ((PySequence_GetItem)(PY_PyObject *)((PY_PyObject *)(PY_Py_ssize_t))) \
129  ((PyList_New)(PY_PyObject *)((PY_Py_ssize_t))) \
130  ((PyList_Size)(PY_Py_ssize_t)((PY_PyObject *))) \
131  ((PyList_GetItem)(PY_PyObject *)((PY_PyObject *)(PY_Py_ssize_t))) \
132  ((PyList_SetItem)(int)((PY_PyObject *)(PY_Py_ssize_t)(PY_PyObject *))) \
133  ((PyList_Append)(int)((PY_PyObject *)(PY_PyObject *))) \
134  ((PyTuple_New)(PY_PyObject *)((PY_Py_ssize_t))) \
135  ((PyTuple_SetItem)(int)((PY_PyObject *)(PY_Py_ssize_t)(PY_PyObject *))) \
136  ((PyTuple_Size)(PY_Py_ssize_t)((PY_PyObject *))) \
137  ((PyDict_Copy)(PY_PyObject *)((PY_PyObject *))) \
138  ((PyDict_New)(PY_PyObject *)()) \
139  ((PyDict_Clear)(void)((PY_PyObject *))) \
140  ((PyDict_Next)(int) \
141  ((PY_PyObject *)(PY_Py_ssize_t *)(PY_PyObject **)(PY_PyObject **))) \
142  ((PyDict_GetItemString)(PY_PyObject *)((PY_PyObject *)(const char *))) \
143  ((PyDict_SetItemString)(int) \
144  ((PY_PyObject *)(const char *)(PY_PyObject *))) \
145  ((PyDict_SetItem)(int)((PY_PyObject *)(PY_PyObject *)(PY_PyObject *))) \
146  ((PyDict_DelItemString)(PY_PyObject *)((PY_PyObject *)(const char *))) \
147  ((PyIter_Next)(PY_PyObject *)((PY_PyObject *))) \
148  ((PyFunction_GetCode)(PY_PyObject *)((PY_PyObject *))) \
149  ((PyCallable_Check)(int)((PY_PyObject *))) \
150  ((PyImport_ImportModule)(PY_PyObject *)((const char *))) \
151  ((PyImport_ExecCodeModule)(PY_PyObject *)((char *)(PY_PyObject *))) \
152  ((PyImport_AddModule)(PY_PyObject *)((const char *))) \
153  ((PyImport_GetMagicNumber)(long)()) \
154  ((PyModule_GetDict)(PY_PyObject *)((PY_PyObject *))) \
155  ((PyModule_AddObject)(int)((PY_PyObject *)(const char *)(PY_PyObject *))) \
156  ((PySys_SetArgv)(void)((int)(char **))) \
157  ((PySys_SetArgvEx)(void)((int)(char **)(int))) \
158  ((PySys_GetObject)(PY_PyObject *)((char *))) \
159  ((PyOS_InitInterrupts)(void)()) \
160  ((PyMarshal_ReadObjectFromString)(PY_PyObject *)((char *)(PY_Py_ssize_t))) \
161  ((PyMarshal_WriteObjectToString)(PY_PyObject *)((PY_PyObject *)(int))) \
162  ((PyRun_InteractiveLoopFlags)(int) \
163  ((FILE *)(const char *)(PY_PyCompilerFlags *)))
164 
165 // These are the constants we'll automatically wrap, of the form
166 // (constant_name)(type). The names of the constants must correspond directly
167 // to symbols in the Python shared object.
168 typedef char *(*PY_PyOS_ReadlineFunctionPointerType)(FILE *, FILE *, char *);
169 #define PY_WRAPPED_CONSTANTS \
170  ((Py_TabcheckFlag)(int)) \
171  ((_Py_NoneStruct)(PY_PyObject)) \
172  ((_PyOS_ReadlineTState)(PY_PyThreadState *)) \
173  ((_PyThreadState_Current)(PY_PyThreadState *)) \
174  ((PyOS_ReadlineFunctionPointer)(PY_PyOS_ReadlineFunctionPointerType)) \
175  ((PyExc_RuntimeError)(PY_PyObject *)) \
176  ((PyExc_IndexError)(PY_PyObject *)) \
177  ((PyExc_AttributeError)(PY_PyObject *)) \
178  ((PyExc_TypeError)(PY_PyObject *)) \
179  ((PyExc_KeyboardInterrupt)(PY_PyObject *)) \
180  ((PyExc_SystemExit)(PY_PyObject *)) \
181  ((PyExc_BaseException)(PY_PyObject *)) \
182  ((PyLong_Type)(PY_PyTypeObject)) \
183  ((PyBool_Type)(PY_PyTypeObject)) \
184  ((PyFloat_Type)(PY_PyTypeObject)) \
185  ((PyUnicode_Type)(PY_PyTypeObject)) \
186  ((PyTuple_Type)(PY_PyTypeObject)) \
187  ((PyList_Type)(PY_PyTypeObject)) \
188  ((PyDict_Type)(PY_PyTypeObject)) \
189  ((PyFunction_Type)(PY_PyTypeObject))
190 
191 //----------------------------------------------------------------------------
192 
193 // These initial macros simplify usage of Boost's preprocessor SEQ_FOR_EACH
194 // macros. SEQ_FOR_EACH works as follows: you pass it a callback macro,
195 // some extra data, and a sequence of the form (a)(b)(c). It then calls
196 // the callback macro with a repetition construct (named r, and only used
197 // internally by boost), the extra data you pased in, and each element of
198 // the sequence.
199 //
200 // Almost all the time, you don't need extra data, so you pass in _ and
201 // ingore the second parameter in the callback macro.
202 //
203 // Often, the sequence contains other sequences, and that's where these
204 // macros come in handy. PY_FOR_EACH_SEQ_OF_2, for example, takes a callback
205 // and a sequence of pair sequences of the form ((A1)(A2))((B1)(B2))((C1)(C2)).
206 // It then calls the callback with (A1, A2), then with (B1, B2), and so on.
207 // PY_FOR_EACH_SEQ_OF_3 works with sequences of triplets.
208 
209 // This helper macro works by passing in the caller's macro as the extra data.
210 // The caller's macro will then be the second argument of the
211 // PY_CALLBACK_SEQ_OF_2 macro.
212 #define PY_FOR_EACH_SEQ_OF_2(macro, seq) \
213  HBOOST_PP_SEQ_FOR_EACH(PY_CALLBACK_SEQ_OF_2, macro, seq)
214 
215 // Pull off the first and second elements of the sequence, and pass them into
216 // the caller's macro.
217 #define PY_CALLBACK_SEQ_OF_2(r, macro, seq) \
218  macro(PY_SEQ_ELEM_0(seq), PY_SEQ_ELEM_1(seq))
219 
220 
221 // This helper macro is similar to the last one, except it works with
222 // subsequences of 3 elements.
223 #define PY_FOR_EACH_SEQ_OF_3(macro, seq) \
224  HBOOST_PP_SEQ_FOR_EACH(PY_CALLBACK_SEQ_OF_3, macro, seq)
225 #define PY_CALLBACK_SEQ_OF_3(r, macro, seq) \
226  macro(PY_SEQ_ELEM_0(seq), PY_SEQ_ELEM_1(seq), PY_SEQ_ELEM_2(seq))
227 
228 
229 // These helper macros extract the first, second, and third elements from
230 // an (a)(b)(c) style sequence.
231 #define PY_SEQ_ELEM_0(seq) \
232  HBOOST_PP_SEQ_HEAD(seq)
233 #define PY_SEQ_ELEM_1(seq) \
234  HBOOST_PP_SEQ_HEAD(HBOOST_PP_SEQ_TAIL(seq))
235 #define PY_SEQ_ELEM_2(seq) \
236  HBOOST_PP_SEQ_HEAD(HBOOST_PP_SEQ_TAIL(HBOOST_PP_SEQ_TAIL(seq)))
237 
238 //----------------------------------------------------------------------------
239 
240 // This macro takes a sequence of of parameter types (T1)(T2)(T3) and creates a
241 // parameter list of the form T1 arg1, T2 arg2, T3 arg3. SEQ_FOR_EACH_I
242 // passes in the index as well as the element into PY_MAKE_PARM.
243 #define PY_MAKE_PARM_LIST(parm_types) \
244  HBOOST_PP_SEQ_FOR_EACH_I(PY_MAKE_PARM, _, parm_types)
245 
246 // unused_data is the second parameter passed to FOR_EACH_I. i is the index
247 // of the element in the parm types sequence, and elem is that element.
248 // If i is not 0, we add a comma to make sure the parameters are comma-
249 // separated.
250 #define PY_MAKE_PARM(r, unused_data, i, elem) \
251  HBOOST_PP_COMMA_IF(i) elem HBOOST_PP_CAT(arg, i)
252 
253 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
254 
255 // This macro takes a sequence of of parameter types (T1)(T2)(T3) and creates
256 // an argument list of the form arg1, arg2, arg3. The length of the parameter
257 // type sequence is used, but the actual parameter types are not.
258 #define PY_MAKE_ARG_LIST(parm_types) \
259  HBOOST_PP_SEQ_FOR_EACH_I(PY_MAKE_ARG, _, parm_types)
260 
261 #define PY_MAKE_ARG(r, unused_data, i, unused_elem) \
262  HBOOST_PP_COMMA_IF(i) HBOOST_PP_CAT(arg, i)
263 
264 //============================================================================
265 
266 // These enums and opaque types are used by the wrapped Python functions:
273 
274 #if defined(WIN32)
275  // Windows doesn't define ssize_t (a signed version of size_t). pyconfig.h
276  // (included by Python.h) does define it, though. So, rather than defining
277  // ssize_t and risking a compiler error if Python.h is ever included in our
278  // code or HDK code, we instead just define PY_Py_ssize_t directly.
279  #if SIZEOF_VOID_P == 8
280  typedef int64 PY_Py_ssize_t;
281  #else
282  typedef int32 PY_Py_ssize_t;
283  #endif
284 #else
285  typedef ssize_t PY_Py_ssize_t;
286 #endif
287 
288 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
289 
290 // This helper function is used below to emulate the result of looking up a
291 // struct element via an offset, without actually knowing the type definition
292 // of the object.
293 template <typename RETURN_TYPE>
294 static inline RETURN_TYPE &
295 pyAccessStructMember(void *c_struct, int offset)
296 { return *(RETURN_TYPE *)((char *)c_struct + offset); }
297 
298 // We can't actually declare Python structures, since their size can change
299 // depending on defines that were set when Python was compiled. Also, their
300 // size and organization could change in future Python versions. Instead,
301 // we create wrapper classes with methods to look up the corresponding
302 // data member. For example, if you would normally write op->ob_type using
303 // the standard Python API, you instead write op->ob_type() using this API.
304 //
305 // Start by building up the Python class hierarchy. Since Python is written
306 // in C, the class hierarchy is built manually, using macros for common
307 // members between subclasses. These members aren't likely to change between
308 // Python versions, but if they do we can still preserve the API (i.e. using
309 // methods to access members), although we'll have to manually give some class
310 // definitions instead of letting macros take care of everything.
311 
312 // PY_PyObject_HEAD_EXTRA is at the beginning of most Python object structures.
313 // Unfortunately, its size varies depending on compiler defines, so we compute
314 // its size when we load the Python shared object. Its constructor is private
315 // and unimplemented so that you can't accidentally instance it or any of its
316 // subclasses.
318 {
319 public:
320  static int theStructSize;
321 private:
322  PY_PyObject_HEAD_EXTRA(); // unimplemented
323 };
324 
325 // This macro creates a new subclass, adding one member.
326 #define PY_ADD_SUBCLASS_1(subclass, base_class, member1_type, member1_name) \
327 class PY_API subclass : public base_class \
328 { \
329 public: \
330  member1_type member1_name() \
331  { return pyAccessStructMember<member1_type>( \
332  this, base_class::theStructSize); } \
333 \
334  static void computeStructSize() \
335  { theStructSize = base_class::theStructSize + sizeof(member1_type); } \
336 \
337  static int theStructSize; \
338 };
339 
340 // This macro creates a new subclass, adding two members.
341 #define PY_ADD_SUBCLASS_2(subclass, base_class, \
342  member1_type, member1_name, member2_type, member2_name) \
343 class PY_API subclass : public base_class \
344 { \
345 public: \
346  member1_type member1_name() \
347  { return pyAccessStructMember<member1_type>( \
348  this, base_class::theStructSize); } \
349 \
350  member2_type member2_name() \
351  { return pyAccessStructMember<member2_type>( \
352  this, base_class::theStructSize + sizeof(member1_type)); } \
353 \
354  static void computeStructSize() \
355  { theStructSize = base_class::theStructSize + \
356  sizeof(member1_type) + sizeof(member2_type); } \
357 \
358  static int theStructSize; \
359 };
360 
361 // The elements in the PyObject_HEAD_EXTRA are dependent on Python compilation
362 // settings. See the .C file for more information. The other structures
363 // we create are:
364 //
365 // PyObject_HEAD (common to all PyObject's) is defined as: (It's changed
366 // slightly in Python 3.0, but the size is the same. See PEP 3123.)
367 // PyObject_HEAD_EXTRA
368 // Py_ssize_t ob_refcnt;
369 // PyTypeObject *ob_type;
370 //
371 // PyObject_VAR_HEAD (common to all PyVarObject's) is defined as:
372 // PyObject_HEAD
373 // Py_ssize_t ob_size;
374 //
375 // PyTypeObject starts with:
376 // PyObject_VAR_HEAD
377 // const char *tp_name;
378 // ...more...
379 //
380 // PyFrameObject starts with:
381 // PyObject_VAR_HEAD
382 // PyFrameObject *f_back;
383 // PyCodeObject *f_code;
384 // ...more...
385 //
386 // PyListObject starts with
387 // PyObject_VAR_HEAD
388 // PyObject **ob_item;
389 // ...more...
390 //
391 // PyTupleObject starts with
392 // PyObject_VAR_HEAD
393 // PyObject *ob_item[1];
394 // ...more...
395 
397  PY_Py_ssize_t, ob_refcnt,
398  PY_PyTypeObject *, ob_type)
399 
401  PY_Py_ssize_t, ob_size)
402 
403 PY_ADD_SUBCLASS_1(PY_PyTypeObject, PY_PyVarObject,
404  const char *, tp_name)
405 
406 PY_ADD_SUBCLASS_2(PY_PyFrameObject, PY_PyVarObject,
407  PY_PyFrameObject *, f_back,
408  PY_PyCodeObject *, f_code)
409 
410 PY_ADD_SUBCLASS_1(PY_PyListObject, PY_PyVarObject,
411  PY_PyObject **, ob_item)
412 
413 // We need to manually specify PY_PyTupleObject. Its struct member is declared
414 // as "PyObject *ob_item[1]", so ob_item is a "PyObject **" whose value is
415 // computed as an offset from the "this" pointer, _without_ doing any
416 // dereferencing.
417 class PY_API PY_PyTupleObject : public PY_PyVarObject
418 {
419 public:
420  PY_PyObject **ob_item()
421  { return (PY_PyObject **)((char *)this + PY_PyVarObject::theStructSize); }
422 };
423 
424 //----------------------------------------------------------------------------
425 
426 // These few concrete types make it much easier to extend Python to add new
427 // classes, methods, etc. If Python changes their definition in future
428 // versions, we'll have to rework how we do things. Currently, though, these
429 // few classes are small.
430 
431 typedef PY_PyObject *(*PY_PyCFunction)(PY_PyObject *, PY_PyObject *);
432 
434 {
435  const char *ml_name;
437  int ml_flags;
438  const char *ml_doc;
439 };
440 
441 //----------------------------------------------------------------------------
442 
443 // This macro takes a (function_name)(return_type)(parm_types) sequence and
444 // calls PY_DECLARE_FUNCTION.
445 #define PY_DECLARE_FUNCTION_FROM_SEQ(seq) \
446  PY_CALLBACK_SEQ_OF_3(_, PY_DECLARE_FUNCTION, seq)
447 
448 // This macro forward declares a function, prefixing "PY_" to the function
449 // name.
450 #define PY_DECLARE_FUNCTION(function_name, return_type, parm_types) \
451  PY_API return_type HBOOST_PP_CAT(PY_, function_name)( \
452  PY_MAKE_PARM_LIST(parm_types));
453 
454 // This macro forward declares the function that wraps a constant, prefixing
455 // "PY_" to the constant's name.
456 #define PY_DECLARE_WRAPPED_CONSTANT(constant_name, type) \
457  PY_API type &HBOOST_PP_CAT(PY_, constant_name)();
458 
459 // For each element in the big list of wrapped functions, create forward
460 // declarations. Create similar forward declarations for the big list of
461 // constants.
464 
465 //----------------------------------------------------------------------------
466 
467 // These functions need to be manually-wrapped:
469 PY_API int PY_PyArg_ParseTuple(PY_PyObject *args, const char *format, ...);
470 PY_API PY_PyObject *PY_Py_BuildValue(const char *format, ...);
472  PY_PyObject *o, const char *name, const char *format, ...);
473 
474 #define PY_FUNC_SEQ_PyInt_AsLong (PyInt_AsLong)(long)((PY_PyObject *))
475 #define PY_FUNC_SEQ_PyInt_FromLong (PyInt_FromLong)(PY_PyObject *)((long))
476 #define PY_FUNC_SEQ_PyString_AsString \
477  (PyString_AsString)(char *)((PY_PyObject *))
478 #define PY_FUNC_SEQ_PyString_AsStringAndSize \
479  (PyString_AsStringAndSize)(int)((PY_PyObject *)(char **)(PY_Py_ssize_t *))
480 #define PY_FUNC_SEQ_PyString_FromString \
481  (PyString_FromString)(PY_PyObject *)((const char *))
482 #define PY_FUNC_SEQ_PyString_FromStringAndSize \
483  (PyString_FromStringAndSize)(PY_PyObject *)((const char *)(PY_Py_ssize_t))
484 #define PY_FUNC_SEQ_PyString_AsUTF8String \
485  (PyString_AsUTF8String)(PY_PyObject *)((PY_PyObject *))
486 
494 
495 
496 // These wrapper functions replace constants. If the constants change between
497 // Python versions, we can use the value of pyPythonVersion to decide what to
498 // return. First do a search to make sure they're not called from static
499 // constructors, though (like PY_METH_VARARGS), since pyPythonVersion may not
500 // be intialized yet.
501 static inline int PY_Py_single_input() { return 256; }
502 static inline int PY_Py_file_input() { return 257; }
503 static inline int PY_Py_eval_input() { return 258; }
504 static inline int PY_Py_MARSHAL_VERSION() { return 2; }
505 static inline int PY_METH_VARARGS() { return 0x0001; }
506 static inline int PY_Py_EQ() { return 2; }
507 static inline int PY_PyGILState_LOCKED() { return 0; }
508 static inline int PY_PyGILState_UNLOCKED() { return 1; }
509 
510 
511 // _Py_TrueStruct and _Py_ZeroStruct need to be manually wrapped. Before
512 // Python 3.0, _Py_TrueStruct and _Py_ZeroStruct are PyIntObjects. Starting in
513 // 3.0, they're PyLongObjects, since ints were removed. For our purposes, we
514 // don't care -- we just treat them as PyObjects. However, the automatically-
515 // generated wrapper functions will give compiler errors because it can't
516 // convert PyIntObject/PyLongObjects to PyObjects. So, we wrap them manually.
518 PY_DECLARE_WRAPPED_CONSTANT(_Py_ZeroStruct, PY_PyObject)
519 
520 // Even though int and string have been replaced by long and unicode in Python
521 // 3.0, we still support the old API, including the old types. They're simply
522 // mapped to the replacement types internally. So, PyInt_Check and
523 // PyString_Check will actually be using longs and unicodes in Python 3.0.
524 PY_DECLARE_WRAPPED_CONSTANT(PyInt_Type, PY_PyTypeObject)
525 PY_DECLARE_WRAPPED_CONSTANT(PyString_Type, PY_PyTypeObject)
526 #define PY_PyInt_Check(op) PY_PyObject_TypeCheck(op, &PY_PyInt_Type())
527 #define PY_PyString_Check(op) PY_PyObject_TypeCheck(op, &PY_PyString_Type())
528 
529 
530 // Various macros follow, to account for API elements that do not have symbols
531 // in the Python shared object. Note that they might need to be modified if
532 // the they change between Python versions.
533 #define PY_Py_INCREF PY_Py_IncRef
534 #define PY_Py_DECREF PY_Py_DecRef
535 #define PY_Py_XDECREF(op) if ((op) == NULL) ; else PY_Py_DECREF(op)
536 
537 #define PY_Py_False() (&PY__Py_ZeroStruct())
538 #define PY_Py_True() (&PY__Py_TrueStruct())
539 #define PY_Py_None() (&PY__Py_NoneStruct())
540 
541 #define PY_Py_RETURN_NONE return PY_Py_INCREF(PY_Py_None()), PY_Py_None()
542 #define PY_PyMODINIT_FUNC extern "C" SYS_VISIBILITY_EXPORT void
543 
544 #define PY_PyRun_SimpleString(s) PY_PyRun_SimpleStringFlags(s, NULL)
545 #define PY_Py_CompileString(str, p, s) PY_Py_CompileStringFlags(str, p, s, NULL)
546 #define PY_PyRun_InteractiveLoop(f, p) PY_PyRun_InteractiveLoopFlags(f, p, NULL)
547 
548 #define PY_PyList_SET_ITEM(op, i, v) \
549  (((PY_PyListObject *)(op))->ob_item()[i] = (v))
550 #define PY_PyTuple_SET_ITEM(op, i, v) \
551  (((PY_PyTupleObject *)(op))->ob_item()[i] = (v))
552 
553 #define PY_PyList_GET_ITEM(op, i, v) \
554  (((PY_PyListObject *)(op))->ob_item()[i])
555 #define PY_PyTuple_GET_ITEM(op, i) \
556  (((PY_PyTupleObject *)(op))->ob_item()[i])
557 
558 // TODO: These checking defines may need to change for Python 3.0.
559 #define PY_PyObject_TypeCheck(ob, tp) \
560  ((ob)->ob_type() == (tp) || PY_PyType_IsSubtype((ob)->ob_type(), (tp)))
561 #define PY_PyCObject_Check(op) (op->ob_type() == &PY_PyCObject_Type())
562 #define PY_PyFloat_Check(op) PY_PyObject_TypeCheck(op, &PY_PyFloat_Type())
563 #define PY_PyLong_Check(op) PY_PyObject_TypeCheck(op, &PY_PyLong_Type())
564 #define PY_PyBool_Check(op) PY_PyObject_TypeCheck(op, &PY_PyBool_Type())
565 #define PY_PyUnicode_Check(op) PY_PyObject_TypeCheck(op, &PY_PyUnicode_Type())
566 #define PY_PyTuple_Check(op) PY_PyObject_TypeCheck(op, &PY_PyTuple_Type())
567 #define PY_PyList_Check(op) PY_PyObject_TypeCheck(op, &PY_PyList_Type())
568 #define PY_PyDict_Check(op) PY_PyObject_TypeCheck(op, &PY_PyDict_Type())
569 #define PY_PyFunction_Check(op) ((op)->ob_type() == &PY_PyFunction_Type())
570 
571 // TODO: Before Python 3.0, this macro also checks returns true if
572 // PyInstance_Check returns true. Starting with 3.0, it no longer
573 // does the PyInstance_Check.
574 // To do the proper thing, We need to make this a manually-implemented function.
575 #define PY_PyExceptionInstance_Check(op) \
576  PY_PyObject_TypeCheck(op, (PY_PyTypeObject *)&PY_PyExc_BaseException())
577 
578 // Unfortunately, there's no uniform way to directly access struct members
579 // for both compile-time and non-compile-time dependencies on Python. The
580 // compile-time acccess doesn't use a method call, but the non-compile-time
581 // one does. So, we instead use macros.
582 #define PY_PYFRAME_BACK(op) ((op)->f_back())
583 #define PY_PYFRAME_CODE(op) ((op)->f_code())
584 
585 // This function loads the appropriate Python library and looks up symbols
586 // in it. It should only be called from PY_InterpreterAutoLock, when it
587 // initializes Python.
589 
590 // This function returns the Python major and minor version (e.g. a return
591 // value of 25 means version 2.5). It is up to you to ensure that you call
592 // this function after the Python library has been loaded (creating a
593 // PY_InterpreterAutoLock will load the Python library and initialize the
594 // interpreter).
596 
597 // This function returns the path to HFS's Python's site-packages directory.
598 PY_API void PYgetHFSPythonSitePackagesPath(UT_WorkBuffer &site_packages_path);
599 
600 // Return true if the Python library in $HFS is loaded.
602 
603 #endif
const char * ml_doc
#define PY_FUNC_SEQ_PyString_AsString
#define PY_WRAPPED_FUNCTIONS
Definition: PY_CPythonAPI.h:70
#define PY_FUNC_SEQ_PyString_FromStringAndSize
const char * ml_name
int PY_PyGILState_STATE
PY_PyObject *(* PY_PyCFunction)(PY_PyObject *, PY_PyObject *)
PY_API int PY_PyArg_ParseTuple(PY_PyObject *args, const char *format,...)
typedef void(APIENTRYP PFNGLCULLFACEPROC)(GLenum mode)
PY_API PY_PyObject * PY_Py_BuildValue(const char *format,...)
PY_API bool PYisHFSPythonLibraryLoaded()
#define PY_FOR_EACH_SEQ_OF_3(macro, seq)
PY_API int PYgetPythonVersion()
class PY_API PY_PyCodeObject
PY_API void PYgetHFSPythonSitePackagesPath(UT_WorkBuffer &site_packages_path)
#define PY_WRAPPED_CONSTANTS
long long int64
Definition: SYS_Types.h:107
#define PY_FUNC_SEQ_PyInt_AsLong
PY_PyObject
PY_API void PYloadPythonLibrary()
GLintptr offset
Definition: glcorearb.h:664
class PY_API PY_PyThreadState
class PY_API PY_PyTypeObject
#define PY_API
Definition: PY_API.h:10
#define PY_FUNC_SEQ_PyInt_FromLong
ob_size PY_PyVarObject
#define PY_ADD_SUBCLASS_1(subclass, base_class, member1_type, member1_name)
void(*)( PY_AtExitFunc)
Definition: PY_CPythonAPI.h:64
class PY_API PY_PyCompilerFlags
GLuint const GLchar * name
Definition: glcorearb.h:785
#define PY_ADD_SUBCLASS_2(subclass, base_class, member1_type, member1_name, member2_type, member2_name)
int int32
Definition: SYS_Types.h:35
#define PY_DECLARE_FUNCTION_FROM_SEQ(seq)
#define PY_DECLARE_WRAPPED_CONSTANT(constant_name, type)
#define PY_FOR_EACH_SEQ_OF_2(macro, seq)
GLint GLint GLsizei GLint GLenum format
Definition: glcorearb.h:107
#define PY_FUNC_SEQ_PyString_AsStringAndSize
PY_API PY_PyObject * PY_PyObject_CallMethod(PY_PyObject *o, const char *name, const char *format,...)
PY_API PY_PyObject * PY_Py_InitModule(const char *name, PY_PyMethodDef *methods)
#define PY_DECLARE_FUNCTION(function_name, return_type, parm_types)
#define PY_FUNC_SEQ_PyString_FromString
#define const
Definition: zconf.h:214
ssize_t PY_Py_ssize_t
ob_size PY_PyFrameObject f_back
#define PY_FUNC_SEQ_PyString_AsUTF8String
PY_PyCFunction ml_meth