blob: 27cdf9e60509796cb800620d077994b3b02c9068 [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};
13 static _PyArg_Parser _parser = {"|OO:int", _keywords, 0};
14 PyObject *x = NULL;
15 PyObject *obase = NULL;
16
17 if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser,
18 &x, &obase)) {
19 goto exit;
20 }
21 return_value = long_new_impl(type, x, obase);
22
23exit:
24 return return_value;
25}
26
Serhiy Storchaka495e8802017-02-01 23:12:20 +020027PyDoc_STRVAR(int___getnewargs____doc__,
28"__getnewargs__($self, /)\n"
29"--\n"
30"\n");
31
32#define INT___GETNEWARGS___METHODDEF \
33 {"__getnewargs__", (PyCFunction)int___getnewargs__, METH_NOARGS, int___getnewargs____doc__},
34
35static PyObject *
36int___getnewargs___impl(PyObject *self);
37
38static PyObject *
39int___getnewargs__(PyObject *self, PyObject *Py_UNUSED(ignored))
40{
41 return int___getnewargs___impl(self);
42}
43
44PyDoc_STRVAR(int___format____doc__,
45"__format__($self, format_spec, /)\n"
46"--\n"
47"\n");
48
49#define INT___FORMAT___METHODDEF \
50 {"__format__", (PyCFunction)int___format__, METH_O, int___format____doc__},
51
52static PyObject *
53int___format___impl(PyObject *self, PyObject *format_spec);
54
55static PyObject *
56int___format__(PyObject *self, PyObject *arg)
57{
58 PyObject *return_value = NULL;
59 PyObject *format_spec;
60
Serhiy Storchaka32d96a22018-12-25 13:23:47 +020061 if (!PyUnicode_Check(arg)) {
62 _PyArg_BadArgument("__format__", "str", arg);
Serhiy Storchaka495e8802017-02-01 23:12:20 +020063 goto exit;
64 }
Serhiy Storchaka32d96a22018-12-25 13:23:47 +020065 if (PyUnicode_READY(arg) == -1) {
66 goto exit;
67 }
68 format_spec = arg;
Serhiy Storchaka495e8802017-02-01 23:12:20 +020069 return_value = int___format___impl(self, format_spec);
70
71exit:
72 return return_value;
73}
74
75PyDoc_STRVAR(int___sizeof____doc__,
76"__sizeof__($self, /)\n"
77"--\n"
78"\n"
79"Returns size in memory, in bytes.");
80
81#define INT___SIZEOF___METHODDEF \
82 {"__sizeof__", (PyCFunction)int___sizeof__, METH_NOARGS, int___sizeof____doc__},
83
84static Py_ssize_t
85int___sizeof___impl(PyObject *self);
86
87static PyObject *
88int___sizeof__(PyObject *self, PyObject *Py_UNUSED(ignored))
89{
90 PyObject *return_value = NULL;
91 Py_ssize_t _return_value;
92
93 _return_value = int___sizeof___impl(self);
94 if ((_return_value == -1) && PyErr_Occurred()) {
95 goto exit;
96 }
97 return_value = PyLong_FromSsize_t(_return_value);
98
99exit:
100 return return_value;
101}
102
103PyDoc_STRVAR(int_bit_length__doc__,
104"bit_length($self, /)\n"
105"--\n"
106"\n"
107"Number of bits necessary to represent self in binary.\n"
108"\n"
109">>> bin(37)\n"
110"\'0b100101\'\n"
111">>> (37).bit_length()\n"
112"6");
113
114#define INT_BIT_LENGTH_METHODDEF \
115 {"bit_length", (PyCFunction)int_bit_length, METH_NOARGS, int_bit_length__doc__},
116
117static PyObject *
118int_bit_length_impl(PyObject *self);
119
120static PyObject *
121int_bit_length(PyObject *self, PyObject *Py_UNUSED(ignored))
122{
123 return int_bit_length_impl(self);
124}
125
Lisa Roach5ac70432018-09-13 23:56:23 -0700126PyDoc_STRVAR(int_as_integer_ratio__doc__,
127"as_integer_ratio($self, /)\n"
128"--\n"
129"\n"
130"Return integer ratio.\n"
131"\n"
132"Return a pair of integers, whose ratio is exactly equal to the original int\n"
133"and with a positive denominator.\n"
134"\n"
135">>> (10).as_integer_ratio()\n"
136"(10, 1)\n"
137">>> (-10).as_integer_ratio()\n"
138"(-10, 1)\n"
139">>> (0).as_integer_ratio()\n"
140"(0, 1)");
141
142#define INT_AS_INTEGER_RATIO_METHODDEF \
143 {"as_integer_ratio", (PyCFunction)int_as_integer_ratio, METH_NOARGS, int_as_integer_ratio__doc__},
144
145static PyObject *
146int_as_integer_ratio_impl(PyObject *self);
147
148static PyObject *
149int_as_integer_ratio(PyObject *self, PyObject *Py_UNUSED(ignored))
150{
151 return int_as_integer_ratio_impl(self);
152}
153
Serhiy Storchaka495e8802017-02-01 23:12:20 +0200154PyDoc_STRVAR(int_to_bytes__doc__,
155"to_bytes($self, /, length, byteorder, *, signed=False)\n"
156"--\n"
157"\n"
158"Return an array of bytes representing an integer.\n"
159"\n"
160" length\n"
161" Length of bytes object to use. An OverflowError is raised if the\n"
162" integer is not representable with the given number of bytes.\n"
163" byteorder\n"
164" The byte order used to represent the integer. If byteorder is \'big\',\n"
165" the most significant byte is at the beginning of the byte array. If\n"
166" byteorder is \'little\', the most significant byte is at the end of the\n"
167" byte array. To request the native byte order of the host system, use\n"
168" `sys.byteorder\' as the byte order value.\n"
169" signed\n"
170" Determines whether two\'s complement is used to represent the integer.\n"
171" If signed is False and a negative integer is given, an OverflowError\n"
172" is raised.");
173
174#define INT_TO_BYTES_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200175 {"to_bytes", (PyCFunction)(void(*)(void))int_to_bytes, METH_FASTCALL|METH_KEYWORDS, int_to_bytes__doc__},
Serhiy Storchaka495e8802017-02-01 23:12:20 +0200176
177static PyObject *
178int_to_bytes_impl(PyObject *self, Py_ssize_t length, PyObject *byteorder,
179 int is_signed);
180
181static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200182int_to_bytes(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka495e8802017-02-01 23:12:20 +0200183{
184 PyObject *return_value = NULL;
185 static const char * const _keywords[] = {"length", "byteorder", "signed", NULL};
186 static _PyArg_Parser _parser = {"nU|$p:to_bytes", _keywords, 0};
187 Py_ssize_t length;
188 PyObject *byteorder;
189 int is_signed = 0;
190
191 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
192 &length, &byteorder, &is_signed)) {
193 goto exit;
194 }
195 return_value = int_to_bytes_impl(self, length, byteorder, is_signed);
196
197exit:
198 return return_value;
199}
200
201PyDoc_STRVAR(int_from_bytes__doc__,
202"from_bytes($type, /, bytes, byteorder, *, signed=False)\n"
203"--\n"
204"\n"
205"Return the integer represented by the given array of bytes.\n"
206"\n"
207" bytes\n"
208" Holds the array of bytes to convert. The argument must either\n"
209" support the buffer protocol or be an iterable object producing bytes.\n"
210" Bytes and bytearray are examples of built-in objects that support the\n"
211" buffer protocol.\n"
212" byteorder\n"
213" The byte order used to represent the integer. If byteorder is \'big\',\n"
214" the most significant byte is at the beginning of the byte array. If\n"
215" byteorder is \'little\', the most significant byte is at the end of the\n"
216" byte array. To request the native byte order of the host system, use\n"
217" `sys.byteorder\' as the byte order value.\n"
218" signed\n"
219" Indicates whether two\'s complement is used to represent the integer.");
220
221#define INT_FROM_BYTES_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200222 {"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 +0200223
224static PyObject *
225int_from_bytes_impl(PyTypeObject *type, PyObject *bytes_obj,
226 PyObject *byteorder, int is_signed);
227
228static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200229int_from_bytes(PyTypeObject *type, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka495e8802017-02-01 23:12:20 +0200230{
231 PyObject *return_value = NULL;
232 static const char * const _keywords[] = {"bytes", "byteorder", "signed", NULL};
233 static _PyArg_Parser _parser = {"OU|$p:from_bytes", _keywords, 0};
234 PyObject *bytes_obj;
235 PyObject *byteorder;
236 int is_signed = 0;
237
238 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
239 &bytes_obj, &byteorder, &is_signed)) {
240 goto exit;
241 }
242 return_value = int_from_bytes_impl(type, bytes_obj, byteorder, is_signed);
243
244exit:
245 return return_value;
246}
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200247/*[clinic end generated code: output=7436b5f4decdcf9d input=a9049054013a1b77]*/