Debian Codegraph @

Objects/clinic/longobject.c.h

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