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