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