blob: 4bd47b116f883c60d7ae729447b5ac06b1fca8ae [file] [log] [blame]
Serhiy Storchaka495e8802017-02-01 23:12:20 +02001/*[clinic input]
2preserve
3[clinic start generated code]*/
4
Serhiy Storchaka18b250f2017-03-19 08:51:07 +02005static PyObject *
6long_new_impl(PyTypeObject *type, PyObject *x, PyObject *obase);
7
8static PyObject *
9long_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
10{
11 PyObject *return_value = NULL;
12 static const char * const _keywords[] = {"", "base", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +020013 static _PyArg_Parser _parser = {NULL, _keywords, "int", 0};
14 PyObject *argsbuf[2];
15 PyObject * const *fastargs;
16 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
17 Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0;
Serhiy Storchaka18b250f2017-03-19 08:51:07 +020018 PyObject *x = NULL;
19 PyObject *obase = NULL;
20
Serhiy Storchaka31913912019-03-14 10:32:22 +020021 fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 0, 2, 0, argsbuf);
22 if (!fastargs) {
Serhiy Storchaka18b250f2017-03-19 08:51:07 +020023 goto exit;
24 }
Serhiy Storchaka31913912019-03-14 10:32:22 +020025 if (nargs < 1) {
26 goto skip_optional_posonly;
27 }
28 noptargs--;
29 x = fastargs[0];
30skip_optional_posonly:
31 if (!noptargs) {
32 goto skip_optional_pos;
33 }
34 obase = fastargs[1];
35skip_optional_pos:
Serhiy Storchaka18b250f2017-03-19 08:51:07 +020036 return_value = long_new_impl(type, x, obase);
37
38exit:
39 return return_value;
40}
41
Serhiy Storchaka495e8802017-02-01 23:12:20 +020042PyDoc_STRVAR(int___getnewargs____doc__,
43"__getnewargs__($self, /)\n"
44"--\n"
45"\n");
46
47#define INT___GETNEWARGS___METHODDEF \
48 {"__getnewargs__", (PyCFunction)int___getnewargs__, METH_NOARGS, int___getnewargs____doc__},
49
50static PyObject *
51int___getnewargs___impl(PyObject *self);
52
53static PyObject *
54int___getnewargs__(PyObject *self, PyObject *Py_UNUSED(ignored))
55{
56 return int___getnewargs___impl(self);
57}
58
59PyDoc_STRVAR(int___format____doc__,
60"__format__($self, format_spec, /)\n"
61"--\n"
62"\n");
63
64#define INT___FORMAT___METHODDEF \
65 {"__format__", (PyCFunction)int___format__, METH_O, int___format____doc__},
66
67static PyObject *
68int___format___impl(PyObject *self, PyObject *format_spec);
69
70static PyObject *
71int___format__(PyObject *self, PyObject *arg)
72{
73 PyObject *return_value = NULL;
74 PyObject *format_spec;
75
Serhiy Storchaka32d96a22018-12-25 13:23:47 +020076 if (!PyUnicode_Check(arg)) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +020077 _PyArg_BadArgument("__format__", "argument", "str", arg);
Serhiy Storchaka495e8802017-02-01 23:12:20 +020078 goto exit;
79 }
Serhiy Storchaka32d96a22018-12-25 13:23:47 +020080 if (PyUnicode_READY(arg) == -1) {
81 goto exit;
82 }
83 format_spec = arg;
Serhiy Storchaka495e8802017-02-01 23:12:20 +020084 return_value = int___format___impl(self, format_spec);
85
86exit:
87 return return_value;
88}
89
Serhiy Storchaka5a2bac72020-07-20 15:57:37 +030090PyDoc_STRVAR(int___round____doc__,
91"__round__($self, ndigits=<unrepresentable>, /)\n"
92"--\n"
93"\n"
94"Rounding an Integral returns itself.\n"
95"\n"
96"Rounding with an ndigits argument also returns an integer.");
97
98#define INT___ROUND___METHODDEF \
99 {"__round__", (PyCFunction)(void(*)(void))int___round__, METH_FASTCALL, int___round____doc__},
100
101static PyObject *
102int___round___impl(PyObject *self, PyObject *o_ndigits);
103
104static PyObject *
105int___round__(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
106{
107 PyObject *return_value = NULL;
108 PyObject *o_ndigits = NULL;
109
110 if (!_PyArg_CheckPositional("__round__", nargs, 0, 1)) {
111 goto exit;
112 }
113 if (nargs < 1) {
114 goto skip_optional;
115 }
116 o_ndigits = args[0];
117skip_optional:
118 return_value = int___round___impl(self, o_ndigits);
119
120exit:
121 return return_value;
122}
123
Serhiy Storchaka495e8802017-02-01 23:12:20 +0200124PyDoc_STRVAR(int___sizeof____doc__,
125"__sizeof__($self, /)\n"
126"--\n"
127"\n"
128"Returns size in memory, in bytes.");
129
130#define INT___SIZEOF___METHODDEF \
131 {"__sizeof__", (PyCFunction)int___sizeof__, METH_NOARGS, int___sizeof____doc__},
132
133static Py_ssize_t
134int___sizeof___impl(PyObject *self);
135
136static PyObject *
137int___sizeof__(PyObject *self, PyObject *Py_UNUSED(ignored))
138{
139 PyObject *return_value = NULL;
140 Py_ssize_t _return_value;
141
142 _return_value = int___sizeof___impl(self);
143 if ((_return_value == -1) && PyErr_Occurred()) {
144 goto exit;
145 }
146 return_value = PyLong_FromSsize_t(_return_value);
147
148exit:
149 return return_value;
150}
151
152PyDoc_STRVAR(int_bit_length__doc__,
153"bit_length($self, /)\n"
154"--\n"
155"\n"
156"Number of bits necessary to represent self in binary.\n"
157"\n"
158">>> bin(37)\n"
159"\'0b100101\'\n"
160">>> (37).bit_length()\n"
161"6");
162
163#define INT_BIT_LENGTH_METHODDEF \
164 {"bit_length", (PyCFunction)int_bit_length, METH_NOARGS, int_bit_length__doc__},
165
166static PyObject *
167int_bit_length_impl(PyObject *self);
168
169static PyObject *
170int_bit_length(PyObject *self, PyObject *Py_UNUSED(ignored))
171{
172 return int_bit_length_impl(self);
173}
174
Niklas Fiekas8bd216d2020-05-29 18:28:02 +0200175PyDoc_STRVAR(int_bit_count__doc__,
176"bit_count($self, /)\n"
177"--\n"
178"\n"
179"Number of ones in the binary representation of the absolute value of self.\n"
180"\n"
181"Also known as the population count.\n"
182"\n"
183">>> bin(13)\n"
184"\'0b1101\'\n"
185">>> (13).bit_count()\n"
186"3");
187
188#define INT_BIT_COUNT_METHODDEF \
189 {"bit_count", (PyCFunction)int_bit_count, METH_NOARGS, int_bit_count__doc__},
190
191static PyObject *
192int_bit_count_impl(PyObject *self);
193
194static PyObject *
195int_bit_count(PyObject *self, PyObject *Py_UNUSED(ignored))
196{
197 return int_bit_count_impl(self);
198}
199
Lisa Roach5ac70432018-09-13 23:56:23 -0700200PyDoc_STRVAR(int_as_integer_ratio__doc__,
201"as_integer_ratio($self, /)\n"
202"--\n"
203"\n"
204"Return integer ratio.\n"
205"\n"
206"Return a pair of integers, whose ratio is exactly equal to the original int\n"
207"and with a positive denominator.\n"
208"\n"
209">>> (10).as_integer_ratio()\n"
210"(10, 1)\n"
211">>> (-10).as_integer_ratio()\n"
212"(-10, 1)\n"
213">>> (0).as_integer_ratio()\n"
214"(0, 1)");
215
216#define INT_AS_INTEGER_RATIO_METHODDEF \
217 {"as_integer_ratio", (PyCFunction)int_as_integer_ratio, METH_NOARGS, int_as_integer_ratio__doc__},
218
219static PyObject *
220int_as_integer_ratio_impl(PyObject *self);
221
222static PyObject *
223int_as_integer_ratio(PyObject *self, PyObject *Py_UNUSED(ignored))
224{
225 return int_as_integer_ratio_impl(self);
226}
227
Serhiy Storchaka495e8802017-02-01 23:12:20 +0200228PyDoc_STRVAR(int_to_bytes__doc__,
229"to_bytes($self, /, length, byteorder, *, signed=False)\n"
230"--\n"
231"\n"
232"Return an array of bytes representing an integer.\n"
233"\n"
234" length\n"
235" Length of bytes object to use. An OverflowError is raised if the\n"
236" integer is not representable with the given number of bytes.\n"
237" byteorder\n"
238" The byte order used to represent the integer. If byteorder is \'big\',\n"
239" the most significant byte is at the beginning of the byte array. If\n"
240" byteorder is \'little\', the most significant byte is at the end of the\n"
241" byte array. To request the native byte order of the host system, use\n"
242" `sys.byteorder\' as the byte order value.\n"
243" signed\n"
244" Determines whether two\'s complement is used to represent the integer.\n"
245" If signed is False and a negative integer is given, an OverflowError\n"
246" is raised.");
247
248#define INT_TO_BYTES_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200249 {"to_bytes", (PyCFunction)(void(*)(void))int_to_bytes, METH_FASTCALL|METH_KEYWORDS, int_to_bytes__doc__},
Serhiy Storchaka495e8802017-02-01 23:12:20 +0200250
251static PyObject *
252int_to_bytes_impl(PyObject *self, Py_ssize_t length, PyObject *byteorder,
253 int is_signed);
254
255static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200256int_to_bytes(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka495e8802017-02-01 23:12:20 +0200257{
258 PyObject *return_value = NULL;
259 static const char * const _keywords[] = {"length", "byteorder", "signed", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200260 static _PyArg_Parser _parser = {NULL, _keywords, "to_bytes", 0};
261 PyObject *argsbuf[3];
262 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
Serhiy Storchaka495e8802017-02-01 23:12:20 +0200263 Py_ssize_t length;
264 PyObject *byteorder;
265 int is_signed = 0;
266
Serhiy Storchaka31913912019-03-14 10:32:22 +0200267 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
268 if (!args) {
Serhiy Storchaka495e8802017-02-01 23:12:20 +0200269 goto exit;
270 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200271 {
272 Py_ssize_t ival = -1;
Serhiy Storchaka5f4b229d2020-05-28 10:33:45 +0300273 PyObject *iobj = _PyNumber_Index(args[0]);
Serhiy Storchaka31913912019-03-14 10:32:22 +0200274 if (iobj != NULL) {
275 ival = PyLong_AsSsize_t(iobj);
276 Py_DECREF(iobj);
277 }
278 if (ival == -1 && PyErr_Occurred()) {
279 goto exit;
280 }
281 length = ival;
282 }
283 if (!PyUnicode_Check(args[1])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200284 _PyArg_BadArgument("to_bytes", "argument 'byteorder'", "str", args[1]);
Serhiy Storchaka31913912019-03-14 10:32:22 +0200285 goto exit;
286 }
287 if (PyUnicode_READY(args[1]) == -1) {
288 goto exit;
289 }
290 byteorder = args[1];
291 if (!noptargs) {
292 goto skip_optional_kwonly;
293 }
294 is_signed = PyObject_IsTrue(args[2]);
295 if (is_signed < 0) {
296 goto exit;
297 }
298skip_optional_kwonly:
Serhiy Storchaka495e8802017-02-01 23:12:20 +0200299 return_value = int_to_bytes_impl(self, length, byteorder, is_signed);
300
301exit:
302 return return_value;
303}
304
305PyDoc_STRVAR(int_from_bytes__doc__,
306"from_bytes($type, /, bytes, byteorder, *, signed=False)\n"
307"--\n"
308"\n"
309"Return the integer represented by the given array of bytes.\n"
310"\n"
311" bytes\n"
312" Holds the array of bytes to convert. The argument must either\n"
313" support the buffer protocol or be an iterable object producing bytes.\n"
314" Bytes and bytearray are examples of built-in objects that support the\n"
315" buffer protocol.\n"
316" byteorder\n"
317" The byte order used to represent the integer. If byteorder is \'big\',\n"
318" the most significant byte is at the beginning of the byte array. If\n"
319" byteorder is \'little\', the most significant byte is at the end of the\n"
320" byte array. To request the native byte order of the host system, use\n"
321" `sys.byteorder\' as the byte order value.\n"
322" signed\n"
323" Indicates whether two\'s complement is used to represent the integer.");
324
325#define INT_FROM_BYTES_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200326 {"from_bytes", (PyCFunction)(void(*)(void))int_from_bytes, METH_FASTCALL|METH_KEYWORDS|METH_CLASS, int_from_bytes__doc__},
Serhiy Storchaka495e8802017-02-01 23:12:20 +0200327
328static PyObject *
329int_from_bytes_impl(PyTypeObject *type, PyObject *bytes_obj,
330 PyObject *byteorder, int is_signed);
331
332static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200333int_from_bytes(PyTypeObject *type, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka495e8802017-02-01 23:12:20 +0200334{
335 PyObject *return_value = NULL;
336 static const char * const _keywords[] = {"bytes", "byteorder", "signed", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200337 static _PyArg_Parser _parser = {NULL, _keywords, "from_bytes", 0};
338 PyObject *argsbuf[3];
339 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
Serhiy Storchaka495e8802017-02-01 23:12:20 +0200340 PyObject *bytes_obj;
341 PyObject *byteorder;
342 int is_signed = 0;
343
Serhiy Storchaka31913912019-03-14 10:32:22 +0200344 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
345 if (!args) {
Serhiy Storchaka495e8802017-02-01 23:12:20 +0200346 goto exit;
347 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200348 bytes_obj = args[0];
349 if (!PyUnicode_Check(args[1])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200350 _PyArg_BadArgument("from_bytes", "argument 'byteorder'", "str", args[1]);
Serhiy Storchaka31913912019-03-14 10:32:22 +0200351 goto exit;
352 }
353 if (PyUnicode_READY(args[1]) == -1) {
354 goto exit;
355 }
356 byteorder = args[1];
357 if (!noptargs) {
358 goto skip_optional_kwonly;
359 }
360 is_signed = PyObject_IsTrue(args[2]);
361 if (is_signed < 0) {
362 goto exit;
363 }
364skip_optional_kwonly:
Serhiy Storchaka495e8802017-02-01 23:12:20 +0200365 return_value = int_from_bytes_impl(type, bytes_obj, byteorder, is_signed);
366
367exit:
368 return return_value;
369}
Raymond Hettinger4e0ce822020-10-07 16:43:44 -0700370/*[clinic end generated code: output=ea18e51af5b53591 input=a9049054013a1b77]*/