Coverage Report

Created: 2026-05-30 06:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython/Objects/clinic/longobject.c.h
Line
Count
Source
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
4.67k
{
18
4.67k
    PyObject *return_value = NULL;
19
4.67k
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
20
21
4.67k
    #define NUM_KEYWORDS 1
22
4.67k
    static struct {
23
4.67k
        PyGC_Head _this_is_not_used;
24
4.67k
        PyObject_VAR_HEAD
25
4.67k
        Py_hash_t ob_hash;
26
4.67k
        PyObject *ob_item[NUM_KEYWORDS];
27
4.67k
    } _kwtuple = {
28
4.67k
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
29
4.67k
        .ob_hash = -1,
30
4.67k
        .ob_item = { &_Py_ID(base), },
31
4.67k
    };
32
4.67k
    #undef NUM_KEYWORDS
33
4.67k
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
34
35
    #else  // !Py_BUILD_CORE
36
    #  define KWTUPLE NULL
37
    #endif  // !Py_BUILD_CORE
38
39
4.67k
    static const char * const _keywords[] = {"", "base", NULL};
40
4.67k
    static _PyArg_Parser _parser = {
41
4.67k
        .keywords = _keywords,
42
4.67k
        .fname = "int",
43
4.67k
        .kwtuple = KWTUPLE,
44
4.67k
    };
45
4.67k
    #undef KWTUPLE
46
4.67k
    PyObject *argsbuf[2];
47
4.67k
    PyObject * const *fastargs;
48
4.67k
    Py_ssize_t nargs = PyTuple_GET_SIZE(args);
49
4.67k
    Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0;
50
4.67k
    PyObject *x = NULL;
51
4.67k
    PyObject *obase = NULL;
52
53
4.67k
    fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser,
54
4.67k
            /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
55
4.67k
    if (!fastargs) {
56
0
        goto exit;
57
0
    }
58
4.67k
    if (nargs < 1) {
59
28
        goto skip_optional_posonly;
60
28
    }
61
4.64k
    noptargs--;
62
4.64k
    x = fastargs[0];
63
4.67k
skip_optional_posonly:
64
4.67k
    if (!noptargs) {
65
4.67k
        goto skip_optional_pos;
66
4.67k
    }
67
0
    obase = fastargs[1];
68
4.67k
skip_optional_pos:
69
4.67k
    return_value = long_new_impl(type, x, obase);
70
71
4.67k
exit:
72
4.67k
    return return_value;
73
4.67k
}
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
3.66M
{
107
3.66M
    PyObject *return_value = NULL;
108
3.66M
    PyObject *format_spec;
109
110
3.66M
    if (!PyUnicode_Check(arg)) {
111
0
        _PyArg_BadArgument("__format__", "argument", "str", arg);
112
0
        goto exit;
113
0
    }
114
3.66M
    format_spec = arg;
115
3.66M
    return_value = int___format___impl(self, format_spec);
116
117
3.66M
exit:
118
3.66M
    return return_value;
119
3.66M
}
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
3.40M
{
203
3.40M
    return int_bit_length_impl(self);
204
3.40M
}
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\n"
266
"    the integer is not representable with the given number of bytes.\n"
267
"    Default is length 1.\n"
268
"  byteorder\n"
269
"    The byte order used to represent the integer.  If byteorder is\n"
270
"    \'big\', the most significant byte is at the beginning of the byte\n"
271
"    array.  If byteorder is \'little\', the most significant byte is at\n"
272
"    the end of the byte array.  To request the native byte order of\n"
273
"    the host system, use sys.byteorder as the byte order value.\n"
274
"    Default is to use \'big\'.\n"
275
"  signed\n"
276
"    Determines whether two\'s complement is used to represent the\n"
277
"    integer.  If signed is False and a negative integer is given,\n"
278
"    an OverflowError is raised.");
279
280
#define INT_TO_BYTES_METHODDEF    \
281
    {"to_bytes", _PyCFunction_CAST(int_to_bytes), METH_FASTCALL|METH_KEYWORDS, int_to_bytes__doc__},
282
283
static PyObject *
284
int_to_bytes_impl(PyObject *self, Py_ssize_t length, PyObject *byteorder,
285
                  int is_signed);
286
287
static PyObject *
288
int_to_bytes(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
289
285k
{
290
285k
    PyObject *return_value = NULL;
291
285k
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
292
293
285k
    #define NUM_KEYWORDS 3
294
285k
    static struct {
295
285k
        PyGC_Head _this_is_not_used;
296
285k
        PyObject_VAR_HEAD
297
285k
        Py_hash_t ob_hash;
298
285k
        PyObject *ob_item[NUM_KEYWORDS];
299
285k
    } _kwtuple = {
300
285k
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
301
285k
        .ob_hash = -1,
302
285k
        .ob_item = { &_Py_ID(length), &_Py_ID(byteorder), &_Py_ID(signed), },
303
285k
    };
304
285k
    #undef NUM_KEYWORDS
305
285k
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
306
307
    #else  // !Py_BUILD_CORE
308
    #  define KWTUPLE NULL
309
    #endif  // !Py_BUILD_CORE
310
311
285k
    static const char * const _keywords[] = {"length", "byteorder", "signed", NULL};
312
285k
    static _PyArg_Parser _parser = {
313
285k
        .keywords = _keywords,
314
285k
        .fname = "to_bytes",
315
285k
        .kwtuple = KWTUPLE,
316
285k
    };
317
285k
    #undef KWTUPLE
318
285k
    PyObject *argsbuf[3];
319
285k
    Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
320
285k
    Py_ssize_t length = 1;
321
285k
    PyObject *byteorder = NULL;
322
285k
    int is_signed = 0;
323
324
285k
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
325
285k
            /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
326
285k
    if (!args) {
327
0
        goto exit;
328
0
    }
329
285k
    if (!noptargs) {
330
0
        goto skip_optional_pos;
331
0
    }
332
285k
    if (args[0]) {
333
285k
        {
334
285k
            Py_ssize_t ival = -1;
335
285k
            PyObject *iobj = _PyNumber_Index(args[0]);
336
285k
            if (iobj != NULL) {
337
285k
                ival = PyLong_AsSsize_t(iobj);
338
285k
                Py_DECREF(iobj);
339
285k
            }
340
285k
            if (ival == -1 && PyErr_Occurred()) {
341
0
                goto exit;
342
0
            }
343
285k
            length = ival;
344
285k
            if (length < 0) {
345
0
                PyErr_SetString(PyExc_ValueError,
346
0
                                "length cannot be negative");
347
0
                goto exit;
348
0
            }
349
285k
        }
350
285k
        if (!--noptargs) {
351
0
            goto skip_optional_pos;
352
0
        }
353
285k
    }
354
285k
    if (args[1]) {
355
285k
        if (!PyUnicode_Check(args[1])) {
356
0
            _PyArg_BadArgument("to_bytes", "argument 'byteorder'", "str", args[1]);
357
0
            goto exit;
358
0
        }
359
285k
        byteorder = args[1];
360
285k
        if (!--noptargs) {
361
174k
            goto skip_optional_pos;
362
174k
        }
363
285k
    }
364
285k
skip_optional_pos:
365
285k
    if (!noptargs) {
366
174k
        goto skip_optional_kwonly;
367
174k
    }
368
110k
    is_signed = PyObject_IsTrue(args[2]);
369
110k
    if (is_signed < 0) {
370
0
        goto exit;
371
0
    }
372
285k
skip_optional_kwonly:
373
285k
    return_value = int_to_bytes_impl(self, length, byteorder, is_signed);
374
375
285k
exit:
376
285k
    return return_value;
377
285k
}
378
379
PyDoc_STRVAR(int_from_bytes__doc__,
380
"from_bytes($type, /, bytes, byteorder=\'big\', *, signed=False)\n"
381
"--\n"
382
"\n"
383
"Return the integer represented by the given array of bytes.\n"
384
"\n"
385
"  bytes\n"
386
"    Holds the array of bytes to convert.  The argument must either\n"
387
"    support the buffer protocol or be an iterable object producing\n"
388
"    bytes.  Bytes and bytearray are examples of built-in objects that\n"
389
"    support the buffer protocol.\n"
390
"  byteorder\n"
391
"    The byte order used to represent the integer.  If byteorder is\n"
392
"    \'big\', the most significant byte is at the beginning of the byte\n"
393
"    array.  If byteorder is \'little\', the most significant byte is at\n"
394
"    the end of the byte array.  To request the native byte order of\n"
395
"    the host system, use sys.byteorder as the byte order value.\n"
396
"    Default is to use \'big\'.\n"
397
"  signed\n"
398
"    Indicates whether two\'s complement is used to represent the\n"
399
"    integer.");
400
401
#define INT_FROM_BYTES_METHODDEF    \
402
    {"from_bytes", _PyCFunction_CAST(int_from_bytes), METH_FASTCALL|METH_KEYWORDS|METH_CLASS, int_from_bytes__doc__},
403
404
static PyObject *
405
int_from_bytes_impl(PyTypeObject *type, PyObject *bytes_obj,
406
                    PyObject *byteorder, int is_signed);
407
408
static PyObject *
409
int_from_bytes(PyObject *type, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
410
16.7M
{
411
16.7M
    PyObject *return_value = NULL;
412
16.7M
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
413
414
16.7M
    #define NUM_KEYWORDS 3
415
16.7M
    static struct {
416
16.7M
        PyGC_Head _this_is_not_used;
417
16.7M
        PyObject_VAR_HEAD
418
16.7M
        Py_hash_t ob_hash;
419
16.7M
        PyObject *ob_item[NUM_KEYWORDS];
420
16.7M
    } _kwtuple = {
421
16.7M
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
422
16.7M
        .ob_hash = -1,
423
16.7M
        .ob_item = { &_Py_ID(bytes), &_Py_ID(byteorder), &_Py_ID(signed), },
424
16.7M
    };
425
16.7M
    #undef NUM_KEYWORDS
426
16.7M
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
427
428
    #else  // !Py_BUILD_CORE
429
    #  define KWTUPLE NULL
430
    #endif  // !Py_BUILD_CORE
431
432
16.7M
    static const char * const _keywords[] = {"bytes", "byteorder", "signed", NULL};
433
16.7M
    static _PyArg_Parser _parser = {
434
16.7M
        .keywords = _keywords,
435
16.7M
        .fname = "from_bytes",
436
16.7M
        .kwtuple = KWTUPLE,
437
16.7M
    };
438
16.7M
    #undef KWTUPLE
439
16.7M
    PyObject *argsbuf[3];
440
16.7M
    Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
441
16.7M
    PyObject *bytes_obj;
442
16.7M
    PyObject *byteorder = NULL;
443
16.7M
    int is_signed = 0;
444
445
16.7M
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
446
16.7M
            /*minpos*/ 1, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
447
16.7M
    if (!args) {
448
0
        goto exit;
449
0
    }
450
16.7M
    bytes_obj = args[0];
451
16.7M
    if (!noptargs) {
452
0
        goto skip_optional_pos;
453
0
    }
454
16.7M
    if (args[1]) {
455
16.7M
        if (!PyUnicode_Check(args[1])) {
456
0
            _PyArg_BadArgument("from_bytes", "argument 'byteorder'", "str", args[1]);
457
0
            goto exit;
458
0
        }
459
16.7M
        byteorder = args[1];
460
16.7M
        if (!--noptargs) {
461
16.7M
            goto skip_optional_pos;
462
16.7M
        }
463
16.7M
    }
464
16.7M
skip_optional_pos:
465
16.7M
    if (!noptargs) {
466
16.7M
        goto skip_optional_kwonly;
467
16.7M
    }
468
52
    is_signed = PyObject_IsTrue(args[2]);
469
52
    if (is_signed < 0) {
470
0
        goto exit;
471
0
    }
472
16.7M
skip_optional_kwonly:
473
16.7M
    return_value = int_from_bytes_impl((PyTypeObject *)type, bytes_obj, byteorder, is_signed);
474
475
16.7M
exit:
476
16.7M
    return return_value;
477
16.7M
}
478
479
PyDoc_STRVAR(int_is_integer__doc__,
480
"is_integer($self, /)\n"
481
"--\n"
482
"\n"
483
"Returns True. Exists for duck type compatibility with float.is_integer.");
484
485
#define INT_IS_INTEGER_METHODDEF    \
486
    {"is_integer", (PyCFunction)int_is_integer, METH_NOARGS, int_is_integer__doc__},
487
488
static PyObject *
489
int_is_integer_impl(PyObject *self);
490
491
static PyObject *
492
int_is_integer(PyObject *self, PyObject *Py_UNUSED(ignored))
493
0
{
494
0
    return int_is_integer_impl(self);
495
0
}
496
/*[clinic end generated code: output=d95766fb7ff46963 input=a9049054013a1b77]*/