blob: 7db89650aea63391300b83082b69abc1d033c73c [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
90PyDoc_STRVAR(int___sizeof____doc__,
91"__sizeof__($self, /)\n"
92"--\n"
93"\n"
94"Returns size in memory, in bytes.");
95
96#define INT___SIZEOF___METHODDEF \
97 {"__sizeof__", (PyCFunction)int___sizeof__, METH_NOARGS, int___sizeof____doc__},
98
99static Py_ssize_t
100int___sizeof___impl(PyObject *self);
101
102static PyObject *
103int___sizeof__(PyObject *self, PyObject *Py_UNUSED(ignored))
104{
105 PyObject *return_value = NULL;
106 Py_ssize_t _return_value;
107
108 _return_value = int___sizeof___impl(self);
109 if ((_return_value == -1) && PyErr_Occurred()) {
110 goto exit;
111 }
112 return_value = PyLong_FromSsize_t(_return_value);
113
114exit:
115 return return_value;
116}
117
118PyDoc_STRVAR(int_bit_length__doc__,
119"bit_length($self, /)\n"
120"--\n"
121"\n"
122"Number of bits necessary to represent self in binary.\n"
123"\n"
124">>> bin(37)\n"
125"\'0b100101\'\n"
126">>> (37).bit_length()\n"
127"6");
128
129#define INT_BIT_LENGTH_METHODDEF \
130 {"bit_length", (PyCFunction)int_bit_length, METH_NOARGS, int_bit_length__doc__},
131
132static PyObject *
133int_bit_length_impl(PyObject *self);
134
135static PyObject *
136int_bit_length(PyObject *self, PyObject *Py_UNUSED(ignored))
137{
138 return int_bit_length_impl(self);
139}
140
Lisa Roach5ac70432018-09-13 23:56:23 -0700141PyDoc_STRVAR(int_as_integer_ratio__doc__,
142"as_integer_ratio($self, /)\n"
143"--\n"
144"\n"
145"Return integer ratio.\n"
146"\n"
147"Return a pair of integers, whose ratio is exactly equal to the original int\n"
148"and with a positive denominator.\n"
149"\n"
150">>> (10).as_integer_ratio()\n"
151"(10, 1)\n"
152">>> (-10).as_integer_ratio()\n"
153"(-10, 1)\n"
154">>> (0).as_integer_ratio()\n"
155"(0, 1)");
156
157#define INT_AS_INTEGER_RATIO_METHODDEF \
158 {"as_integer_ratio", (PyCFunction)int_as_integer_ratio, METH_NOARGS, int_as_integer_ratio__doc__},
159
160static PyObject *
161int_as_integer_ratio_impl(PyObject *self);
162
163static PyObject *
164int_as_integer_ratio(PyObject *self, PyObject *Py_UNUSED(ignored))
165{
166 return int_as_integer_ratio_impl(self);
167}
168
Serhiy Storchaka495e8802017-02-01 23:12:20 +0200169PyDoc_STRVAR(int_to_bytes__doc__,
170"to_bytes($self, /, length, byteorder, *, signed=False)\n"
171"--\n"
172"\n"
173"Return an array of bytes representing an integer.\n"
174"\n"
175" length\n"
176" Length of bytes object to use. An OverflowError is raised if the\n"
177" integer is not representable with the given number of bytes.\n"
178" byteorder\n"
179" The byte order used to represent the integer. If byteorder is \'big\',\n"
180" the most significant byte is at the beginning of the byte array. If\n"
181" byteorder is \'little\', the most significant byte is at the end of the\n"
182" byte array. To request the native byte order of the host system, use\n"
183" `sys.byteorder\' as the byte order value.\n"
184" signed\n"
185" Determines whether two\'s complement is used to represent the integer.\n"
186" If signed is False and a negative integer is given, an OverflowError\n"
187" is raised.");
188
189#define INT_TO_BYTES_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200190 {"to_bytes", (PyCFunction)(void(*)(void))int_to_bytes, METH_FASTCALL|METH_KEYWORDS, int_to_bytes__doc__},
Serhiy Storchaka495e8802017-02-01 23:12:20 +0200191
192static PyObject *
193int_to_bytes_impl(PyObject *self, Py_ssize_t length, PyObject *byteorder,
194 int is_signed);
195
196static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200197int_to_bytes(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka495e8802017-02-01 23:12:20 +0200198{
199 PyObject *return_value = NULL;
200 static const char * const _keywords[] = {"length", "byteorder", "signed", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200201 static _PyArg_Parser _parser = {NULL, _keywords, "to_bytes", 0};
202 PyObject *argsbuf[3];
203 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
Serhiy Storchaka495e8802017-02-01 23:12:20 +0200204 Py_ssize_t length;
205 PyObject *byteorder;
206 int is_signed = 0;
207
Serhiy Storchaka31913912019-03-14 10:32:22 +0200208 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
209 if (!args) {
Serhiy Storchaka495e8802017-02-01 23:12:20 +0200210 goto exit;
211 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200212 {
213 Py_ssize_t ival = -1;
Serhiy Storchaka5f4b229d2020-05-28 10:33:45 +0300214 PyObject *iobj = _PyNumber_Index(args[0]);
Serhiy Storchaka31913912019-03-14 10:32:22 +0200215 if (iobj != NULL) {
216 ival = PyLong_AsSsize_t(iobj);
217 Py_DECREF(iobj);
218 }
219 if (ival == -1 && PyErr_Occurred()) {
220 goto exit;
221 }
222 length = ival;
223 }
224 if (!PyUnicode_Check(args[1])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200225 _PyArg_BadArgument("to_bytes", "argument 'byteorder'", "str", args[1]);
Serhiy Storchaka31913912019-03-14 10:32:22 +0200226 goto exit;
227 }
228 if (PyUnicode_READY(args[1]) == -1) {
229 goto exit;
230 }
231 byteorder = args[1];
232 if (!noptargs) {
233 goto skip_optional_kwonly;
234 }
235 is_signed = PyObject_IsTrue(args[2]);
236 if (is_signed < 0) {
237 goto exit;
238 }
239skip_optional_kwonly:
Serhiy Storchaka495e8802017-02-01 23:12:20 +0200240 return_value = int_to_bytes_impl(self, length, byteorder, is_signed);
241
242exit:
243 return return_value;
244}
245
246PyDoc_STRVAR(int_from_bytes__doc__,
247"from_bytes($type, /, bytes, byteorder, *, signed=False)\n"
248"--\n"
249"\n"
250"Return the integer represented by the given array of bytes.\n"
251"\n"
252" bytes\n"
253" Holds the array of bytes to convert. The argument must either\n"
254" support the buffer protocol or be an iterable object producing bytes.\n"
255" Bytes and bytearray are examples of built-in objects that support the\n"
256" buffer protocol.\n"
257" byteorder\n"
258" The byte order used to represent the integer. If byteorder is \'big\',\n"
259" the most significant byte is at the beginning of the byte array. If\n"
260" byteorder is \'little\', the most significant byte is at the end of the\n"
261" byte array. To request the native byte order of the host system, use\n"
262" `sys.byteorder\' as the byte order value.\n"
263" signed\n"
264" Indicates whether two\'s complement is used to represent the integer.");
265
266#define INT_FROM_BYTES_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200267 {"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 +0200268
269static PyObject *
270int_from_bytes_impl(PyTypeObject *type, PyObject *bytes_obj,
271 PyObject *byteorder, int is_signed);
272
273static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200274int_from_bytes(PyTypeObject *type, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka495e8802017-02-01 23:12:20 +0200275{
276 PyObject *return_value = NULL;
277 static const char * const _keywords[] = {"bytes", "byteorder", "signed", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200278 static _PyArg_Parser _parser = {NULL, _keywords, "from_bytes", 0};
279 PyObject *argsbuf[3];
280 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
Serhiy Storchaka495e8802017-02-01 23:12:20 +0200281 PyObject *bytes_obj;
282 PyObject *byteorder;
283 int is_signed = 0;
284
Serhiy Storchaka31913912019-03-14 10:32:22 +0200285 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
286 if (!args) {
Serhiy Storchaka495e8802017-02-01 23:12:20 +0200287 goto exit;
288 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200289 bytes_obj = args[0];
290 if (!PyUnicode_Check(args[1])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200291 _PyArg_BadArgument("from_bytes", "argument 'byteorder'", "str", args[1]);
Serhiy Storchaka31913912019-03-14 10:32:22 +0200292 goto exit;
293 }
294 if (PyUnicode_READY(args[1]) == -1) {
295 goto exit;
296 }
297 byteorder = args[1];
298 if (!noptargs) {
299 goto skip_optional_kwonly;
300 }
301 is_signed = PyObject_IsTrue(args[2]);
302 if (is_signed < 0) {
303 goto exit;
304 }
305skip_optional_kwonly:
Serhiy Storchaka495e8802017-02-01 23:12:20 +0200306 return_value = int_from_bytes_impl(type, bytes_obj, byteorder, is_signed);
307
308exit:
309 return return_value;
310}
Serhiy Storchaka5f4b229d2020-05-28 10:33:45 +0300311/*[clinic end generated code: output=63b8274fc784d617 input=a9049054013a1b77]*/