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