Coverage Report

Created: 2025-07-04 06:49

/src/cpython/Objects/clinic/longobject.c.h
Line
Count
Source (jump to first uncovered line)
1
/*[clinic input]
2
preserve
3
[clinic start generated code]*/
4
5
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
6
#  include "pycore_gc.h"          // PyGC_Head
7
#  include "pycore_runtime.h"     // _Py_ID()
8
#endif
9
#include "pycore_abstract.h"      // _PyNumber_Index()
10
#include "pycore_modsupport.h"    // _PyArg_UnpackKeywords()
11
12
static PyObject *
13
long_new_impl(PyTypeObject *type, PyObject *x, PyObject *obase);
14
15
static PyObject *
16
long_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
17
2.10k
{
18
2.10k
    PyObject *return_value = NULL;
19
2.10k
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
20
21
2.10k
    #define NUM_KEYWORDS 1
22
2.10k
    static struct {
23
2.10k
        PyGC_Head _this_is_not_used;
24
2.10k
        PyObject_VAR_HEAD
25
2.10k
        Py_hash_t ob_hash;
26
2.10k
        PyObject *ob_item[NUM_KEYWORDS];
27
2.10k
    } _kwtuple = {
28
2.10k
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
29
2.10k
        .ob_hash = -1,
30
2.10k
        .ob_item = { &_Py_ID(base), },
31
2.10k
    };
32
2.10k
    #undef NUM_KEYWORDS
33
2.10k
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
34
35
    #else  // !Py_BUILD_CORE
36
    #  define KWTUPLE NULL
37
    #endif  // !Py_BUILD_CORE
38
39
2.10k
    static const char * const _keywords[] = {"", "base", NULL};
40
2.10k
    static _PyArg_Parser _parser = {
41
2.10k
        .keywords = _keywords,
42
2.10k
        .fname = "int",
43
2.10k
        .kwtuple = KWTUPLE,
44
2.10k
    };
45
2.10k
    #undef KWTUPLE
46
2.10k
    PyObject *argsbuf[2];
47
2.10k
    PyObject * const *fastargs;
48
2.10k
    Py_ssize_t nargs = PyTuple_GET_SIZE(args);
49
2.10k
    Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0;
50
2.10k
    PyObject *x = NULL;
51
2.10k
    PyObject *obase = NULL;
52
53
2.10k
    fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser,
54
2.10k
            /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
55
2.10k
    if (!fastargs) {
56
0
        goto exit;
57
0
    }
58
2.10k
    if (nargs < 1) {
59
11
        goto skip_optional_posonly;
60
11
    }
61
2.09k
    noptargs--;
62
2.09k
    x = fastargs[0];
63
2.10k
skip_optional_posonly:
64
2.10k
    if (!noptargs) {
65
2.10k
        goto skip_optional_pos;
66
2.10k
    }
67
0
    obase = fastargs[1];
68
2.10k
skip_optional_pos:
69
2.10k
    return_value = long_new_impl(type, x, obase);
70
71
2.10k
exit:
72
2.10k
    return return_value;
73
2.10k
}
74
75
PyDoc_STRVAR(int___getnewargs____doc__,
76
"__getnewargs__($self, /)\n"
77
"--\n"
78
"\n");
79
80
#define INT___GETNEWARGS___METHODDEF    \
81
    {"__getnewargs__", (PyCFunction)int___getnewargs__, METH_NOARGS, int___getnewargs____doc__},
82
83
static PyObject *
84
int___getnewargs___impl(PyObject *self);
85
86
static PyObject *
87
int___getnewargs__(PyObject *self, PyObject *Py_UNUSED(ignored))
88
0
{
89
0
    return int___getnewargs___impl(self);
90
0
}
91
92
PyDoc_STRVAR(int___format____doc__,
93
"__format__($self, format_spec, /)\n"
94
"--\n"
95
"\n"
96
"Convert to a string according to format_spec.");
97
98
#define INT___FORMAT___METHODDEF    \
99
    {"__format__", (PyCFunction)int___format__, METH_O, int___format____doc__},
100
101
static PyObject *
102
int___format___impl(PyObject *self, PyObject *format_spec);
103
104
static PyObject *
105
int___format__(PyObject *self, PyObject *arg)
106
318
{
107
318
    PyObject *return_value = NULL;
108
318
    PyObject *format_spec;
109
110
318
    if (!PyUnicode_Check(arg)) {
111
0
        _PyArg_BadArgument("__format__", "argument", "str", arg);
112
0
        goto exit;
113
0
    }
114
318
    format_spec = arg;
115
318
    return_value = int___format___impl(self, format_spec);
116
117
318
exit:
118
318
    return return_value;
119
318
}
120
121
PyDoc_STRVAR(int___round____doc__,
122
"__round__($self, ndigits=None, /)\n"
123
"--\n"
124
"\n"
125
"Rounding an Integral returns itself.\n"
126
"\n"
127
"Rounding with an ndigits argument also returns an integer.");
128
129
#define INT___ROUND___METHODDEF    \
130
    {"__round__", _PyCFunction_CAST(int___round__), METH_FASTCALL, int___round____doc__},
131
132
static PyObject *
133
int___round___impl(PyObject *self, PyObject *o_ndigits);
134
135
static PyObject *
136
int___round__(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
137
0
{
138
0
    PyObject *return_value = NULL;
139
0
    PyObject *o_ndigits = Py_None;
140
141
0
    if (!_PyArg_CheckPositional("__round__", nargs, 0, 1)) {
142
0
        goto exit;
143
0
    }
144
0
    if (nargs < 1) {
145
0
        goto skip_optional;
146
0
    }
147
0
    o_ndigits = args[0];
148
0
skip_optional:
149
0
    return_value = int___round___impl(self, o_ndigits);
150
151
0
exit:
152
0
    return return_value;
153
0
}
154
155
PyDoc_STRVAR(int___sizeof____doc__,
156
"__sizeof__($self, /)\n"
157
"--\n"
158
"\n"
159
"Returns size in memory, in bytes.");
160
161
#define INT___SIZEOF___METHODDEF    \
162
    {"__sizeof__", (PyCFunction)int___sizeof__, METH_NOARGS, int___sizeof____doc__},
163
164
static Py_ssize_t
165
int___sizeof___impl(PyObject *self);
166
167
static PyObject *
168
int___sizeof__(PyObject *self, PyObject *Py_UNUSED(ignored))
169
0
{
170
0
    PyObject *return_value = NULL;
171
0
    Py_ssize_t _return_value;
172
173
0
    _return_value = int___sizeof___impl(self);
174
0
    if ((_return_value == -1) && PyErr_Occurred()) {
175
0
        goto exit;
176
0
    }
177
0
    return_value = PyLong_FromSsize_t(_return_value);
178
179
0
exit:
180
0
    return return_value;
181
0
}
182
183
PyDoc_STRVAR(int_bit_length__doc__,
184
"bit_length($self, /)\n"
185
"--\n"
186
"\n"
187
"Number of bits necessary to represent self in binary.\n"
188
"\n"
189
">>> bin(37)\n"
190
"\'0b100101\'\n"
191
">>> (37).bit_length()\n"
192
"6");
193
194
#define INT_BIT_LENGTH_METHODDEF    \
195
    {"bit_length", (PyCFunction)int_bit_length, METH_NOARGS, int_bit_length__doc__},
196
197
static PyObject *
198
int_bit_length_impl(PyObject *self);
199
200
static PyObject *
201
int_bit_length(PyObject *self, PyObject *Py_UNUSED(ignored))
202
27
{
203
27
    return int_bit_length_impl(self);
204
27
}
205
206
PyDoc_STRVAR(int_bit_count__doc__,
207
"bit_count($self, /)\n"
208
"--\n"
209
"\n"
210
"Number of ones in the binary representation of the absolute value of self.\n"
211
"\n"
212
"Also known as the population count.\n"
213
"\n"
214
">>> bin(13)\n"
215
"\'0b1101\'\n"
216
">>> (13).bit_count()\n"
217
"3");
218
219
#define INT_BIT_COUNT_METHODDEF    \
220
    {"bit_count", (PyCFunction)int_bit_count, METH_NOARGS, int_bit_count__doc__},
221
222
static PyObject *
223
int_bit_count_impl(PyObject *self);
224
225
static PyObject *
226
int_bit_count(PyObject *self, PyObject *Py_UNUSED(ignored))
227
0
{
228
0
    return int_bit_count_impl(self);
229
0
}
230
231
PyDoc_STRVAR(int_as_integer_ratio__doc__,
232
"as_integer_ratio($self, /)\n"
233
"--\n"
234
"\n"
235
"Return a pair of integers, whose ratio is equal to the original int.\n"
236
"\n"
237
"The ratio is in lowest terms and has a positive denominator.\n"
238
"\n"
239
">>> (10).as_integer_ratio()\n"
240
"(10, 1)\n"
241
">>> (-10).as_integer_ratio()\n"
242
"(-10, 1)\n"
243
">>> (0).as_integer_ratio()\n"
244
"(0, 1)");
245
246
#define INT_AS_INTEGER_RATIO_METHODDEF    \
247
    {"as_integer_ratio", (PyCFunction)int_as_integer_ratio, METH_NOARGS, int_as_integer_ratio__doc__},
248
249
static PyObject *
250
int_as_integer_ratio_impl(PyObject *self);
251
252
static PyObject *
253
int_as_integer_ratio(PyObject *self, PyObject *Py_UNUSED(ignored))
254
0
{
255
0
    return int_as_integer_ratio_impl(self);
256
0
}
257
258
PyDoc_STRVAR(int_to_bytes__doc__,
259
"to_bytes($self, /, length=1, byteorder=\'big\', *, signed=False)\n"
260
"--\n"
261
"\n"
262
"Return an array of bytes representing an integer.\n"
263
"\n"
264
"  length\n"
265
"    Length of bytes object to use.  An OverflowError is raised if the\n"
266
"    integer is not representable with the given number of bytes.  Default\n"
267
"    is length 1.\n"
268
"  byteorder\n"
269
"    The byte order used to represent the integer.  If byteorder is \'big\',\n"
270
"    the most significant byte is at the beginning of the byte array.  If\n"
271
"    byteorder is \'little\', the most significant byte is at the end of the\n"
272
"    byte array.  To request the native byte order of the host system, use\n"
273
"    sys.byteorder as the byte order value.  Default is to use \'big\'.\n"
274
"  signed\n"
275
"    Determines whether two\'s complement is used to represent the integer.\n"
276
"    If signed is False and a negative integer is given, an OverflowError\n"
277
"    is raised.");
278
279
#define INT_TO_BYTES_METHODDEF    \
280
    {"to_bytes", _PyCFunction_CAST(int_to_bytes), METH_FASTCALL|METH_KEYWORDS, int_to_bytes__doc__},
281
282
static PyObject *
283
int_to_bytes_impl(PyObject *self, Py_ssize_t length, PyObject *byteorder,
284
                  int is_signed);
285
286
static PyObject *
287
int_to_bytes(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
288
664
{
289
664
    PyObject *return_value = NULL;
290
664
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
291
292
664
    #define NUM_KEYWORDS 3
293
664
    static struct {
294
664
        PyGC_Head _this_is_not_used;
295
664
        PyObject_VAR_HEAD
296
664
        Py_hash_t ob_hash;
297
664
        PyObject *ob_item[NUM_KEYWORDS];
298
664
    } _kwtuple = {
299
664
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
300
664
        .ob_hash = -1,
301
664
        .ob_item = { &_Py_ID(length), &_Py_ID(byteorder), &_Py_ID(signed), },
302
664
    };
303
664
    #undef NUM_KEYWORDS
304
664
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
305
306
    #else  // !Py_BUILD_CORE
307
    #  define KWTUPLE NULL
308
    #endif  // !Py_BUILD_CORE
309
310
664
    static const char * const _keywords[] = {"length", "byteorder", "signed", NULL};
311
664
    static _PyArg_Parser _parser = {
312
664
        .keywords = _keywords,
313
664
        .fname = "to_bytes",
314
664
        .kwtuple = KWTUPLE,
315
664
    };
316
664
    #undef KWTUPLE
317
664
    PyObject *argsbuf[3];
318
664
    Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
319
664
    Py_ssize_t length = 1;
320
664
    PyObject *byteorder = NULL;
321
664
    int is_signed = 0;
322
323
664
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
324
664
            /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
325
664
    if (!args) {
326
0
        goto exit;
327
0
    }
328
664
    if (!noptargs) {
329
0
        goto skip_optional_pos;
330
0
    }
331
664
    if (args[0]) {
332
664
        {
333
664
            Py_ssize_t ival = -1;
334
664
            PyObject *iobj = _PyNumber_Index(args[0]);
335
664
            if (iobj != NULL) {
336
664
                ival = PyLong_AsSsize_t(iobj);
337
664
                Py_DECREF(iobj);
338
664
            }
339
664
            if (ival == -1 && PyErr_Occurred()) {
340
0
                goto exit;
341
0
            }
342
664
            length = ival;
343
664
        }
344
664
        if (!--noptargs) {
345
0
            goto skip_optional_pos;
346
0
        }
347
664
    }
348
664
    if (args[1]) {
349
664
        if (!PyUnicode_Check(args[1])) {
350
0
            _PyArg_BadArgument("to_bytes", "argument 'byteorder'", "str", args[1]);
351
0
            goto exit;
352
0
        }
353
664
        byteorder = args[1];
354
664
        if (!--noptargs) {
355
664
            goto skip_optional_pos;
356
664
        }
357
664
    }
358
664
skip_optional_pos:
359
664
    if (!noptargs) {
360
664
        goto skip_optional_kwonly;
361
664
    }
362
0
    is_signed = PyObject_IsTrue(args[2]);
363
0
    if (is_signed < 0) {
364
0
        goto exit;
365
0
    }
366
664
skip_optional_kwonly:
367
664
    return_value = int_to_bytes_impl(self, length, byteorder, is_signed);
368
369
664
exit:
370
664
    return return_value;
371
664
}
372
373
PyDoc_STRVAR(int_from_bytes__doc__,
374
"from_bytes($type, /, bytes, byteorder=\'big\', *, signed=False)\n"
375
"--\n"
376
"\n"
377
"Return the integer represented by the given array of bytes.\n"
378
"\n"
379
"  bytes\n"
380
"    Holds the array of bytes to convert.  The argument must either\n"
381
"    support the buffer protocol or be an iterable object producing bytes.\n"
382
"    Bytes and bytearray are examples of built-in objects that support the\n"
383
"    buffer protocol.\n"
384
"  byteorder\n"
385
"    The byte order used to represent the integer.  If byteorder is \'big\',\n"
386
"    the most significant byte is at the beginning of the byte array.  If\n"
387
"    byteorder is \'little\', the most significant byte is at the end of the\n"
388
"    byte array.  To request the native byte order of the host system, use\n"
389
"    sys.byteorder as the byte order value.  Default is to use \'big\'.\n"
390
"  signed\n"
391
"    Indicates whether two\'s complement is used to represent the integer.");
392
393
#define INT_FROM_BYTES_METHODDEF    \
394
    {"from_bytes", _PyCFunction_CAST(int_from_bytes), METH_FASTCALL|METH_KEYWORDS|METH_CLASS, int_from_bytes__doc__},
395
396
static PyObject *
397
int_from_bytes_impl(PyTypeObject *type, PyObject *bytes_obj,
398
                    PyObject *byteorder, int is_signed);
399
400
static PyObject *
401
int_from_bytes(PyObject *type, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
402
2.09k
{
403
2.09k
    PyObject *return_value = NULL;
404
2.09k
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
405
406
2.09k
    #define NUM_KEYWORDS 3
407
2.09k
    static struct {
408
2.09k
        PyGC_Head _this_is_not_used;
409
2.09k
        PyObject_VAR_HEAD
410
2.09k
        Py_hash_t ob_hash;
411
2.09k
        PyObject *ob_item[NUM_KEYWORDS];
412
2.09k
    } _kwtuple = {
413
2.09k
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
414
2.09k
        .ob_hash = -1,
415
2.09k
        .ob_item = { &_Py_ID(bytes), &_Py_ID(byteorder), &_Py_ID(signed), },
416
2.09k
    };
417
2.09k
    #undef NUM_KEYWORDS
418
2.09k
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
419
420
    #else  // !Py_BUILD_CORE
421
    #  define KWTUPLE NULL
422
    #endif  // !Py_BUILD_CORE
423
424
2.09k
    static const char * const _keywords[] = {"bytes", "byteorder", "signed", NULL};
425
2.09k
    static _PyArg_Parser _parser = {
426
2.09k
        .keywords = _keywords,
427
2.09k
        .fname = "from_bytes",
428
2.09k
        .kwtuple = KWTUPLE,
429
2.09k
    };
430
2.09k
    #undef KWTUPLE
431
2.09k
    PyObject *argsbuf[3];
432
2.09k
    Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
433
2.09k
    PyObject *bytes_obj;
434
2.09k
    PyObject *byteorder = NULL;
435
2.09k
    int is_signed = 0;
436
437
2.09k
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
438
2.09k
            /*minpos*/ 1, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
439
2.09k
    if (!args) {
440
0
        goto exit;
441
0
    }
442
2.09k
    bytes_obj = args[0];
443
2.09k
    if (!noptargs) {
444
0
        goto skip_optional_pos;
445
0
    }
446
2.09k
    if (args[1]) {
447
2.09k
        if (!PyUnicode_Check(args[1])) {
448
0
            _PyArg_BadArgument("from_bytes", "argument 'byteorder'", "str", args[1]);
449
0
            goto exit;
450
0
        }
451
2.09k
        byteorder = args[1];
452
2.09k
        if (!--noptargs) {
453
2.09k
            goto skip_optional_pos;
454
2.09k
        }
455
2.09k
    }
456
2.09k
skip_optional_pos:
457
2.09k
    if (!noptargs) {
458
2.09k
        goto skip_optional_kwonly;
459
2.09k
    }
460
0
    is_signed = PyObject_IsTrue(args[2]);
461
0
    if (is_signed < 0) {
462
0
        goto exit;
463
0
    }
464
2.09k
skip_optional_kwonly:
465
2.09k
    return_value = int_from_bytes_impl((PyTypeObject *)type, bytes_obj, byteorder, is_signed);
466
467
2.09k
exit:
468
2.09k
    return return_value;
469
2.09k
}
470
471
PyDoc_STRVAR(int_is_integer__doc__,
472
"is_integer($self, /)\n"
473
"--\n"
474
"\n"
475
"Returns True. Exists for duck type compatibility with float.is_integer.");
476
477
#define INT_IS_INTEGER_METHODDEF    \
478
    {"is_integer", (PyCFunction)int_is_integer, METH_NOARGS, int_is_integer__doc__},
479
480
static PyObject *
481
int_is_integer_impl(PyObject *self);
482
483
static PyObject *
484
int_is_integer(PyObject *self, PyObject *Py_UNUSED(ignored))
485
0
{
486
0
    return int_is_integer_impl(self);
487
0
}
488
/*[clinic end generated code: output=d23f8ce5bdf08a30 input=a9049054013a1b77]*/