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