HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PyImathAPI.h
Go to the documentation of this file.
1 //
2 // SPDX-License-Identifier: BSD-3-Clause
3 // Copyright Contributors to the OpenEXR Project.
4 //
5 
6 // clang-format off
7 
8 #ifndef _PyImathAPI_h_
9 #define _PyImathAPI_h_
10 
11 #include <patchlevel.h>
12 
13 #if PY_MAJOR_VERSION >= 3
14 
15  // Big changes in Python3 with regard to PyClass. Most of these functions
16  // are gone so the equivalent functionality is done this way...
17  #define PyClass_Check(object) \
18  PyObject_IsInstance (object, reinterpret_cast<PyObject *> (&PyType_Type))
19 
20  // Py_FindMethod is gone and so you must search for functions by searching
21  // through an object's attributes.
22  #define Py_FindMethod(methods, self, name) \
23  PyObject_GenericGetAttr(self, PyBytes_FromString(name))
24 
25  // One of the biggest differences between 2&3 is use support for Unicode.
26  // Working with strings at the C API level one has be careful that the
27  // returned object will not be Unicode and thus need to be decoded before
28  // being interpreted. These macros will return the PyBytes type of PyObject
29  // pointer that replaces PyString.
30  #define PyString_Check(str) PyBytes_Check(str)
31  #define PyString_FromString(str) PyBytes_FromString(str)
32  #define PyString_AsString(obj) PyBytes_AsString(obj)
33  #define PyString_AsStringAndSize(obj, str, len) PyBytes_AsStringAndSize(obj, str, len)
34 
35  // Python3 interprets all integers as long types and has deprecated PyInt.
36  #define PyInt_Check(x) PyLong_Check(x)
37  #define PyInt_AsLong(x) PyLong_AsLong(x)
38  #define PyInt_AS_LONG(x) PyLong_AsLong(x)
39  #define PyInt_AsSsize_t(x) PyLong_AsSsize_t(x)
40  #define PyInt_FromLong(x) PyLong_FromLong(x)
41 
42  // These flags are not present in Python3 and must be replaced with the
43  // default set of flags so that OR'ing them together doesn't alter the
44  // flags.
45  #define Py_TPFLAGS_CHECKTYPES Py_TPFLAGS_DEFAULT
46  #define Py_TPFLAGS_HAVE_RICHCOMPARE Py_TPFLAGS_DEFAULT
47 
48  // The __repr__ for a TypeObject will be encoded and needs to be
49  // processed as a PyBytes object before it can be return as a string.
50  #define PYUTIL_OBJECT_REPR(obj) PyObject_Str (PyObject_Repr (obj))
51 
52 #else
53 
54  // Python2 code will need to access PyObject_Repr() via this macro so
55  // that both 2&3 can compile without modification.
56  #define PYUTIL_OBJECT_REPR(obj) PyObject_Repr (obj)
57 
58 #endif
59 
60 #endif // _PyImathAPI_h_