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 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
|
/****************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt for Python.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef PEP384IMPL_H
#define PEP384IMPL_H
// PYSIDE-1436: Adapt to Python 3.10
#if PY_VERSION_HEX < 0x030900A4
# define Py_SET_REFCNT(obj, refcnt) ((Py_REFCNT(obj) = (refcnt)), (void)0)
#endif
extern "C"
{
/*****************************************************************************
*
* RESOLVED: memoryobject.h
*
*/
// Extracted into bufferprocs27.h
#ifdef Py_LIMITED_API
#include "bufferprocs_py37.h"
#endif
/*****************************************************************************
*
* RESOLVED: object.h
*
*/
#ifdef Py_LIMITED_API
// Why the hell is this useful debugging function not allowed?
// BTW: When used, it breaks on Windows, intentionally!
LIBSHIBOKEN_API void _PyObject_Dump(PyObject *);
#endif
/*
* There are a few structures that are needed, but cannot be used without
* breaking the API. We use some heuristics to get those fields anyway
* and validate that we really found them, see pep384impl.cpp .
*/
#ifdef Py_LIMITED_API
/*
* These are the type object fields that we use.
* We will verify that they never change.
* The unused fields are intentionally named as "void *Xnn" because
* the chance is smaller to forget to validate a field.
* When we need more fields, we replace it back and add it to the
* validation.
*/
typedef struct _typeobject {
PyVarObject ob_base;
const char *tp_name;
Py_ssize_t tp_basicsize;
void *X03; // Py_ssize_t tp_itemsize;
destructor tp_dealloc;
void *X05; // Py_ssize_t tp_vectorcall_offset;
void *X06; // getattrfunc tp_getattr;
void *X07; // setattrfunc tp_setattr;
void *X08; // PyAsyncMethods *tp_as_async;
reprfunc tp_repr;
void *X10; // PyNumberMethods *tp_as_number;
void *X11; // PySequenceMethods *tp_as_sequence;
void *X12; // PyMappingMethods *tp_as_mapping;
void *X13; // hashfunc tp_hash;
ternaryfunc tp_call;
reprfunc tp_str;
getattrofunc tp_getattro;
setattrofunc tp_setattro;
void *X18; // PyBufferProcs *tp_as_buffer;
unsigned long tp_flags;
void *X20; // const char *tp_doc;
traverseproc tp_traverse;
inquiry tp_clear;
void *X23; // richcmpfunc tp_richcompare;
Py_ssize_t tp_weaklistoffset;
void *X25; // getiterfunc tp_iter;
iternextfunc tp_iternext;
struct PyMethodDef *tp_methods;
struct PyMemberDef *tp_members;
struct PyGetSetDef *tp_getset;
struct _typeobject *tp_base;
PyObject *tp_dict;
descrgetfunc tp_descr_get;
descrsetfunc tp_descr_set;
Py_ssize_t tp_dictoffset;
initproc tp_init;
allocfunc tp_alloc;
newfunc tp_new;
freefunc tp_free;
inquiry tp_is_gc; /* For PyObject_IS_GC */
PyObject *tp_bases;
PyObject *tp_mro; /* method resolution order */
} PyTypeObject;
#ifndef PyObject_IS_GC
/* Test if an object has a GC head */
#define PyObject_IS_GC(o) \
(PyType_IS_GC(Py_TYPE(o)) \
&& (Py_TYPE(o)->tp_is_gc == NULL || Py_TYPE(o)->tp_is_gc(o)))
#endif
// This was a macro error in the limited API from the beginning.
// It was fixed in Python master, but did make it only into Python 3.8 .
// PYSIDE-1797: This must be a runtime decision.
// Remove that when the minimum Python version is 3.8,
// because the macro PyIndex_Check bug was fixed then.
/// FIXME: Remove PyIndex_Check and pep384_issue33738.cpp when Python 3.7 is gone.
#undef PyIndex_Check
LIBSHIBOKEN_API int PyIndex_Check(PyObject *obj);
#endif // Py_LIMITED_API
struct SbkObjectTypePrivate;
struct PySideQFlagsTypePrivate;
struct _SbkGenericTypePrivate;
#define PepHeapType_SIZE \
(reinterpret_cast<PyTypeObject *>(&PyType_Type)->tp_basicsize)
#define _genericTypeExtender(etype) \
(reinterpret_cast<char *>(etype) + PepHeapType_SIZE)
#define PepType_SOTP(etype) \
(*reinterpret_cast<SbkObjectTypePrivate **>(_genericTypeExtender(etype)))
#define PepType_SETP(etype) \
(reinterpret_cast<SbkEnumTypePrivate *>(_genericTypeExtender(etype)))
#define PepType_PFTP(etype) \
(reinterpret_cast<PySideQFlagsTypePrivate *>(_genericTypeExtender(etype)))
#define PepType_SGTP(etype) \
(reinterpret_cast<_SbkGenericTypePrivate *>(_genericTypeExtender(etype)))
// functions used everywhere
LIBSHIBOKEN_API const char *PepType_GetNameStr(PyTypeObject *type);
/*****************************************************************************
*
* RESOLVED: longobject.h
*
*/
#ifdef Py_LIMITED_API
LIBSHIBOKEN_API int _PepLong_AsInt(PyObject *);
#else
#define _PepLong_AsInt _PyLong_AsInt
#endif
/*****************************************************************************
*
* RESOLVED: pydebug.h
*
*/
#ifdef Py_LIMITED_API
/*
* We have no direct access to Py_VerboseFlag because debugging is not
* supported. The python developers are partially a bit too rigorous.
* Instead, we compute the value and use a function call macro.
* Was before: extern LIBSHIBOKEN_API int Py_VerboseFlag;
*/
LIBSHIBOKEN_API int Pep_GetFlag(const char *name);
LIBSHIBOKEN_API int Pep_GetVerboseFlag(void);
#define Py_VerboseFlag Pep_GetVerboseFlag()
#endif
/*****************************************************************************
*
* RESOLVED: unicodeobject.h
*
*/
///////////////////////////////////////////////////////////////////////
//
// PYSIDE-813: About The Length Of Unicode Objects
// -----------------------------------------------
//
// In Python 2 and before Python 3.3, the macro PyUnicode_GET_SIZE
// worked fine and really like a macro.
//
// Meanwhile, the unicode objects have changed their layout very much,
// and the former cheap macro call has become a real function call
// that converts objects and needs PyMemory.
//
// That is not only inefficient, but also requires the GIL!
// This problem was visible by debug Python and qdatastream_test.py .
// It was found while fixing the refcount problem of PYSIDE-813 which
// needed a debug Python.
//
// PyUnicode_GetSize is deprecated in favor of PyUnicode_GetLength.
#if PY_VERSION_HEX < 0x03000000
#define PepUnicode_GetLength(op) PyUnicode_GetSize((PyObject *)(op))
#else
#define PepUnicode_GetLength(op) PyUnicode_GetLength((PyObject *)(op))
#endif
#ifdef Py_LIMITED_API
LIBSHIBOKEN_API char *_PepUnicode_AsString(PyObject *);
#else
#define _PepUnicode_AsString PyUnicode_AsUTF8
#endif
/*****************************************************************************
*
* RESOLVED: bytesobject.h
*
*/
#ifdef Py_LIMITED_API
#define PyBytes_AS_STRING(op) PyBytes_AsString(op)
#define PyBytes_GET_SIZE(op) PyBytes_Size(op)
#endif
/*****************************************************************************
*
* RESOLVED: floatobject.h
*
*/
#ifdef Py_LIMITED_API
#define PyFloat_AS_DOUBLE(op) PyFloat_AsDouble(op)
#endif
/*****************************************************************************
*
* RESOLVED: tupleobject.h
*
*/
#ifdef Py_LIMITED_API
#define PyTuple_GET_ITEM(op, i) PyTuple_GetItem((PyObject *)op, i)
#define PyTuple_SET_ITEM(op, i, v) PyTuple_SetItem(op, i, v)
#define PyTuple_GET_SIZE(op) PyTuple_Size((PyObject *)op)
#endif
/*****************************************************************************
*
* RESOLVED: listobject.h
*
*/
#ifdef Py_LIMITED_API
#define PyList_GET_ITEM(op, i) PyList_GetItem(op, i)
#define PyList_SET_ITEM(op, i, v) PyList_SetItem(op, i, v)
#define PyList_GET_SIZE(op) PyList_Size(op)
#endif
/*****************************************************************************
*
* RESOLVED: dictobject.h
*
* PYSIDE-803, PYSIDE-813: We need PyDict_GetItemWithError in order to
* avoid the GIL.
*/
#if PY_VERSION_HEX < 0x03000000
LIBSHIBOKEN_API PyObject *PyDict_GetItemWithError(PyObject *mp, PyObject *key);
#endif
/*****************************************************************************
*
* RESOLVED: methodobject.h
*
*/
#ifdef Py_LIMITED_API
typedef struct _pycfunc PyCFunctionObject;
#define PyCFunction_GET_FUNCTION(func) PyCFunction_GetFunction((PyObject *)func)
#define PyCFunction_GET_SELF(func) PyCFunction_GetSelf((PyObject *)func)
#define PyCFunction_GET_FLAGS(func) PyCFunction_GetFlags((PyObject *)func)
#define PepCFunction_GET_NAMESTR(func) \
_PepUnicode_AsString(PyObject_GetAttrString((PyObject *)func, "__name__"))
#else
#define PepCFunction_GET_NAMESTR(func) ((func)->m_ml->ml_name)
#endif
/*****************************************************************************
*
* RESOLVED: pythonrun.h
*
*/
#ifdef Py_LIMITED_API
LIBSHIBOKEN_API PyObject *PyRun_String(const char *, int, PyObject *, PyObject *);
#endif
/*****************************************************************************
*
* RESOLVED: abstract.h
*
*/
#ifdef Py_LIMITED_API
// This definition breaks the limited API a little, because it re-enables the
// buffer functions.
// But this is no problem as we check it's validity for every version.
// PYSIDE-1960 The buffer interface is since Python 3.11 part of the stable
// API and we do not need to check the compatibility by hand anymore.
typedef struct {
getbufferproc bf_getbuffer;
releasebufferproc bf_releasebuffer;
} PyBufferProcs;
typedef struct _Pepbuffertype {
PyVarObject ob_base;
void *skip[17];
PyBufferProcs *tp_as_buffer;
} PepBufferType;
#define PepType_AS_BUFFER(type) \
reinterpret_cast<PepBufferType *>(type)->tp_as_buffer
#define PyObject_CheckBuffer(obj) \
((PepType_AS_BUFFER(Py_TYPE(obj)) != NULL) && \
(PepType_AS_BUFFER(Py_TYPE(obj))->bf_getbuffer != NULL))
LIBSHIBOKEN_API int PyObject_GetBuffer(PyObject *ob, Pep_buffer *view, int flags);
LIBSHIBOKEN_API void PyBuffer_Release(Pep_buffer *view);
#else
#define Pep_buffer Py_buffer
#define PepType_AS_BUFFER(type) ((type)->tp_as_buffer)
#endif /* Py_LIMITED_API */
/*****************************************************************************
*
* RESOLVED: funcobject.h
*
*/
#ifdef Py_LIMITED_API
typedef struct _func PyFunctionObject;
extern LIBSHIBOKEN_API PyTypeObject *PepFunction_TypePtr;
LIBSHIBOKEN_API PyObject *PepFunction_Get(PyObject *, const char *);
#define PyFunction_Check(op) (Py_TYPE(op) == PepFunction_TypePtr)
#define PyFunction_GET_CODE(func) PyFunction_GetCode(func)
#define PyFunction_GetCode(func) PepFunction_Get((PyObject *)func, "__code__")
#define PepFunction_GetName(func) PepFunction_Get((PyObject *)func, "__name__")
#else
#define PepFunction_TypePtr (&PyFunction_Type)
#define PepFunction_GetName(func) (((PyFunctionObject *)func)->func_name)
#endif
/*****************************************************************************
*
* RESOLVED: classobject.h
*
*/
#ifdef Py_LIMITED_API
typedef struct _meth PyMethodObject;
extern LIBSHIBOKEN_API PyTypeObject *PepMethod_TypePtr;
LIBSHIBOKEN_API PyObject *PyMethod_New(PyObject *, PyObject *);
LIBSHIBOKEN_API PyObject *PyMethod_Function(PyObject *);
LIBSHIBOKEN_API PyObject *PyMethod_Self(PyObject *);
#define PyMethod_Check(op) ((op)->ob_type == PepMethod_TypePtr)
#define PyMethod_GET_SELF(op) PyMethod_Self(op)
#define PyMethod_GET_FUNCTION(op) PyMethod_Function(op)
#endif
/*****************************************************************************
*
* RESOLVED: code.h
*
*/
#ifdef Py_LIMITED_API
/* Bytecode object */
// we have to grab the code object from python
typedef struct _code PepCodeObject;
LIBSHIBOKEN_API int PepCode_Get(PepCodeObject *co, const char *name);
# define PepCode_GET_FLAGS(o) PepCode_Get(o, "co_flags")
# define PepCode_GET_ARGCOUNT(o) PepCode_Get(o, "co_argcount")
/* Masks for co_flags above */
# define CO_OPTIMIZED 0x0001
# define CO_NEWLOCALS 0x0002
# define CO_VARARGS 0x0004
# define CO_VARKEYWORDS 0x0008
# define CO_NESTED 0x0010
# define CO_GENERATOR 0x0020
#else
# define PepCodeObject PyCodeObject
# define PepCode_GET_FLAGS(o) ((o)->co_flags)
# define PepCode_GET_ARGCOUNT(o) ((o)->co_argcount)
#endif
/*****************************************************************************
*
* RESOLVED: datetime.h
*
*/
#ifdef Py_LIMITED_API
LIBSHIBOKEN_API int PyDateTime_Get(PyObject *ob, const char *name);
#define PyDateTime_GetYear(o) PyDateTime_Get(o, "year")
#define PyDateTime_GetMonth(o) PyDateTime_Get(o, "month")
#define PyDateTime_GetDay(o) PyDateTime_Get(o, "day")
#define PyDateTime_GetHour(o) PyDateTime_Get(o, "hour")
#define PyDateTime_GetMinute(o) PyDateTime_Get(o, "minute")
#define PyDateTime_GetSecond(o) PyDateTime_Get(o, "second")
#define PyDateTime_GetMicrosecond(o) PyDateTime_Get(o, "microsecond")
#define PyDateTime_GetFold(o) PyDateTime_Get(o, "fold")
#define PyDateTime_GET_YEAR(o) PyDateTime_GetYear(o)
#define PyDateTime_GET_MONTH(o) PyDateTime_GetMonth(o)
#define PyDateTime_GET_DAY(o) PyDateTime_GetDay(o)
#define PyDateTime_DATE_GET_HOUR(o) PyDateTime_GetHour(o)
#define PyDateTime_DATE_GET_MINUTE(o) PyDateTime_GetMinute(o)
#define PyDateTime_DATE_GET_SECOND(o) PyDateTime_GetSecond(o)
#define PyDateTime_DATE_GET_MICROSECOND(o) PyDateTime_GetMicrosecond(o)
#define PyDateTime_DATE_GET_FOLD(o) PyDateTime_GetFold(o)
#define PyDateTime_TIME_GET_HOUR(o) PyDateTime_GetHour(o)
#define PyDateTime_TIME_GET_MINUTE(o) PyDateTime_GetMinute(o)
#define PyDateTime_TIME_GET_SECOND(o) PyDateTime_GetSecond(o)
#define PyDateTime_TIME_GET_MICROSECOND(o) PyDateTime_GetMicrosecond(o)
#define PyDateTime_TIME_GET_FOLD(o) PyDateTime_GetFold(o)
/* Define structure slightly similar to C API. */
typedef struct {
PyObject *module;
/* type objects */
PyTypeObject *DateType;
PyTypeObject *DateTimeType;
PyTypeObject *TimeType;
PyTypeObject *DeltaType;
PyTypeObject *TZInfoType;
} datetime_struc;
LIBSHIBOKEN_API datetime_struc *init_DateTime(void);
#define PyDateTime_IMPORT PyDateTimeAPI = init_DateTime()
extern LIBSHIBOKEN_API datetime_struc *PyDateTimeAPI;
#define PyDate_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->DateType)
#define PyDateTime_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->DateTimeType)
#define PyTime_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->TimeType)
LIBSHIBOKEN_API PyObject *PyDate_FromDate(int year, int month, int day);
LIBSHIBOKEN_API PyObject *PyDateTime_FromDateAndTime(
int year, int month, int day, int hour, int min, int sec, int usec);
LIBSHIBOKEN_API PyObject *PyTime_FromTime(
int hour, int minute, int second, int usecond);
#endif /* Py_LIMITED_API */
/*****************************************************************************
*
* Extra support for name mangling
*
*/
// PYSIDE-772: This function supports the fix, but is not meant as public.
LIBSHIBOKEN_API PyObject *_Pep_PrivateMangle(PyObject *self, PyObject *name);
/*****************************************************************************
*
* Extra support for signature.cpp
*
*/
#ifdef Py_LIMITED_API
extern LIBSHIBOKEN_API PyTypeObject *PepStaticMethod_TypePtr;
LIBSHIBOKEN_API PyObject *PyStaticMethod_New(PyObject *callable);
#else
#define PepStaticMethod_TypePtr &PyStaticMethod_Type
#endif
// Although not PEP specific, we resolve this similar issue, here:
#if PY_VERSION_HEX < 0x03000000
extern LIBSHIBOKEN_API PyTypeObject *PepMethodDescr_TypePtr;
#else
#define PepMethodDescr_TypePtr &PyMethodDescr_Type
#endif
/*****************************************************************************
*
* Newly introduced convenience functions
*
* This is not defined if Py_LIMITED_API is defined.
*/
#if PY_VERSION_HEX < 0x03070000 || defined(Py_LIMITED_API)
LIBSHIBOKEN_API PyObject *PyImport_GetModule(PyObject *name);
#endif // PY_VERSION_HEX < 0x03070000 || defined(Py_LIMITED_API)
// Evaluate a script and return the variable `result`
LIBSHIBOKEN_API PyObject *PepRun_GetResult(const char *command);
/*****************************************************************************
*
* Python 2 incompatibilities
*
* This is incompatibly implemented as macro in Python 2.
*/
#if PY_VERSION_HEX < 0x03000000
extern LIBSHIBOKEN_API PyObject *PepMapping_Items(PyObject *o);
#else
#define PepMapping_Items PyMapping_Items
#endif
/*****************************************************************************
*
* Runtime support for Python 3.8 incompatibilities
*
*/
#ifndef Py_TPFLAGS_METHOD_DESCRIPTOR
/* Objects behave like an unbound method */
#define Py_TPFLAGS_METHOD_DESCRIPTOR (1UL << 17)
#endif
extern LIBSHIBOKEN_API int PepRuntime_38_flag;
/*****************************************************************************
*
* Module Initialization
*
*/
LIBSHIBOKEN_API void Pep384_Init(void);
} // extern "C"
#endif // PEP384IMPL_H
|