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