1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
|
// This contains the implementation of the Q_ENUM, Q_ENUMS, Q_FLAG and Q_FLAGS
// support.
//
// Copyright (c) 2018 Riverbank Computing Limited <info@riverbankcomputing.com>
//
// This file is part of PyQt5.
//
// This file may be used under the terms of the GNU General Public License
// version 3.0 as published by the Free Software Foundation and appearing in
// the file LICENSE included in the packaging of this file. Please review the
// following information to ensure the GNU General Public License version 3.0
// requirements will be met: http://www.gnu.org/copyleft/gpl.html.
//
// If you do not wish to use this file under the terms of the GPL version 3.0
// then you may purchase a commercial license. For more information contact
// info@riverbankcomputing.com.
//
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#include <Python.h>
#include <QMultiHash>
#include "qpycore_chimera.h"
#include "qpycore_enums_flags.h"
#include "sipAPIQtCore.h"
// Forward declarations.
static bool add_enum_flag(PyObject *arg, bool flag, const char *context,
struct _frame *frame);
static void add_key_value(EnumFlag &enum_flag, PyObject *key, PyObject *value);
static struct _frame *get_calling_frame();
static bool objectify(const char *s, PyObject **objp);
static PyObject *parse_enum_flag(PyObject *arg, bool flag,
const char *context);
static PyObject *parse_enums_flags(PyObject *args, bool flags,
const char *context);
static bool trawl_members(PyObject *members, EnumFlag &enum_flag);
static void trawl_type_dict(PyObject *arg, EnumFlag &enum_flag);
// The enums and flags defined in each frame.
static QMultiHash<const struct _frame *, EnumFlag> enums_flags_hash;
// Add the given Q_ENUM() argument to the current enums/flags hash.
PyObject *qpycore_Enum(PyObject *arg)
{
return parse_enum_flag(arg, false, "Q_ENUM");
}
// Add the given Q_ENUMS() arguments to the current enums/flags hash.
PyObject *qpycore_Enums(PyObject *args)
{
return parse_enums_flags(args, false, "Q_ENUMS");
}
// Add the given Q_FLAG() arguments to the current enums/flags hash.
PyObject *qpycore_Flag(PyObject *arg)
{
return parse_enum_flag(arg, true, "Q_FLAG");
}
// Add the given Q_FLAGS() arguments to the current enums/flags hash.
PyObject *qpycore_Flags(PyObject *args)
{
return parse_enums_flags(args, true, "Q_FLAGS");
}
// Return the calling frame.
static struct _frame *get_calling_frame()
{
struct _frame *frame = sipGetFrame(1);
if (!frame)
{
PyErr_SetString(PyExc_RuntimeError, "no previous frame");
return 0;
}
return frame;
}
// Add the given Q_ENUM() or Q_FLAG() argument to the current enums/flags hash.
static PyObject *parse_enum_flag(PyObject *arg, bool flag, const char *context)
{
struct _frame *frame = get_calling_frame();
if (!frame)
return 0;
if (!add_enum_flag(arg, flag, context, frame))
return 0;
Py_INCREF(Py_None);
return Py_None;
}
// Add the given Q_ENUMS() or Q_FLAGS() arguments to the current enums/flags
// hash.
static PyObject *parse_enums_flags(PyObject *args, bool flags,
const char *context)
{
struct _frame *frame = get_calling_frame();
if (!frame)
return 0;
// Each argument is a separate enum/flag.
for (Py_ssize_t i = 0; i < PyTuple_Size(args); ++i)
{
PyObject *arg = PyTuple_GetItem(args, i);
if (!add_enum_flag(arg, flags, context, frame))
return 0;
}
Py_INCREF(Py_None);
return Py_None;
}
// Add the given enum/flag to the current hash. Return true if successfull.
static bool add_enum_flag(PyObject *arg, bool flag, const char *context,
struct _frame *frame)
{
// Check the argument's type is type. This will also pass Enums.
if (!PyType_Check(arg))
{
PyErr_Format(PyExc_TypeError,
"arguments to %s() must be type or enum.Enum objects",
context);
return false;
}
// Create the basic enum/flag.
EnumFlag enum_flag(sipPyTypeName((PyTypeObject *)arg), flag);
// See if it an Enum. We assume it is if it has a __members__ attribute.
static PyObject *members_s = 0;
if (!objectify("__members__", &members_s))
return false;
PyObject *members = PyObject_GetAttr(arg, members_s);
if (members)
{
bool ok = trawl_members(members, enum_flag);
Py_DECREF(members);
if (!ok)
return false;
enum_flag.isScoped = true;
}
else
{
trawl_type_dict(arg, enum_flag);
}
enums_flags_hash.insert(frame, enum_flag);
Chimera::registerPyEnum(arg);
// Make sure there are no exceptions left after failed value conversions.
PyErr_Clear();
return true;
}
// Trawl a __members__ mapping for int keys.
static bool trawl_members(PyObject *members, EnumFlag &enum_flag)
{
static PyObject *value_s = 0;
if (!objectify("value", &value_s))
return false;
PyObject *items;
Py_ssize_t nr_items;
// Get the contents of __members__.
#if PY_MAJOR_VERSION >= 3
items = PyMapping_Items(members);
#else
// Python v2 implements PyMapping_Items() as a macro that expands to the
// following - but without the const_cast. It isn't a problem for the
// version of MSVC supported by Python v2 but the const_cast is needed by
// later versions of MSVC and people will want to use them even though they
// are not supported.
items = PyObject_CallMethod(members, const_cast<char *>("items"), NULL);
#endif
if (!items)
goto return_error;
nr_items = PySequence_Length(items);
if (nr_items < 0)
goto release_items;
for (Py_ssize_t i = 0; i < nr_items; ++i)
{
PyObject *item, *key, *member, *value;
item = PySequence_GetItem(items, i);
if (!item)
goto release_items;
// The item should be a 2-element sequence of the key name and an
// object containing the value.
key = PySequence_GetItem(item, 0);
member = PySequence_GetItem(item, 1);
Py_DECREF(item);
if (!key || !member)
{
Py_XDECREF(key);
Py_XDECREF(member);
goto release_items;
}
// Get the value.
value = PyObject_GetAttr(member, value_s);
Py_DECREF(member);
if (!value)
{
Py_DECREF(key);
goto release_items;
}
add_key_value(enum_flag, key, value);
Py_DECREF(key);
Py_DECREF(value);
}
Py_DECREF(items);
return true;
release_items:
Py_DECREF(items);
return_error:
return false;
}
// Trawl the dictionary of a type looking for int attributes.
static void trawl_type_dict(PyObject *arg, EnumFlag &enum_flag)
{
Py_ssize_t pos = 0;
PyObject *key, *value, *dict;
dict = sipPyTypeDict((PyTypeObject *)arg);
while (PyDict_Next(dict, &pos, &key, &value))
add_key_value(enum_flag, key, value);
}
// Add a key/value to an enum/flag.
static void add_key_value(EnumFlag &enum_flag, PyObject *key, PyObject *value)
{
PyErr_Clear();
int i_value = sipLong_AsInt(value);
if (!PyErr_Occurred())
{
const char *s_key = sipString_AsASCIIString(&key);
if (s_key)
{
enum_flag.keys.insert(s_key, i_value);
Py_DECREF(key);
}
}
}
// Convert an ASCII string to a Python object if it hasn't already been done.
static bool objectify(const char *s, PyObject **objp)
{
if (*objp == NULL)
{
#if PY_MAJOR_VERSION >= 3
*objp = PyUnicode_FromString(s);
#else
*objp = PyString_FromString(s);
#endif
if (*objp == NULL)
return false;
}
return true;
}
// Return the current enums/flags list.
QList<EnumFlag> qpycore_get_enums_flags_list()
{
struct _frame *frame = sipGetFrame(0);
QList<EnumFlag> enums_flags_list = enums_flags_hash.values(frame);
enums_flags_hash.remove(frame);
return enums_flags_list;
}
|