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