File: int_convertors.c

package info (click to toggle)
wxpython4.0 4.2.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 221,752 kB
  • sloc: cpp: 962,555; python: 230,573; ansic: 170,731; makefile: 51,756; sh: 9,342; perl: 1,564; javascript: 584; php: 326; xml: 200
file content (350 lines) | stat: -rw-r--r-- 7,969 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/* SPDX-License-Identifier: BSD-2-Clause */

/*
 * The implementation of the Python object to C/C++ integer convertors.
 *
 * Copyright (c) 2024 Phil Thompson <phil@riverbankcomputing.com>
 */


/*
 * NOTES
 *
 * The legacy integer conversions (ie. without support for overflow checking)
 * are flawed and inconsistent.  Large Python signed values were converted to
 * -1 whereas small values were truncated.  When converting function arguments
 * all overlows were ignored, however when converting the results returned by
 * Python re-implementations then large Python values raised an exception
 * whereas small values were truncated.
 *
 * With the new integer conversions large Python signed values will always
 * raise an overflow exception (even if overflow checking is disabled).  This
 * is because a truncated value is not available - it would have to be
 * computed.  This may cause new exceptions to be raised but is justified in
 * that the value that was being used bore no relation to the original value.
 */


/* Remove when Python v3.12 is no longer supported. */
#define PY_SSIZE_T_CLEAN
#include <Python.h>

#include <limits.h>

#include "sipint.h"


/* Wrappers to deal with lack of long long support. */
#if defined(HAVE_LONG_LONG)
#define SIPLong_AsLongLong              PyLong_AsLongLong
#define SIP_LONG_LONG                   PY_LONG_LONG
#define SIP_LONG_LONG_FORMAT            "%lld"
#define SIP_UNSIGNED_LONG_LONG_FORMAT   "%llu"
#else
#define SIPLong_AsLongLong              PyLong_AsLong
#define SIP_LONG_LONG                   long
#define SIP_LONG_LONG_FORMAT            "%ld"
#define SIP_UNSIGNED_LONG_LONG_FORMAT   "%lu"
#endif


static int overflow_checking = FALSE;   /* Check for overflows. */

static SIP_LONG_LONG long_as_long_long(PyObject *o, SIP_LONG_LONG min,
        SIP_LONG_LONG max);
static unsigned long long_as_unsigned_long(PyObject *o, unsigned long max);
static void raise_signed_overflow(SIP_LONG_LONG min, SIP_LONG_LONG max);
static void raise_unsigned_overflow(unsigned SIP_LONG_LONG max);


/*
 * Enable or disable overflow checking (Python API).
 */
PyObject *sipEnableOverflowChecking(PyObject *self, PyObject *args)
{
    int enable;

    (void)self;

    if (PyArg_ParseTuple(args, "i:enableoverflowchecking", &enable))
    {
        PyObject *res;

        res = (sip_api_enable_overflow_checking(enable) ? Py_True : Py_False);

        Py_INCREF(res);
        return res;
    }

    return NULL;
}


/*
 * Enable or disable overflow checking (C API).
 */
int sip_api_enable_overflow_checking(int enable)
{
    int was_enabled = overflow_checking;

    overflow_checking = enable;

    return was_enabled;
}


/*
 * Convert a Python object to a C++ bool (returned as an int).
 */
int sip_api_convert_to_bool(PyObject *o)
{
    int was_enabled, v;

    /* Convert the object to an int while checking for overflow. */
    was_enabled = sip_api_enable_overflow_checking(TRUE);
    v = sip_api_long_as_int(o);
    sip_api_enable_overflow_checking(was_enabled);

    if (PyErr_Occurred())
    {
        if (PyErr_ExceptionMatches(PyExc_OverflowError))
        {
            PyErr_Clear();

            /* The value must have been non-zero. */
            v = 1;
        }
        else
        {
            PyErr_Format(PyExc_TypeError, "a 'bool' is expected not '%s'",
                    Py_TYPE(o)->tp_name);

            v = -1;
        }
    }
    else if (v != 0)
    {
        v = 1;
    }

    return v;
}


/*
 * Convert a Python object to a C char.
 */
char sip_api_long_as_char(PyObject *o)
{
    return (char)long_as_long_long(o, CHAR_MIN, CHAR_MAX);
}


/*
 * Convert a Python object to a C signed char.
 */
signed char sip_api_long_as_signed_char(PyObject *o)
{
    return (signed char)long_as_long_long(o, SCHAR_MIN, SCHAR_MAX);
}


/*
 * Convert a Python object to a C unsigned char.
 */
unsigned char sip_api_long_as_unsigned_char(PyObject *o)
{
    return (unsigned char)long_as_unsigned_long(o, UCHAR_MAX);
}


/*
 * Convert a Python object to a C short.
 */
short sip_api_long_as_short(PyObject *o)
{
    return (short)long_as_long_long(o, SHRT_MIN, SHRT_MAX);
}


/*
 * Convert a Python object to a C unsigned short.
 */
unsigned short sip_api_long_as_unsigned_short(PyObject *o)
{
    return (unsigned short)long_as_unsigned_long(o, USHRT_MAX);
}


/*
 * Convert a Python object to a C int.
 */
int sip_api_long_as_int(PyObject *o)
{
    return (int)long_as_long_long(o, INT_MIN, INT_MAX);
}


/*
 * Convert a Python object to a C unsigned int.
 */
unsigned sip_api_long_as_unsigned_int(PyObject *o)
{
    return (unsigned)long_as_unsigned_long(o, UINT_MAX);
}


/*
 * Convert a Python object to a C size_t.
 */
size_t sip_api_long_as_size_t(PyObject *o)
{
    return (size_t)long_as_unsigned_long(o, SIZE_MAX);
}


/*
 * Convert a Python object to a C long.
 */
long sip_api_long_as_long(PyObject *o)
{
    return (long)long_as_long_long(o, LONG_MIN, LONG_MAX);
}


/*
 * Convert a Python object to a C unsigned long.
 */
unsigned long sip_api_long_as_unsigned_long(PyObject *o)
{
    return long_as_unsigned_long(o, ULONG_MAX);
}


#if defined(HAVE_LONG_LONG)
/*
 * Convert a Python object to a C long long.
 */
PY_LONG_LONG sip_api_long_as_long_long(PyObject *o)
{
    return long_as_long_long(o, LLONG_MIN, LLONG_MAX);
}


/*
 * Convert a Python object to a C unsigned long long.
 */
unsigned PY_LONG_LONG sip_api_long_as_unsigned_long_long(PyObject *o)
{
    unsigned PY_LONG_LONG value;

    /*
     * Note that this doesn't handle Python v2 int objects, but the old
     * convertors didn't either.
     */

    PyErr_Clear();

    if (overflow_checking)
    {
        value = PyLong_AsUnsignedLongLong(o);

        if (PyErr_Occurred())
        {
            /* Provide a better exception message. */
            if (PyErr_ExceptionMatches(PyExc_OverflowError))
                raise_unsigned_overflow(ULLONG_MAX);
        }
    }
    else
    {
        value = PyLong_AsUnsignedLongLongMask(o);
    }

    return value;
}
#endif


/*
 * Convert a Python object to a long long checking that the value is within a
 * range if overflow checking is enabled.
 */
static SIP_LONG_LONG long_as_long_long(PyObject *o, SIP_LONG_LONG min,
        SIP_LONG_LONG max)
{
    SIP_LONG_LONG value;

    PyErr_Clear();

    value = SIPLong_AsLongLong(o);

    if (PyErr_Occurred())
    {
        /* Provide a better exception message. */
        if (PyErr_ExceptionMatches(PyExc_OverflowError))
            raise_signed_overflow(min, max);
    }
    else if (overflow_checking && (value < min || value > max))
    {
        raise_signed_overflow(min, max);
    }

    return value;
}


/*
 * Convert a Python object to an unsigned long checking that the value is
 * within a range if overflow checking is enabled.
 */
static unsigned long long_as_unsigned_long(PyObject *o, unsigned long max)
{
    unsigned long value;

    PyErr_Clear();

    if (overflow_checking)
    {
        value = PyLong_AsUnsignedLong(o);

        if (PyErr_Occurred())
        {
            /* Provide a better exception message. */
            if (PyErr_ExceptionMatches(PyExc_OverflowError))
                raise_unsigned_overflow(max);
        }
        else if (value > max)
        {
            raise_unsigned_overflow(max);
        }
    }
    else
    {
        value = PyLong_AsUnsignedLongMask(o);
    }

    return value;
}


/*
 * Raise an overflow exception if a signed value is out of range.
 */
static void raise_signed_overflow(SIP_LONG_LONG min, SIP_LONG_LONG max)
{
    PyErr_Format(PyExc_OverflowError,
            "value must be in the range " SIP_LONG_LONG_FORMAT  " to " SIP_LONG_LONG_FORMAT,
            min, max);
}


/*
 * Raise an overflow exception if an unsigned value is out of range.
 */
static void raise_unsigned_overflow(unsigned SIP_LONG_LONG max)
{
    PyErr_Format(PyExc_OverflowError,
            "value must be in the range 0 to " SIP_UNSIGNED_LONG_LONG_FORMAT,
            max);
}