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