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