From c150102ffb94cab1def2c0cd6fee64ae2463c82b Mon Sep 17 00:00:00 2001 From: Robin Dunn Date: Mon, 4 Jan 2021 15:36:14 -0800 Subject: [PATCH] Update siplib (cherry picked from commit f07c09c2931972cbc26ac6c3074efdba873614e5) --- sip/siplib/apiversions.c | 14 +- sip/siplib/array.c | 227 +---- sip/siplib/array.h | 6 +- sip/siplib/bool.cpp | 2 +- sip/siplib/descriptors.c | 20 +- sip/siplib/int_convertors.c | 22 +- sip/siplib/qtlib.c | 18 +- sip/siplib/sip.h | 472 ++------- sip/siplib/sipint.h | 19 +- sip/siplib/siplib.c | 1919 +++++++++-------------------------- sip/siplib/threads.c | 4 +- sip/siplib/voidptr.c | 417 +------- 12 files changed, 657 insertions(+), 2483 deletions(-) diff --git a/sip/siplib/apiversions.c b/sip/siplib/apiversions.c index 464b308e..692c4efb 100644 --- a/sip/siplib/apiversions.c +++ b/sip/siplib/apiversions.c @@ -1,7 +1,7 @@ /* * The implementation of the supprt for setting API versions. * - * Copyright (c) 2016 Riverbank Computing Limited + * Copyright (c) 2019 Riverbank Computing Limited * * This file is part of SIP. * @@ -124,7 +124,7 @@ int sipInitAPI(sipExportedModuleDef *em, PyObject *mod_dict) if ((pmd = sip_api_malloc(sizeof (PyMethodDef))) == NULL) return -1; - pmd->ml_name = SIP_MLNAME_CAST(func_name); + pmd->ml_name = func_name; pmd->ml_meth = vf->vf_function; pmd->ml_flags = vf->vf_flags; pmd->ml_doc = vf->vf_docstring; @@ -186,6 +186,9 @@ PyObject *sipGetAPI(PyObject *self, PyObject *args) (void)self; + if (sip_api_deprecated(NULL, "getapi") < 0) + return NULL; + if (!PyArg_ParseTuple(args, "s:getapi", &api)) return NULL; @@ -195,11 +198,7 @@ PyObject *sipGetAPI(PyObject *self, PyObject *args) return NULL; } -#if PY_MAJOR_VERSION >= 3 return PyLong_FromLong(avd->version_nr); -#else - return PyInt_FromLong(avd->version_nr); -#endif } @@ -214,6 +213,9 @@ PyObject *sipSetAPI(PyObject *self, PyObject *args) (void)self; + if (sip_api_deprecated(NULL, "setapi") < 0) + return NULL; + if (!PyArg_ParseTuple(args, "si:setapi", &api, &version_nr)) return NULL; diff --git a/sip/siplib/array.c b/sip/siplib/array.c index c157d938..5bb83a81 100644 --- a/sip/siplib/array.c +++ b/sip/siplib/array.c @@ -1,7 +1,7 @@ /* * This file implements the API for the array type. * - * Copyright (c) 2018 Riverbank Computing Limited + * Copyright (c) 2019 Riverbank Computing Limited * * This file is part of SIP. * @@ -34,33 +34,27 @@ typedef struct { const sipTypeDef *td; const char *format; size_t stride; - SIP_SSIZE_T len; + Py_ssize_t len; int flags; PyObject *owner; } sipArrayObject; static int check_writable(sipArrayObject *array); -static int check_index(sipArrayObject *array, SIP_SSIZE_T idx); +static int check_index(sipArrayObject *array, Py_ssize_t idx); static void *get_value(sipArrayObject *array, PyObject *value); -static void *get_slice(sipArrayObject *array, PyObject *value, - SIP_SSIZE_T len); -#if PY_VERSION_HEX < 0x02050000 -static void fix_bounds(int len, int *left, int *right); -#endif -#if PY_VERSION_HEX >= 0x02050000 +static void *get_slice(sipArrayObject *array, PyObject *value, Py_ssize_t len); static void bad_key(PyObject *key); -#endif -static void *element(sipArrayObject *array, SIP_SSIZE_T idx); +static void *element(sipArrayObject *array, Py_ssize_t idx); static PyObject *make_array(void *data, const sipTypeDef *td, - const char *format, size_t stride, SIP_SSIZE_T len, int flags, + const char *format, size_t stride, Py_ssize_t len, int flags, PyObject *owner); /* * Implement len() for the type. */ -static SIP_SSIZE_T sipArray_length(PyObject *self) +static Py_ssize_t sipArray_length(PyObject *self) { return ((sipArrayObject *)self)->len; } @@ -69,7 +63,7 @@ static SIP_SSIZE_T sipArray_length(PyObject *self) /* * Implement sequence item sub-script for the type. */ -static PyObject *sipArray_item(PyObject *self, SIP_SSIZE_T idx) +static PyObject *sipArray_item(PyObject *self, Py_ssize_t idx) { sipArrayObject *array = (sipArrayObject *)self; PyObject *py_item; @@ -89,7 +83,7 @@ static PyObject *sipArray_item(PyObject *self, SIP_SSIZE_T idx) switch (*array->format) { case 'b': - py_item = SIPLong_FromLong(*(char *)data); + py_item = PyLong_FromLong(*(char *)data); break; case 'B': @@ -97,7 +91,7 @@ static PyObject *sipArray_item(PyObject *self, SIP_SSIZE_T idx) break; case 'h': - py_item = SIPLong_FromLong(*(short *)data); + py_item = PyLong_FromLong(*(short *)data); break; case 'H': @@ -105,7 +99,7 @@ static PyObject *sipArray_item(PyObject *self, SIP_SSIZE_T idx) break; case 'i': - py_item = SIPLong_FromLong(*(int *)data); + py_item = PyLong_FromLong(*(int *)data); break; case 'I': @@ -129,91 +123,21 @@ static PyObject *sipArray_item(PyObject *self, SIP_SSIZE_T idx) } -#if PY_VERSION_HEX < 0x02050000 -/* - * Implement sequence slice sub-script for the type. - */ -static PyObject *sipArray_slice(PyObject *self, int left, int right) -{ - sipArrayObject *array = (sipArrayObject *)self; - - fix_bounds(array->len, &left, &right); - - if (left == right) - left = right = 0; - - return make_array(element(array, left), array->td, array->format, - array->stride, right - left, (array->flags & ~SIP_OWNS_MEMORY), - array->owner); -} - - -/* - * Implement sequence assignment item sub-script for the type. - */ -static int sipArray_ass_item(PyObject *self, int idx, PyObject *value) -{ - sipArrayObject *array = (sipArrayObject *)self; - void *value_data; - - if (check_writable(array) < 0 || check_index(array, idx) < 0) - return -1; - - if ((value_data = get_value(array, value)) == NULL) - return -1; - - memmove(element(array, idx), value_data, array->stride); - - return 0; -} - - -/* - * Implement sequence assignment slice sub-script for the type. - */ -static int sipArray_ass_slice(PyObject *self, int left, int right, - PyObject *value) -{ - sipArrayObject *array = (sipArrayObject *)self; - void *value_data; - - if (check_writable(array) < 0) - return -1; - - fix_bounds(array->len, &left, &right); - - if ((value_data = get_slice(array, value, right - left)) == NULL) - return -1; - - memmove(element(array, left), value_data, (right - left) * array->stride); - - return 0; -} -#endif - - /* The sequence methods data structure. */ static PySequenceMethods sipArray_SequenceMethods = { sipArray_length, /* sq_length */ 0, /* sq_concat */ 0, /* sq_repeat */ sipArray_item, /* sq_item */ -#if PY_VERSION_HEX >= 0x02050000 0, /* sq_slice */ 0, /* sq_ass_item */ 0, /* sq_ass_slice */ -#else - sipArray_slice, /* sq_slice */ - sipArray_ass_item, /* sq_ass_item */ - sipArray_ass_slice, /* sq_ass_slice */ -#endif 0, /* sq_contains */ 0, /* sq_inplace_concat */ 0, /* sq_inplace_repeat */ }; -#if PY_VERSION_HEX >= 0x02050000 /* * Implement mapping sub-script for the type. */ @@ -265,7 +189,7 @@ static int sipArray_ass_subscript(PyObject *self, PyObject *key, PyObject *value) { sipArrayObject *array = (sipArrayObject *)self; - SIP_SSIZE_T start, len; + Py_ssize_t start, len; void *value_data; if (check_writable(array) < 0) @@ -324,10 +248,8 @@ static PyMappingMethods sipArray_MappingMethods = { sipArray_subscript, /* mp_subscript */ sipArray_ass_subscript, /* mp_ass_subscript */ }; -#endif -#if PY_VERSION_HEX >= 0x02060300 /* * The buffer implementation for Python v2.6.3 and later. */ @@ -371,84 +293,12 @@ static int sipArray_getbuffer(PyObject *self, Py_buffer *view, int flags) return 0; } -#endif - - -#if PY_MAJOR_VERSION < 3 -/* - * The read buffer implementation for Python v2. - */ -static SIP_SSIZE_T sipArray_getreadbuffer(PyObject *self, SIP_SSIZE_T seg, - void **ptr) -{ - sipArrayObject *array = (sipArrayObject *)self; - - if (seg != 0) - { - PyErr_SetString(PyExc_SystemError, "invalid buffer segment"); - return -1; - } - - *ptr = array->data; - - return array->len; -} -#endif - - -#if PY_MAJOR_VERSION < 3 -/* - * The write buffer implementation for Python v2. - */ -static SIP_SSIZE_T sipArray_getwritebuffer(PyObject *self, SIP_SSIZE_T seg, - void **ptr) -{ - if (check_writable((sipArrayObject *)self) < 0) - return -1; - - return sipArray_getreadbuffer(self, seg, ptr); -} -#endif - - -#if PY_MAJOR_VERSION < 3 -/* - * The segment count implementation for Python v2. - */ -static SIP_SSIZE_T sipArray_getsegcount(PyObject *self, SIP_SSIZE_T *lenp) -{ - SIP_SSIZE_T segs, len; - - len = ((sipArrayObject *)self)->len; - segs = (len < 0 ? 0 : 1); - - if (lenp != NULL) - *lenp = len; - - return segs; -} -#endif /* The buffer methods data structure. */ static PyBufferProcs sipArray_BufferProcs = { -#if PY_MAJOR_VERSION >= 3 sipArray_getbuffer, /* bf_getbuffer */ 0 /* bf_releasebuffer */ -#else - sipArray_getreadbuffer, /* bf_getreadbuffer */ - sipArray_getwritebuffer, /* bf_getwritebuffer */ - sipArray_getsegcount, /* bf_getsegcount */ -#if PY_VERSION_HEX >= 0x02050000 - (charbufferproc)sipArray_getreadbuffer, /* bf_getcharbuffer */ -#if PY_VERSION_HEX >= 0x02060300 - sipArray_getbuffer, /* bf_getbuffer */ - 0 /* bf_releasebuffer */ -#endif -#else - (getcharbufferproc)sipArray_getreadbuffer /* bf_getcharbuffer */ -#endif -#endif }; @@ -474,15 +324,11 @@ PyTypeObject sipArray_Type = { 0, /* tp_print */ 0, /* tp_getattr */ 0, /* tp_setattr */ - 0, /* tp_reserved (Python v3), tp_compare (Python v2) */ + 0, /* tp_reserved */ 0, /* tp_repr */ 0, /* tp_as_number */ &sipArray_SequenceMethods, /* tp_as_sequence */ -#if PY_VERSION_HEX >= 0x02050000 &sipArray_MappingMethods, /* tp_as_mapping */ -#else - 0, /* tp_as_mapping */ -#endif 0, /* tp_hash */ 0, /* tp_call */ 0, /* tp_str */ @@ -520,11 +366,10 @@ PyTypeObject sipArray_Type = { 0, /* tp_subclasses */ 0, /* tp_weaklist */ 0, /* tp_del */ -#if PY_VERSION_HEX >= 0x02060000 0, /* tp_version_tag */ -#endif -#if PY_VERSION_HEX >= 0x03040000 0, /* tp_finalize */ +#if PY_VERSION_HEX >= 0x03080000 + 0, /* tp_vectorcall */ #endif }; @@ -547,7 +392,7 @@ static int check_writable(sipArrayObject *array) /* * Check that an index is valid for an array. */ -static int check_index(sipArrayObject *array, SIP_SSIZE_T idx) +static int check_index(sipArrayObject *array, Py_ssize_t idx) { if (idx >= 0 && idx < array->len) return 0; @@ -558,27 +403,6 @@ static int check_index(sipArrayObject *array, SIP_SSIZE_T idx) } -#if PY_VERSION_HEX < 0x02050000 -/* - * Fix the bounds of a slice in the same way that the Python buffer object - * does. - */ -static void fix_bounds(int len, int *left, int *right) -{ - if (*left < 0) - *left = 0; - else if (*left > len) - *left = len; - - if (*right < *left) - *right = *left; - else if (*right > len) - *right = len; -} -#endif - - -#if PY_VERSION_HEX >= 0x02050000 /* * Raise an exception about a bad sub-script key. */ @@ -588,13 +412,12 @@ static void bad_key(PyObject *key) "cannot index a sip.array object using '%s'", Py_TYPE(key)->tp_name); } -#endif /* * Get the address of an element of an array. */ -static void *element(sipArrayObject *array, SIP_SSIZE_T idx) +static void *element(sipArrayObject *array, Py_ssize_t idx) { return (unsigned char *)(array->data) + idx * array->stride; } @@ -686,7 +509,7 @@ static void *get_value(sipArrayObject *array, PyObject *value) /* * Get the address of an value that will be copied to an array slice. */ -static void *get_slice(sipArrayObject *array, PyObject *value, SIP_SSIZE_T len) +static void *get_slice(sipArrayObject *array, PyObject *value, Py_ssize_t len) { sipArrayObject *other = (sipArrayObject *)value; @@ -748,8 +571,7 @@ static void *get_slice(sipArrayObject *array, PyObject *value, SIP_SSIZE_T len) if (other->len != len) { PyErr_Format(PyExc_TypeError, - "the array being assigned must have length " SIP_SSIZE_T_FORMAT, - len); + "the array being assigned must have length %zd", len); return NULL; } @@ -757,13 +579,8 @@ static void *get_slice(sipArrayObject *array, PyObject *value, SIP_SSIZE_T len) if (other->stride == array->stride) { PyErr_Format(PyExc_TypeError, -#if PY_VERSION_HEX >= 0x02050000 "the array being assigned must have stride %zu", array->stride); -#else - "the array being assigned must have stride %ld", - (unsigned long)array->stride); -#endif return NULL; } @@ -776,7 +593,7 @@ static void *get_slice(sipArrayObject *array, PyObject *value, SIP_SSIZE_T len) * Do the work of creating an array. */ static PyObject *make_array(void *data, const sipTypeDef *td, - const char *format, size_t stride, SIP_SSIZE_T len, int flags, + const char *format, size_t stride, Py_ssize_t len, int flags, PyObject *owner) { sipArrayObject *array; @@ -812,7 +629,7 @@ static PyObject *make_array(void *data, const sipTypeDef *td, * short), "i" (int), "I" (unsigned int), "f" (float) or "d" (double). */ PyObject *sip_api_convert_to_array(void *data, const char *format, - SIP_SSIZE_T len, int flags) + Py_ssize_t len, int flags) { size_t stride; @@ -871,7 +688,7 @@ PyObject *sip_api_convert_to_array(void *data, const char *format, * Wrap an array of instances of a defined type. */ PyObject *sip_api_convert_to_typed_array(void *data, const sipTypeDef *td, - const char *format, size_t stride, SIP_SSIZE_T len, int flags) + const char *format, size_t stride, Py_ssize_t len, int flags) { if (data == NULL) { diff --git a/sip/siplib/array.h b/sip/siplib/array.h index 6df7ab80..fbe5525d 100644 --- a/sip/siplib/array.h +++ b/sip/siplib/array.h @@ -1,7 +1,7 @@ /* * This file defines the API for the array type. * - * Copyright (c) 2016 Riverbank Computing Limited + * Copyright (c) 2019 Riverbank Computing Limited * * This file is part of SIP. * @@ -34,9 +34,9 @@ extern "C" { extern PyTypeObject sipArray_Type; PyObject *sip_api_convert_to_array(void *data, const char *format, - SIP_SSIZE_T len, int flags); + Py_ssize_t len, int flags); PyObject *sip_api_convert_to_typed_array(void *data, const sipTypeDef *td, - const char *format, size_t stride, SIP_SSIZE_T len, int flags); + const char *format, size_t stride, Py_ssize_t len, int flags); #ifdef __cplusplus diff --git a/sip/siplib/bool.cpp b/sip/siplib/bool.cpp index 7c096d25..781babbf 100644 --- a/sip/siplib/bool.cpp +++ b/sip/siplib/bool.cpp @@ -18,5 +18,5 @@ // Set a C++ bool for the main C implementation of the module. extern "C" void sipSetBool(void *ptr, int val) { - *reinterpret_cast(ptr) = !!val; + *reinterpret_cast(ptr) = !!val; } diff --git a/sip/siplib/descriptors.c b/sip/siplib/descriptors.c index 17f701ad..06bc53f3 100644 --- a/sip/siplib/descriptors.c +++ b/sip/siplib/descriptors.c @@ -1,7 +1,7 @@ /* * The implementation of the different descriptors. * - * Copyright (c) 2016 Riverbank Computing Limited + * Copyright (c) 2019 Riverbank Computing Limited * * This file is part of SIP. * @@ -102,11 +102,10 @@ PyTypeObject sipMethodDescr_Type = { 0, /* tp_subclasses */ 0, /* tp_weaklist */ 0, /* tp_del */ -#if PY_VERSION_HEX >= 0x02060000 0, /* tp_version_tag */ -#endif -#if PY_VERSION_HEX >= 0x03040000 0, /* tp_finalize */ +#if PY_VERSION_HEX >= 0x03080000 + 0, /* tp_vectorcall */ #endif }; @@ -173,13 +172,7 @@ static PyObject *sipMethodDescr_repr(PyObject *self) { sipMethodDescr *md = (sipMethodDescr *)self; - return -#if PY_MAJOR_VERSION >= 3 - PyUnicode_FromFormat -#else - PyString_FromFormat -#endif - ("", md->pmd->ml_name); + return PyUnicode_FromFormat("", md->pmd->ml_name); } @@ -311,11 +304,10 @@ PyTypeObject sipVariableDescr_Type = { 0, /* tp_subclasses */ 0, /* tp_weaklist */ 0, /* tp_del */ -#if PY_VERSION_HEX >= 0x02060000 0, /* tp_version_tag */ -#endif -#if PY_VERSION_HEX >= 0x03040000 0, /* tp_finalize */ +#if PY_VERSION_HEX >= 0x03080000 + 0, /* tp_vectorcall */ #endif }; diff --git a/sip/siplib/int_convertors.c b/sip/siplib/int_convertors.c index fd774320..081ef0ba 100644 --- a/sip/siplib/int_convertors.c +++ b/sip/siplib/int_convertors.c @@ -1,7 +1,7 @@ /* * The implementation of the Python object to C/C++ integer convertors. * - * Copyright (c) 2018 Riverbank Computing Limited + * Copyright (c) 2019 Riverbank Computing Limited * * This file is part of SIP. * @@ -329,26 +329,6 @@ static unsigned long long_as_unsigned_long(PyObject *o, unsigned long max) } else { -#if PY_VERSION_HEX < 0x02040000 - /* - * Work around a bug in Python versions prior to v2.4 where an integer - * (or a named enum) causes an error. - */ - if (!PyLong_Check(o) && PyInt_Check(o)) - { - long v = PyInt_AsLong(o); - - if (v < 0) - { - raise_unsigned_overflow(max); - - return (unsigned long)-1; - } - - return v; - } -#endif - value = PyLong_AsUnsignedLongMask(o); } diff --git a/sip/siplib/qtlib.c b/sip/siplib/qtlib.c index 6f7f7481..dc457adf 100644 --- a/sip/siplib/qtlib.c +++ b/sip/siplib/qtlib.c @@ -2,7 +2,7 @@ * The SIP library code that implements the interface to the optional module * supplied Qt support. * - * Copyright (c) 2016 Riverbank Computing Limited + * Copyright (c) 2020 Riverbank Computing Limited * * This file is part of SIP. * @@ -169,13 +169,7 @@ PyObject *sip_api_invoke_slot_ex(const sipSlot *slot, PyObject *sigargs, return Py_None; } -#if PY_MAJOR_VERSION >= 3 - sfunc = PyMethod_New(slot->meth.mfunc, self); -#else - sfunc = PyMethod_New(slot->meth.mfunc, self, slot->meth.mclass); -#endif - - if (sfunc == NULL) + if ((sfunc = PyMethod_New(slot->meth.mfunc, self)) == NULL) { Py_XDECREF(sref); return NULL; @@ -218,7 +212,7 @@ PyObject *sip_api_invoke_slot_ex(const sipSlot *slot, PyObject *sigargs, { PyObject *nsa, *xtype, *xvalue, *xtb, *resobj; - if ((resobj = PyEval_CallObject(sfunc, sa)) != NULL) + if ((resobj = PyObject_CallObject(sfunc, sa)) != NULL) { Py_DECREF(sfunc); Py_XDECREF(sref); @@ -344,11 +338,7 @@ int sip_api_same_slot(const sipSlot *sp, PyObject *rxObj, const char *slot) return 0; return (sp->meth.mfunc == PyMethod_GET_FUNCTION(rxObj) - && sp->meth.mself == PyMethod_GET_SELF(rxObj) -#if PY_MAJOR_VERSION < 3 - && sp->meth.mclass == PyMethod_GET_CLASS(rxObj) -#endif - ); + && sp->meth.mself == PyMethod_GET_SELF(rxObj)); } /* See if they are wrapped C++ methods. */ diff --git a/sip/siplib/sip.h b/sip/siplib/sip.h index 09d2ab67..aa911aee 100644 --- a/sip/siplib/sip.h +++ b/sip/siplib/sip.h @@ -1,7 +1,7 @@ /* * The SIP module interface. * - * Copyright (c) 2018 Riverbank Computing Limited + * Copyright (c) 2020 Riverbank Computing Limited * * This file is part of SIP. * @@ -21,22 +21,11 @@ #define _SIP_H -/* - * This gets round a problem with Qt's moc and Python v2.3. Strictly speaking - * it's a Qt problem but later versions of Python include a fix for it so we - * might as well too. - */ -#undef slots - - #include -/* - * There is a mis-feature somewhere with the Borland compiler. This works - * around it. - */ -#if defined(__BORLANDC__) -#include +/* Sanity check on the Python version. */ +#if PY_VERSION_HEX < 0x03050000 +#error "This version of SIP requires Python v3.5 or later" #endif @@ -45,234 +34,35 @@ extern "C" { #endif -/* Sanity check on the Python version. */ -#if PY_VERSION_HEX < 0x02030000 -#error "This version of SIP requires Python v2.3 or later" -#endif +/* The patch version of this implementation of the ABI. */ +#define SIP_MODULE_PATCH_VERSION 1 /* - * Define the SIP version number. + * The changes to this version of the ABI. + * + * Preserve any current exception in the wrapper tp_dealloc functions. */ -#define SIP_VERSION 0x041310 -#define SIP_VERSION_STR "4.19.16" -/* - * Define the current API version number. SIP must handle modules with the - * same major number and with the same or earlier minor number. Whenever - * members are added to non-embedded data structures they must be appended and - * the minor number incremented. Whenever data structure members are removed - * or their offset changed then the major number must be incremented and the - * minor number set * to 0. - * - * History: - * - * 12.6 Added sip_api_long_as_size_t() to the public API. - * Added the '=' format character to sip_api_build_result(). - * Added the '=' format character to sip_api_parse_result_ex(). - * - * 12.5 Replaced the sipConvertFromSliceObject() macro with - * sip_api_convert_from_slice_object() in the public API. - * - * 12.4 Added sip_api_instance_destroyed_ex() to the private API. - * - * 12.3 Added SIP_TYPE_SCOPED_ENUM to the sipTypeDef flags. - * Added sip_api_convert_to_enum() to the public API. - * Added sip_api_convert_to_bool() to the public API. - * Added sip_api_long_as_char(), sip_api_long_as_signed_char(), - * sip_api_long_as_unsigned_char(), sip_api_long_as_short(), - * sip_api_long_as_unsigned_short(), sip_api_long_as_int(), - * sip_api_long_as_unsigned_int(), sip_api_long_as_long(), - * sip_api_long_as_unsigned_long(), sip_api_long_as_long_long(), - * sip_api_long_as_unsigned_long_long() to the public API. - * Deprecated sip_api_can_convert_to_enum(). - * - * 12.2 Added sip_api_print_object() to the public API. - * Renamed sip_api_common_dtor() to sip_api_instance_destroyed() and added - * it to the public API. - * Added sipEventType and sip_api_register_event_handler() to the public - * API. - * - * 12.1 Added sip_api_enable_gc() to the public API. - * - * 12.0 Added SIP_TYPE_LIMITED_API to the sipTypeDef flags. - * Added sip_api_py_type_dict() and sip_api_py_type_name() to the public - * API. - * Added sip_api_set_new_user_type_handler() to the public API. - * Added sip_api_is_user_type() to the public API. - * Added sip_api_set_type_user_data() and sip_api_get_type_user_data() to - * the public API. - * Added sip_api_set_user_object() and sip_api_get_user_object() to the - * public API. - * Added sip_api_get_method() and sip_api_from_method() to the public API. - * Added sip_api_get_c_function() to the public API. - * Added sip_api_get_date() and sip_api_from_date() to the public API. - * Added sip_api_get_datetime() and sip_api_from_datetime() to the public - * API. - * Added sip_api_get_time() and sip_api_from_time() to the public API. - * Added sip_api_get_frame() to the public API. - * Added sip_api_check_plugin_for_type() to the public API. - * Added sip_api_unicode_new(), sip_api_unicode_write() and - * sip_api_unicode_data() to the public API. - * Added sip_api_get_buffer_info() and sip_api_relese_buffer_info() to the - * public API. - * Added sip_api_call_procedure_method() to the public API. - * Added sip_api_is_owned_by_python() to the private API. - * Added sip_api_is_derived_class() to the private API. - * Removed the im_version member from sipImportedModuleDef. - * Removed the im_module member from sipImportedModuleDef. - * Removed the em_version member from sipExportedModuleDef. - * Removed the em_virthandlers member from sipExportedModuleDef. - * Re-ordered the API functions. - * - * 11.3 Added sip_api_get_interpreter() to the public API. - * - * 11.2 Added sip_api_get_reference() to the private API. - * - * 11.1 Added sip_api_invoke_slot_ex(). - * - * 11.0 Added the pyqt5QtSignal and pyqt5ClassTypeDef structures. - * Removed qt_interface from pyqt4ClassTypeDef. - * Added hack to pyqt4QtSignal. - * - * 10.1 Added ctd_final to sipClassTypeDef. - * Added ctd_init_mixin to sipClassTypeDef. - * Added sip_api_get_mixin_address() to the public API. - * Added sip_api_convert_from_new_pytype() to the public API. - * Added sip_api_convert_to_array() to the public API. - * Added sip_api_convert_to_typed_array() to the public API. - * Added sip_api_register_proxy_resolver() to the public API. - * Added sip_api_init_mixin() to the private API. - * Added qt_interface to pyqt4ClassTypeDef. - * - * 10.0 Added sip_api_set_destroy_on_exit(). - * Added sip_api_enable_autoconversion(). - * Removed sip_api_call_error_handler_old(). - * Removed sip_api_start_thread(). - * - * 9.2 Added sip_gilstate_t and SIP_RELEASE_GIL to the public API. - * Renamed sip_api_call_error_handler() to - * sip_api_call_error_handler_old(). - * Added the new sip_api_call_error_handler() to the private API. - * - * 9.1 Added the capsule type. - * Added the 'z' format character to sip_api_build_result(). - * Added the 'z', '!' and '$' format characters to - * sip_api_parse_result_ex(). - * - * 9.0 Changed the sipVariableGetterFunc signature. - * Added sip_api_parse_result_ex() to the private API. - * Added sip_api_call_error_handler() to the private API. - * Added em_virterrorhandlers to sipExportedModuleDef. - * Re-ordered the API functions. - * - * 8.1 Revised the sipVariableDef structure. - * sip_api_get_address() is now part of the public API. - * - * 8.0 Changed the size of the sipSimpleWrapper structure. - * Added sip_api_get_address(). - * - * 7.1 Added the 'H' format character to sip_api_parse_result(). - * Deprecated the 'D' format character of sip_api_parse_result(). - * - * 7.0 Added sip_api_parse_kwd_args(). - * Added sipErrorState, sip_api_add_exception(). - * The type initialisation function is now passed a dictionary of keyword - * arguments. - * All argument parsers now update a set of error messages rather than an - * argument count. - * The signatures of sip_api_no_function() and sip_api_no_method() have - * changed. - * Added ctd_docstring to sipClassTypeDef. - * Added vf_docstring to sipVersionedFunctionDef. - * - * 6.0 Added the sipContainerDef structure to define the contents of a class - * or mapped type. Restructured sipClassDef and sipMappedTypeDef - * accordingly. - * Added the 'r' format character to sip_api_parse_args(). - * Added the 'r' format character to sip_api_call_method() and - * sip_api_build_result(). - * Added the assignment, array and copy allocation helpers. - * - * 5.0 Added sip_api_is_api_enabled(). - * Renamed the td_version_nr member of sipTypeDef to be int and where -1 - * indicates it is not versioned. - * Added the em_versions member to sipExportedModuleDef. - * Added the em_versioned_functions member to sipExportedModuleDef. - * - * 4.0 Much refactoring. - * - * 3.8 Added sip_api_register_qt_metatype() and sip_api_deprecated(). - * Added qt_register_meta_type() to the Qt support API. - * The C/C++ names of enums and types are now always defined in the - * relevant structures and don't default to the Python name. - * Added the 'XE' format characters to sip_api_parse_args(). - * - * 3.7 Added sip_api_convert_from_const_void_ptr(), - * sip_api_convert_from_void_ptr_and_size() and - * sip_api_convert_from_const_void_ptr_and_size(). - * Added the 'g' and 'G' format characters (to replace the now deprecated - * 'a' and 'A' format characters) to sip_api_build_result(), - * sip_api_call_method() and sip_api_parse_result(). - * Added the 'k' and 'K' format characters (to replace the now deprecated - * 'a' and 'A' format characters) to sip_api_parse_args(). - * Added sip_api_invoke_slot(). - * Added sip_api_parse_type(). - * Added sip_api_is_exact_wrapped_type(). - * Added the td_assign and td_qt fields to the sipTypeDef structure. - * Added the mt_assign field to the sipMappedType structure. - * - * 3.6 Added the 'g' format character to sip_api_parse_args(). - * - * 3.5 Added the td_pickle field to the sipTypeDef structure. - * Added sip_api_transfer_break(). - * - * 3.4 Added qt_find_connection() to the Qt support API. - * Added sip_api_string_as_char(), sip_api_unicode_as_wchar(), - * sip_api_unicode_as_wstring(), sip_api_find_class(), - * sip_api_find_named_enum() and sip_api_parse_signature(). - * Added the 'A', 'w' and 'x' format characters to sip_api_parse_args(), - * sip_api_parse_result(), sip_api_build_result() and - * sip_api_call_method(). - * - * 3.3 Added sip_api_register_int_types(). - * - * 3.2 Added sip_api_export_symbol() and sip_api_import_symbol(). - * - * 3.1 Added sip_api_add_mapped_type_instance(). - * - * 3.0 Moved the Qt support out of the sip module and into PyQt. This is - * such a dramatic change that there is no point in attempting to maintain - * backwards compatibility. - * - * 2.0 Added the td_flags field to the sipTypeDef structure. - * Added the first_child, sibling_next, sibling_prev and parent fields to - * the sipWrapper structure. - * Added the td_traverse and td_clear fields to the sipTypeDef structure. - * Added the em_api_minor field to the sipExportedModuleDef structure. - * Added sip_api_bad_operator_arg(). - * Added sip_api_wrapper_check(). - * - * 1.1 Added support for __pos__ and __abs__. - * - * 1.0 Removed all deprecated parts of the API. - * Removed the td_proxy field from the sipTypeDef structure. - * Removed the create proxy function from the 'q' and 'y' format - * characters to sip_api_parse_args(). - * Removed sip_api_emit_to_slot(). - * Reworked the enum related structures. - * - * 0.2 Added the 'H' format character to sip_api_parse_args(). - * - * 0.1 Added sip_api_add_class_instance(). - * Added the 't' format character to sip_api_parse_args(). - * Deprecated the 'J' and 'K' format characters to sip_api_parse_result(). - * - * 0.0 Original version. - */ -#define SIP_API_MAJOR_NR 12 -#define SIP_API_MINOR_NR 6 +/* The ABI version implemented. */ +#define SIP_ABI_MAJOR_VERSION 12 +#define SIP_ABI_MINOR_VERSION 8 + +/* The version of the code generator. */ +#define SIP_VERSION 0x50500 +#define SIP_VERSION_STR "5.5.0" + +/* These are all dependent on the user-specified name of the sip module. */ +#define _SIP_MODULE_FQ_NAME "wx.siplib" +#define _SIP_MODULE_NAME "siplib" +#define _SIP_MODULE_SHARED 1 +#define _SIP_MODULE_ENTRY PyInit_siplib +#define _SIP_MODULE_LEGACY 0 + +/* Support the historical names. */ +#define SIP_API_MAJOR_NR SIP_ABI_MAJOR_VERSION +#define SIP_API_MINOR_NR SIP_ABI_MINOR_VERSION /* @@ -320,29 +110,16 @@ typedef unsigned int uint; #endif -/* Some Python compatibility stuff. */ -#if PY_VERSION_HEX >= 0x02050000 - -#define SIP_SSIZE_T Py_ssize_t -#define SIP_SSIZE_T_FORMAT "%zd" - -#define SIP_MLNAME_CAST(s) (s) -#define SIP_MLDOC_CAST(s) (s) -#define SIP_TPNAME_CAST(s) (s) - -#else - -#define SIP_SSIZE_T int -#define SIP_SSIZE_T_FORMAT "%d" - -#define SIP_MLNAME_CAST(s) ((char *)(s)) -#define SIP_MLDOC_CAST(s) ((char *)(s)) -#define SIP_TPNAME_CAST(s) ((char *)(s)) - -#endif - -#if PY_MAJOR_VERSION >= 3 +/* Remove in v5.1. */ +#define SIP_SSIZE_T Py_ssize_t +#define SIP_SSIZE_T_FORMAT "%zd" +#define SIP_USE_PYCAPSULE +#define SIP_MODULE_RETURN(v) return (v) +/* + * Remove in v5.1. These are undocumented and can be removed when PyQt5 drops + * support for Python v2. + */ #define SIPLong_Check PyLong_Check #define SIPLong_FromLong PyLong_FromLong #define SIPLong_AsLong PyLong_AsLong @@ -355,57 +132,6 @@ typedef unsigned int uint; #define SIPBytes_AS_STRING PyBytes_AS_STRING #define SIPBytes_GET_SIZE PyBytes_GET_SIZE -#if PY_MINOR_VERSION >= 1 -#define SIP_USE_PYCAPSULE -#endif - -#if PY_MINOR_VERSION < 2 -#define SIP_SUPPORT_PYCOBJECT -#endif - -#else - -#define SIPLong_Check PyInt_Check -#define SIPLong_FromLong PyInt_FromLong -#define SIPLong_AsLong PyInt_AsLong - -#define SIPBytes_Check PyString_Check -#define SIPBytes_FromString PyString_FromString -#define SIPBytes_FromStringAndSize PyString_FromStringAndSize -#define SIPBytes_AsString PyString_AsString -#define SIPBytes_Size PyString_Size -#define SIPBytes_AS_STRING PyString_AS_STRING -#define SIPBytes_GET_SIZE PyString_GET_SIZE - -#if PY_MINOR_VERSION >= 7 -#define SIP_USE_PYCAPSULE -#endif - -#define SIP_SUPPORT_PYCOBJECT - -#endif - -#if !defined(Py_REFCNT) -#define Py_REFCNT(ob) (((PyObject*)(ob))->ob_refcnt) -#endif - -#if !defined(Py_TYPE) -#define Py_TYPE(ob) (((PyObject*)(ob))->ob_type) -#endif - -#if !defined(PyVarObject_HEAD_INIT) -#define PyVarObject_HEAD_INIT(type, size) PyObject_HEAD_INIT(type) size, -#endif - - -#if defined(SIP_USE_PYCAPSULE) -#define SIPCapsule_FromVoidPtr(p, n) PyCapsule_New((p), (n), NULL) -#define SIPCapsule_AsVoidPtr(p, n) PyCapsule_GetPointer((p), (n)) -#else -#define SIPCapsule_FromVoidPtr(p, n) sipConvertFromVoidPtr((p)) -#define SIPCapsule_AsVoidPtr(p, n) sipConvertToVoidPtr((p)) -#endif - /* * The mask that can be passed to sipTrace(). @@ -508,17 +234,12 @@ typedef int (*sipFinalFunc)(PyObject *, void *, PyObject *, PyObject **); typedef void *(*sipAccessFunc)(sipSimpleWrapper *, AccessFuncOp); typedef int (*sipTraverseFunc)(void *, visitproc, void *); typedef int (*sipClearFunc)(void *); -#if PY_MAJOR_VERSION >= 3 typedef int (*sipGetBufferFuncLimited)(PyObject *, void *, sipBufferDef *); typedef void (*sipReleaseBufferFuncLimited)(PyObject *, void *); #if !defined(Py_LIMITED_API) typedef int (*sipGetBufferFunc)(PyObject *, void *, Py_buffer *, int); typedef void (*sipReleaseBufferFunc)(PyObject *, void *, Py_buffer *); #endif -#else -typedef SIP_SSIZE_T (*sipBufferFunc)(PyObject *, void *, SIP_SSIZE_T, void **); -typedef SIP_SSIZE_T (*sipSegCountFunc)(PyObject *, void *, SIP_SSIZE_T *); -#endif typedef void (*sipDeallocFunc)(sipSimpleWrapper *); typedef void *(*sipCastFunc)(void *, const sipTypeDef *); typedef const sipTypeDef *(*sipSubClassConvertFunc)(void **); @@ -527,9 +248,9 @@ typedef PyObject *(*sipConvertFromFunc)(void *, PyObject *); typedef void (*sipVirtErrorHandlerFunc)(sipSimpleWrapper *, sip_gilstate_t); typedef int (*sipVirtHandlerFunc)(sip_gilstate_t, sipVirtErrorHandlerFunc, sipSimpleWrapper *, PyObject *, ...); -typedef void (*sipAssignFunc)(void *, SIP_SSIZE_T, void *); -typedef void *(*sipArrayFunc)(SIP_SSIZE_T); -typedef void *(*sipCopyFunc)(const void *, SIP_SSIZE_T); +typedef void (*sipAssignFunc)(void *, Py_ssize_t, void *); +typedef void *(*sipArrayFunc)(Py_ssize_t); +typedef void *(*sipCopyFunc)(const void *, Py_ssize_t); typedef void (*sipReleaseFunc)(void *, int); typedef PyObject *(*sipPickleFunc)(void *); typedef int (*sipAttrGetterFunc)(const sipTypeDef *, PyObject *); @@ -537,9 +258,10 @@ typedef PyObject *(*sipVariableGetterFunc)(void *, PyObject *, PyObject *); typedef int (*sipVariableSetterFunc)(void *, PyObject *, PyObject *); typedef void *(*sipProxyResolverFunc)(void *); typedef int (*sipNewUserTypeFunc)(sipWrapperType *); +typedef void (*sipWrapperVisitorFunc)(sipSimpleWrapper *, void *); -#if !defined(Py_LIMITED_API) || PY_VERSION_HEX < 0x03020000 +#if !defined(Py_LIMITED_API) /* * The meta-type of a wrapper type. */ @@ -633,6 +355,7 @@ struct _sipWrapper { /* + * Removed in v5.1. * The meta-type of an enum type. (This is exposed only to support the * deprecated sipConvertFromNamedEnum() macro.) */ @@ -756,7 +479,7 @@ struct _sipBufferDef { void *bd_buffer; /* The length of the buffer. */ - SIP_SSIZE_T bd_length; + Py_ssize_t bd_length; /* Set if the buffer is read-only. */ int bd_readonly; @@ -777,7 +500,7 @@ struct _sipBufferInfoDef { PyObject *bi_obj; /* The length of the buffer in bytes. */ - SIP_SSIZE_T bi_len; + Py_ssize_t bi_len; /* The number of dimensions. */ int bi_ndim; @@ -808,11 +531,6 @@ struct _sipMethodDef { /* The bound object. */ PyObject *pm_self; - -#if PY_MAJOR_VERSION < 3 - /* The class. */ - PyObject *pm_class; -#endif }; @@ -866,9 +584,6 @@ typedef enum { typedef enum { str_slot, /* __str__ */ int_slot, /* __int__ */ -#if PY_MAJOR_VERSION < 3 - long_slot, /* __long__ */ -#endif float_slot, /* __float__ */ len_slot, /* __len__ */ contains_slot, /* __contains__ */ @@ -911,18 +626,13 @@ typedef enum { ne_slot, /* __ne__ */ gt_slot, /* __gt__ */ ge_slot, /* __ge__ */ -#if PY_MAJOR_VERSION < 3 - cmp_slot, /* __cmp__ */ -#endif bool_slot, /* __bool__, __nonzero__ */ neg_slot, /* __neg__ */ repr_slot, /* __repr__ */ hash_slot, /* __hash__ */ pos_slot, /* __pos__ */ abs_slot, /* __abs__ */ -#if PY_VERSION_HEX >= 0x02050000 index_slot, /* __index__ */ -#endif iter_slot, /* __iter__ */ next_slot, /* __next__ */ setattr_slot, /* __setattr__, __delattr__ */ @@ -1034,14 +744,8 @@ struct _sipTypeDef { /* The C/C++ name of the type. */ int td_cname; - /* - * The Python type object. This needs to be a union until we remove the - * deprecated sipClass_* macros. - */ - union { - PyTypeObject *td_py_type; - sipWrapperType *td_wrapper_type; - } u; + /* The Python type object. */ + PyTypeObject *td_py_type; /* Any additional fixed data generated by a plugin. */ void *td_plugin_data; @@ -1125,7 +829,6 @@ typedef struct _sipClassTypeDef { /* The clear function. */ sipClearFunc ctd_clear; -#if PY_MAJOR_VERSION >= 3 /* The get buffer function. */ #if defined(Py_LIMITED_API) sipGetBufferFuncLimited ctd_getbuffer; @@ -1139,19 +842,6 @@ typedef struct _sipClassTypeDef { #else sipReleaseBufferFunc ctd_releasebuffer; #endif -#else - /* The read buffer function. */ - sipBufferFunc ctd_readbuffer; - - /* The write buffer function. */ - sipBufferFunc ctd_writebuffer; - - /* The segment count function. */ - sipSegCountFunc ctd_segcount; - - /* The char buffer function. */ - sipBufferFunc ctd_charbuffer; -#endif /* The deallocation function. */ sipDeallocFunc ctd_dealloc; @@ -1252,6 +942,7 @@ typedef struct _sipExternalTypeDef { /* + * Remove in v5.1. * The information describing a mapped class. This (and anything that uses it) * is deprecated. */ @@ -1620,8 +1311,9 @@ typedef struct _sipTypeInstanceDef { /* + * Remove in v5.1. * Define a mapping between a wrapped type identified by a string and the - * corresponding Python type. This is deprecated. + * corresponding Python type. */ typedef struct _sipStringTypeClassMap { /* The type as a string. */ @@ -1633,8 +1325,9 @@ typedef struct _sipStringTypeClassMap { /* + * Remove in v5.1. * Define a mapping between a wrapped type identified by an integer and the - * corresponding Python type. This is deprecated. + * corresponding Python type. */ typedef struct _sipIntTypeClassMap { /* The type as an integer. */ @@ -1655,11 +1348,6 @@ typedef struct _sipPyMethod { /* Self if it is a bound method. */ PyObject *mself; - -#if PY_MAJOR_VERSION < 3 - /* The class. */ - PyObject *mclass; -#endif } sipPyMethod; @@ -1702,7 +1390,7 @@ typedef struct _sipAPIDef { PyTypeObject *api_voidptr_type; void (*api_bad_catcher_result)(PyObject *method); - void (*api_bad_length_for_slice)(SIP_SSIZE_T seqlen, SIP_SSIZE_T slicelen); + void (*api_bad_length_for_slice)(Py_ssize_t seqlen, Py_ssize_t slicelen); PyObject *(*api_build_result)(int *isErr, const char *fmt, ...); PyObject *(*api_call_method)(int *isErr, PyObject *method, const char *fmt, ...); @@ -1710,8 +1398,8 @@ typedef struct _sipAPIDef { sipSimpleWrapper *, PyObject *, const char *, ...); PyObject *(*api_connect_rx)(PyObject *txObj, const char *sig, PyObject *rxObj, const char *slot, int type); - SIP_SSIZE_T (*api_convert_from_sequence_index)(SIP_SSIZE_T idx, - SIP_SSIZE_T len); + Py_ssize_t (*api_convert_from_sequence_index)(Py_ssize_t idx, + Py_ssize_t len); int (*api_can_convert_to_type)(PyObject *pyObj, const sipTypeDef *td, int flags); void *(*api_convert_to_type)(PyObject *pyObj, const sipTypeDef *td, @@ -1749,9 +1437,9 @@ typedef struct _sipAPIDef { PyObject *(*api_convert_from_void_ptr)(void *val); PyObject *(*api_convert_from_const_void_ptr)(const void *val); PyObject *(*api_convert_from_void_ptr_and_size)(void *val, - SIP_SSIZE_T size); + Py_ssize_t size); PyObject *(*api_convert_from_const_void_ptr_and_size)(const void *val, - SIP_SSIZE_T size); + Py_ssize_t size); void *(*api_convert_to_void_ptr)(PyObject *obj); int (*api_export_symbol)(const char *name, void *sym); void *(*api_import_symbol)(const char *name); @@ -1772,12 +1460,12 @@ typedef struct _sipAPIDef { PyObject *(*api_convert_from_new_pytype)(void *cpp, PyTypeObject *py_type, sipWrapper *owner, sipSimpleWrapper **selfp, const char *fmt, ...); PyObject *(*api_convert_to_typed_array)(void *data, const sipTypeDef *td, - const char *format, size_t stride, SIP_SSIZE_T len, int flags); + const char *format, size_t stride, Py_ssize_t len, int flags); PyObject *(*api_convert_to_array)(void *data, const char *format, - SIP_SSIZE_T len, int flags); + Py_ssize_t len, int flags); int (*api_register_proxy_resolver)(const sipTypeDef *td, sipProxyResolverFunc resolver); - PyInterpreterState *(*api_get_interpreter)(); + PyInterpreterState *(*api_get_interpreter)(void); sipNewUserTypeFunc (*api_set_new_user_type_handler)(const sipTypeDef *, sipNewUserTypeFunc); void (*api_set_type_user_data)(sipWrapperType *, void *); @@ -1796,9 +1484,9 @@ typedef struct _sipAPIDef { int (*api_is_user_type)(const sipWrapperType *); struct _frame *(*api_get_frame)(int); int (*api_check_plugin_for_type)(const sipTypeDef *, const char *); - PyObject *(*api_unicode_new)(SIP_SSIZE_T, unsigned, int *, void **); + PyObject *(*api_unicode_new)(Py_ssize_t, unsigned, int *, void **); void (*api_unicode_write)(int, void *, int, unsigned); - void *(*api_unicode_data)(PyObject *, int *, SIP_SSIZE_T *); + void *(*api_unicode_data)(PyObject *, int *, Py_ssize_t *); int (*api_get_buffer_info)(PyObject *, sipBufferInfoDef *); void (*api_release_buffer_info)(sipBufferInfoDef *); PyObject *(*api_get_user_object)(const sipSimpleWrapper *); @@ -1871,7 +1559,15 @@ typedef struct _sipAPIDef { int (*api_init_mixin)(PyObject *self, PyObject *args, PyObject *kwds, const sipClassTypeDef *ctd); PyObject *(*api_get_reference)(PyObject *self, int key); + + /* + * The following are part of the public API. + */ int (*api_is_owned_by_python)(sipSimpleWrapper *); + + /* + * The following are not part of the public API. + */ int (*api_is_derived_class)(sipSimpleWrapper *); /* @@ -1935,12 +1631,22 @@ typedef struct _sipAPIDef { /* * The following are part of the public API. */ - int (*api_convert_from_slice_object)(PyObject *slice, SIP_SSIZE_T length, - SIP_SSIZE_T *start, SIP_SSIZE_T *stop, SIP_SSIZE_T *step, - SIP_SSIZE_T *slicelength); + int (*api_convert_from_slice_object)(PyObject *slice, Py_ssize_t length, + Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step, + Py_ssize_t *slicelength); size_t (*api_long_as_size_t)(PyObject *o); + void (*api_visit_wrappers)(sipWrapperVisitorFunc visitor, void *closure); + int (*api_register_exit_notifier)(PyMethodDef *md); + + /* + * The following are not part of the public API. + */ + PyObject *(*api_is_py_method_12_8)(sip_gilstate_t *gil, char *pymc, + sipSimpleWrapper **sipSelfp, const char *cname, const char *mname); } sipAPIDef; +const sipAPIDef *sip_init_library(PyObject *mod_dict); + /* * The API implementing the optional Qt support. @@ -2005,7 +1711,7 @@ typedef struct _sipQtAPI { #define SIP_ACCFUNC 0x0008 /* If there is an access function. */ #define SIP_NOT_IN_MAP 0x0010 /* If Python object is not in the map. */ -#if !defined(Py_LIMITED_API) || PY_VERSION_HEX < 0x03020000 +#if !defined(Py_LIMITED_API) #define SIP_PY_OWNED 0x0020 /* If owned by Python. */ #define SIP_SHARE_MAP 0x0040 /* If the map slot might be occupied. */ #define SIP_CPP_HAS_REF 0x0080 /* If C/C++ has a reference. */ @@ -2053,22 +1759,16 @@ typedef struct _sipQtAPI { #define sipTypeIsMapped(td) (((td)->td_flags & SIP_TYPE_TYPE_MASK) == SIP_TYPE_MAPPED) #define sipTypeIsEnum(td) (((td)->td_flags & SIP_TYPE_TYPE_MASK) == SIP_TYPE_ENUM) #define sipTypeIsScopedEnum(td) (((td)->td_flags & SIP_TYPE_TYPE_MASK) == SIP_TYPE_SCOPED_ENUM) -#define sipTypeAsPyTypeObject(td) ((td)->u.td_py_type) +#define sipTypeAsPyTypeObject(td) ((td)->td_py_type) #define sipTypeName(td) sipNameFromPool((td)->td_module, (td)->td_cname) #define sipTypePluginData(td) ((td)->td_plugin_data) /* - * Note that this was never actually documented as being part of the public - * API. It is now deprecated. sipIsUserType() should be used instead. - */ -#define sipIsExactWrappedType(wt) (sipTypeAsPyTypeObject((wt)->wt_td) == (PyTypeObject *)(wt)) - - -/* - * The following are deprecated parts of the public API. + * Remove in v5.1. */ #define sipClassName(w) PyString_FromString(Py_TYPE(w)->tp_name) +#define sipIsExactWrappedType(wt) (sipTypeAsPyTypeObject((wt)->wt_td) == (PyTypeObject *)(wt)) /* diff --git a/sip/siplib/sipint.h b/sip/siplib/sipint.h index 0637be08..63adf772 100644 --- a/sip/siplib/sipint.h +++ b/sip/siplib/sipint.h @@ -1,7 +1,7 @@ /* * This file defines the SIP library internal interfaces. * - * Copyright (c) 2017 Riverbank Computing Limited + * Copyright (c) 2020 Riverbank Computing Limited * * This file is part of SIP. * @@ -91,9 +91,9 @@ extern PyTypeObject sipVoidPtr_Type; void *sip_api_convert_to_void_ptr(PyObject *obj); PyObject *sip_api_convert_from_void_ptr(void *val); PyObject *sip_api_convert_from_const_void_ptr(const void *val); -PyObject *sip_api_convert_from_void_ptr_and_size(void *val, SIP_SSIZE_T size); +PyObject *sip_api_convert_from_void_ptr_and_size(void *val, Py_ssize_t size); PyObject *sip_api_convert_from_const_void_ptr_and_size(const void *val, - SIP_SSIZE_T size); + Py_ssize_t size); /* @@ -151,9 +151,10 @@ PyObject *sip_api_invoke_slot_ex(const sipSlot *slot, PyObject *sigargs, void *sip_api_convert_rx(sipWrapper *txSelf, const char *sigargs, PyObject *rxObj, const char *slot, const char **memberp, int flags); int sip_api_save_slot(sipSlot *sp, PyObject *rxObj, const char *slot); -int sip_api_convert_from_slice_object(PyObject *slice, SIP_SSIZE_T length, - SIP_SSIZE_T *start, SIP_SSIZE_T *stop, SIP_SSIZE_T *step, - SIP_SSIZE_T *slicelength); +int sip_api_convert_from_slice_object(PyObject *slice, Py_ssize_t length, + Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step, + Py_ssize_t *slicelength); +int sip_api_deprecated(const char *classname, const char *method); /* @@ -163,7 +164,7 @@ sipClassTypeDef *sipGetGeneratedClassType(const sipEncodedTypeDef *enc, const sipClassTypeDef *ctd); void sipSaveMethod(sipPyMethod *pm,PyObject *meth); int sipGetPending(void **pp, sipWrapper **op, int *fp); -int sipIsPending(); +int sipIsPending(void); PyObject *sipWrapInstance(void *cpp, PyTypeObject *py_type, PyObject *args, sipWrapper *owner, int flags); void *sipConvertRxEx(sipWrapper *txSelf, const char *sigargs, @@ -176,7 +177,9 @@ sipSimpleWrapper *sipOMFindObject(sipObjectMap *om, void *key, void sipOMAddObject(sipObjectMap *om, sipSimpleWrapper *val); int sipOMRemoveObject(sipObjectMap *om, sipSimpleWrapper *val); -void sipSetBool(void *ptr,int val); +#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L +#define sipSetBool(p, v) (*(_Bool *)(p) = (v)) +#endif #ifdef __cplusplus diff --git a/sip/siplib/siplib.c b/sip/siplib/siplib.c index db60870c..7e2ebdb3 100644 --- a/sip/siplib/siplib.c +++ b/sip/siplib/siplib.c @@ -1,7 +1,7 @@ /* * SIP library code. * - * Copyright (c) 2018 Riverbank Computing Limited + * Copyright (c) 2020 Riverbank Computing Limited * * This file is part of SIP. * @@ -38,24 +38,6 @@ #endif -/* - * The qualified and base names of the sip module. These should be defined in - * the compiler invocation when creating a package-specific copy. - */ -#if !defined(SIP_MODULE_NAME) -#define SIP_MODULE_NAME sip -#endif - -#if !defined(SIP_MODULE_BASENAME) -#define SIP_MODULE_BASENAME sip -#endif - -#define STRINGIFY_EX(s) #s -#define STRINGIFY(s) STRINGIFY_EX(s) - -#define SIP_MODULE_NAME_STR STRINGIFY(SIP_MODULE_NAME) -#define SIP_MODULE_BASENAME_STR STRINGIFY(SIP_MODULE_BASENAME) - /* * The Python metatype for a C++ wrapper type. We inherit everything from the * standard Python metatype except the init and getattro methods and the size @@ -63,7 +45,7 @@ * we associate with a wrapped type. */ -static PyObject *sipWrapperType_alloc(PyTypeObject *self, SIP_SSIZE_T nitems); +static PyObject *sipWrapperType_alloc(PyTypeObject *self, Py_ssize_t nitems); static PyObject *sipWrapperType_getattro(PyObject *self, PyObject *name); static int sipWrapperType_init(sipWrapperType *self, PyObject *args, PyObject *kwds); @@ -118,8 +100,9 @@ static PyTypeObject sipWrapperType_Type = { 0, /* tp_weaklist */ 0, /* tp_del */ 0, /* tp_version_tag */ -#if PY_VERSION_HEX >= 0x03040000 0, /* tp_finalize */ +#if PY_VERSION_HEX >= 0x03080000 + 0, /* tp_vectorcall */ #endif }; @@ -184,56 +167,40 @@ static sipWrapperType sipWrapper_Type = { 0, /* tp_subclasses */ 0, /* tp_weaklist */ 0, /* tp_del */ -#if PY_VERSION_HEX >= 0x02060000 0, /* tp_version_tag */ -#endif -#if PY_VERSION_HEX >= 0x03040000 0, /* tp_finalize */ +#if PY_VERSION_HEX >= 0x03080000 + 0, /* tp_vectorcall */ #endif }, -#if PY_VERSION_HEX >= 0x03050000 { 0, /* am_await */ 0, /* am_aiter */ 0, /* am_anext */ }, -#endif { 0, /* nb_add */ 0, /* nb_subtract */ 0, /* nb_multiply */ -#if PY_MAJOR_VERSION < 3 - 0, /* nb_divide */ -#endif 0, /* nb_remainder */ 0, /* nb_divmod */ 0, /* nb_power */ 0, /* nb_negative */ 0, /* nb_positive */ 0, /* nb_absolute */ - 0, /* nb_bool (Python v3), nb_nonzero (Python v2) */ + 0, /* nb_bool */ 0, /* nb_invert */ 0, /* nb_lshift */ 0, /* nb_rshift */ 0, /* nb_and */ 0, /* nb_xor */ 0, /* nb_or */ -#if PY_MAJOR_VERSION < 3 - 0, /* nb_coerce */ -#endif 0, /* nb_int */ - 0, /* nb_reserved (Python v3), nb_long (Python v2) */ + 0, /* nb_reserved */ 0, /* nb_float */ -#if PY_MAJOR_VERSION < 3 - 0, /* nb_oct */ - 0, /* nb_hex */ -#endif 0, /* nb_inplace_add */ 0, /* nb_inplace_subtract */ 0, /* nb_inplace_multiply */ -#if PY_MAJOR_VERSION < 3 - 0, /* nb_inplace_divide */ -#endif 0, /* nb_inplace_remainder */ 0, /* nb_inplace_power */ 0, /* nb_inplace_lshift */ @@ -241,19 +208,13 @@ static sipWrapperType sipWrapper_Type = { 0, /* nb_inplace_and */ 0, /* nb_inplace_xor */ 0, /* nb_inplace_or */ -#if PY_VERSION_HEX >= 0x02020000 0, /* nb_floor_divide */ 0, /* nb_true_divide */ 0, /* nb_inplace_floor_divide */ 0, /* nb_inplace_true_divide */ -#endif -#if PY_VERSION_HEX >= 0x02050000 0, /* nb_index */ -#endif -#if PY_VERSION_HEX >= 0x03050000 0, /* nb_matrix_multiply */ 0, /* nb_inplace_matrix_multiply */ -#endif }, { 0, /* mp_length */ @@ -273,23 +234,15 @@ static sipWrapperType sipWrapper_Type = { 0, /* sq_inplace_repeat */ }, { -#if PY_MAJOR_VERSION >= 3 0, /* bf_getbuffer */ 0, /* bf_releasebuffer */ -#else - 0, /* bf_getreadbuffer */ - 0, /* bf_getwritebuffer */ - 0, /* bf_getsegcount */ - 0, /* bf_getcharbuffer */ - 0, /* bf_getbuffer */ - 0, /* bf_releasebuffer */ -#endif }, 0, /* ht_name */ 0, /* ht_slots */ -#if PY_MAJOR_VERSION >= 3 0, /* ht_qualname */ 0, /* ht_cached_keys */ +#if PY_VERSION_HEX >= 0x03090000 + 0, /* ht_module */ #endif #if !defined(STACKLESS) }, @@ -305,16 +258,16 @@ static sipWrapperType sipWrapper_Type = { static void sip_api_bad_catcher_result(PyObject *method); -static void sip_api_bad_length_for_slice(SIP_SSIZE_T seqlen, - SIP_SSIZE_T slicelen); +static void sip_api_bad_length_for_slice(Py_ssize_t seqlen, + Py_ssize_t slicelen); static PyObject *sip_api_build_result(int *isErr, const char *fmt, ...); static PyObject *sip_api_call_method(int *isErr, PyObject *method, const char *fmt, ...); static void sip_api_call_procedure_method(sip_gilstate_t gil_state, sipVirtErrorHandlerFunc error_handler, sipSimpleWrapper *py_self, PyObject *method, const char *fmt, ...); -static SIP_SSIZE_T sip_api_convert_from_sequence_index(SIP_SSIZE_T idx, - SIP_SSIZE_T len); +static Py_ssize_t sip_api_convert_from_sequence_index(Py_ssize_t idx, + Py_ssize_t len); static int sip_api_can_convert_to_type(PyObject *pyObj, const sipTypeDef *td, int flags); static void *sip_api_convert_to_type(PyObject *pyObj, const sipTypeDef *td, @@ -363,6 +316,8 @@ static void sip_api_bad_class(const char *classname); static void *sip_api_get_complex_cpp_ptr(sipSimpleWrapper *sw); static PyObject *sip_api_is_py_method(sip_gilstate_t *gil, char *pymc, sipSimpleWrapper *sipSelf, const char *cname, const char *mname); +static PyObject *sip_api_is_py_method_12_8(sip_gilstate_t *gil, char *pymc, + sipSimpleWrapper **sipSelfp, const char *cname, const char *mname); static void sip_api_call_hook(const char *hookname); static void sip_api_raise_unknown_exception(void); static void sip_api_raise_type_exception(const sipTypeDef *td, void *ptr); @@ -397,7 +352,6 @@ static int sip_api_unicode_as_wchar(PyObject *obj); static int *sip_api_unicode_as_wstring(PyObject *obj); #endif static void sip_api_transfer_break(PyObject *self); -static int sip_api_deprecated(const char *classname, const char *method); static int sip_api_register_py_type(PyTypeObject *supertype); static PyObject *sip_api_convert_from_enum(int eval, const sipTypeDef *td); static const sipTypeDef *sip_api_type_from_py_type_object(PyTypeObject *py_type); @@ -420,7 +374,7 @@ static void *sip_api_get_mixin_address(sipSimpleWrapper *w, const sipTypeDef *td); static int sip_api_register_proxy_resolver(const sipTypeDef *td, sipProxyResolverFunc resolver); -static PyInterpreterState *sip_api_get_interpreter(); +static PyInterpreterState *sip_api_get_interpreter(void); static sipNewUserTypeFunc sip_api_set_new_user_type_handler( const sipTypeDef *td, sipNewUserTypeFunc handler); static void sip_api_set_type_user_data(sipWrapperType *wt, void *data); @@ -442,12 +396,12 @@ static int sip_api_is_user_type(const sipWrapperType *wt); static struct _frame *sip_api_get_frame(int); static int sip_api_check_plugin_for_type(const sipTypeDef *td, const char *name); -static PyObject *sip_api_unicode_new(SIP_SSIZE_T len, unsigned maxchar, +static PyObject *sip_api_unicode_new(Py_ssize_t len, unsigned maxchar, int *kind, void **data); static void sip_api_unicode_write(int kind, void *data, int index, unsigned value); static void *sip_api_unicode_data(PyObject *obj, int *char_size, - SIP_SSIZE_T *len); + Py_ssize_t *len); static int sip_api_get_buffer_info(PyObject *obj, sipBufferInfoDef *bi); static void sip_api_release_buffer_info(sipBufferInfoDef *bi); static PyObject *sip_api_get_user_object(const sipSimpleWrapper *sw); @@ -457,6 +411,9 @@ static void sip_api_print_object(PyObject *o); static int sip_api_register_event_handler(sipEventType type, const sipTypeDef *td, void *handler); static void sip_api_instance_destroyed_ex(sipSimpleWrapper **sipSelfp); +static void sip_api_visit_wrappers(sipWrapperVisitorFunc visitor, + void *closure); +static int sip_api_register_exit_notifier(PyMethodDef *md); /* @@ -593,7 +550,13 @@ static const sipAPIDef sip_api = { sip_api_call_error_handler, sip_api_init_mixin, sip_api_get_reference, + /* + * The following are part of the public API. + */ sip_api_is_owned_by_python, + /* + * The following are not part of the public API. + */ sip_api_is_derived_class, /* * The following may be used by Qt support code but by no other handwritten @@ -648,6 +611,12 @@ static const sipAPIDef sip_api = { */ sip_api_convert_from_slice_object, sip_api_long_as_size_t, + sip_api_visit_wrappers, + sip_api_register_exit_notifier, + /* + * The following are not part of the public API. + */ + sip_api_is_py_method_12_8, }; @@ -751,7 +720,7 @@ typedef struct _sipEventHandler { * The structures to support a Python type to hold a named enum. *****************************************************************************/ -static PyObject *sipEnumType_alloc(PyTypeObject *self, SIP_SSIZE_T nitems); +static PyObject *sipEnumType_alloc(PyTypeObject *self, Py_ssize_t nitems); static PyObject *sipEnumType_getattro(PyObject *self, PyObject *name); @@ -808,8 +777,9 @@ static PyTypeObject sipEnumType_Type = { 0, /* tp_weaklist */ 0, /* tp_del */ 0, /* tp_version_tag */ -#if PY_VERSION_HEX >= 0x03040000 0, /* tp_finalize */ +#if PY_VERSION_HEX >= 0x03080000 + 0, /* tp_vectorcall */ #endif }; @@ -860,7 +830,7 @@ static void *findSlotInClass(const sipClassTypeDef *psd, sipPySlotType st); static void *findSlotInSlotList(sipPySlotDef *psd, sipPySlotType st); static int objobjargprocSlot(PyObject *self, PyObject *arg1, PyObject *arg2, sipPySlotType st); -static int ssizeobjargprocSlot(PyObject *self, SIP_SSIZE_T arg1, +static int ssizeobjargprocSlot(PyObject *self, Py_ssize_t arg1, PyObject *arg2, sipPySlotType st); static PyObject *buildObject(PyObject *tup, const char *fmt, va_list va); static int parseKwdArgs(PyObject **parseErrp, PyObject *sipArgs, @@ -874,13 +844,13 @@ static int parsePass2(sipSimpleWrapper *self, int selfarg, PyObject *sipArgs, va_list va); static int parseResult(PyObject *method, PyObject *res, sipSimpleWrapper *py_self, const char *fmt, va_list va); -static PyObject *signature_FromDocstring(const char *doc, SIP_SSIZE_T line); +static PyObject *signature_FromDocstring(const char *doc, Py_ssize_t line); static PyObject *detail_FromFailure(PyObject *failure_obj); static int isQObject(PyObject *obj); static int canConvertFromSequence(PyObject *seq, const sipTypeDef *td); static int convertFromSequence(PyObject *seq, const sipTypeDef *td, - void **array, SIP_SSIZE_T *nr_elem); -static PyObject *convertToSequence(void *array, SIP_SSIZE_T nr_elem, + void **array, Py_ssize_t *nr_elem); +static PyObject *convertToSequence(void *array, Py_ssize_t nr_elem, const sipTypeDef *td); static int getSelfFromArgs(sipTypeDef *td, PyObject *args, int argnr, sipSimpleWrapper **selfp); @@ -950,6 +920,7 @@ static PyObject *unwrapInstance(PyObject *self, PyObject *args); static PyObject *transferBack(PyObject *self, PyObject *args); static PyObject *transferTo(PyObject *self, PyObject *args); static PyObject *setDestroyOnExit(PyObject *self, PyObject *args); +static void clear_wrapper(sipSimpleWrapper *sw); static void print_object(const char *label, PyObject *obj); static void addToParent(sipWrapper *self, sipWrapper *owner); static void removeFromParent(sipWrapper *self); @@ -957,7 +928,7 @@ static void detachChildren(sipWrapper *self); static void release(void *addr, const sipTypeDef *td, int state); static void callPyDtor(sipSimpleWrapper *self); static int parseBytes_AsCharArray(PyObject *obj, const char **ap, - SIP_SSIZE_T *aszp); + Py_ssize_t *aszp); static int parseBytes_AsChar(PyObject *obj, char *ap); static int parseBytes_AsString(PyObject *obj, const char **ap); static int parseString_AsASCIIChar(PyObject *obj, char *ap); @@ -970,8 +941,8 @@ static int parseString_AsEncodedChar(PyObject *bytes, PyObject *obj, char *ap); static PyObject *parseString_AsEncodedString(PyObject *bytes, PyObject *obj, const char **ap); #if defined(HAVE_WCHAR_H) -static int parseWCharArray(PyObject *obj, wchar_t **ap, SIP_SSIZE_T *aszp); -static int convertToWCharArray(PyObject *obj, wchar_t **ap, SIP_SSIZE_T *aszp); +static int parseWCharArray(PyObject *obj, wchar_t **ap, Py_ssize_t *aszp); +static int convertToWCharArray(PyObject *obj, wchar_t **ap, Py_ssize_t *aszp); static int parseWChar(PyObject *obj, wchar_t *ap); static int convertToWChar(PyObject *obj, wchar_t *ap); static int parseWCharString(PyObject *obj, wchar_t **ap); @@ -1000,7 +971,6 @@ static int addMethod(PyObject *dict, PyMethodDef *pmd); static PyObject *create_property(sipVariableDef *vd); static PyObject *create_function(PyMethodDef *ml); static PyObject *sip_exit(PyObject *self, PyObject *args); -static void register_exit_notifier(void); static sipConvertFromFunc get_from_convertor(const sipTypeDef *td); static sipPyObject **autoconversion_disabled(const sipTypeDef *td); static void fix_slots(PyTypeObject *py_type, sipPySlotDef *psd); @@ -1013,7 +983,6 @@ static sipSimpleWrapper *deref_mixin(sipSimpleWrapper *w); static PyObject *wrap_simple_instance(void *cpp, const sipTypeDef *td, sipWrapper *owner, int flags); static void *resolve_proxy(const sipTypeDef *td, void *proxy); -static void clear_wrapper(sipSimpleWrapper *sw); static PyObject *call_method(PyObject *method, const char *fmt, va_list va); static int importTypes(sipExportedModuleDef *client, sipImportedModuleDef *im, sipExportedModuleDef *em); @@ -1025,44 +994,25 @@ static int is_subtype(const sipClassTypeDef *ctd, const sipClassTypeDef *base_ctd); static PyObject *import_module_attr(const char *module, const char *attr); static const sipContainerDef *get_container(const sipTypeDef *td); -#if PY_VERSION_HEX >= 0x03030000 static PyObject *get_qualname(const sipTypeDef *td, PyObject *name); -#endif static int convert_to_enum(PyObject *obj, const sipTypeDef *td, int allow_int); static void handle_failed_int_conversion(sipParseFailure *pf, PyObject *arg); static void enum_expected(PyObject *obj, const sipTypeDef *td); static int long_as_nonoverflow_int(PyObject *val_obj); +static int dict_set_and_discard(PyObject *dict, const char *name, + PyObject *obj); /* - * The Python module initialisation function. + * Initialise the module as a library. */ -#if PY_MAJOR_VERSION >= 3 -#define SIP_MODULE_ENTRY_PREFIX PyInit_ -#define SIP_MODULE_TYPE PyObject * -#define SIP_MODULE_DISCARD(m) Py_DECREF(m) -#define SIP_FATAL(s) return NULL -#define SIP_MODULE_RETURN(m) return (m) -#else -#define SIP_MODULE_ENTRY_PREFIX init -#define SIP_MODULE_TYPE void -#define SIP_MODULE_DISCARD(m) -#define SIP_FATAL(s) Py_FatalError(s) -#define SIP_MODULE_RETURN(m) -#endif - -#define CONCAT_EX(PREFIX, NAME) PREFIX ## NAME -#define CONCAT(PREFIX, NAME) CONCAT_EX(PREFIX, NAME) - -#define SIP_MODULE_ENTRY CONCAT(SIP_MODULE_ENTRY_PREFIX, SIP_MODULE_BASENAME) - -#if defined(SIP_STATIC_MODULE) -SIP_MODULE_TYPE SIP_MODULE_ENTRY(void) -#else -PyMODINIT_FUNC SIP_MODULE_ENTRY(void) -#endif +const sipAPIDef *sip_init_library(PyObject *mod_dict) { static PyMethodDef methods[] = { + /* This must be first. */ + {"_unpickle_enum", unpickle_enum, METH_VARARGS, NULL}, + /* This must be second. */ + {"_unpickle_type", unpickle_type, METH_VARARGS, NULL}, {"assign", assign, METH_VARARGS, NULL}, {"cast", cast, METH_VARARGS, NULL}, {"delete", callDtor, METH_VARARGS, NULL}, @@ -1081,27 +1031,15 @@ PyMODINIT_FUNC SIP_MODULE_ENTRY(void) {"transferto", transferTo, METH_VARARGS, NULL}, {"wrapinstance", wrapInstance, METH_VARARGS, NULL}, {"unwrapinstance", unwrapInstance, METH_VARARGS, NULL}, - {"_unpickle_type", unpickle_type, METH_VARARGS, NULL}, - {"_unpickle_enum", unpickle_enum, METH_VARARGS, NULL}, {NULL, NULL, 0, NULL} }; -#if PY_MAJOR_VERSION >= 3 - static PyModuleDef module_def = { - PyModuleDef_HEAD_INIT, - SIP_MODULE_NAME_STR, /* m_name */ - NULL, /* m_doc */ - -1, /* m_size */ - methods, /* m_methods */ - NULL, /* m_reload */ - NULL, /* m_traverse */ - NULL, /* m_clear */ - NULL, /* m_free */ + static PyMethodDef sip_exit_md = { + "_sip_exit", sip_exit, METH_NOARGS, NULL }; -#endif - int rc; - PyObject *mod, *mod_dict, *obj; + PyObject *obj; + PyMethodDef *md; /* * Remind ourselves to add support for capsule variables when we have @@ -1111,179 +1049,204 @@ PyMODINIT_FUNC SIP_MODULE_ENTRY(void) #error "Add support for capsule variables" #endif -#ifdef WITH_THREAD +#if PY_VERSION_HEX < 0x03070000 && defined(WITH_THREAD) PyEval_InitThreads(); #endif + /* Add the SIP version number. */ + obj = PyLong_FromLong(SIP_VERSION); + + if (dict_set_and_discard(mod_dict, "SIP_VERSION", obj) < 0) + return NULL; + + obj = PyUnicode_FromString(SIP_VERSION_STR); + + if (dict_set_and_discard(mod_dict, "SIP_VERSION_STR", obj) < 0) + return NULL; + + /* Add the methods. */ + for (md = methods; md->ml_name != NULL; ++md) + { + PyObject *meth = PyCFunction_New(md, NULL); + + if (dict_set_and_discard(mod_dict, md->ml_name, meth) < 0) + return NULL; + + if (md == &methods[0]) + { + Py_INCREF(meth); + enum_unpickler = meth; + } + else if (md == &methods[1]) + { + Py_INCREF(meth); + type_unpickler = meth; + } + } + /* Initialise the types. */ sipWrapperType_Type.tp_base = &PyType_Type; if (PyType_Ready(&sipWrapperType_Type) < 0) - SIP_FATAL(SIP_MODULE_NAME_STR ": Failed to initialise sip.wrappertype type"); + return NULL; if (PyType_Ready((PyTypeObject *)&sipSimpleWrapper_Type) < 0) - SIP_FATAL(SIP_MODULE_NAME_STR ": Failed to initialise sip.simplewrapper type"); + return NULL; if (sip_api_register_py_type((PyTypeObject *)&sipSimpleWrapper_Type) < 0) - SIP_FATAL(SIP_MODULE_NAME_STR ": Failed to register sip.simplewrapper type"); + return NULL; #if defined(STACKLESS) sipWrapper_Type.super.tp_base = (PyTypeObject *)&sipSimpleWrapper_Type; -#elif PY_VERSION_HEX >= 0x02050000 - sipWrapper_Type.super.ht_type.tp_base = (PyTypeObject *)&sipSimpleWrapper_Type; #else - sipWrapper_Type.super.type.tp_base = (PyTypeObject *)&sipSimpleWrapper_Type; + sipWrapper_Type.super.ht_type.tp_base = (PyTypeObject *)&sipSimpleWrapper_Type; #endif if (PyType_Ready((PyTypeObject *)&sipWrapper_Type) < 0) - SIP_FATAL(SIP_MODULE_NAME_STR ": Failed to initialise sip.wrapper type"); + return NULL; if (PyType_Ready(&sipMethodDescr_Type) < 0) - SIP_FATAL(SIP_MODULE_NAME_STR ": Failed to initialise sip.methoddescriptor type"); + return NULL; if (PyType_Ready(&sipVariableDescr_Type) < 0) - SIP_FATAL(SIP_MODULE_NAME_STR ": Failed to initialise sip.variabledescriptor type"); + return NULL; sipEnumType_Type.tp_base = &PyType_Type; if (PyType_Ready(&sipEnumType_Type) < 0) - SIP_FATAL(SIP_MODULE_NAME_STR ": Failed to initialise sip.enumtype type"); + return NULL; if (PyType_Ready(&sipVoidPtr_Type) < 0) - SIP_FATAL(SIP_MODULE_NAME_STR ": Failed to initialise sip.voidptr type"); + return NULL; if (PyType_Ready(&sipArray_Type) < 0) - SIP_FATAL(SIP_MODULE_NAME_STR ": Failed to initialise sip.array type"); + return NULL; -#if PY_MAJOR_VERSION >= 3 - mod = PyModule_Create(&module_def); -#else - mod = Py_InitModule(SIP_MODULE_NAME_STR, methods); -#endif + /* Add the public types. */ + if (PyDict_SetItemString(mod_dict, "wrappertype", (PyObject *)&sipWrapperType_Type) < 0) + return NULL; - if (mod == NULL) - SIP_FATAL(SIP_MODULE_NAME_STR ": Failed to initialise sip module"); + if (PyDict_SetItemString(mod_dict, "simplewrapper", (PyObject *)&sipSimpleWrapper_Type) < 0) + return NULL; - mod_dict = PyModule_GetDict(mod); + if (PyDict_SetItemString(mod_dict, "wrapper", (PyObject *)&sipWrapper_Type) < 0) + return NULL; - /* Get a reference to the pickle helpers. */ - type_unpickler = PyDict_GetItemString(mod_dict, "_unpickle_type"); - enum_unpickler = PyDict_GetItemString(mod_dict, "_unpickle_enum"); - - if (type_unpickler == NULL || enum_unpickler == NULL) - { - SIP_MODULE_DISCARD(mod); - SIP_FATAL(SIP_MODULE_NAME_STR ": Failed to get pickle helpers"); - } - - /* Publish the SIP API. */ -#if defined(SIP_USE_PYCAPSULE) - obj = PyCapsule_New((void *)&sip_api, SIP_MODULE_NAME_STR "._C_API", NULL); -#else - obj = PyCObject_FromVoidPtr((void *)&sip_api, NULL); -#endif - - if (obj == NULL) - { - SIP_MODULE_DISCARD(mod); - SIP_FATAL(SIP_MODULE_NAME_STR ": Failed to create _C_API object"); - } - - rc = PyDict_SetItemString(mod_dict, "_C_API", obj); - Py_DECREF(obj); - - if (rc < 0) - { - SIP_MODULE_DISCARD(mod); - SIP_FATAL(SIP_MODULE_NAME_STR ": Failed to add _C_API object to module dictionary"); - } + if (PyDict_SetItemString(mod_dict, "voidptr", (PyObject *)&sipVoidPtr_Type) < 0) + return NULL; /* These will always be needed. */ if (objectify("__init__", &init_name) < 0) - { - SIP_MODULE_DISCARD(mod); - SIP_FATAL(SIP_MODULE_NAME_STR ": Failed to objectify '__init__'"); - } + return NULL; if ((empty_tuple = PyTuple_New(0)) == NULL) - { - SIP_MODULE_DISCARD(mod); - SIP_FATAL(SIP_MODULE_NAME_STR ": Failed to create empty tuple"); - } + return NULL; - /* Add the SIP version number, but don't worry about errors. */ -#if PY_MAJOR_VERSION >= 3 - obj = PyLong_FromLong(SIP_VERSION); -#else - obj = PyInt_FromLong(SIP_VERSION); -#endif + /* Initialise the object map. */ + sipOMInit(&cppPyMap); - if (obj != NULL) - { - PyDict_SetItemString(mod_dict, "SIP_VERSION", obj); - Py_DECREF(obj); - } + /* Make sure we are notified at the end of the exit process. */ + if (Py_AtExit(finalise) < 0) + return NULL; -#if PY_MAJOR_VERSION >= 3 - obj = PyUnicode_FromString(SIP_VERSION_STR); -#else - obj = PyString_FromString(SIP_VERSION_STR); -#endif - - if (obj != NULL) - { - PyDict_SetItemString(mod_dict, "SIP_VERSION_STR", obj); - Py_DECREF(obj); - } - - /* Add the type objects, but don't worry about errors. */ - PyDict_SetItemString(mod_dict, "wrappertype", - (PyObject *)&sipWrapperType_Type); - PyDict_SetItemString(mod_dict, "simplewrapper", - (PyObject *)&sipSimpleWrapper_Type); - PyDict_SetItemString(mod_dict, "wrapper", (PyObject *)&sipWrapper_Type); - PyDict_SetItemString(mod_dict, "voidptr", (PyObject *)&sipVoidPtr_Type); - - /* Initialise the module if it hasn't already been done. */ - if (sipInterpreter == NULL) - { - Py_AtExit(finalise); - - /* Initialise the object map. */ - sipOMInit(&cppPyMap); - - sipQtSupport = NULL; - - /* - * Get the current interpreter. This will be shared between all - * threads. - */ - sipInterpreter = PyThreadState_Get()->interp; - } - - /* Make sure we are notified when starting to exit. */ - register_exit_notifier(); + /* Make sure we are notified at the start of the exit process. */ + if (sip_api_register_exit_notifier(&sip_exit_md) < 0) + return NULL; /* - * Also install the package-specific module at the top level for backwards - * compatibility. + * Get the current interpreter. This will be shared between all threads. */ - if (strcmp(SIP_MODULE_NAME_STR, "sip") != 0 && strcmp(SIP_MODULE_BASENAME_STR, "sip") == 0) + sipInterpreter = PyThreadState_Get()->interp; + + return &sip_api; +} + + +/* + * Set a dictionary item and discard the reference to the item even if there + * was an error. + */ +static int dict_set_and_discard(PyObject *dict, const char *name, PyObject *obj) +{ + int rc; + + if (obj == NULL) + return -1; + + rc = PyDict_SetItemString(dict, name, obj); + + Py_DECREF(obj); + + return rc; +} + + +#if _SIP_MODULE_SHARED +/* + * The Python module initialisation function. + */ +#if defined(SIP_STATIC_MODULE) +PyObject *_SIP_MODULE_ENTRY(void) +#else +PyMODINIT_FUNC _SIP_MODULE_ENTRY(void) +#endif +{ + static PyModuleDef module_def = { + PyModuleDef_HEAD_INIT, + _SIP_MODULE_FQ_NAME, /* m_name */ + NULL, /* m_doc */ + -1, /* m_size */ + NULL, /* m_methods */ + NULL, /* m_reload */ + NULL, /* m_traverse */ + NULL, /* m_clear */ + NULL, /* m_free */ + }; + + const sipAPIDef *api; + PyObject *mod, *mod_dict, *api_obj; + + /* Create the module. */ + if ((mod = PyModule_Create(&module_def)) == NULL) + return NULL; + + mod_dict = PyModule_GetDict(mod); + + /* Initialise the module dictionary and static variables. */ + if ((api = sip_init_library(mod_dict)) == NULL) + return NULL; + + /* Publish the SIP API. */ + api_obj = PyCapsule_New((void *)api, _SIP_MODULE_FQ_NAME "._C_API", NULL); + + if (dict_set_and_discard(mod_dict, "_C_API", api_obj) < 0) { + Py_DECREF(mod); + return NULL; + } + +#if _SIP_MODULE_LEGACY + { + /* + * Also install the package-specific module at the top level for + * backwards compatibility. + */ PyObject *modules = PySys_GetObject("modules"); if (modules != NULL) PyDict_SetItemString(modules, "sip", mod); } +#endif - SIP_MODULE_RETURN(mod); + return mod; } +#endif /* * Return the current interpreter, if there is one. */ -static PyInterpreterState *sip_api_get_interpreter() +static PyInterpreterState *sip_api_get_interpreter(void) { return sipInterpreter; } @@ -1350,11 +1313,7 @@ static PyObject *dumpWrapper(PyObject *self, PyObject *arg) print_object(NULL, (PyObject *)sw); -#if PY_VERSION_HEX >= 0x02050000 printf(" Reference count: %" PY_FORMAT_SIZE_T "d\n", Py_REFCNT(sw)); -#else - printf(" Reference count: %d\n", Py_REFCNT(sw)); -#endif printf(" Address of wrapped object: %p\n", sip_api_get_address(sw)); printf(" Created by: %s\n", (sipIsDerived(sw) ? "Python" : "C/C++")); printf(" To be destroyed by: %s\n", (sipIsPyOwned(sw) ? "Python" : "C/C++")); @@ -1826,13 +1785,7 @@ static int sip_api_export_module(sipExportedModuleDef *client, } /* Convert the module name to an object. */ -#if PY_MAJOR_VERSION >= 3 - client->em_nameobj = PyUnicode_FromString(full_name); -#else - client->em_nameobj = PyString_FromString(full_name); -#endif - - if (client->em_nameobj == NULL) + if ((client->em_nameobj = PyUnicode_FromString(full_name)) == NULL) return -1; /* Add it to the list of client modules. */ @@ -2007,15 +1960,16 @@ static int sip_api_init_module(sipExportedModuleDef *client, /* Create the module's enum members. */ for (emd = client->em_enummembers, i = 0; i < client->em_nrenummembers; ++i, ++emd) { + sipTypeDef *etd = client->em_types[emd->em_enum]; PyObject *mo; - if ((mo = sip_api_convert_from_enum(emd->em_val, client->em_types[emd->em_enum])) == NULL) - return -1; + if (sipTypeIsScopedEnum(etd)) + continue; - if (PyDict_SetItemString(mod_dict, emd->em_name, mo) < 0) - return -1; + mo = sip_api_convert_from_enum(emd->em_val, etd); - Py_DECREF(mo); + if (dict_set_and_discard(mod_dict, emd->em_name, mo) < 0) + return -1; } @@ -2316,7 +2270,7 @@ static PyObject *call_method(PyObject *method, const char *fmt, va_list va) return NULL; if (buildObject(args, fmt, va) != NULL) - res = PyEval_CallObject(method, args); + res = PyObject_CallObject(method, args); else res = NULL; @@ -2455,14 +2409,14 @@ static PyObject *buildObject(PyObject *obj, const char *fmt, va_list va) case 'g': { char *s; - SIP_SSIZE_T l; + Py_ssize_t l; s = va_arg(va, char *); - l = va_arg(va, SIP_SSIZE_T); + l = va_arg(va, Py_ssize_t); if (s != NULL) { - el = SIPBytes_FromStringAndSize(s, l); + el = PyBytes_FromStringAndSize(s, l); } else { @@ -2477,10 +2431,10 @@ static PyObject *buildObject(PyObject *obj, const char *fmt, va_list va) #if defined(HAVE_WCHAR_H) { wchar_t *s; - SIP_SSIZE_T l; + Py_ssize_t l; s = va_arg(va, wchar_t *); - l = va_arg(va, SIP_SSIZE_T); + l = va_arg(va, Py_ssize_t); if (s != NULL) el = PyUnicode_FromWideChar(s, l); @@ -2505,7 +2459,7 @@ static PyObject *buildObject(PyObject *obj, const char *fmt, va_list va) { char c = va_arg(va, int); - el = SIPBytes_FromStringAndSize(&c, 1); + el = PyBytes_FromStringAndSize(&c, 1); } break; @@ -2514,11 +2468,7 @@ static PyObject *buildObject(PyObject *obj, const char *fmt, va_list va) { char c = va_arg(va, int); -#if PY_MAJOR_VERSION >= 3 el = PyUnicode_FromStringAndSize(&c, 1); -#else - el = PyString_FromStringAndSize(&c, 1); -#endif } break; @@ -2539,7 +2489,7 @@ static PyObject *buildObject(PyObject *obj, const char *fmt, va_list va) case 'E': { - /* This is deprecated. */ + /* Remove in v5.1. */ int ev = va_arg(va, int); PyTypeObject *et = va_arg(va, PyTypeObject *); @@ -2569,11 +2519,7 @@ static PyObject *buildObject(PyObject *obj, const char *fmt, va_list va) case 'h': case 'i': case 'L': -#if PY_MAJOR_VERSION >= 3 el = PyLong_FromLong(va_arg(va, int)); -#else - el = PyInt_FromLong(va_arg(va, int)); -#endif break; case 'l': @@ -2606,7 +2552,7 @@ static PyObject *buildObject(PyObject *obj, const char *fmt, va_list va) if (s != NULL) { - el = SIPBytes_FromString(s); + el = PyBytes_FromString(s); } else { @@ -2622,11 +2568,9 @@ static PyObject *buildObject(PyObject *obj, const char *fmt, va_list va) char *s = va_arg(va, char *); if (s != NULL) -#if PY_MAJOR_VERSION >= 3 + { el = PyUnicode_FromString(s); -#else - el = PyString_FromString(s); -#endif + } else { Py_INCREF(Py_None); @@ -2642,7 +2586,7 @@ static PyObject *buildObject(PyObject *obj, const char *fmt, va_list va) wchar_t *s = va_arg(va, wchar_t *); if (s != NULL) - el = PyUnicode_FromWideChar(s, (SIP_SSIZE_T)wcslen(s)); + el = PyUnicode_FromWideChar(s, (Py_ssize_t)wcslen(s)); else { Py_INCREF(Py_None); @@ -2668,7 +2612,7 @@ static PyObject *buildObject(PyObject *obj, const char *fmt, va_list va) case 'B': { - /* This is deprecated. */ + /* Remove in v5.1. */ void *p = va_arg(va,void *); sipWrapperType *wt = va_arg(va, sipWrapperType *); @@ -2692,7 +2636,7 @@ static PyObject *buildObject(PyObject *obj, const char *fmt, va_list va) case 'C': { - /* This is deprecated. */ + /* Remove in v5.1. */ void *p = va_arg(va,void *); sipWrapperType *wt = va_arg(va, sipWrapperType *); @@ -2717,7 +2661,7 @@ static PyObject *buildObject(PyObject *obj, const char *fmt, va_list va) case 'r': { void *p = va_arg(va, void *); - SIP_SSIZE_T l = va_arg(va, SIP_SSIZE_T); + Py_ssize_t l = va_arg(va, Py_ssize_t); const sipTypeDef *td = va_arg(va, const sipTypeDef *); el = convertToSequence(p, l, td); @@ -2750,11 +2694,7 @@ static PyObject *buildObject(PyObject *obj, const char *fmt, va_list va) } else { -#if defined(SIP_USE_PYCAPSULE) el = PyCapsule_New(p, name, NULL); -#else - el = sip_api_convert_from_void_ptr(p); -#endif } } @@ -2936,7 +2876,7 @@ static int parseResult(PyObject *method, PyObject *res, case 'g': { const char **p = va_arg(va, const char **); - SIP_SSIZE_T *szp = va_arg(va, SIP_SSIZE_T *); + Py_ssize_t *szp = va_arg(va, Py_ssize_t *); if (parseBytes_AsCharArray(arg, p, szp) < 0) invalid = TRUE; @@ -2948,7 +2888,7 @@ static int parseResult(PyObject *method, PyObject *res, #if defined(HAVE_WCHAR_H) { wchar_t **p = va_arg(va, wchar_t **); - SIP_SSIZE_T *szp = va_arg(va, SIP_SSIZE_T *); + Py_ssize_t *szp = va_arg(va, Py_ssize_t *); if (parseWCharArray(arg, p, szp) < 0) invalid = TRUE; @@ -3042,7 +2982,7 @@ static int parseResult(PyObject *method, PyObject *res, case 'E': { - /* This is deprecated. */ + /* Remove in v5.1. */ PyTypeObject *et = va_arg(va, PyTypeObject *); int *p = va_arg(va, int *); @@ -3251,7 +3191,7 @@ static int parseResult(PyObject *method, PyObject *res, case 's': { - /* This is deprecated. */ + /* Remove in v5.1. */ const char **p = va_arg(va, const char **); @@ -3323,7 +3263,7 @@ static int parseResult(PyObject *method, PyObject *res, case 'C': { - /* This is deprecated. */ + /* Remove in v5.1. */ if (*fmt == '\0') { @@ -3357,7 +3297,7 @@ static int parseResult(PyObject *method, PyObject *res, case 'D': { - /* This is deprecated. */ + /* Remove in v5.1. */ if (*fmt == '\0') { @@ -3543,13 +3483,7 @@ static int parseResult(PyObject *method, PyObject *res, { PyObject **p = va_arg(va, PyObject **); -#if PY_VERSION_HEX >= 0x03000000 if (PyObject_CheckBuffer(arg)) -#elif PY_VERSION_HEX >= 0x02060300 - if (PyObject_CheckBuffer(arg) || PyObject_CheckReadBuffer(arg)) -#else - if (PyObject_CheckReadBuffer(arg)) -#endif { if (p != NULL) { @@ -3569,13 +3503,7 @@ static int parseResult(PyObject *method, PyObject *res, { PyObject **p = va_arg(va, PyObject **); -#if PY_VERSION_HEX >= 0x03000000 if (arg == Py_None || PyObject_CheckBuffer(arg)) -#elif PY_VERSION_HEX >= 0x02060300 - if (arg == Py_None || PyObject_CheckBuffer(arg) || PyObject_CheckReadBuffer(arg)) -#else - if (arg == Py_None || PyObject_CheckReadBuffer(arg)) -#endif { if (p != NULL) { @@ -3757,13 +3685,8 @@ static int parseKwdArgs(PyObject **parseErrp, PyObject *sipArgs, */ static PyObject *bad_type_str(int arg_nr, PyObject *arg) { -#if PY_MAJOR_VERSION >= 3 return PyUnicode_FromFormat("argument %d has unexpected type '%s'", arg_nr, Py_TYPE(arg)->tp_name); -#else - return PyString_FromFormat("argument %d has unexpected type '%s'", arg_nr, - Py_TYPE(arg)->tp_name); -#endif } @@ -3826,7 +3749,6 @@ static void sip_api_add_exception(sipErrorState es, PyObject **parseErrp) /* * The dtor for parse failure wrapped in a Python object. */ -#if defined(SIP_USE_PYCAPSULE) static void failure_dtor(PyObject *capsule) { sipParseFailure *failure = (sipParseFailure *)PyCapsule_GetPointer(capsule, NULL); @@ -3835,16 +3757,6 @@ static void failure_dtor(PyObject *capsule) sip_api_free(failure); } -#else -static void failure_dtor(void *ptr) -{ - sipParseFailure *failure = (sipParseFailure *)ptr; - - Py_XDECREF(failure->detail_obj); - - sip_api_free(failure); -} -#endif /* @@ -3874,13 +3786,7 @@ static void add_failure(PyObject **parseErrp, sipParseFailure *failure) *failure_copy = *failure; -#if defined(SIP_USE_PYCAPSULE) - failure_obj = PyCapsule_New(failure_copy, NULL, failure_dtor); -#else - failure_obj = PyCObject_FromVoidPtr(failure_copy, failure_dtor); -#endif - - if (failure_obj == NULL) + if ((failure_obj = PyCapsule_New(failure_copy, NULL, failure_dtor)) == NULL) { sip_api_free(failure_copy); failure->reason = Raised; @@ -3985,7 +3891,7 @@ static int parsePass1(PyObject **parseErrp, sipSimpleWrapper **selfp, const char **kwdlist, PyObject **unused, const char *fmt, va_list va) { int compulsory, argnr, nr_args; - SIP_SSIZE_T nr_pos_args, nr_kwd_args, nr_kwd_args_used; + Py_ssize_t nr_pos_args, nr_kwd_args, nr_kwd_args_used; sipParseFailure failure; failure.reason = Ok; @@ -4090,7 +3996,7 @@ static int parsePass1(PyObject **parseErrp, sipSimpleWrapper **selfp, else { PyObject *key, *value, *unused_dict = NULL; - SIP_SSIZE_T pos = 0; + Py_ssize_t pos = 0; /* * Go through the keyword arguments to find any that were @@ -4101,11 +4007,7 @@ static int parsePass1(PyObject **parseErrp, sipSimpleWrapper **selfp, { int a; -#if PY_MAJOR_VERSION >= 3 if (!PyUnicode_Check(key)) -#else - if (!PyString_Check(key)) -#endif { failure.reason = KeywordNotString; failure.detail_obj = key; @@ -4123,11 +4025,7 @@ static int parsePass1(PyObject **parseErrp, sipSimpleWrapper **selfp, if (name == NULL) continue; -#if PY_MAJOR_VERSION >= 3 if (PyUnicode_CompareWithASCIIString(key, name) == 0) -#else - if (strcmp(PyString_AS_STRING(key), name) == 0) -#endif break; } } @@ -4245,17 +4143,13 @@ static int parsePass1(PyObject **parseErrp, sipSimpleWrapper **selfp, if (unused == NULL && sipKwdArgs != NULL && nr_kwd_args_used != nr_kwd_args) { PyObject *key, *value; - SIP_SSIZE_T pos = 0; + Py_ssize_t pos = 0; while (PyDict_Next(sipKwdArgs, &pos, &key, &value)) { int a; -#if PY_MAJOR_VERSION >= 3 if (!PyUnicode_Check(key)) -#else - if (!PyString_Check(key)) -#endif { failure.reason = KeywordNotString; failure.detail_obj = key; @@ -4273,11 +4167,7 @@ static int parsePass1(PyObject **parseErrp, sipSimpleWrapper **selfp, if (name == NULL) continue; -#if PY_MAJOR_VERSION >= 3 if (PyUnicode_CompareWithASCIIString(key, name) == 0) -#else - if (strcmp(PyString_AS_STRING(key), name) == 0) -#endif break; } } @@ -4411,9 +4301,9 @@ static int parsePass1(PyObject **parseErrp, sipSimpleWrapper **selfp, *sname = NULL; *scall = NULL; - if (SIPBytes_Check(arg)) + if (PyBytes_Check(arg)) { - char *s = SIPBytes_AS_STRING(arg); + char *s = PyBytes_AS_STRING(arg); if (*s == '1' || *s == '2' || *s == '9') { @@ -4449,9 +4339,9 @@ static int parsePass1(PyObject **parseErrp, sipSimpleWrapper **selfp, if (arg != NULL) { - if (SIPBytes_Check(arg)) + if (PyBytes_Check(arg)) { - char *s = SIPBytes_AS_STRING(arg); + char *s = PyBytes_AS_STRING(arg); if (*s == '1' || *s == '2' || *s == '9') { @@ -4483,9 +4373,9 @@ static int parsePass1(PyObject **parseErrp, sipSimpleWrapper **selfp, if (arg != NULL) { - if (SIPBytes_Check(arg)) + if (PyBytes_Check(arg)) { - char *s = SIPBytes_AS_STRING(arg); + char *s = PyBytes_AS_STRING(arg); if (*s == '2' || *s == '9') { @@ -4517,7 +4407,7 @@ static int parsePass1(PyObject **parseErrp, sipSimpleWrapper **selfp, td = va_arg(va, const sipTypeDef *); va_arg(va, void **); - va_arg(va, SIP_SSIZE_T *); + va_arg(va, Py_ssize_t *); if (arg != NULL && !canConvertFromSequence(arg, td)) { @@ -4699,13 +4589,7 @@ static int parsePass1(PyObject **parseErrp, sipSimpleWrapper **selfp, if (arg != NULL) { -#if PY_VERSION_HEX >= 0x03000000 if (PyObject_CheckBuffer(arg)) -#elif PY_VERSION_HEX >= 0x02060300 - if (PyObject_CheckBuffer(arg) || PyObject_CheckReadBuffer(arg)) -#else - if (PyObject_CheckReadBuffer(arg)) -#endif { *p = arg; } @@ -4730,13 +4614,7 @@ static int parsePass1(PyObject **parseErrp, sipSimpleWrapper **selfp, if (arg != NULL) { -#if PY_VERSION_HEX >= 0x03000000 if (arg == Py_None || PyObject_CheckBuffer(arg)) -#elif PY_VERSION_HEX >= 0x02060300 - if (arg == Py_None || PyObject_CheckBuffer(arg) || PyObject_CheckReadBuffer(arg)) -#else - if (arg == Py_None || PyObject_CheckReadBuffer(arg)) -#endif { *p = arg; } @@ -4829,7 +4707,7 @@ static int parsePass1(PyObject **parseErrp, sipSimpleWrapper **selfp, /* Char array or None. */ const char **p = va_arg(va, const char **); - SIP_SSIZE_T *szp = va_arg(va, SIP_SSIZE_T *); + Py_ssize_t *szp = va_arg(va, Py_ssize_t *); if (arg != NULL && parseBytes_AsCharArray(arg, p, szp) < 0) { @@ -4847,7 +4725,7 @@ static int parsePass1(PyObject **parseErrp, sipSimpleWrapper **selfp, /* Wide char array or None. */ wchar_t **p = va_arg(va, wchar_t **); - SIP_SSIZE_T *szp = va_arg(va, SIP_SSIZE_T *); + Py_ssize_t *szp = va_arg(va, Py_ssize_t *); if (arg != NULL && parseWCharArray(arg, p, szp) < 0) { @@ -5303,11 +5181,7 @@ static int parsePass1(PyObject **parseErrp, sipSimpleWrapper **selfp, { /* Integer. */ -#if PY_MAJOR_VERSION >= 3 if (PyLong_Check(arg)) -#else - if (PyInt_Check(arg) || PyLong_Check(arg)) -#endif { *(int *)p = sip_api_long_as_int(arg); @@ -5394,11 +5268,7 @@ static int parsePass1(PyObject **parseErrp, sipSimpleWrapper **selfp, } else if (arg != NULL) { -#if defined(SIP_USE_PYCAPSULE) void *v = PyCapsule_GetPointer(arg, name); -#else - void *v = sip_api_convert_to_void_ptr(arg); -#endif if (PyErr_Occurred()) { @@ -5435,44 +5305,17 @@ static int parsePass1(PyObject **parseErrp, sipSimpleWrapper **selfp, * has been found to overflow. Raise an appropriate exception and * ensure we don't parse any subsequent overloads. */ -#if PY_MAJOR_VERSION < 3 - PyObject *exc_str_obj; - const char *exc_str; - - if ((exc_str_obj = PyObject_Str(failure.detail_obj)) == NULL) - exc_str = NULL; - else - exc_str = PyString_AsString(exc_str_obj); - - if (exc_str == NULL) - exc_str = "invalid exception text"; -#endif - if (failure.overflow_arg_nr >= 0) { -#if PY_MAJOR_VERSION >= 3 PyErr_Format(PyExc_OverflowError, "argument %d overflowed: %S", failure.overflow_arg_nr, failure.detail_obj); -#else - PyErr_Format(PyExc_OverflowError, "argument %d overflowed: %s", - failure.overflow_arg_nr, exc_str); -#endif } else { -#if PY_MAJOR_VERSION >= 3 PyErr_Format(PyExc_OverflowError, "argument '%s' overflowed: %S", failure.overflow_arg_name, failure.detail_obj); -#else - PyErr_Format(PyExc_OverflowError, "argument '%s' overflowed: %s", - failure.overflow_arg_name, exc_str); -#endif } -#if PY_MAJOR_VERSION < 3 - Py_XDECREF(exc_str_obj); -#endif - /* The overflow exception has now been raised. */ failure.reason = Raised; } @@ -5539,7 +5382,7 @@ static int parsePass2(sipSimpleWrapper *self, int selfarg, PyObject *sipArgs, va_list va) { int a, ok; - SIP_SSIZE_T nr_pos_args; + Py_ssize_t nr_pos_args; /* Handle the converions of "self" first. */ switch (*fmt++) @@ -5728,11 +5571,11 @@ static int parsePass2(sipSimpleWrapper *self, int selfarg, PyObject *sipArgs, const sipTypeDef *td; void **array; - SIP_SSIZE_T *nr_elem; + Py_ssize_t *nr_elem; td = va_arg(va, const sipTypeDef *); array = va_arg(va, void **); - nr_elem = va_arg(va, SIP_SSIZE_T *); + nr_elem = va_arg(va, Py_ssize_t *); if (arg != NULL && !convertFromSequence(arg, td, array, nr_elem)) return FALSE; @@ -5962,7 +5805,7 @@ static int isQObject(PyObject *obj) */ static int canConvertFromSequence(PyObject *seq, const sipTypeDef *td) { - SIP_SSIZE_T i, size = PySequence_Size(seq); + Py_ssize_t i, size = PySequence_Size(seq); if (size < 0) return FALSE; @@ -5993,10 +5836,10 @@ static int canConvertFromSequence(PyObject *seq, const sipTypeDef *td) * canConvertFromSequence(). Return TRUE if the conversion was successful. */ static int convertFromSequence(PyObject *seq, const sipTypeDef *td, - void **array, SIP_SSIZE_T *nr_elem) + void **array, Py_ssize_t *nr_elem) { int iserr = 0; - SIP_SSIZE_T i, size = PySequence_Size(seq); + Py_ssize_t i, size = PySequence_Size(seq); sipArrayFunc array_helper; sipAssignFunc assign_helper; void *array_mem; @@ -6051,10 +5894,10 @@ static int convertFromSequence(PyObject *seq, const sipTypeDef *td, /* * Convert an array of a type to a Python sequence. */ -static PyObject *convertToSequence(void *array, SIP_SSIZE_T nr_elem, +static PyObject *convertToSequence(void *array, Py_ssize_t nr_elem, const sipTypeDef *td) { - SIP_SSIZE_T i; + Py_ssize_t i; PyObject *seq; sipCopyFunc copy_helper; @@ -6101,14 +5944,21 @@ void sip_api_instance_destroyed(sipSimpleWrapper *sw) */ static void sip_api_instance_destroyed_ex(sipSimpleWrapper **sipSelfp) { + /* If there is no interpreter just to the minimum and get out. */ + if (sipInterpreter == NULL) + { + *sipSelfp = NULL; + return; + } + + SIP_BLOCK_THREADS + sipSimpleWrapper *sipSelf = *sipSelfp; - if (sipSelf != NULL && sipInterpreter != NULL) + if (sipSelf != NULL) { PyObject *xtype, *xvalue, *xtb; - SIP_BLOCK_THREADS - /* We may be tidying up after an exception so preserve it. */ PyErr_Fetch(&xtype, &xvalue, &xtb); callPyDtor(sipSelf); @@ -6137,16 +5987,16 @@ static void sip_api_instance_destroyed_ex(sipSimpleWrapper **sipSelfp) removeFromParent((sipWrapper *)sipSelf); } - SIP_UNBLOCK_THREADS + /* + * Normally this is done in the generated dealloc function. However + * this is only called if the pointer/access function has not been + * reset (which it has). It acts as a guard to prevent any further + * invocations of reimplemented virtuals. + */ + *sipSelfp = NULL; } - /* - * Normally this is done in the generated dealloc function. However this - * is only called if the pointer/access function has not been reset (which - * it has). It acts as a guard to prevent any further invocations of - * reimplemented virtuals. - */ - *sipSelfp = NULL; + SIP_UNBLOCK_THREADS } @@ -6175,7 +6025,8 @@ static void callPyDtor(sipSimpleWrapper *self) char pymc = 0; PyObject *meth; - meth = sip_api_is_py_method(&sipGILState, &pymc, self, NULL, "__dtor__"); + meth = sip_api_is_py_method_12_8(&sipGILState, &pymc, &self, NULL, + "__dtor__"); if (meth != NULL) { @@ -6261,8 +6112,8 @@ static void detachChildren(sipWrapper *self) * Convert a sequence index. Return the index or a negative value if there was * an error. */ -static SIP_SSIZE_T sip_api_convert_from_sequence_index(SIP_SSIZE_T idx, - SIP_SSIZE_T len) +static Py_ssize_t sip_api_convert_from_sequence_index(Py_ssize_t idx, + Py_ssize_t len) { /* Negative indices start from the other end. */ if (idx < 0) @@ -6288,13 +6139,7 @@ static PyObject *getDefaultBase(void) /* Only do this once. */ if (default_base == NULL) { -#if PY_VERSION_HEX >= 0x02040000 - default_base = PyTuple_Pack(1, (PyObject *)&sipWrapper_Type); -#else - default_base = Py_BuildValue("(O)", &sipWrapper_Type); -#endif - - if (default_base == NULL) + if ((default_base = PyTuple_Pack(1, (PyObject *)&sipWrapper_Type)) == NULL) return NULL; } @@ -6315,14 +6160,7 @@ static PyObject *getDefaultSimpleBase(void) /* Only do this once. */ if (default_simple_base == NULL) { -#if PY_VERSION_HEX >= 0x02040000 - default_simple_base = PyTuple_Pack(1, - (PyObject *)&sipSimpleWrapper_Type); -#else - default_simple_base = Py_BuildValue("(O)", &sipSimpleWrapper_Type); -#endif - - if (default_simple_base == NULL) + if ((default_simple_base = PyTuple_Pack(1, (PyObject *)&sipSimpleWrapper_Type)) == NULL) return NULL; } @@ -6386,23 +6224,11 @@ static PyObject *createContainerType(sipContainerDef *cod, sipTypeDef *td, } /* Create an object corresponding to the type name. */ -#if PY_MAJOR_VERSION >= 3 - name = PyUnicode_FromString(sipPyNameOfContainer(cod, td)); -#else - name = PyString_FromString(sipPyNameOfContainer(cod, td)); -#endif - - if (name == NULL) + if ((name = PyUnicode_FromString(sipPyNameOfContainer(cod, td))) == NULL) goto reterr; /* Create the type by calling the metatype. */ -#if PY_VERSION_HEX >= 0x02040000 - args = PyTuple_Pack(3, name, bases, type_dict); -#else - args = Py_BuildValue("OOO", name, bases, type_dict); -#endif - - if (args == NULL) + if ((args = PyTuple_Pack(3, name, bases, type_dict)) == NULL) goto relname; /* Pass the type via the back door. */ @@ -6414,7 +6240,6 @@ static PyObject *createContainerType(sipContainerDef *cod, sipTypeDef *td, if (py_type == NULL) goto relargs; -#if PY_VERSION_HEX >= 0x03030000 /* Fix __qualname__ if there is a scope. */ if (scope_td != NULL) { @@ -6429,7 +6254,6 @@ static PyObject *createContainerType(sipContainerDef *cod, sipTypeDef *td, Py_CLEAR(ht->ht_qualname); ht->ht_qualname = qualname; } -#endif /* Add the type to the "parent" dictionary. */ if (PyDict_SetItem(scope_dict, name, py_type) < 0) @@ -6489,11 +6313,7 @@ static int createClassType(sipExportedModuleDef *client, sipClassTypeDef *ctd, if ((supertype = findPyType(supertype_name)) == NULL) goto reterr; -#if PY_VERSION_HEX >= 0x02040000 bases = PyTuple_Pack(1, supertype); -#else - bases = Py_BuildValue("(O)", supertype); -#endif } if (bases == NULL) @@ -6670,25 +6490,14 @@ static sipExportedModuleDef *getModule(PyObject *mname_obj) /* Find the module definition. */ for (em = moduleList; em != NULL; em = em->em_next) -#if PY_MAJOR_VERSION >= 3 if (PyUnicode_Compare(mname_obj, em->em_nameobj) == 0) -#else - if (strcmp(PyString_AS_STRING(mname_obj), sipNameOfModule(em)) == 0) -#endif break; Py_DECREF(mod); if (em == NULL) - { -#if PY_MAJOR_VERSION >= 3 PyErr_Format(PyExc_SystemError, "unable to find to find module: %U", mname_obj); -#else - PyErr_Format(PyExc_SystemError, "unable to find to find module: %s", - PyString_AS_STRING(mname_obj)); -#endif - } return em; } @@ -6706,13 +6515,7 @@ static PyObject *unpickle_type(PyObject *obj, PyObject *args) (void)obj; - if (!PyArg_ParseTuple(args, -#if PY_MAJOR_VERSION >= 3 - "UsO!:_unpickle_type", -#else - "SsO!:_unpickle_type", -#endif - &mname_obj, &tname, &PyTuple_Type, &init_args)) + if (!PyArg_ParseTuple(args, "UsO!:_unpickle_type", &mname_obj, &tname, &PyTuple_Type, &init_args)) return NULL; /* Get the module definition. */ @@ -6809,13 +6612,7 @@ static PyObject *unpickle_enum(PyObject *obj, PyObject *args) (void)obj; - if (!PyArg_ParseTuple(args, -#if PY_MAJOR_VERSION >= 3 - "UsO:_unpickle_enum", -#else - "SsO:_unpickle_enum", -#endif - &mname_obj, &ename, &evalue_obj)) + if (!PyArg_ParseTuple(args, "UsO:_unpickle_enum", &mname_obj, &ename, &evalue_obj)) return NULL; /* Get the module definition. */ @@ -6848,13 +6645,7 @@ static PyObject *pickle_enum(PyObject *obj, PyObject *args) (void)args; return Py_BuildValue("O(Osi)", enum_unpickler, td->td_module->em_nameobj, - sipPyNameOfEnum((sipEnumTypeDef *)td), -#if PY_MAJOR_VERSION >= 3 - (int)PyLong_AS_LONG(obj) -#else - (int)PyInt_AS_LONG(obj) -#endif - ); + sipPyNameOfEnum((sipEnumTypeDef *)td), (int)PyLong_AS_LONG(obj)); } @@ -6904,13 +6695,7 @@ static int createEnum(sipExportedModuleDef *client, sipEnumTypeDef *etd, return -1; /* Create an object corresponding to the type name. */ -#if PY_MAJOR_VERSION >= 3 - name = PyUnicode_FromString(sipPyNameOfEnum(etd)); -#else - name = PyString_FromString(sipPyNameOfEnum(etd)); -#endif - - if (name == NULL) + if ((name = PyUnicode_FromString(sipPyNameOfEnum(etd))) == NULL) return -1; /* Create the enum. */ @@ -6948,29 +6733,15 @@ static PyObject *createUnscopedEnum(sipExportedModuleDef *client, /* Create the base type tuple if it hasn't already been done. */ if (bases == NULL) - { -#if PY_MAJOR_VERSION >= 3 - bases = PyTuple_Pack(1, (PyObject *)&PyLong_Type); -#elif PY_VERSION_HEX >= 0x02040000 - bases = PyTuple_Pack(1, (PyObject *)&PyInt_Type); -#else - bases = Py_BuildValue("(O)", &PyInt_Type); -#endif - - if (bases == NULL) + if ((bases = PyTuple_Pack(1, (PyObject *)&PyLong_Type)) == NULL) return NULL; - } /* Create the type dictionary. */ if ((type_dict = createTypeDict(client)) == NULL) return NULL; /* Create the type by calling the metatype. */ -#if PY_VERSION_HEX >= 0x02040000 args = PyTuple_Pack(3, name, bases, type_dict); -#else - args = Py_BuildValue("OOO", name, bases, type_dict); -#endif Py_DECREF(type_dict); @@ -6992,7 +6763,6 @@ static PyObject *createUnscopedEnum(sipExportedModuleDef *client, if (etd->etd_pyslots != NULL) fix_slots((PyTypeObject *)eto, etd->etd_pyslots); -#if PY_VERSION_HEX >= 0x03030000 /* * If the enum has a scope then the default __qualname__ will be incorrect. */ @@ -7009,7 +6779,6 @@ static PyObject *createUnscopedEnum(sipExportedModuleDef *client, return NULL; } } -#endif return (PyObject *)eto; } @@ -7022,9 +6791,7 @@ static PyObject *createScopedEnum(sipExportedModuleDef *client, sipEnumTypeDef *etd, int enum_nr, PyObject *name) { static PyObject *enum_type = NULL, *module_arg = NULL; -#if PY_VERSION_HEX >= 0x03030000 static PyObject *qualname_arg = NULL; -#endif int i, nr_members; sipEnumMemberDef *enm; PyObject *members, *enum_obj, *args, *kw_args; @@ -7062,36 +6829,16 @@ static PyObject *createScopedEnum(sipExportedModuleDef *client, { if (enm->em_enum == enum_nr) { - int rc; - PyObject *val; + PyObject *val = PyLong_FromLong(enm->em_val); -#if PY_MAJOR_VERSION >= 3 - val = PyLong_FromLong(enm->em_val); -#else - val = PyInt_FromLong(enm->em_val); -#endif - - if (val == NULL) - goto rel_members; - - rc = PyDict_SetItemString(members, enm->em_name, val); - - Py_DECREF(val); - - if (rc < 0) + if (dict_set_and_discard(members, enm->em_name, val) < 0) goto rel_members; } ++enm; } -#if PY_VERSION_HEX >= 0x02040000 - args = PyTuple_Pack(2, name, members); -#else - args = Py_BuildValue("(OO)", name, members); -#endif - - if (args == NULL) + if ((args = PyTuple_Pack(2, name, members)) == NULL) goto rel_members; if ((kw_args = PyDict_New()) == NULL) @@ -7103,7 +6850,6 @@ static PyObject *createScopedEnum(sipExportedModuleDef *client, if (PyDict_SetItem(kw_args, module_arg, client->em_nameobj) < 0) goto rel_kw_args; -#if PY_VERSION_HEX >= 0x03030000 /* * If the enum has a scope then the default __qualname__ will be incorrect. */ @@ -7125,7 +6871,6 @@ static PyObject *createScopedEnum(sipExportedModuleDef *client, if (rc < 0) goto rel_kw_args; } -#endif if ((enum_obj = PyObject_Call(enum_type, args, kw_args)) == NULL) goto rel_kw_args; @@ -7135,7 +6880,7 @@ static PyObject *createScopedEnum(sipExportedModuleDef *client, Py_DECREF(members); /* Note that it isn't actually a PyTypeObject. */ - etd->etd_base.u.td_py_type = (PyTypeObject *)enum_obj; + etd->etd_base.td_py_type = (PyTypeObject *)enum_obj; return enum_obj; @@ -7187,16 +6932,8 @@ static PyObject *createTypeDict(sipExportedModuleDef *em) static int 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) + if ((*objp = PyUnicode_FromString(s)) == NULL) return -1; - } return 0; } @@ -7277,10 +7014,8 @@ static int isNonlazyMethod(PyMethodDef *pmd) "__getattr__", "__enter__", "__exit__", -#if PY_VERSION_HEX >= 0x03050000 "__aenter__", "__aexit__", -#endif NULL }; @@ -7299,17 +7034,9 @@ static int isNonlazyMethod(PyMethodDef *pmd) */ static int addMethod(PyObject *dict, PyMethodDef *pmd) { - int rc; - PyObject *descr; + PyObject *descr = sipMethodDescr_New(pmd); - if ((descr = sipMethodDescr_New(pmd)) == NULL) - return -1; - - rc = PyDict_SetItemString(dict, pmd->ml_name, descr); - - Py_DECREF(descr); - - return rc; + return dict_set_and_discard(dict, pmd->ml_name, descr); } @@ -7338,17 +7065,12 @@ static int add_lazy_container_attrs(sipTypeDef *td, sipContainerDef *cod, /* Do the unscoped enum members. */ for (enm = cod->cod_enummembers, i = 0; i < cod->cod_nrenummembers; ++i, ++enm) { - int rc; PyObject *val; if (enm->em_enum < 0) { /* It's an unnamed unscoped enum. */ -#if PY_MAJOR_VERSION >= 3 val = PyLong_FromLong(enm->em_val); -#else - val = PyInt_FromLong(enm->em_val); -#endif } else { @@ -7360,21 +7082,13 @@ static int add_lazy_container_attrs(sipTypeDef *td, sipContainerDef *cod, val = sip_api_convert_from_enum(enm->em_val, etd); } - if (val == NULL) - return -1; - - rc = PyDict_SetItemString(dict, enm->em_name, val); - - Py_DECREF(val); - - if (rc < 0) + if (dict_set_and_discard(dict, enm->em_name, val) < 0) return -1; } /* Do the variables. */ for (vd = cod->cod_variables, i = 0; i < cod->cod_nrvariables; ++i, ++vd) { - int rc; PyObject *descr; if (vd->vd_type == PropertyVariable) @@ -7382,14 +7096,7 @@ static int add_lazy_container_attrs(sipTypeDef *td, sipContainerDef *cod, else descr = sipVariableDescr_New(vd, td, cod); - if (descr == NULL) - return -1; - - rc = PyDict_SetItemString(dict, vd->vd_name, descr); - - Py_DECREF(descr); - - if (rc < 0) + if (dict_set_and_discard(dict, vd->vd_name, descr) < 0) return -1; } @@ -7420,11 +7127,7 @@ static PyObject *create_property(sipVariableDef *vd) doc = Py_None; Py_INCREF(doc); } -#if PY_MAJOR_VERSION >= 3 else if ((doc = PyUnicode_FromString(vd->vd_docstring)) == NULL) -#else - else if ((doc = PyString_FromString(vd->vd_docstring)) == NULL) -#endif { goto done; } @@ -7498,9 +7201,7 @@ static int add_lazy_attrs(sipTypeDef *td) wt->wt_dict_complete = TRUE; -#if PY_VERSION_HEX >= 0x02060000 PyType_Modified((PyTypeObject *)wt); -#endif return 0; } @@ -7588,17 +7289,15 @@ static const sipTypeDef *sip_api_type_scope(const sipTypeDef *td) */ static int sip_api_can_convert_to_enum(PyObject *obj, const sipTypeDef *td) { + /* Remove in v5.1. */ + assert(sipTypeIsEnum(td)); /* If the object is an enum then it must be the right enum. */ if (PyObject_TypeCheck((PyObject *)Py_TYPE(obj), &sipEnumType_Type)) return (PyObject_TypeCheck(obj, sipTypeAsPyTypeObject(td))); -#if PY_MAJOR_VERSION >= 3 return PyLong_Check(obj); -#else - return PyInt_Check(obj); -#endif } @@ -7656,7 +7355,7 @@ static int convert_to_enum(PyObject *obj, const sipTypeDef *td, int allow_int) /* This will never overflow. */ val = long_as_nonoverflow_int(obj); } - else if (allow_int && SIPLong_Check(obj)) + else if (allow_int && PyLong_Check(obj)) { val = long_as_nonoverflow_int(obj); } @@ -7796,14 +7495,7 @@ static void sip_api_no_method(PyObject *parseErr, const char *scope, if (doc_obj != NULL) { -#if PY_MAJOR_VERSION >= 3 exc = PyUnicode_FromFormat("%U: %U", doc_obj, detail); -#else - exc = PyString_FromFormat("%s: %s", - PyString_AS_STRING(doc_obj), - PyString_AS_STRING(detail)); -#endif - Py_DECREF(doc_obj); } else @@ -7813,13 +7505,8 @@ static void sip_api_no_method(PyObject *parseErr, const char *scope, } else { -#if PY_MAJOR_VERSION >= 3 exc = PyUnicode_FromFormat("%s%s%s(): %U", scope, sep, method, detail); -#else - exc = PyString_FromFormat("%s%s%s(): %s", scope, sep, - method, PyString_AS_STRING(detail)); -#endif } Py_DECREF(detail); @@ -7833,26 +7520,13 @@ static void sip_api_no_method(PyObject *parseErr, const char *scope, { static const char *summary = "arguments did not match any overloaded call:"; - SIP_SSIZE_T i; + Py_ssize_t i; if (doc != NULL) - { -#if PY_MAJOR_VERSION >= 3 exc = PyUnicode_FromString(summary); -#else - exc = PyString_FromString(summary); -#endif - } else - { -#if PY_MAJOR_VERSION >= 3 exc = PyUnicode_FromFormat("%s%s%s(): %s", scope, sep, method, summary); -#else - exc = PyString_FromFormat("%s%s%s(): %s", scope, sep, method, - summary); -#endif - } for (i = 0; i < PyList_GET_SIZE(parseErr); ++i) { @@ -7868,14 +7542,8 @@ static void sip_api_no_method(PyObject *parseErr, const char *scope, if (doc_obj != NULL) { -#if PY_MAJOR_VERSION >= 3 failure = PyUnicode_FromFormat("\n %U: %U", doc_obj, detail); -#else - failure = PyString_FromFormat("\n %s: %s", - PyString_AS_STRING(doc_obj), - PyString_AS_STRING(detail)); -#endif Py_DECREF(doc_obj); } @@ -7888,24 +7556,13 @@ static void sip_api_no_method(PyObject *parseErr, const char *scope, } else { -#if PY_MAJOR_VERSION >= 3 - failure = PyUnicode_FromFormat( - "\n overload " SIP_SSIZE_T_FORMAT ": %U", i + 1, - detail); -#else - failure = PyString_FromFormat( - "\n overload " SIP_SSIZE_T_FORMAT ": %s", i + 1, - PyString_AS_STRING(detail)); -#endif + failure = PyUnicode_FromFormat("\n overload %zd: %U", + i + 1, detail); } Py_DECREF(detail); -#if PY_MAJOR_VERSION >= 3 PyUnicode_AppendAndDel(&exc, failure); -#else - PyString_ConcatAndDel(&exc, failure); -#endif } else { @@ -7939,10 +7596,10 @@ static void sip_api_no_method(PyObject *parseErr, const char *scope, * Return a string/unicode object extracted from a particular line of a * docstring. */ -static PyObject *signature_FromDocstring(const char *doc, SIP_SSIZE_T line) +static PyObject *signature_FromDocstring(const char *doc, Py_ssize_t line) { const char *eol; - SIP_SSIZE_T size = 0; + Py_ssize_t size = 0; /* * Find the start of the line. If there is a non-default versioned @@ -7964,11 +7621,7 @@ static PyObject *signature_FromDocstring(const char *doc, SIP_SSIZE_T line) if (*eol == ')') size = eol - doc + 1; -#if PY_MAJOR_VERSION >= 3 return PyUnicode_FromStringAndSize(doc, size); -#else - return PyString_FromStringAndSize(doc, size); -#endif } @@ -7980,106 +7633,48 @@ static PyObject *detail_FromFailure(PyObject *failure_obj) sipParseFailure *failure; PyObject *detail; -#if defined(SIP_USE_PYCAPSULE) failure = (sipParseFailure *)PyCapsule_GetPointer(failure_obj, NULL); -#else - failure = (sipParseFailure *)PyCObject_AsVoidPtr(failure_obj); -#endif switch (failure->reason) { case Unbound: -#if PY_MAJOR_VERSION >= 3 detail = PyUnicode_FromFormat( "first argument of unbound method must have type '%s'", failure->detail_str); -#else - detail = PyString_FromFormat( - "first argument of unbound method must have type '%s'", - failure->detail_str); -#endif break; case TooFew: -#if PY_MAJOR_VERSION >= 3 detail = PyUnicode_FromString("not enough arguments"); -#else - detail = PyString_FromString("not enough arguments"); -#endif break; case TooMany: -#if PY_MAJOR_VERSION >= 3 detail = PyUnicode_FromString("too many arguments"); -#else - detail = PyString_FromString("too many arguments"); -#endif break; case KeywordNotString: -#if PY_MAJOR_VERSION >= 3 detail = PyUnicode_FromFormat( "%S keyword argument name is not a string", failure->detail_obj); -#else - { - PyObject *str = PyObject_Str(failure->detail_obj); - - if (str != NULL) - { - detail = PyString_FromFormat( - "%s keyword argument name is not a string", - PyString_AsString(str)); - - Py_DECREF(str); - } - else - { - detail = NULL; - } - } -#endif break; case UnknownKeyword: -#if PY_MAJOR_VERSION >= 3 detail = PyUnicode_FromFormat("'%U' is not a valid keyword argument", failure->detail_obj); -#else - detail = PyString_FromFormat("'%s' is not a valid keyword argument", - PyString_AS_STRING(failure->detail_obj)); -#endif break; case Duplicate: -#if PY_MAJOR_VERSION >= 3 detail = PyUnicode_FromFormat( "'%U' has already been given as a positional argument", failure->detail_obj); -#else - detail = PyString_FromFormat( - "'%s' has already been given as a positional argument", - PyString_AS_STRING(failure->detail_obj)); -#endif break; case WrongType: if (failure->arg_nr >= 0) - { detail = bad_type_str(failure->arg_nr, failure->detail_obj); - } else - { -#if PY_MAJOR_VERSION >= 3 detail = PyUnicode_FromFormat( "argument '%s' has unexpected type '%s'", failure->arg_name, Py_TYPE(failure->detail_obj)->tp_name); -#else - detail = PyString_FromFormat( - "argument '%s' has unexpected type '%s'", - failure->arg_name, Py_TYPE(failure->detail_obj)->tp_name); -#endif - } break; @@ -8095,11 +7690,7 @@ static PyObject *detail_FromFailure(PyObject *failure_obj) /* Drop through. */ default: -#if PY_MAJOR_VERSION >= 3 detail = PyUnicode_FromString("unknown reason"); -#else - detail = PyString_FromString("unknown reason"); -#endif } return detail; @@ -8120,7 +7711,7 @@ static void sip_api_abstract_method(const char *classname, const char *method) /* * Report a deprecated class or method. */ -static int sip_api_deprecated(const char *classname, const char *method) +int sip_api_deprecated(const char *classname, const char *method) { char buf[100]; @@ -8133,11 +7724,7 @@ static int sip_api_deprecated(const char *classname, const char *method) PyOS_snprintf(buf, sizeof (buf), "%s.%s() is deprecated", classname, method); -#if PY_VERSION_HEX >= 0x02050000 return PyErr_WarnEx(PyExc_DeprecationWarning, buf, 1); -#else - return PyErr_Warn(PyExc_DeprecationWarning, buf); -#endif } @@ -8183,11 +7770,11 @@ static void sip_api_bad_operator_arg(PyObject *self, PyObject *arg, /* * Report a sequence length that does not match the length of a slice. */ -static void sip_api_bad_length_for_slice(SIP_SSIZE_T seqlen, - SIP_SSIZE_T slicelen) +static void sip_api_bad_length_for_slice(Py_ssize_t seqlen, + Py_ssize_t slicelen) { PyErr_Format(PyExc_ValueError, - "attempt to assign sequence of size " SIP_SSIZE_T_FORMAT " to slice of size " SIP_SSIZE_T_FORMAT, + "attempt to assign sequence of size %zd to slice of size %zd", seqlen, slicelen); } @@ -8235,31 +7822,14 @@ static void sip_api_bad_catcher_result(PyObject *method) if (evalue != NULL) { -#if PY_MAJOR_VERSION >= 3 PyErr_Format(etype, "invalid result from %s.%U(), %S", Py_TYPE(PyMethod_GET_SELF(method))->tp_name, mname, evalue); -#else - PyObject *evalue_s = PyObject_Str(evalue); - - PyErr_Format(etype, "invalid result from %s.%s(), %s", - Py_TYPE(PyMethod_GET_SELF(method))->tp_name, - PyString_AsString(mname), PyString_AsString(evalue_s)); - - Py_XDECREF(evalue_s); -#endif - Py_DECREF(evalue); } else { -#if PY_MAJOR_VERSION >= 3 PyErr_Format(PyExc_TypeError, "invalid result from %s.%U()", Py_TYPE(PyMethod_GET_SELF(method))->tp_name, mname); -#else - PyErr_Format(PyExc_TypeError, "invalid result from %s.%s()", - Py_TYPE(PyMethod_GET_SELF(method))->tp_name, - PyString_AsString(mname)); -#endif } Py_XDECREF(etype); @@ -8296,6 +7866,8 @@ static void sip_api_transfer_back(PyObject *self) */ static void sip_api_transfer_break(PyObject *self) { + /* Remove in v5.1. */ + if (self != NULL && PyObject_TypeCheck(self, (PyTypeObject *)&sipWrapper_Type)) { sipSimpleWrapper *sw = (sipSimpleWrapper *)self; @@ -8425,13 +7997,7 @@ static int addLicense(PyObject *dict,sipLicenseDef *lc) if (lc->lc_type == NULL) goto deldict; -#if PY_MAJOR_VERSION >= 3 - o = PyUnicode_FromString(lc->lc_type); -#else - o = PyString_FromString(lc->lc_type); -#endif - - if (o == NULL) + if ((o = PyUnicode_FromString(lc->lc_type)) == NULL) goto deldict; rc = PyDict_SetItem(ldict,typeName,o); @@ -8442,13 +8008,7 @@ static int addLicense(PyObject *dict,sipLicenseDef *lc) if (lc->lc_licensee != NULL) { -#if PY_MAJOR_VERSION >= 3 - o = PyUnicode_FromString(lc->lc_licensee); -#else - o = PyString_FromString(lc->lc_licensee); -#endif - - if (o == NULL) + if ((o = PyUnicode_FromString(lc->lc_licensee)) == NULL) goto deldict; rc = PyDict_SetItem(ldict,licenseeName,o); @@ -8460,13 +8020,7 @@ static int addLicense(PyObject *dict,sipLicenseDef *lc) if (lc->lc_timestamp != NULL) { -#if PY_MAJOR_VERSION >= 3 - o = PyUnicode_FromString(lc->lc_timestamp); -#else - o = PyString_FromString(lc->lc_timestamp); -#endif - - if (o == NULL) + if ((o = PyUnicode_FromString(lc->lc_timestamp)) == NULL) goto deldict; rc = PyDict_SetItem(ldict,timestampName,o); @@ -8478,13 +8032,7 @@ static int addLicense(PyObject *dict,sipLicenseDef *lc) if (lc->lc_signature != NULL) { -#if PY_MAJOR_VERSION >= 3 - o = PyUnicode_FromString(lc->lc_signature); -#else - o = PyString_FromString(lc->lc_signature); -#endif - - if (o == NULL) + if ((o = PyUnicode_FromString(lc->lc_signature)) == NULL) goto deldict; rc = PyDict_SetItem(ldict,signatureName,o); @@ -8519,16 +8067,9 @@ static int addVoidPtrInstances(PyObject *dict,sipVoidPtrInstanceDef *vi) { while (vi->vi_name != NULL) { - int rc; - PyObject *w; + PyObject *w = sip_api_convert_from_void_ptr(vi->vi_val); - if ((w = sip_api_convert_from_void_ptr(vi->vi_val)) == NULL) - return -1; - - rc = PyDict_SetItemString(dict,vi->vi_name,w); - Py_DECREF(w); - - if (rc < 0) + if (dict_set_and_discard(dict, vi->vi_name, w) < 0) return -1; ++vi; @@ -8545,7 +8086,6 @@ static int addCharInstances(PyObject *dict, sipCharInstanceDef *ci) { while (ci->ci_name != NULL) { - int rc; PyObject *w; switch (ci->ci_encoding) @@ -8559,24 +8099,14 @@ static int addCharInstances(PyObject *dict, sipCharInstanceDef *ci) break; case '8': -#if PY_MAJOR_VERSION >= 3 w = PyUnicode_FromStringAndSize(&ci->ci_val, 1); -#else - w = PyUnicode_DecodeUTF8(&ci->ci_val, 1, NULL); -#endif break; default: - w = SIPBytes_FromStringAndSize(&ci->ci_val, 1); + w = PyBytes_FromStringAndSize(&ci->ci_val, 1); } - if (w == NULL) - return -1; - - rc = PyDict_SetItemString(dict, ci->ci_name, w); - Py_DECREF(w); - - if (rc < 0) + if (dict_set_and_discard(dict, ci->ci_name, w) < 0) return -1; ++ci; @@ -8593,7 +8123,6 @@ static int addStringInstances(PyObject *dict, sipStringInstanceDef *si) { while (si->si_name != NULL) { - int rc; PyObject *w; switch (si->si_encoding) @@ -8607,11 +8136,7 @@ static int addStringInstances(PyObject *dict, sipStringInstanceDef *si) break; case '8': -#if PY_MAJOR_VERSION >= 3 w = PyUnicode_FromString(si->si_val); -#else - w = PyUnicode_DecodeUTF8(si->si_val, strlen(si->si_val), NULL); -#endif break; case 'w': @@ -8636,16 +8161,10 @@ static int addStringInstances(PyObject *dict, sipStringInstanceDef *si) #endif default: - w = SIPBytes_FromString(si->si_val); + w = PyBytes_FromString(si->si_val); } - if (w == NULL) - return -1; - - rc = PyDict_SetItemString(dict, si->si_name, w); - Py_DECREF(w); - - if (rc < 0) + if (dict_set_and_discard(dict, si->si_name, w) < 0) return -1; ++si; @@ -8662,22 +8181,9 @@ static int addIntInstances(PyObject *dict, sipIntInstanceDef *ii) { while (ii->ii_name != NULL) { - int rc; - PyObject *w; + PyObject *w = PyLong_FromLong(ii->ii_val); -#if PY_MAJOR_VERSION >= 3 - w = PyLong_FromLong(ii->ii_val); -#else - w = PyInt_FromLong(ii->ii_val); -#endif - - if (w == NULL) - return -1; - - rc = PyDict_SetItemString(dict, ii->ii_name, w); - Py_DECREF(w); - - if (rc < 0) + if (dict_set_and_discard(dict, ii->ii_name, w) < 0) return -1; ++ii; @@ -8694,16 +8200,9 @@ static int addLongInstances(PyObject *dict,sipLongInstanceDef *li) { while (li->li_name != NULL) { - int rc; - PyObject *w; + PyObject *w = PyLong_FromLong(li->li_val); - if ((w = PyLong_FromLong(li->li_val)) == NULL) - return -1; - - rc = PyDict_SetItemString(dict,li->li_name,w); - Py_DECREF(w); - - if (rc < 0) + if (dict_set_and_discard(dict, li->li_name, w) < 0) return -1; ++li; @@ -8720,16 +8219,9 @@ static int addUnsignedLongInstances(PyObject *dict, sipUnsignedLongInstanceDef * { while (uli->uli_name != NULL) { - int rc; - PyObject *w; + PyObject *w = PyLong_FromUnsignedLong(uli->uli_val); - if ((w = PyLong_FromUnsignedLong(uli->uli_val)) == NULL) - return -1; - - rc = PyDict_SetItemString(dict, uli->uli_name, w); - Py_DECREF(w); - - if (rc < 0) + if (dict_set_and_discard(dict, uli->uli_name, w) < 0) return -1; ++uli; @@ -8746,20 +8238,15 @@ static int addLongLongInstances(PyObject *dict, sipLongLongInstanceDef *lli) { while (lli->lli_name != NULL) { - int rc; PyObject *w; #if defined(HAVE_LONG_LONG) - if ((w = PyLong_FromLongLong(lli->lli_val)) == NULL) + w = PyLong_FromLongLong(lli->lli_val); #else - if ((w = PyLong_FromLong(lli->lli_val)) == NULL) + w = PyLong_FromLong(lli->lli_val); #endif - return -1; - rc = PyDict_SetItemString(dict, lli->lli_name, w); - Py_DECREF(w); - - if (rc < 0) + if (dict_set_and_discard(dict, lli->lli_name, w) < 0) return -1; ++lli; @@ -8776,20 +8263,15 @@ static int addUnsignedLongLongInstances(PyObject *dict, sipUnsignedLongLongInsta { while (ulli->ulli_name != NULL) { - int rc; PyObject *w; #if defined(HAVE_LONG_LONG) - if ((w = PyLong_FromUnsignedLongLong(ulli->ulli_val)) == NULL) + w = PyLong_FromUnsignedLongLong(ulli->ulli_val); #else - if ((w = PyLong_FromUnsignedLong(ulli->ulli_val)) == NULL) + w = PyLong_FromUnsignedLong(ulli->ulli_val); #endif - return -1; - rc = PyDict_SetItemString(dict, ulli->ulli_name, w); - Py_DECREF(w); - - if (rc < 0) + if (dict_set_and_discard(dict, ulli->ulli_name, w) < 0) return -1; ++ulli; @@ -8806,16 +8288,9 @@ static int addDoubleInstances(PyObject *dict,sipDoubleInstanceDef *di) { while (di->di_name != NULL) { - int rc; - PyObject *w; + PyObject *w = PyFloat_FromDouble(di->di_val); - if ((w = PyFloat_FromDouble(di->di_val)) == NULL) - return -1; - - rc = PyDict_SetItemString(dict,di->di_name,w); - Py_DECREF(w); - - if (rc < 0) + if (dict_set_and_discard(dict, di->di_name, w) < 0) return -1; ++di; @@ -8848,7 +8323,6 @@ static int addTypeInstances(PyObject *dict, sipTypeInstanceDef *ti) static int addSingleTypeInstance(PyObject *dict, const char *name, void *cppPtr, const sipTypeDef *td, int initflags) { - int rc; PyObject *obj; if (sipTypeIsEnum(td) || sipTypeIsScopedEnum(td)) @@ -8869,13 +8343,7 @@ static int addSingleTypeInstance(PyObject *dict, const char *name, obj = wrap_simple_instance(cppPtr, td, NULL, initflags); } - if (obj == NULL) - return -1; - - rc = PyDict_SetItemString(dict, name, obj); - Py_DECREF(obj); - - return rc; + return dict_set_and_discard(dict, name, obj); } @@ -8904,13 +8372,26 @@ static PyObject *getDictFromObject(PyObject *obj) /* * Return a Python reimplementation corresponding to a C/C++ virtual function, - * if any. If one was found then the GIL is acquired. + * if any. If one was found then the GIL is acquired. This is deprecated, use + * sip_api_is_python_method_12_8() instead. */ static PyObject *sip_api_is_py_method(sip_gilstate_t *gil, char *pymc, sipSimpleWrapper *sipSelf, const char *cname, const char *mname) { + return sip_api_is_py_method_12_8(gil, pymc, &sipSelf, cname, mname); +} + + +/* + * Return a Python reimplementation corresponding to a C/C++ virtual function, + * if any. If one was found then the GIL is acquired. + */ +static PyObject *sip_api_is_py_method_12_8(sip_gilstate_t *gil, char *pymc, + sipSimpleWrapper **sipSelfp, const char *cname, const char *mname) +{ + sipSimpleWrapper *sipSelf; PyObject *mname_obj, *reimp, *mro, *cls; - SIP_SSIZE_T i; + Py_ssize_t i; /* * This is the most common case (where there is no Python reimplementation) @@ -8927,6 +8408,9 @@ static PyObject *sip_api_is_py_method(sip_gilstate_t *gil, char *pymc, *gil = PyGILState_Ensure(); #endif + /* Only read this when we have the GIL. */ + sipSelf = *sipSelfp; + /* * It's possible that the Python object has been deleted but the underlying * C++ instance is still working and trying to handle virtual functions. @@ -8954,13 +8438,7 @@ static PyObject *sip_api_is_py_method(sip_gilstate_t *gil, char *pymc, /* Get any reimplementation. */ -#if PY_MAJOR_VERSION >= 3 - mname_obj = PyUnicode_FromString(mname); -#else - mname_obj = PyString_FromString(mname); -#endif - - if (mname_obj == NULL) + if ((mname_obj = PyUnicode_FromString(mname)) == NULL) goto release_gil; /* @@ -8997,15 +8475,7 @@ static PyObject *sip_api_is_py_method(sip_gilstate_t *gil, char *pymc, cls = PyTuple_GET_ITEM(mro, i); -#if PY_MAJOR_VERSION >= 3 cls_dict = ((PyTypeObject *)cls)->tp_dict; -#else - /* Allow for classic classes as mixins. */ - if (PyClass_Check(cls)) - cls_dict = ((PyClassObject *)cls)->cl_dict; - else - cls_dict = ((PyTypeObject *)cls)->tp_dict; -#endif /* * Check any possible reimplementation is not the wrapped C++ method or @@ -9030,27 +8500,14 @@ static PyObject *sip_api_is_py_method(sip_gilstate_t *gil, char *pymc, { /* It's already a method but make sure it is bound. */ if (PyMethod_GET_SELF(reimp) != NULL) - { Py_INCREF(reimp); - } else - { -#if PY_MAJOR_VERSION >= 3 reimp = PyMethod_New(PyMethod_GET_FUNCTION(reimp), (PyObject *)sipSelf); -#else - reimp = PyMethod_New(PyMethod_GET_FUNCTION(reimp), - (PyObject *)sipSelf, PyMethod_GET_CLASS(reimp)); -#endif - } } else if (PyFunction_Check(reimp)) { -#if PY_MAJOR_VERSION >= 3 reimp = PyMethod_New(reimp, (PyObject *)sipSelf); -#else - reimp = PyMethod_New(reimp, (PyObject *)sipSelf, cls); -#endif } else if (Py_TYPE(reimp)->tp_descr_get) { @@ -9271,13 +8728,7 @@ static void sip_api_keep_reference(PyObject *self, int key, PyObject *obj) ((sipSimpleWrapper *)self)->extra_refs = dict; } -#if PY_MAJOR_VERSION >= 3 - key_obj = PyLong_FromLong(key); -#else - key_obj = PyInt_FromLong(key); -#endif - - if (key_obj != NULL) + if ((key_obj = PyLong_FromLong(key)) != NULL) { /* This can happen if the argument was optional. */ if (obj == NULL) @@ -9300,16 +8751,11 @@ static PyObject *sip_api_get_reference(PyObject *self, int key) if ((dict = ((sipSimpleWrapper *)self)->extra_refs) == NULL) return NULL; -#if PY_MAJOR_VERSION >= 3 - key_obj = PyLong_FromLong(key); -#else - key_obj = PyInt_FromLong(key); -#endif - - if (key_obj == NULL) + if ((key_obj = PyLong_FromLong(key)) == NULL) return NULL; obj = PyDict_GetItem(dict, key_obj); + Py_DECREF(key_obj); Py_XINCREF(obj); return obj; @@ -9781,6 +9227,8 @@ static const sipTypeDef *sip_api_find_type(const char *type) */ static const sipMappedType *sip_api_find_mapped_type(const char *type) { + /* Remove in v5.1. */ + const sipTypeDef *td = sip_api_find_type(type); if (td != NULL && sipTypeIsMapped(td)) @@ -9795,6 +9243,8 @@ static const sipMappedType *sip_api_find_mapped_type(const char *type) */ static sipWrapperType *sip_api_find_class(const char *type) { + /* Remove in v5.1. */ + const sipTypeDef *td = sip_api_find_type(type); if (td != NULL && sipTypeIsClass(td)) @@ -9810,6 +9260,8 @@ static sipWrapperType *sip_api_find_class(const char *type) */ static PyTypeObject *sip_api_find_named_enum(const char *type) { + /* Remove in v5.1. */ + const sipTypeDef *td = sip_api_find_type(type); if (td != NULL && sipTypeIsEnum(td)) @@ -9826,9 +9278,6 @@ void sipSaveMethod(sipPyMethod *pm, PyObject *meth) { pm->mfunc = PyMethod_GET_FUNCTION(meth); pm->mself = PyMethod_GET_SELF(meth); -#if PY_MAJOR_VERSION < 3 - pm->mclass = PyMethod_GET_CLASS(meth); -#endif } @@ -9843,15 +9292,9 @@ static void sip_api_call_hook(const char *hookname) if ((dictofmods = PyImport_GetModuleDict()) == NULL) return; -#if PY_MAJOR_VERSION >= 3 /* Get the builtins module. */ if ((mod = PyDict_GetItemString(dictofmods, "builtins")) == NULL) return; -#else - /* Get the __builtin__ module. */ - if ((mod = PyDict_GetItemString(dictofmods, "__builtin__")) == NULL) - return; -#endif /* Get it's dictionary. */ if ((dict = PyModule_GetDict(mod)) == NULL) @@ -9988,6 +9431,8 @@ static int compareStringMapEntry(const void *key,const void *el) static sipWrapperType *sip_api_map_string_to_class(const char *typeString, const sipStringTypeClassMap *map, int maplen) { + /* Remove in v5.1. */ + sipStringTypeClassMap *me; me = (sipStringTypeClassMap *)bsearch((const void *)typeString, @@ -10024,6 +9469,8 @@ static int compareIntMapEntry(const void *keyp,const void *el) static sipWrapperType *sip_api_map_int_to_class(int typeInt, const sipIntTypeClassMap *map, int maplen) { + /* Remove in v5.1. */ + sipIntTypeClassMap *me; me = (sipIntTypeClassMap *)bsearch((const void *)&typeInt, @@ -10218,16 +9665,9 @@ static int objobjargprocSlot(PyObject *self, PyObject *arg1, PyObject *arg2, args = arg1; Py_INCREF(args); } - else + else if ((args = PyTuple_Pack(2, arg1, arg2)) == NULL) { -#if PY_VERSION_HEX >= 0x02040000 - args = PyTuple_Pack(2, arg1, arg2); -#else - args = Py_BuildValue("(OO)", arg1, arg2); -#endif - - if (args == NULL) - return -1; + return -1; } res = f(self, args); @@ -10246,7 +9686,7 @@ static int objobjargprocSlot(PyObject *self, PyObject *arg1, PyObject *arg2, /* * Handle an ssizeobjargproc slot. */ -static int ssizeobjargprocSlot(PyObject *self, SIP_SSIZE_T arg1, +static int ssizeobjargprocSlot(PyObject *self, Py_ssize_t arg1, PyObject *arg2, sipPySlotType st) { int (*f)(PyObject *, PyObject *); @@ -10263,19 +9703,9 @@ static int ssizeobjargprocSlot(PyObject *self, SIP_SSIZE_T arg1, * optional. */ if (arg2 == NULL) -#if PY_MAJOR_VERSION >= 3 args = PyLong_FromSsize_t(arg1); -#elif PY_VERSION_HEX >= 0x02050000 - args = PyInt_FromSsize_t(arg1); -#else - args = PyInt_FromLong(arg1); -#endif - else -#if PY_VERSION_HEX >= 0x02050000 + else args = Py_BuildValue("(nO)", arg1, arg2); -#else - args = Py_BuildValue("(iO)", arg1, arg2); -#endif if (args == NULL) return -1; @@ -10296,7 +9726,7 @@ static int ssizeobjargprocSlot(PyObject *self, SIP_SSIZE_T arg1, /* * The metatype alloc slot. */ -static PyObject *sipWrapperType_alloc(PyTypeObject *self, SIP_SSIZE_T nitems) +static PyObject *sipWrapperType_alloc(PyTypeObject *self, Py_ssize_t nitems) { PyObject *o; @@ -10395,9 +9825,9 @@ static int sipWrapperType_init(sipWrapperType *self, PyObject *args, * We must be a generated type so remember the type object in the * generated type structure. */ - assert(self->wt_td->u.td_py_type == NULL); + assert(self->wt_td->td_py_type == NULL); - self->wt_td->u.td_py_type = (PyTypeObject *)self; + self->wt_td->td_py_type = (PyTypeObject *)self; } return 0; @@ -10766,28 +10196,13 @@ static int sipSimpleWrapper_init(sipSimpleWrapper *self, PyObject *args, if (PyDict_Size(unused) != 0) { PyObject *key, *value; - SIP_SSIZE_T pos = 0; + Py_ssize_t pos = 0; /* Just report one of the unused arguments. */ PyDict_Next(unused, &pos, &key, &value); -#if PY_MAJOR_VERSION >= 3 PyErr_Format(PyExc_TypeError, "'%S' is an unknown keyword argument", key); -#else - { - PyObject *key_s = PyObject_Str(key); - - if (key_s != NULL) - { - PyErr_Format(PyExc_TypeError, - "'%s' is an unknown keyword argument", - PyString_AsString(key_s)); - - Py_DECREF(key_s); - } - } -#endif Py_DECREF(unused); @@ -10831,17 +10246,15 @@ static int sip_api_init_mixin(PyObject *self, PyObject *args, PyObject *kwds, const sipClassTypeDef *ctd) { int rc; - SIP_SSIZE_T pos; + Py_ssize_t pos; PyObject *unused, *mixin, *mixin_name, *key, *value; PyTypeObject *self_wt = sipTypeAsPyTypeObject(((sipWrapperType *)Py_TYPE(self))->wt_td); PyTypeObject *wt = sipTypeAsPyTypeObject(&ctd->ctd_base); -#if PY_MAJOR_VERSION >= 3 static PyObject *double_us = NULL; if (objectify("__", &double_us) < 0) return -1; -#endif /* If we are not a mixin to another wrapped class then behave as normal. */ if (PyType_IsSubtype(self_wt, wt)) @@ -10865,13 +10278,7 @@ static int sip_api_init_mixin(PyObject *self, PyObject *args, PyObject *kwds, ((sipSimpleWrapper *)mixin)->mixin_main = self; Py_INCREF(self); -#if PY_MAJOR_VERSION >= 3 - mixin_name = PyUnicode_FromString(sipTypeName(&ctd->ctd_base)); -#else - mixin_name = PyString_FromString(sipTypeName(&ctd->ctd_base)); -#endif - - if (mixin_name == NULL) + if ((mixin_name = PyUnicode_FromString(sipTypeName(&ctd->ctd_base))) == NULL) { Py_DECREF(mixin); goto gc_unused; @@ -10893,7 +10300,6 @@ static int sip_api_init_mixin(PyObject *self, PyObject *args, PyObject *kwds, continue; /* Skip values with names that start with double underscore. */ -#if PY_MAJOR_VERSION >= 3 if (!PyUnicode_Check(key)) continue; @@ -10908,13 +10314,6 @@ static int sip_api_init_mixin(PyObject *self, PyObject *args, PyObject *kwds, if (rc > 0) continue; -#else - if (!PyString_Check(key)) - continue; - - if (PyString_GET_SIZE(key) >= 2 && strncmp(PyString_AS_STRING(key), "__", 2) == 0) - continue; -#endif if (PyObject_IsInstance(value, (PyObject *)&sipMethodDescr_Type)) { @@ -10962,7 +10361,7 @@ gc_unused: */ static PyObject *next_in_mro(PyObject *self, PyObject *after) { - SIP_SSIZE_T i; + Py_ssize_t i; PyObject *mro; mro = Py_TYPE(self)->tp_mro; @@ -11151,9 +10550,8 @@ static int sipSimpleWrapper_clear(sipSimpleWrapper *self) } -#if PY_MAJOR_VERSION >= 3 /* - * The instance get buffer slot for Python v3. + * The instance get buffer slot. */ static int sipSimpleWrapper_getbuffer(sipSimpleWrapper *self, Py_buffer *buf, int flags) @@ -11185,12 +10583,10 @@ static int sipSimpleWrapper_getbuffer(sipSimpleWrapper *self, Py_buffer *buf, return ctd->ctd_getbuffer((PyObject *)self, ptr, buf, flags); } -#endif -#if PY_MAJOR_VERSION >= 3 /* - * The instance release buffer slot for Python v3. + * The instance release buffer slot. */ static void sipSimpleWrapper_releasebuffer(sipSimpleWrapper *self, Py_buffer *buf) @@ -11212,79 +10608,6 @@ static void sipSimpleWrapper_releasebuffer(sipSimpleWrapper *self, ctd->ctd_releasebuffer((PyObject *)self, ptr, buf); } -#endif - - -#if PY_MAJOR_VERSION < 3 -/* - * The instance read buffer slot for Python v2. - */ -static SIP_SSIZE_T sipSimpleWrapper_getreadbuffer(sipSimpleWrapper *self, - SIP_SSIZE_T segment, void **ptrptr) -{ - void *ptr; - const sipClassTypeDef *ctd; - - if ((ptr = getPtrTypeDef(self, &ctd)) == NULL) - return -1; - - return ctd->ctd_readbuffer((PyObject *)self, ptr, segment, ptrptr); -} -#endif - - -#if PY_MAJOR_VERSION < 3 -/* - * The instance write buffer slot for Python v2. - */ -static SIP_SSIZE_T sipSimpleWrapper_getwritebuffer(sipSimpleWrapper *self, - SIP_SSIZE_T segment, void **ptrptr) -{ - void *ptr; - const sipClassTypeDef *ctd; - - if ((ptr = getPtrTypeDef(self, &ctd)) == NULL) - return -1; - - return ctd->ctd_writebuffer((PyObject *)self, ptr, segment, ptrptr); -} -#endif - - -#if PY_MAJOR_VERSION < 3 -/* - * The instance segment count slot for Python v2. - */ -static SIP_SSIZE_T sipSimpleWrapper_getsegcount(sipSimpleWrapper *self, - SIP_SSIZE_T *lenp) -{ - void *ptr; - const sipClassTypeDef *ctd; - - if ((ptr = getPtrTypeDef(self, &ctd)) == NULL) - return 0; - - return ctd->ctd_segcount((PyObject *)self, ptr, lenp); -} -#endif - - -#if PY_MAJOR_VERSION < 3 -/* - * The instance char buffer slot for Python v2. - */ -static SIP_SSIZE_T sipSimpleWrapper_getcharbuffer(sipSimpleWrapper *self, - SIP_SSIZE_T segment, void **ptrptr) -{ - void *ptr; - const sipClassTypeDef *ctd; - - if ((ptr = getPtrTypeDef(self, &ctd)) == NULL) - return -1; - - return ctd->ctd_charbuffer((PyObject *)self, ptr, segment, ptrptr); -} -#endif /* @@ -11292,6 +10615,11 @@ static SIP_SSIZE_T sipSimpleWrapper_getcharbuffer(sipSimpleWrapper *self, */ static void sipSimpleWrapper_dealloc(sipSimpleWrapper *self) { + PyObject *error_type, *error_value, *error_traceback; + + /* Save the current exception, if any. */ + PyErr_Fetch(&error_type, &error_value, &error_traceback); + forgetObject(self); /* @@ -11303,6 +10631,9 @@ static void sipSimpleWrapper_dealloc(sipSimpleWrapper *self) /* Call the standard super-type dealloc. */ PyBaseObject_Type.tp_dealloc((PyObject *)self); + + /* Restore the saved exception. */ + PyErr_Restore(error_type, error_value, error_traceback); } @@ -11324,20 +10655,12 @@ static PyObject *slot_call(PyObject *self, PyObject *args, PyObject *kw) /* * The sequence type item slot. */ -static PyObject *slot_sq_item(PyObject *self, SIP_SSIZE_T n) +static PyObject *slot_sq_item(PyObject *self, Py_ssize_t n) { PyObject *(*f)(PyObject *,PyObject *); PyObject *arg, *res; -#if PY_MAJOR_VERSION >= 3 - arg = PyLong_FromSsize_t(n); -#elif PY_VERSION_HEX >= 0x02050000 - arg = PyInt_FromSsize_t(n); -#else - arg = PyInt_FromLong(n); -#endif - - if (arg == NULL) + if ((arg = PyLong_FromSsize_t(n)) == NULL) return NULL; f = (PyObject *(*)(PyObject *,PyObject *))findSlot(self, getitem_slot); @@ -11366,7 +10689,7 @@ static int slot_mp_ass_subscript(PyObject *self, PyObject *key, /* * The sequence type assign item slot. */ -static int slot_sq_ass_item(PyObject *self, SIP_SSIZE_T i, PyObject *o) +static int slot_sq_ass_item(PyObject *self, Py_ssize_t i, PyObject *o) { return ssizeobjargprocSlot(self, i, o, (o != NULL ? setitem_slot : delitem_slot)); @@ -11535,53 +10858,39 @@ sipWrapperType sipSimpleWrapper_Type = { 0, /* tp_weaklist */ 0, /* tp_del */ 0, /* tp_version_tag */ -#if PY_VERSION_HEX >= 0x03040000 0, /* tp_finalize */ +#if PY_VERSION_HEX >= 0x03080000 + 0, /* tp_vectorcall */ #endif }, -#if PY_VERSION_HEX >= 0x03050000 { 0, /* am_await */ 0, /* am_aiter */ 0, /* am_anext */ }, -#endif { 0, /* nb_add */ 0, /* nb_subtract */ 0, /* nb_multiply */ -#if PY_MAJOR_VERSION < 3 - 0, /* nb_divide */ -#endif 0, /* nb_remainder */ 0, /* nb_divmod */ 0, /* nb_power */ 0, /* nb_negative */ 0, /* nb_positive */ 0, /* nb_absolute */ - 0, /* nb_bool (Python v3), nb_nonzero (Python v2) */ + 0, /* nb_bool */ 0, /* nb_invert */ 0, /* nb_lshift */ 0, /* nb_rshift */ 0, /* nb_and */ 0, /* nb_xor */ 0, /* nb_or */ -#if PY_MAJOR_VERSION < 3 - 0, /* nb_coerce */ -#endif 0, /* nb_int */ - 0, /* nb_reserved (Python v3), nb_long (Python v2) */ + 0, /* nb_reserved */ 0, /* nb_float */ -#if PY_MAJOR_VERSION < 3 - 0, /* nb_oct */ - 0, /* nb_hex */ -#endif 0, /* nb_inplace_add */ 0, /* nb_inplace_subtract */ 0, /* nb_inplace_multiply */ -#if PY_MAJOR_VERSION < 3 - 0, /* nb_inplace_divide */ -#endif 0, /* nb_inplace_remainder */ 0, /* nb_inplace_power */ 0, /* nb_inplace_lshift */ @@ -11589,19 +10898,13 @@ sipWrapperType sipSimpleWrapper_Type = { 0, /* nb_inplace_and */ 0, /* nb_inplace_xor */ 0, /* nb_inplace_or */ -#if PY_VERSION_HEX >= 0x02020000 0, /* nb_floor_divide */ 0, /* nb_true_divide */ 0, /* nb_inplace_floor_divide */ 0, /* nb_inplace_true_divide */ -#endif -#if PY_VERSION_HEX >= 0x02050000 0, /* nb_index */ -#endif -#if PY_VERSION_HEX >= 0x03050000 0, /* nb_matrix_multiply */ 0, /* nb_inplace_matrix_multiply */ -#endif }, { 0, /* mp_length */ @@ -11621,23 +10924,15 @@ sipWrapperType sipSimpleWrapper_Type = { 0, /* sq_inplace_repeat */ }, { -#if PY_MAJOR_VERSION >= 3 0, /* bf_getbuffer */ 0, /* bf_releasebuffer */ -#else - 0, /* bf_getreadbuffer */ - 0, /* bf_getwritebuffer */ - 0, /* bf_getsegcount */ - 0, /* bf_getcharbuffer */ - 0, /* bf_getbuffer */ - 0, /* bf_releasebuffer */ -#endif }, 0, /* ht_name */ 0, /* ht_slots */ -#if PY_MAJOR_VERSION >= 3 0, /* ht_qualname */ 0, /* ht_cached_keys */ +#if PY_VERSION_HEX >= 0x03090000 + 0, /* ht_module */ #endif #if !defined(STACKLESS) }, @@ -11696,6 +10991,11 @@ static int sipWrapper_clear(sipWrapper *self) */ static void sipWrapper_dealloc(sipWrapper *self) { + PyObject *error_type, *error_value, *error_traceback; + + /* Save the current exception, if any. */ + PyErr_Fetch(&error_type, &error_value, &error_traceback); + /* * We can't simply call the super-type because things have to be done in a * certain order. The first thing is to get rid of the wrapped instance. @@ -11706,6 +11006,9 @@ static void sipWrapper_dealloc(sipWrapper *self) /* Skip the super-type's dealloc. */ PyBaseObject_Type.tp_dealloc((PyObject *)self); + + /* Restore the saved exception. */ + PyErr_Restore(error_type, error_value, error_traceback); } @@ -11773,41 +11076,11 @@ static void addClassSlots(sipWrapperType *wt, const sipClassTypeDef *ctd) PyBufferProcs *bp = &heap_to->as_buffer; /* Add the buffer interface. */ -#if PY_MAJOR_VERSION >= 3 if (ctd->ctd_getbuffer != NULL) bp->bf_getbuffer = (getbufferproc)sipSimpleWrapper_getbuffer; if (ctd->ctd_releasebuffer != NULL) bp->bf_releasebuffer = (releasebufferproc)sipSimpleWrapper_releasebuffer; -#else - if (ctd->ctd_readbuffer != NULL) -#if PY_VERSION_HEX >= 0x02050000 - bp->bf_getreadbuffer = (readbufferproc)sipSimpleWrapper_getreadbuffer; -#else - bp->bf_getreadbuffer = (getreadbufferproc)sipSimpleWrapper_getreadbuffer; -#endif - - if (ctd->ctd_writebuffer != NULL) -#if PY_VERSION_HEX >= 0x02050000 - bp->bf_getwritebuffer = (writebufferproc)sipSimpleWrapper_getwritebuffer; -#else - bp->bf_getwritebuffer = (getwritebufferproc)sipSimpleWrapper_getwritebuffer; -#endif - - if (ctd->ctd_segcount != NULL) -#if PY_VERSION_HEX >= 0x02050000 - bp->bf_getsegcount = (segcountproc)sipSimpleWrapper_getsegcount; -#else - bp->bf_getsegcount = (getsegcountproc)sipSimpleWrapper_getsegcount; -#endif - - if (ctd->ctd_charbuffer != NULL) -#if PY_VERSION_HEX >= 0x02050000 - bp->bf_getcharbuffer = (charbufferproc)sipSimpleWrapper_getcharbuffer; -#else - bp->bf_getcharbuffer = (getcharbufferproc)sipSimpleWrapper_getcharbuffer; -#endif -#endif /* Add the slots for this type. */ if (ctd->ctd_pyslots != NULL) @@ -11824,18 +11097,14 @@ static void addTypeSlots(PyHeapTypeObject *heap_to, sipPySlotDef *slots) PyNumberMethods *nb; PySequenceMethods *sq; PyMappingMethods *mp; -#if PY_VERSION_HEX >= 0x03050000 PyAsyncMethods *am; -#endif void *f; to = &heap_to->ht_type; nb = &heap_to->as_number; sq = &heap_to->as_sequence; mp = &heap_to->as_mapping; -#if PY_VERSION_HEX >= 0x03050000 am = &heap_to->as_async; -#endif while ((f = slots->psd_func) != NULL) switch (slots++->psd_type) @@ -11848,24 +11117,13 @@ static void addTypeSlots(PyHeapTypeObject *heap_to, sipPySlotDef *slots) nb->nb_int = (unaryfunc)f; break; -#if PY_MAJOR_VERSION < 3 - case long_slot: - nb->nb_long = (unaryfunc)f; - break; -#endif - case float_slot: nb->nb_float = (unaryfunc)f; break; case len_slot: -#if PY_VERSION_HEX >= 0x02050000 mp->mp_length = (lenfunc)f; sq->sq_length = (lenfunc)f; -#else - mp->mp_length = (inquiry)f; - sq->sq_length = (inquiry)f; -#endif break; case contains_slot: @@ -11889,18 +11147,11 @@ static void addTypeSlots(PyHeapTypeObject *heap_to, sipPySlotDef *slots) break; case repeat_slot: -#if PY_VERSION_HEX >= 0x02050000 sq->sq_repeat = (ssizeargfunc)f; -#else - sq->sq_repeat = (intargfunc)f; -#endif break; case div_slot: nb->nb_true_divide = (binaryfunc)f; -#if PY_MAJOR_VERSION < 3 - nb->nb_divide = (binaryfunc)f; -#endif break; case mod_slot: @@ -11952,18 +11203,11 @@ static void addTypeSlots(PyHeapTypeObject *heap_to, sipPySlotDef *slots) break; case irepeat_slot: -#if PY_VERSION_HEX >= 0x02050000 sq->sq_inplace_repeat = (ssizeargfunc)f; -#else - sq->sq_inplace_repeat = (intargfunc)f; -#endif break; case idiv_slot: nb->nb_inplace_true_divide = (binaryfunc)f; -#if PY_MAJOR_VERSION < 3 - nb->nb_inplace_divide = (binaryfunc)f; -#endif break; case imod_slot: @@ -12026,18 +11270,8 @@ static void addTypeSlots(PyHeapTypeObject *heap_to, sipPySlotDef *slots) to->tp_richcompare = slot_richcompare; break; -#if PY_MAJOR_VERSION < 3 - case cmp_slot: - to->tp_compare = (cmpfunc)f; - break; -#endif - case bool_slot: -#if PY_MAJOR_VERSION >= 3 nb->nb_bool = (inquiry)f; -#else - nb->nb_nonzero = (inquiry)f; -#endif break; case neg_slot: @@ -12060,11 +11294,9 @@ static void addTypeSlots(PyHeapTypeObject *heap_to, sipPySlotDef *slots) nb->nb_absolute = (unaryfunc)f; break; -#if PY_VERSION_HEX >= 0x02050000 case index_slot: nb->nb_index = (unaryfunc)f; break; -#endif case iter_slot: to->tp_iter = (getiterfunc)f; @@ -12078,7 +11310,6 @@ static void addTypeSlots(PyHeapTypeObject *heap_to, sipPySlotDef *slots) to->tp_setattro = (setattrofunc)f; break; -#if PY_VERSION_HEX >= 0x03050000 case matmul_slot: nb->nb_matrix_multiply = (binaryfunc)f; break; @@ -12098,7 +11329,6 @@ static void addTypeSlots(PyHeapTypeObject *heap_to, sipPySlotDef *slots) case anext_slot: am->am_anext = (unaryfunc)f; break; -#endif /* Suppress a compiler warning. */ default: @@ -12305,12 +11535,7 @@ static char sip_api_bytes_as_char(PyObject *obj) if (parseBytes_AsChar(obj, &ch) < 0) { - PyErr_Format(PyExc_TypeError, -#if PY_MAJOR_VERSION >= 3 - "bytes of length 1 expected not '%s'", -#else - "string of length 1 expected not '%s'", -#endif + PyErr_Format(PyExc_TypeError, "bytes of length 1 expected not '%s'", Py_TYPE(obj)->tp_name); return '\0'; @@ -12330,12 +11555,7 @@ static const char *sip_api_bytes_as_string(PyObject *obj) if (parseBytes_AsString(obj, &a) < 0) { - PyErr_Format(PyExc_TypeError, -#if PY_MAJOR_VERSION >= 3 - "bytes expected not '%s'", -#else - "string expected not '%s'", -#endif + PyErr_Format(PyExc_TypeError, "bytes expected not '%s'", Py_TYPE(obj)->tp_name); return NULL; @@ -12368,17 +11588,9 @@ static int parseString_AsASCIIChar(PyObject *obj, char *ap) if (parseString_AsEncodedChar(PyUnicode_AsASCIIString(obj), obj, ap) < 0) { /* Use the exception set if it was an encoding error. */ -#if PY_VERSION_HEX >= 0x03030000 if (!PyUnicode_Check(obj) || PyUnicode_GET_LENGTH(obj) != 1) -#else - if (!PyUnicode_Check(obj) || PyUnicode_GET_SIZE(obj) != 1) -#endif PyErr_SetString(PyExc_TypeError, -#if PY_MAJOR_VERSION >= 3 "bytes or ASCII string of length 1 expected"); -#else - "string or ASCII unicode of length 1 expected"); -#endif return -1; } @@ -12410,17 +11622,9 @@ static int parseString_AsLatin1Char(PyObject *obj, char *ap) if (parseString_AsEncodedChar(PyUnicode_AsLatin1String(obj), obj, ap) < 0) { /* Use the exception set if it was an encoding error. */ -#if PY_VERSION_HEX >= 0x03030000 if (!PyUnicode_Check(obj) || PyUnicode_GET_LENGTH(obj) != 1) -#else - if (!PyUnicode_Check(obj) || PyUnicode_GET_SIZE(obj) != 1) -#endif PyErr_SetString(PyExc_TypeError, -#if PY_MAJOR_VERSION >= 3 "bytes or Latin-1 string of length 1 expected"); -#else - "string or Latin-1 unicode of length 1 expected"); -#endif return -1; } @@ -12452,17 +11656,9 @@ static int parseString_AsUTF8Char(PyObject *obj, char *ap) if (parseString_AsEncodedChar(PyUnicode_AsUTF8String(obj), obj, ap) < 0) { /* Use the exception set if it was an encoding error. */ -#if PY_VERSION_HEX >= 0x03030000 if (!PyUnicode_Check(obj) || PyUnicode_GET_LENGTH(obj) != 1) -#else - if (!PyUnicode_Check(obj) || PyUnicode_GET_SIZE(obj) != 1) -#endif PyErr_SetString(PyExc_TypeError, -#if PY_MAJOR_VERSION >= 3 "bytes or UTF-8 string of length 1 expected"); -#else - "string or UTF-8 unicode of length 1 expected"); -#endif return -1; } @@ -12476,7 +11672,7 @@ static int parseString_AsUTF8Char(PyObject *obj, char *ap) */ static int parseString_AsEncodedChar(PyObject *bytes, PyObject *obj, char *ap) { - SIP_SSIZE_T size; + Py_ssize_t size; if (bytes == NULL) { @@ -12485,7 +11681,7 @@ static int parseString_AsEncodedChar(PyObject *bytes, PyObject *obj, char *ap) return parseBytes_AsChar(obj, ap); } - size = SIPBytes_GET_SIZE(bytes); + size = PyBytes_GET_SIZE(bytes); if (size != 1) { @@ -12494,7 +11690,7 @@ static int parseString_AsEncodedChar(PyObject *bytes, PyObject *obj, char *ap) } if (ap != NULL) - *ap = *SIPBytes_AS_STRING(bytes); + *ap = *PyBytes_AS_STRING(bytes); Py_DECREF(bytes); @@ -12517,11 +11713,7 @@ static const char *sip_api_string_as_ascii_string(PyObject **obj) /* Use the exception set if it was an encoding error. */ if (!PyUnicode_Check(s)) PyErr_Format(PyExc_TypeError, -#if PY_MAJOR_VERSION >= 3 "bytes or ASCII string expected not '%s'", -#else - "string or ASCII unicode expected not '%s'", -#endif Py_TYPE(s)->tp_name); return NULL; @@ -12556,11 +11748,7 @@ static const char *sip_api_string_as_latin1_string(PyObject **obj) /* Use the exception set if it was an encoding error. */ if (!PyUnicode_Check(s)) PyErr_Format(PyExc_TypeError, -#if PY_MAJOR_VERSION >= 3 "bytes or Latin-1 string expected not '%s'", -#else - "string or Latin-1 unicode expected not '%s'", -#endif Py_TYPE(s)->tp_name); return NULL; @@ -12595,11 +11783,7 @@ static const char *sip_api_string_as_utf8_string(PyObject **obj) /* Use the exception set if it was an encoding error. */ if (!PyUnicode_Check(s)) PyErr_Format(PyExc_TypeError, -#if PY_MAJOR_VERSION >= 3 "bytes or UTF-8 string expected not '%s'", -#else - "string or UTF-8 unicode expected not '%s'", -#endif Py_TYPE(s)->tp_name); return NULL; @@ -12628,7 +11812,7 @@ static PyObject *parseString_AsEncodedString(PyObject *bytes, PyObject *obj, { if (bytes != NULL) { - *ap = SIPBytes_AS_STRING(bytes); + *ap = PyBytes_AS_STRING(bytes); return bytes; } @@ -12652,22 +11836,21 @@ static PyObject *parseString_AsEncodedString(PyObject *bytes, PyObject *obj, * Parse a character array and return it's address and length. */ static int parseBytes_AsCharArray(PyObject *obj, const char **ap, - SIP_SSIZE_T *aszp) + Py_ssize_t *aszp) { const char *a; - SIP_SSIZE_T asz; + Py_ssize_t asz; if (obj == Py_None) { a = NULL; asz = 0; } - else if (SIPBytes_Check(obj)) + else if (PyBytes_Check(obj)) { - a = SIPBytes_AS_STRING(obj); - asz = SIPBytes_GET_SIZE(obj); + a = PyBytes_AS_STRING(obj); + asz = PyBytes_GET_SIZE(obj); } -#if PY_MAJOR_VERSION >= 3 else { Py_buffer view; @@ -12680,12 +11863,6 @@ static int parseBytes_AsCharArray(PyObject *obj, const char **ap, PyBuffer_Release(&view); } -#else - else if (PyObject_AsCharBuffer(obj, &a, &asz) < 0) - { - return -1; - } -#endif if (ap != NULL) *ap = a; @@ -12703,14 +11880,13 @@ static int parseBytes_AsCharArray(PyObject *obj, const char **ap, static int parseBytes_AsChar(PyObject *obj, char *ap) { const char *chp; - SIP_SSIZE_T sz; + Py_ssize_t sz; - if (SIPBytes_Check(obj)) + if (PyBytes_Check(obj)) { - chp = SIPBytes_AS_STRING(obj); - sz = SIPBytes_GET_SIZE(obj); + chp = PyBytes_AS_STRING(obj); + sz = PyBytes_GET_SIZE(obj); } -#if PY_MAJOR_VERSION >= 3 else { Py_buffer view; @@ -12723,12 +11899,6 @@ static int parseBytes_AsChar(PyObject *obj, char *ap) PyBuffer_Release(&view); } -#else - else if (PyObject_AsCharBuffer(obj, &chp, &sz) < 0) - { - return -1; - } -#endif if (sz != 1) return -1; @@ -12746,7 +11916,7 @@ static int parseBytes_AsChar(PyObject *obj, char *ap) static int parseBytes_AsString(PyObject *obj, const char **ap) { const char *a; - SIP_SSIZE_T sz; + Py_ssize_t sz; if (parseBytes_AsCharArray(obj, &a, &sz) < 0) return -1; @@ -12769,12 +11939,7 @@ static wchar_t sip_api_unicode_as_wchar(PyObject *obj) if (parseWChar(obj, &ch) < 0) { PyErr_Format(PyExc_ValueError, -#if PY_MAJOR_VERSION >= 3 - "string" -#else - "unicode string" -#endif - " of length 1 expected, not %s", Py_TYPE(obj)->tp_name); + "string of length 1 expected, not %s", Py_TYPE(obj)->tp_name); return L'\0'; } @@ -12793,12 +11958,7 @@ static wchar_t *sip_api_unicode_as_wstring(PyObject *obj) if (parseWCharString(obj, &p) < 0) { PyErr_Format(PyExc_ValueError, -#if PY_MAJOR_VERSION >= 3 - "string" -#else - "unicode string" -#endif - " expected, not %s", Py_TYPE(obj)->tp_name); + "string expected, not %s", Py_TYPE(obj)->tp_name); return NULL; } @@ -12810,10 +11970,10 @@ static wchar_t *sip_api_unicode_as_wstring(PyObject *obj) /* * Parse a wide character array and return it's address and length. */ -static int parseWCharArray(PyObject *obj, wchar_t **ap, SIP_SSIZE_T *aszp) +static int parseWCharArray(PyObject *obj, wchar_t **ap, Py_ssize_t *aszp) { wchar_t *a; - SIP_SSIZE_T asz; + Py_ssize_t asz; if (obj == Py_None) { @@ -12825,22 +11985,6 @@ static int parseWCharArray(PyObject *obj, wchar_t **ap, SIP_SSIZE_T *aszp) if (convertToWCharArray(obj, &a, &asz) < 0) return -1; } -#if PY_MAJOR_VERSION < 3 - else if (PyString_Check(obj)) - { - int rc; - PyObject *uobj; - - if ((uobj = PyUnicode_FromObject(obj)) == NULL) - return -1; - - rc = convertToWCharArray(uobj, &a, &asz); - Py_DECREF(uobj); - - if (rc < 0) - return -1; - } -#endif else { return -1; @@ -12860,27 +12004,17 @@ static int parseWCharArray(PyObject *obj, wchar_t **ap, SIP_SSIZE_T *aszp) * Convert a Unicode object to a wide character array and return it's address * and length. */ -static int convertToWCharArray(PyObject *obj, wchar_t **ap, SIP_SSIZE_T *aszp) +static int convertToWCharArray(PyObject *obj, wchar_t **ap, Py_ssize_t *aszp) { - SIP_SSIZE_T ulen; + Py_ssize_t ulen; wchar_t *wc; -#if PY_VERSION_HEX >= 0x03030000 ulen = PyUnicode_GET_LENGTH(obj); -#else - ulen = PyUnicode_GET_SIZE(obj); -#endif if ((wc = sip_api_malloc(ulen * sizeof (wchar_t))) == NULL) return -1; -#if PY_VERSION_HEX >= 0x03020000 - ulen = PyUnicode_AsWideChar(obj, wc, ulen); -#else - ulen = PyUnicode_AsWideChar((PyUnicodeObject *)obj, wc, ulen); -#endif - - if (ulen < 0) + if ((ulen = PyUnicode_AsWideChar(obj, wc, ulen)) < 0) { sip_api_free(wc); return -1; @@ -12905,22 +12039,6 @@ static int parseWChar(PyObject *obj, wchar_t *ap) if (convertToWChar(obj, &a) < 0) return -1; } -#if PY_MAJOR_VERSION < 3 - else if (PyString_Check(obj)) - { - int rc; - PyObject *uobj; - - if ((uobj = PyUnicode_FromObject(obj)) == NULL) - return -1; - - rc = convertToWChar(uobj, &a); - Py_DECREF(uobj); - - if (rc < 0) - return -1; - } -#endif else { return -1; @@ -12938,18 +12056,10 @@ static int parseWChar(PyObject *obj, wchar_t *ap) */ static int convertToWChar(PyObject *obj, wchar_t *ap) { -#if PY_VERSION_HEX >= 0x03030000 if (PyUnicode_GET_LENGTH(obj) != 1) -#else - if (PyUnicode_GET_SIZE(obj) != 1) -#endif return -1; -#if PY_VERSION_HEX >= 0x03020000 if (PyUnicode_AsWideChar(obj, ap, 1) != 1) -#else - if (PyUnicode_AsWideChar((PyUnicodeObject *)obj, ap, 1) != 1) -#endif return -1; return 0; @@ -12972,22 +12082,6 @@ static int parseWCharString(PyObject *obj, wchar_t **ap) if (convertToWCharString(obj, &a) < 0) return -1; } -#if PY_MAJOR_VERSION < 3 - else if (PyString_Check(obj)) - { - int rc; - PyObject *uobj; - - if ((uobj = PyUnicode_FromObject(obj)) == NULL) - return -1; - - rc = convertToWCharString(uobj, &a); - Py_DECREF(uobj); - - if (rc < 0) - return -1; - } -#endif else { return -1; @@ -13006,25 +12100,15 @@ static int parseWCharString(PyObject *obj, wchar_t **ap) */ static int convertToWCharString(PyObject *obj, wchar_t **ap) { - SIP_SSIZE_T ulen; + Py_ssize_t ulen; wchar_t *wc; -#if PY_VERSION_HEX >= 0x03030000 ulen = PyUnicode_GET_LENGTH(obj); -#else - ulen = PyUnicode_GET_SIZE(obj); -#endif if ((wc = sip_api_malloc((ulen + 1) * sizeof (wchar_t))) == NULL) return -1; -#if PY_VERSION_HEX >= 0x03020000 - ulen = PyUnicode_AsWideChar(obj, wc, ulen); -#else - ulen = PyUnicode_AsWideChar((PyUnicodeObject *)obj, wc, ulen); -#endif - - if (ulen < 0) + if ((ulen = PyUnicode_AsWideChar(obj, wc, ulen)) < 0) { sip_api_free(wc); return -1; @@ -13075,7 +12159,7 @@ static void raiseNoWChar() /* * The enum type alloc slot. */ -static PyObject *sipEnumType_alloc(PyTypeObject *self, SIP_SSIZE_T nitems) +static PyObject *sipEnumType_alloc(PyTypeObject *self, Py_ssize_t nitems) { sipEnumTypeObject *py_type; sipPySlotDef *psd; @@ -13092,7 +12176,7 @@ static PyObject *sipEnumType_alloc(PyTypeObject *self, SIP_SSIZE_T nitems) * structure. Strictly speaking this doesn't need to be done here. */ py_type->type = currentType; - currentType->u.td_py_type = (PyTypeObject *)py_type; + currentType->td_py_type = (PyTypeObject *)py_type; /* * Initialise any slots. This must be done here, after the type is @@ -13130,26 +12214,7 @@ static PyObject *sipEnumType_getattro(PyObject *self, PyObject *name) PyErr_Clear(); /* Get the member name. */ -#if PY_VERSION_HEX >= 0x03030000 - name_str = PyUnicode_AsUTF8(name); -#elif PY_MAJOR_VERSION >= 3 - name_str = _PyUnicode_AsString(name); -#else - /* We don't handle Unicode names. */ - if (!PyString_Check(name)) - { - PyErr_Format(PyExc_TypeError, - "attribute name must be string, not '%.200s'", - Py_TYPE(name)->tp_name); - name_str = NULL; - } - else - { - name_str = PyString_AS_STRING(name); - } -#endif - - if (name_str == NULL) + if ((name_str = PyUnicode_AsUTF8(name)) == NULL) return NULL; etd = (sipEnumTypeDef *)((sipEnumTypeObject *)self)->type; @@ -13192,33 +12257,26 @@ static PyObject *sipEnumType_getattro(PyObject *self, PyObject *name) */ static int check_encoded_string(PyObject *obj) { + Py_buffer view; + if (obj == Py_None) return 0; if (PyUnicode_Check(obj)) return 0; - if (SIPBytes_Check(obj)) + if (PyBytes_Check(obj)) return 0; -#if PY_MAJOR_VERSION >= 3 + if (PyObject_GetBuffer(obj, &view, PyBUF_SIMPLE) < 0) { - Py_buffer view; - - if (PyObject_GetBuffer(obj, &view, PyBUF_SIMPLE) < 0) - { - PyErr_Clear(); - } - else - { - PyBuffer_Release(&view); - return 0; - } + PyErr_Clear(); } -#else - if (PyObject_CheckReadBuffer(obj)) + else + { + PyBuffer_Release(&view); return 0; -#endif + } return -1; } @@ -13241,30 +12299,29 @@ static PyObject *sip_exit(PyObject *self, PyObject *args) /* - * Register the exit notifier with the atexit module. + * Register an exit notifier with the atexit module. */ -static void register_exit_notifier(void) +static int sip_api_register_exit_notifier(PyMethodDef *md) { - static PyMethodDef md = { - "_sip_exit", sip_exit, METH_NOARGS, NULL - }; + static PyObject *register_func = NULL; + PyObject *notifier, *res; - PyObject *notifier, *register_func, *res; + if (register_func == NULL && (register_func = import_module_attr("atexit", "register")) == NULL) + return -1; - if ((notifier = PyCFunction_New(&md, NULL)) == NULL) - return; - - if ((register_func = import_module_attr("atexit", "register")) == NULL) - { - Py_DECREF(notifier); - return; - } + if ((notifier = PyCFunction_New(md, NULL)) == NULL) + return -1; res = PyObject_CallFunctionObjArgs(register_func, notifier, NULL); - Py_XDECREF(res); - Py_DECREF(register_func); Py_DECREF(notifier); + + if (res == NULL) + return -1; + + Py_DECREF(res); + + return 0; } @@ -13518,9 +12575,6 @@ static int sip_api_get_method(PyObject *obj, sipMethodDef *method) { method->pm_self = PyMethod_GET_SELF(obj); method->pm_function = PyMethod_GET_FUNCTION(obj); -#if PY_MAJOR_VERSION < 3 - method->pm_class = PyMethod_GET_CLASS(obj); -#endif } return TRUE; @@ -13532,11 +12586,7 @@ static int sip_api_get_method(PyObject *obj, sipMethodDef *method) */ static PyObject *sip_api_from_method(const sipMethodDef *method) { -#if PY_MAJOR_VERSION < 3 - return PyMethod_New(method->pm_function, method->pm_self, method->pm_class); -#else return PyMethod_New(method->pm_function, method->pm_self); -#endif } @@ -13690,6 +12740,10 @@ static int sip_api_is_user_type(const sipWrapperType *wt) */ static struct _frame *sip_api_get_frame(int depth) { +#if defined(PYPY_VERSION) + /* PyPy only supports a depth of 0. */ + return NULL; +#else struct _frame *frame = PyEval_GetFrame(); while (frame != NULL && depth > 0) @@ -13699,6 +12753,7 @@ static struct _frame *sip_api_get_frame(int depth) } return frame; +#endif } @@ -13743,10 +12798,9 @@ static int sip_api_check_plugin_for_type(const sipTypeDef *td, /* * Create a new Unicode object and return the character size and buffer. */ -static PyObject *sip_api_unicode_new(SIP_SSIZE_T len, unsigned maxchar, +static PyObject *sip_api_unicode_new(Py_ssize_t len, unsigned maxchar, int *kind, void **data) { -#if PY_VERSION_HEX >= 0x03030000 PyObject *obj; if ((obj = PyUnicode_New(len, maxchar)) != NULL) @@ -13756,14 +12810,6 @@ static PyObject *sip_api_unicode_new(SIP_SSIZE_T len, unsigned maxchar, } return obj; -#else - (void)len; - (void)maxchar; - (void)kind; - (void)data; - - return NULL; -#endif } @@ -13773,14 +12819,7 @@ static PyObject *sip_api_unicode_new(SIP_SSIZE_T len, unsigned maxchar, static void sip_api_unicode_write(int kind, void *data, int index, unsigned value) { -#if PY_VERSION_HEX >= 0x03030000 PyUnicode_WRITE(kind, data, index, value); -#else - (void)kind; - (void)data; - (void)index; - (void)value; -#endif } @@ -13789,9 +12828,8 @@ static void sip_api_unicode_write(int kind, void *data, int index, * the length. */ static void *sip_api_unicode_data(PyObject *obj, int *char_size, - SIP_SSIZE_T *len) + Py_ssize_t *len) { -#if PY_VERSION_HEX >= 0x03030000 void *data; /* Assume there will be an error. */ @@ -13824,14 +12862,6 @@ static void *sip_api_unicode_data(PyObject *obj, int *char_size, } return data; -#else - (void)obj; - (void)len; - - *char_size = -1; - - return NULL; -#endif } @@ -13841,7 +12871,6 @@ static void *sip_api_unicode_data(PyObject *obj, int *char_size, */ static int sip_api_get_buffer_info(PyObject *obj, sipBufferInfoDef *bi) { -#if PY_VERSION_HEX >= 0x02060300 int rc; Py_buffer *buffer; @@ -13876,9 +12905,6 @@ static int sip_api_get_buffer_info(PyObject *obj, sipBufferInfoDef *bi) } return rc; -#else - return -1; -#endif } @@ -13888,14 +12914,12 @@ static int sip_api_get_buffer_info(PyObject *obj, sipBufferInfoDef *bi) */ static void sip_api_release_buffer_info(sipBufferInfoDef *bi) { -#if PY_VERSION_HEX >= 0x02060300 if (bi->bi_internal != NULL) { PyBuffer_Release((Py_buffer *)bi->bi_internal); sip_api_free(bi->bi_internal); bi->bi_internal = NULL; } -#endif } @@ -14200,7 +13224,6 @@ static const sipContainerDef *get_container(const sipTypeDef *td) } -#if PY_VERSION_HEX >= 0x03030000 /* * Get the __qualname__ of an object based on its enclosing scope. */ @@ -14214,15 +13237,14 @@ static PyObject *get_qualname(const sipTypeDef *td, PyObject *name) return PyUnicode_FromFormat("%U.%U", ((PyHeapTypeObject *)scope_type)->ht_qualname, name); } -#endif /* * Implement PySlice_GetIndicesEx() (or its subsequent replacement). */ -int sip_api_convert_from_slice_object(PyObject *slice, SIP_SSIZE_T length, - SIP_SSIZE_T *start, SIP_SSIZE_T *stop, SIP_SSIZE_T *step, - SIP_SSIZE_T *slicelength) +int sip_api_convert_from_slice_object(PyObject *slice, Py_ssize_t length, + Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step, + Py_ssize_t *slicelength) { #if PY_VERSION_HEX >= 0x03070000 if (PySlice_Unpack(slice, start, stop, step) < 0) @@ -14231,10 +13253,29 @@ int sip_api_convert_from_slice_object(PyObject *slice, SIP_SSIZE_T length, *slicelength = PySlice_AdjustIndices(length, start, stop, *step); return 0; -#elif PY_VERSION_HEX >= 0x03020000 - return PySlice_GetIndicesEx(slice, length, start, stop, step, slicelength); #else - return PySlice_GetIndicesEx((PySliceObject *)slice, length, start, stop, - step, slicelength); + return PySlice_GetIndicesEx(slice, length, start, stop, step, slicelength); #endif } + + +/* + * Call a visitor function for every wrapped object. + */ +static void sip_api_visit_wrappers(sipWrapperVisitorFunc visitor, + void *closure) +{ + const sipHashEntry *he; + unsigned long i; + + for (he = cppPyMap.hash_array, i = 0; i < cppPyMap.size; ++i, ++he) + { + if (he->key != NULL) + { + sipSimpleWrapper *sw; + + for (sw = he->first; sw != NULL; sw = sw->next) + visitor(sw, closure); + } + } +} diff --git a/sip/siplib/threads.c b/sip/siplib/threads.c index 1d9ba2f1..bb6d50ce 100644 --- a/sip/siplib/threads.c +++ b/sip/siplib/threads.c @@ -3,7 +3,7 @@ * C++ classes that provide a thread interface to interact properly with the * Python threading infrastructure. * - * Copyright (c) 2016 Riverbank Computing Limited + * Copyright (c) 2020 Riverbank Computing Limited * * This file is part of SIP. * @@ -80,7 +80,7 @@ int sipGetPending(void **pp, sipWrapper **op, int *fp) /* * Return TRUE if anything is pending. */ -int sipIsPending() +int sipIsPending(void) { pendingDef *pd; diff --git a/sip/siplib/voidptr.c b/sip/siplib/voidptr.c index 58633490..bc7062d8 100644 --- a/sip/siplib/voidptr.c +++ b/sip/siplib/voidptr.c @@ -1,7 +1,7 @@ /* * SIP library code. * - * Copyright (c) 2018 Riverbank Computing Limited + * Copyright (c) 2019 Riverbank Computing Limited * * This file is part of SIP. * @@ -30,7 +30,7 @@ typedef struct { PyObject_HEAD void *voidptr; - SIP_SSIZE_T size; + Py_ssize_t size; int rw; } sipVoidPtrObject; @@ -38,30 +38,21 @@ typedef struct { /* The structure used to hold the results of a voidptr conversion. */ struct vp_values { void *voidptr; - SIP_SSIZE_T size; + Py_ssize_t size; int rw; }; static int check_size(PyObject *self); static int check_rw(PyObject *self); -static int check_index(PyObject *self, SIP_SSIZE_T idx); -#if PY_VERSION_HEX < 0x02060300 -static SIP_SSIZE_T get_value_data(PyObject *value, void **value_ptr); -#endif -#if PY_VERSION_HEX < 0x02050000 -static void fix_bounds(int size, int *left, int *right); -#endif -#if PY_VERSION_HEX >= 0x02050000 +static int check_index(PyObject *self, Py_ssize_t idx); static void bad_key(PyObject *key); -#endif -static int check_slice_size(SIP_SSIZE_T size, SIP_SSIZE_T value_size); -static PyObject *make_voidptr(void *voidptr, SIP_SSIZE_T size, int rw); +static int check_slice_size(Py_ssize_t size, Py_ssize_t value_size); +static PyObject *make_voidptr(void *voidptr, Py_ssize_t size, int rw); static int vp_convertor(PyObject *arg, struct vp_values *vp); -static SIP_SSIZE_T get_size_from_arg(sipVoidPtrObject *v, SIP_SSIZE_T size); +static Py_ssize_t get_size_from_arg(sipVoidPtrObject *v, Py_ssize_t size); -#if defined(SIP_USE_PYCAPSULE) /* * Implement ascapsule() for the type. */ @@ -71,20 +62,6 @@ static PyObject *sipVoidPtr_ascapsule(sipVoidPtrObject *v, PyObject *arg) return PyCapsule_New(v->voidptr, NULL, NULL); } -#endif - - -#if defined(SIP_SUPPORT_PYCOBJECT) -/* - * Implement ascobject() for the type. - */ -static PyObject *sipVoidPtr_ascobject(sipVoidPtrObject *v, PyObject *arg) -{ - (void)arg; - - return PyCObject_FromVoidPtr(v->voidptr, NULL); -} -#endif /* @@ -95,15 +72,9 @@ static PyObject *sipVoidPtr_asarray(sipVoidPtrObject *v, PyObject *args, { static char *kwlist[] = {"size", NULL}; - SIP_SSIZE_T size = -1; + Py_ssize_t size = -1; - if (!PyArg_ParseTupleAndKeywords(args, kw, -#if PY_VERSION_HEX >= 0x02050000 - "|n:asarray", -#else - "|i:asarray", -#endif - kwlist, &size)) + if (!PyArg_ParseTupleAndKeywords(args, kw, "|n:asarray", kwlist, &size)) return NULL; if ((size = get_size_from_arg(v, size)) < 0) @@ -122,21 +93,15 @@ static PyObject *sipVoidPtr_asstring(sipVoidPtrObject *v, PyObject *args, { static char *kwlist[] = {"size", NULL}; - SIP_SSIZE_T size = -1; + Py_ssize_t size = -1; - if (!PyArg_ParseTupleAndKeywords(args, kw, -#if PY_VERSION_HEX >= 0x02050000 - "|n:asstring", -#else - "|i:asstring", -#endif - kwlist, &size)) + if (!PyArg_ParseTupleAndKeywords(args, kw, "|n:asstring", kwlist, &size)) return NULL; if ((size = get_size_from_arg(v, size)) < 0) return NULL; - return SIPBytes_FromStringAndSize(v->voidptr, size); + return PyBytes_FromStringAndSize(v->voidptr, size); } @@ -147,13 +112,7 @@ static PyObject *sipVoidPtr_getsize(sipVoidPtrObject *v, PyObject *arg) { (void)arg; -#if PY_MAJOR_VERSION >= 3 return PyLong_FromSsize_t(v->size); -#elif PY_VERSION_HEX >= 0x02050000 - return PyInt_FromSsize_t(v->size); -#else - return PyInt_FromLong(v->size); -#endif } @@ -162,15 +121,7 @@ static PyObject *sipVoidPtr_getsize(sipVoidPtrObject *v, PyObject *arg) */ static PyObject *sipVoidPtr_setsize(sipVoidPtrObject *v, PyObject *arg) { - SIP_SSIZE_T size; - -#if PY_MAJOR_VERSION >= 3 - size = PyLong_AsSsize_t(arg); -#elif PY_VERSION_HEX >= 0x02050000 - size = PyInt_AsSsize_t(arg); -#else - size = (int)PyInt_AsLong(arg); -#endif + Py_ssize_t size = PyLong_AsSsize_t(arg); if (PyErr_Occurred()) return NULL; @@ -213,12 +164,7 @@ static PyObject *sipVoidPtr_setwriteable(sipVoidPtrObject *v, PyObject *arg) /* The methods data structure. */ static PyMethodDef sipVoidPtr_Methods[] = { {"asarray", (PyCFunction)sipVoidPtr_asarray, METH_VARARGS|METH_KEYWORDS, NULL}, -#if defined(SIP_USE_PYCAPSULE) {"ascapsule", (PyCFunction)sipVoidPtr_ascapsule, METH_NOARGS, NULL}, -#endif -#if defined(SIP_SUPPORT_PYCOBJECT) - {"ascobject", (PyCFunction)sipVoidPtr_ascobject, METH_NOARGS, NULL}, -#endif {"asstring", (PyCFunction)sipVoidPtr_asstring, METH_VARARGS|METH_KEYWORDS, NULL}, {"getsize", (PyCFunction)sipVoidPtr_getsize, METH_NOARGS, NULL}, {"setsize", (PyCFunction)sipVoidPtr_setsize, METH_O, NULL}, @@ -246,59 +192,30 @@ static PyObject *sipVoidPtr_int(PyObject *self) } -#if PY_MAJOR_VERSION < 3 -/* - * Implement hex() for the type. - */ -static PyObject *sipVoidPtr_hex(PyObject *self) -{ - char buf[2 + 16 + 1]; - - PyOS_snprintf(buf, sizeof (buf), "0x%.*lx", (int)(sizeof (void *) * 2), - (unsigned long)((sipVoidPtrObject *)self)->voidptr); - - return PyString_FromString(buf); -} -#endif - - /* The number methods data structure. */ static PyNumberMethods sipVoidPtr_NumberMethods = { 0, /* nb_add */ 0, /* nb_subtract */ 0, /* nb_multiply */ -#if PY_MAJOR_VERSION < 3 - 0, /* nb_divide */ -#endif 0, /* nb_remainder */ 0, /* nb_divmod */ 0, /* nb_power */ 0, /* nb_negative */ 0, /* nb_positive */ 0, /* nb_absolute */ - sipVoidPtr_bool, /* nb_bool (Python v3), nb_nonzero (Python v2) */ + sipVoidPtr_bool, /* nb_bool */ 0, /* nb_invert */ 0, /* nb_lshift */ 0, /* nb_rshift */ 0, /* nb_and */ 0, /* nb_xor */ 0, /* nb_or */ -#if PY_MAJOR_VERSION < 3 - 0, /* nb_coerce */ -#endif sipVoidPtr_int, /* nb_int */ - 0, /* nb_reserved (Python v3), nb_long (Python v2) */ + 0, /* nb_reserved */ 0, /* nb_float */ -#if PY_MAJOR_VERSION < 3 - 0, /* nb_oct */ - sipVoidPtr_hex, /* nb_hex */ -#endif 0, /* nb_inplace_add */ 0, /* nb_inplace_subtract */ 0, /* nb_inplace_multiply */ -#if PY_MAJOR_VERSION < 3 - 0, /* nb_inplace_divide */ -#endif 0, /* nb_inplace_remainder */ 0, /* nb_inplace_power */ 0, /* nb_inplace_lshift */ @@ -310,20 +227,16 @@ static PyNumberMethods sipVoidPtr_NumberMethods = { 0, /* nb_true_divide */ 0, /* nb_inplace_floor_divide */ 0, /* nb_inplace_true_divide */ -#if PY_VERSION_HEX >= 0x02050000 0, /* nb_index */ -#endif -#if PY_VERSION_HEX >= 0x03050000 0, /* nb_matrix_multiply */ 0, /* nb_inplace_matrix_multiply */ -#endif }; /* * Implement len() for the type. */ -static SIP_SSIZE_T sipVoidPtr_length(PyObject *self) +static Py_ssize_t sipVoidPtr_length(PyObject *self) { if (check_size(self) < 0) return -1; @@ -335,118 +248,31 @@ static SIP_SSIZE_T sipVoidPtr_length(PyObject *self) /* * Implement sequence item sub-script for the type. */ -static PyObject *sipVoidPtr_item(PyObject *self, SIP_SSIZE_T idx) +static PyObject *sipVoidPtr_item(PyObject *self, Py_ssize_t idx) { if (check_size(self) < 0 || check_index(self, idx) < 0) return NULL; - return SIPBytes_FromStringAndSize( + return PyBytes_FromStringAndSize( (char *)((sipVoidPtrObject *)self)->voidptr + idx, 1); } -#if PY_VERSION_HEX < 0x02050000 -/* - * Implement sequence slice sub-script for the type. - */ -static PyObject *sipVoidPtr_slice(PyObject *self, int left, int right) -{ - sipVoidPtrObject *v; - - if (check_size(self) < 0) - return NULL; - - v = (sipVoidPtrObject *)self; - - fix_bounds(v->size, &left, &right); - - if (left == right) - left = right = 0; - - return make_voidptr((char *)(v->voidptr) + left, right - left, v->rw); -} - - -/* - * Implement sequence assignment item sub-script for the type. - */ -static int sipVoidPtr_ass_item(PyObject *self, int idx, PyObject *value) -{ - int value_size; - void *value_ptr; - - if (check_rw(self) < 0 || check_size(self) < 0 || check_index(self, idx) < 0) - return -1; - - if ((value_size = get_value_data(value, &value_ptr)) < 0) - return -1; - - if (value_size != 1) - { - PyErr_SetString(PyExc_TypeError, - "right operand must be a single byte"); - - return -1; - } - - ((char *)((sipVoidPtrObject *)self)->voidptr)[idx] = *(char *)value_ptr; - - return 0; -} - - -/* - * Implement sequence assignment slice sub-script for the type. - */ -static int sipVoidPtr_ass_slice(PyObject *self, int left, int right, - PyObject *value) -{ - sipVoidPtrObject *v; - int value_size; - void *value_ptr; - - if (check_rw(self) < 0 || check_size(self) < 0) - return -1; - - if ((value_size = get_value_data(value, &value_ptr)) < 0) - return -1; - - v = (sipVoidPtrObject *)self; - - fix_bounds(v->size, &left, &right); - - if (check_slice_size(right - left, value_size) < 0) - return -1; - - memmove((char *)(v->voidptr) + left, value_ptr, right - left); - - return 0; -} -#endif - - /* The sequence methods data structure. */ static PySequenceMethods sipVoidPtr_SequenceMethods = { sipVoidPtr_length, /* sq_length */ 0, /* sq_concat */ 0, /* sq_repeat */ sipVoidPtr_item, /* sq_item */ -#if PY_VERSION_HEX >= 0x02050000 0, /* sq_slice */ 0, /* sq_ass_item */ 0, /* sq_ass_slice */ -#else - sipVoidPtr_slice, /* sq_slice */ - sipVoidPtr_ass_item, /* sq_ass_item */ - sipVoidPtr_ass_slice, /* sq_ass_slice */ -#endif 0, /* sq_contains */ 0, /* sq_inplace_concat */ 0, /* sq_inplace_repeat */ }; -#if PY_VERSION_HEX >= 0x02050000 /* * Implement mapping sub-script for the type. */ @@ -502,12 +328,7 @@ static int sipVoidPtr_ass_subscript(PyObject *self, PyObject *key, { sipVoidPtrObject *v; Py_ssize_t start, size; -#if PY_VERSION_HEX >= 0x02060300 Py_buffer value_view; -#else - Py_ssize_t value_size; - void *value_ptr; -#endif if (check_rw(self) < 0 || check_size(self) < 0) return -1; @@ -549,7 +370,6 @@ static int sipVoidPtr_ass_subscript(PyObject *self, PyObject *key, return -1; } -#if PY_VERSION_HEX >= 0x02060300 if (PyObject_GetBuffer(value, &value_view, PyBUF_CONTIG_RO) < 0) return -1; @@ -572,15 +392,6 @@ static int sipVoidPtr_ass_subscript(PyObject *self, PyObject *key, memmove((char *)v->voidptr + start, value_view.buf, size); PyBuffer_Release(&value_view); -#else - if ((value_size = get_value_data(value, &value_ptr)) < 0) - return -1; - - if (check_slice_size(size, value_size) < 0) - return -1; - - memmove((char *)v->voidptr + start, value_ptr, size); -#endif return 0; } @@ -592,10 +403,8 @@ static PyMappingMethods sipVoidPtr_MappingMethods = { sipVoidPtr_subscript, /* mp_subscript */ sipVoidPtr_ass_subscript, /* mp_ass_subscript */ }; -#endif -#if PY_VERSION_HEX >= 0x02060300 /* * The buffer implementation for Python v2.6.3 and later. */ @@ -610,90 +419,12 @@ static int sipVoidPtr_getbuffer(PyObject *self, Py_buffer *buf, int flags) return PyBuffer_FillInfo(buf, self, v->voidptr, v->size, !v->rw, flags); } -#endif - - -#if PY_MAJOR_VERSION < 3 -/* - * The read buffer implementation for Python v2. - */ -static SIP_SSIZE_T sipVoidPtr_getreadbuffer(PyObject *self, SIP_SSIZE_T seg, - void **ptr) -{ - sipVoidPtrObject *v; - - if (seg != 0) - { - PyErr_SetString(PyExc_SystemError, "invalid buffer segment"); - return -1; - } - - if (check_size(self) < 0) - return -1; - - v = (sipVoidPtrObject *)self; - - *ptr = v->voidptr; - - return v->size; -} -#endif - - -#if PY_MAJOR_VERSION < 3 -/* - * The write buffer implementation for Python v2. - */ -static SIP_SSIZE_T sipVoidPtr_getwritebuffer(PyObject *self, SIP_SSIZE_T seg, - void **ptr) -{ - if (((sipVoidPtrObject *)self)->rw) - return sipVoidPtr_getreadbuffer(self, seg, ptr); - - PyErr_SetString(PyExc_TypeError, "sip.voidptr object is not writeable"); - return -1; -} -#endif - - -#if PY_MAJOR_VERSION < 3 -/* - * The segment count implementation for Python v2. - */ -static SIP_SSIZE_T sipVoidPtr_getsegcount(PyObject *self, SIP_SSIZE_T *lenp) -{ - SIP_SSIZE_T segs, len; - - len = ((sipVoidPtrObject *)self)->size; - segs = (len < 0 ? 0 : 1); - - if (lenp != NULL) - *lenp = len; - - return segs; -} -#endif /* The buffer methods data structure. */ static PyBufferProcs sipVoidPtr_BufferProcs = { -#if PY_MAJOR_VERSION >= 3 sipVoidPtr_getbuffer, /* bf_getbuffer */ 0 /* bf_releasebuffer */ -#else - sipVoidPtr_getreadbuffer, /* bf_getreadbuffer */ - sipVoidPtr_getwritebuffer, /* bf_getwritebuffer */ - sipVoidPtr_getsegcount, /* bf_getsegcount */ -#if PY_VERSION_HEX >= 0x02050000 - (charbufferproc)sipVoidPtr_getreadbuffer, /* bf_getcharbuffer */ -#if PY_VERSION_HEX >= 0x02060300 - sipVoidPtr_getbuffer, /* bf_getbuffer */ - 0 /* bf_releasebuffer */ -#endif -#else - (getcharbufferproc)sipVoidPtr_getreadbuffer /* bf_getcharbuffer */ -#endif -#endif }; @@ -706,17 +437,11 @@ static PyObject *sipVoidPtr_new(PyTypeObject *subtype, PyObject *args, static char *kwlist[] = {"address", "size", "writeable", NULL}; struct vp_values vp_conversion; - SIP_SSIZE_T size = -1; + Py_ssize_t size = -1; int rw = -1; PyObject *obj; - if (!PyArg_ParseTupleAndKeywords(args, kw, -#if PY_VERSION_HEX >= 0x02050000 - "O&|ni:voidptr", -#else - "O&|ii:voidptr", -#endif - kwlist, vp_convertor, &vp_conversion, &size, &rw)) + if (!PyArg_ParseTupleAndKeywords(args, kw, "O&|ni:voidptr", kwlist, vp_convertor, &vp_conversion, &size, &rw)) return NULL; /* Use the explicit size if one was given. */ @@ -754,11 +479,7 @@ PyTypeObject sipVoidPtr_Type = { 0, /* tp_repr */ &sipVoidPtr_NumberMethods, /* tp_as_number */ &sipVoidPtr_SequenceMethods, /* tp_as_sequence */ -#if PY_VERSION_HEX >= 0x02050000 &sipVoidPtr_MappingMethods, /* tp_as_mapping */ -#else - 0, /* tp_as_mapping */ -#endif 0, /* tp_hash */ 0, /* tp_call */ 0, /* tp_str */ @@ -796,11 +517,10 @@ PyTypeObject sipVoidPtr_Type = { 0, /* tp_subclasses */ 0, /* tp_weaklist */ 0, /* tp_del */ -#if PY_VERSION_HEX >= 0x02060000 0, /* tp_version_tag */ -#endif -#if PY_VERSION_HEX >= 0x03040000 0, /* tp_finalize */ +#if PY_VERSION_HEX >= 0x03080000 + 0, /* tp_vectorcall */ #endif }; @@ -846,7 +566,7 @@ PyObject *sip_api_convert_from_const_void_ptr(const void *val) /* * Convert a sized C/C++ void pointer to a sip.voidptr object. */ -PyObject *sip_api_convert_from_void_ptr_and_size(void *val, SIP_SSIZE_T size) +PyObject *sip_api_convert_from_void_ptr_and_size(void *val, Py_ssize_t size) { return make_voidptr(val, size, TRUE); } @@ -856,7 +576,7 @@ PyObject *sip_api_convert_from_void_ptr_and_size(void *val, SIP_SSIZE_T size) * Convert a sized C/C++ const void pointer to a sip.voidptr object. */ PyObject *sip_api_convert_from_const_void_ptr_and_size(const void *val, - SIP_SSIZE_T size) + Py_ssize_t size) { return make_voidptr((void *)val, size, FALSE); } @@ -896,7 +616,7 @@ static int check_rw(PyObject *self) /* * Check that an index is valid for a void pointer. */ -static int check_index(PyObject *self, SIP_SSIZE_T idx) +static int check_index(PyObject *self, Py_ssize_t idx) { if (idx >= 0 && idx < ((sipVoidPtrObject *)self)->size) return 0; @@ -907,58 +627,6 @@ static int check_index(PyObject *self, SIP_SSIZE_T idx) } -#if PY_VERSION_HEX < 0x02060300 -/* - * Get the address and size of the data from a value that supports the buffer - * interface. - */ -static SIP_SSIZE_T get_value_data(PyObject *value, void **value_ptr) -{ - PyBufferProcs *bf = Py_TYPE(value)->tp_as_buffer; - - if (bf == NULL || bf->bf_getreadbuffer == NULL || bf->bf_getsegcount == NULL) - { - PyErr_Format(PyExc_TypeError, - "'%s' does not support the buffer interface", - Py_TYPE(value)->tp_name); - - return -1; - } - - if ((*bf->bf_getsegcount)(value, NULL) != 1) - { - PyErr_SetString(PyExc_TypeError, - "single-segment buffer object expected"); - - return -1; - } - - return (*bf->bf_getreadbuffer)(value, 0, value_ptr); -} -#endif - - -#if PY_VERSION_HEX < 0x02050000 -/* - * Fix the bounds of a slice in the same way that the Python buffer object - * does. - */ -static void fix_bounds(int size, int *left, int *right) -{ - if (*left < 0) - *left = 0; - else if (*left > size) - *left = size; - - if (*right < *left) - *right = *left; - else if (*right > size) - *right = size; -} -#endif - - -#if PY_VERSION_HEX >= 0x02050000 /* * Raise an exception about a bad sub-script key. */ @@ -968,14 +636,13 @@ static void bad_key(PyObject *key) "cannot index a sip.voidptr object using '%s'", Py_TYPE(key)->tp_name); } -#endif /* * Check that the size of a value is the same as the size of the slice it is * replacing. */ -static int check_slice_size(SIP_SSIZE_T size, SIP_SSIZE_T value_size) +static int check_slice_size(Py_ssize_t size, Py_ssize_t value_size) { if (value_size == size) return 0; @@ -990,7 +657,7 @@ static int check_slice_size(SIP_SSIZE_T size, SIP_SSIZE_T value_size) /* * Do the work of converting a void pointer. */ -static PyObject *make_voidptr(void *voidptr, SIP_SSIZE_T size, int rw) +static PyObject *make_voidptr(void *voidptr, Py_ssize_t size, int rw) { sipVoidPtrObject *self; @@ -1017,26 +684,23 @@ static PyObject *make_voidptr(void *voidptr, SIP_SSIZE_T size, int rw) static int vp_convertor(PyObject *arg, struct vp_values *vp) { void *ptr; - SIP_SSIZE_T size = -1; + Py_ssize_t size = -1; int rw = TRUE; if (arg == Py_None) + { ptr = NULL; -#if defined(SIP_USE_PYCAPSULE) + } else if (PyCapsule_CheckExact(arg)) + { ptr = PyCapsule_GetPointer(arg, NULL); -#endif -#if defined(SIP_SUPPORT_PYCOBJECT) - else if (PyCObject_Check(arg)) - ptr = PyCObject_AsVoidPtr(arg); -#endif + } else if (PyObject_TypeCheck(arg, &sipVoidPtr_Type)) { ptr = ((sipVoidPtrObject *)arg)->voidptr; size = ((sipVoidPtrObject *)arg)->size; rw = ((sipVoidPtrObject *)arg)->rw; } -#if PY_VERSION_HEX >= 0x02060300 else if (PyObject_CheckBuffer(arg)) { Py_buffer view; @@ -1050,13 +714,6 @@ static int vp_convertor(PyObject *arg, struct vp_values *vp) PyBuffer_Release(&view); } -#endif -#if PY_VERSION_HEX < 0x03000000 - else if (PyObject_AsReadBuffer(arg, (const void **)&ptr, &size) >= 0) - { - rw = (Py_TYPE(arg)->tp_as_buffer->bf_getwritebuffer != NULL); - } -#endif else { PyErr_Clear(); @@ -1064,15 +721,7 @@ static int vp_convertor(PyObject *arg, struct vp_values *vp) if (PyErr_Occurred()) { -#if defined(SIP_USE_PYCAPSULE) -#if defined(SIP_SUPPORT_PYCOBJECT) - PyErr_SetString(PyExc_TypeError, "a single integer, Capsule, CObject, None, bytes-like object or another sip.voidptr object is required"); -#else PyErr_SetString(PyExc_TypeError, "a single integer, Capsule, None, bytes-like object or another sip.voidptr object is required"); -#endif -#else - PyErr_SetString(PyExc_TypeError, "a single integer, CObject, None, bytes-like object or another sip.voidptr object is required"); -#endif return 0; } } @@ -1089,7 +738,7 @@ static int vp_convertor(PyObject *arg, struct vp_values *vp) * Get a size possibly supplied as an argument, otherwise get it from the * object. Raise an exception if there was no size specified. */ -static SIP_SSIZE_T get_size_from_arg(sipVoidPtrObject *v, SIP_SSIZE_T size) +static Py_ssize_t get_size_from_arg(sipVoidPtrObject *v, Py_ssize_t size) { /* Use the current size if one wasn't explicitly given. */ if (size < 0)