1 /****************************************************************************
2 **
3 ** Copyright (C) 2018 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt for Python.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #ifndef SBKPYTHON_H
41 #define SBKPYTHON_H
42 
43 #include "sbkversion.h"
44 #define PyEnumMeta_Check(x) (strcmp(Py_TYPE(x)->tp_name, "EnumMeta") == 0)
45 
46 // Qt's "slots" macro collides with the "slots" member variables
47 // used in some Python structs. For compilers that support push_macro,
48 // temporarily undefine it.
49 #if defined(slots) && (defined(__GNUC__) || defined(_MSC_VER) || defined(__clang__))
50 #  pragma push_macro("slots")
51 #  undef slots
52 /*
53  * Python 2 has function _Py_Mangle directly in Python.h .
54  * This creates wrong language binding unless we define 'extern "C"' here.
55  */
56 extern "C" {
57 /*
58  * Python 2 uses the "register" keyword, which is deprecated in C++ 11
59  * and forbidden in C++17.
60  */
61 #  if defined(__clang__)
62 #    pragma clang diagnostic push
63 #    pragma clang diagnostic ignored "-Wdeprecated-register"
64 #  endif
65 
66 #  include <Python.h>
67 
68 #  if defined(__clang__)
69 #    pragma clang diagnostic pop
70 #  endif
71 }
72 #  include <structmember.h>
73 // Now we have the usual variables from Python.h .
74 #  include "python25compat.h"
75 #  include "shibokenmacros.h"
76 // "pep384impl.h" may nowhere be included but in this file.
77 #  include "pep384impl.h"
78 #  include "typespec.h"
79 #  pragma pop_macro("slots")
80 
81 #else
82 
83 extern "C" {
84 /*
85  * Python 2 uses the "register" keyword, which is deprecated in C++ 11
86  * and forbidden in C++17.
87  */
88 #  if defined(__clang__)
89 #    pragma clang diagnostic push
90 #    pragma clang diagnostic ignored "-Wdeprecated-register"
91 #  endif
92 
93 #  include <Python.h>
94 
95 #  if defined(__clang__)
96 #    pragma clang diagnostic pop
97 #  endif
98 }
99 #  include <structmember.h>
100 // Now we have the usual variables from Python.h .
101 #  include "python25compat.h"
102 #  include "shibokenmacros.h"
103 // "pep384impl.h" may nowhere be included but in this file.
104 #  include "pep384impl.h"
105 #  include "typespec.h"
106 #endif
107 
108 #if PY_MAJOR_VERSION >= 3
109     #define IS_PY3K
110 
111     #define PyInt_Type PyLong_Type
112     #define PyInt_Check PyLong_Check
113     #define PyInt_CheckExact PyLong_CheckExact
114     #define PyInt_FromString PyLong_FromString
115     #define PyInt_FromSsize_t PyLong_FromSsize_t
116     #define PyInt_FromSize_t PyLong_FromSize_t
117     #define PyInt_AS_LONG PyLong_AS_LONG
118     #define PyInt_AsUnsignedLongLongMask PyLong_AsLongLong
119     #define PyInt_FromLong PyLong_FromLong
120     #define PyInt_AsLong PyLong_AsLong
121     #define SbkNumber_Check PyNumber_Check
122     #define Py_TPFLAGS_CHECKTYPES  0
123 
124     #define SBK_NB_BOOL(x) (x).nb_bool
125     #define SBK_PyMethod_New PyMethod_New
126     #define PyInt_AsSsize_t(x)  PyLong_AsSsize_t(x)
127     #define PyString_Type PyUnicode_Type
128 
129     // In Python 3, Py_TPFLAGS_DEFAULT contains Py_TPFLAGS_HAVE_VERSION_TAG,
130     // which will trigger the attribute cache, which is not intended in Qt for Python.
131     // Use a customized Py_TPFLAGS_DEFAULT by defining Py_TPFLAGS_HAVE_VERSION_TAG = 0.
132     #undef Py_TPFLAGS_HAVE_VERSION_TAG
133     #define Py_TPFLAGS_HAVE_VERSION_TAG  (0)
134 
135 #else
136     // Note: if there wasn't for the old-style classes, only a PyNumber_Check would suffice.
137     #define SbkNumber_Check(X) \
138             (PyNumber_Check(X) && (!PyInstance_Check(X) || PyObject_HasAttrString(X, "__trunc__")))
139     #define SBK_NB_BOOL(x) (x).nb_nonzero
140     #define SBK_PyMethod_New(X, Y) PyMethod_New(X, Y, reinterpret_cast<PyObject *>(Py_TYPE(Y)))
141 
142     #define Py_hash_t long
143 #endif
144 
145 #endif
146