Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 1 | /*[clinic input] |
| 2 | preserve |
| 3 | [clinic start generated code]*/ |
| 4 | |
Serhiy Storchaka | 18b250f | 2017-03-19 08:51:07 +0200 | [diff] [blame] | 5 | static PyObject * |
| 6 | long_new_impl(PyTypeObject *type, PyObject *x, PyObject *obase); |
| 7 | |
| 8 | static PyObject * |
| 9 | long_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 10 | { |
| 11 | PyObject *return_value = NULL; |
| 12 | static const char * const _keywords[] = {"", "base", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 13 | 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 Storchaka | 18b250f | 2017-03-19 08:51:07 +0200 | [diff] [blame] | 18 | PyObject *x = NULL; |
| 19 | PyObject *obase = NULL; |
| 20 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 21 | fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 0, 2, 0, argsbuf); |
| 22 | if (!fastargs) { |
Serhiy Storchaka | 18b250f | 2017-03-19 08:51:07 +0200 | [diff] [blame] | 23 | goto exit; |
| 24 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 25 | if (nargs < 1) { |
| 26 | goto skip_optional_posonly; |
| 27 | } |
| 28 | noptargs--; |
| 29 | x = fastargs[0]; |
| 30 | skip_optional_posonly: |
| 31 | if (!noptargs) { |
| 32 | goto skip_optional_pos; |
| 33 | } |
| 34 | obase = fastargs[1]; |
| 35 | skip_optional_pos: |
Serhiy Storchaka | 18b250f | 2017-03-19 08:51:07 +0200 | [diff] [blame] | 36 | return_value = long_new_impl(type, x, obase); |
| 37 | |
| 38 | exit: |
| 39 | return return_value; |
| 40 | } |
| 41 | |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 42 | PyDoc_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 | |
| 50 | static PyObject * |
| 51 | int___getnewargs___impl(PyObject *self); |
| 52 | |
| 53 | static PyObject * |
| 54 | int___getnewargs__(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 55 | { |
| 56 | return int___getnewargs___impl(self); |
| 57 | } |
| 58 | |
| 59 | PyDoc_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 | |
| 67 | static PyObject * |
| 68 | int___format___impl(PyObject *self, PyObject *format_spec); |
| 69 | |
| 70 | static PyObject * |
| 71 | int___format__(PyObject *self, PyObject *arg) |
| 72 | { |
| 73 | PyObject *return_value = NULL; |
| 74 | PyObject *format_spec; |
| 75 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 76 | if (!PyUnicode_Check(arg)) { |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame] | 77 | _PyArg_BadArgument("__format__", "argument", "str", arg); |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 78 | goto exit; |
| 79 | } |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 80 | if (PyUnicode_READY(arg) == -1) { |
| 81 | goto exit; |
| 82 | } |
| 83 | format_spec = arg; |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 84 | return_value = int___format___impl(self, format_spec); |
| 85 | |
| 86 | exit: |
| 87 | return return_value; |
| 88 | } |
| 89 | |
Serhiy Storchaka | 5a2bac7 | 2020-07-20 15:57:37 +0300 | [diff] [blame] | 90 | PyDoc_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 | |
| 101 | static PyObject * |
| 102 | int___round___impl(PyObject *self, PyObject *o_ndigits); |
| 103 | |
| 104 | static PyObject * |
| 105 | int___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]; |
| 117 | skip_optional: |
| 118 | return_value = int___round___impl(self, o_ndigits); |
| 119 | |
| 120 | exit: |
| 121 | return return_value; |
| 122 | } |
| 123 | |
Robert Smallshire | 58a7da9 | 2020-10-01 18:30:08 +0200 | [diff] [blame] | 124 | PyDoc_STRVAR(int_is_integer__doc__, |
| 125 | "is_integer($self, /)\n" |
| 126 | "--\n" |
| 127 | "\n" |
| 128 | "Returns True for all integers."); |
| 129 | |
| 130 | #define INT_IS_INTEGER_METHODDEF \ |
| 131 | {"is_integer", (PyCFunction)int_is_integer, METH_NOARGS, int_is_integer__doc__}, |
| 132 | |
| 133 | static PyObject * |
| 134 | int_is_integer_impl(PyObject *self); |
| 135 | |
| 136 | static PyObject * |
| 137 | int_is_integer(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 138 | { |
| 139 | return int_is_integer_impl(self); |
| 140 | } |
| 141 | |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 142 | PyDoc_STRVAR(int___sizeof____doc__, |
| 143 | "__sizeof__($self, /)\n" |
| 144 | "--\n" |
| 145 | "\n" |
| 146 | "Returns size in memory, in bytes."); |
| 147 | |
| 148 | #define INT___SIZEOF___METHODDEF \ |
| 149 | {"__sizeof__", (PyCFunction)int___sizeof__, METH_NOARGS, int___sizeof____doc__}, |
| 150 | |
| 151 | static Py_ssize_t |
| 152 | int___sizeof___impl(PyObject *self); |
| 153 | |
| 154 | static PyObject * |
| 155 | int___sizeof__(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 156 | { |
| 157 | PyObject *return_value = NULL; |
| 158 | Py_ssize_t _return_value; |
| 159 | |
| 160 | _return_value = int___sizeof___impl(self); |
| 161 | if ((_return_value == -1) && PyErr_Occurred()) { |
| 162 | goto exit; |
| 163 | } |
| 164 | return_value = PyLong_FromSsize_t(_return_value); |
| 165 | |
| 166 | exit: |
| 167 | return return_value; |
| 168 | } |
| 169 | |
| 170 | PyDoc_STRVAR(int_bit_length__doc__, |
| 171 | "bit_length($self, /)\n" |
| 172 | "--\n" |
| 173 | "\n" |
| 174 | "Number of bits necessary to represent self in binary.\n" |
| 175 | "\n" |
| 176 | ">>> bin(37)\n" |
| 177 | "\'0b100101\'\n" |
| 178 | ">>> (37).bit_length()\n" |
| 179 | "6"); |
| 180 | |
| 181 | #define INT_BIT_LENGTH_METHODDEF \ |
| 182 | {"bit_length", (PyCFunction)int_bit_length, METH_NOARGS, int_bit_length__doc__}, |
| 183 | |
| 184 | static PyObject * |
| 185 | int_bit_length_impl(PyObject *self); |
| 186 | |
| 187 | static PyObject * |
| 188 | int_bit_length(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 189 | { |
| 190 | return int_bit_length_impl(self); |
| 191 | } |
| 192 | |
Niklas Fiekas | 8bd216d | 2020-05-29 18:28:02 +0200 | [diff] [blame] | 193 | PyDoc_STRVAR(int_bit_count__doc__, |
| 194 | "bit_count($self, /)\n" |
| 195 | "--\n" |
| 196 | "\n" |
| 197 | "Number of ones in the binary representation of the absolute value of self.\n" |
| 198 | "\n" |
| 199 | "Also known as the population count.\n" |
| 200 | "\n" |
| 201 | ">>> bin(13)\n" |
| 202 | "\'0b1101\'\n" |
| 203 | ">>> (13).bit_count()\n" |
| 204 | "3"); |
| 205 | |
| 206 | #define INT_BIT_COUNT_METHODDEF \ |
| 207 | {"bit_count", (PyCFunction)int_bit_count, METH_NOARGS, int_bit_count__doc__}, |
| 208 | |
| 209 | static PyObject * |
| 210 | int_bit_count_impl(PyObject *self); |
| 211 | |
| 212 | static PyObject * |
| 213 | int_bit_count(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 214 | { |
| 215 | return int_bit_count_impl(self); |
| 216 | } |
| 217 | |
Lisa Roach | 5ac7043 | 2018-09-13 23:56:23 -0700 | [diff] [blame] | 218 | PyDoc_STRVAR(int_as_integer_ratio__doc__, |
| 219 | "as_integer_ratio($self, /)\n" |
| 220 | "--\n" |
| 221 | "\n" |
| 222 | "Return integer ratio.\n" |
| 223 | "\n" |
| 224 | "Return a pair of integers, whose ratio is exactly equal to the original int\n" |
| 225 | "and with a positive denominator.\n" |
| 226 | "\n" |
| 227 | ">>> (10).as_integer_ratio()\n" |
| 228 | "(10, 1)\n" |
| 229 | ">>> (-10).as_integer_ratio()\n" |
| 230 | "(-10, 1)\n" |
| 231 | ">>> (0).as_integer_ratio()\n" |
| 232 | "(0, 1)"); |
| 233 | |
| 234 | #define INT_AS_INTEGER_RATIO_METHODDEF \ |
| 235 | {"as_integer_ratio", (PyCFunction)int_as_integer_ratio, METH_NOARGS, int_as_integer_ratio__doc__}, |
| 236 | |
| 237 | static PyObject * |
| 238 | int_as_integer_ratio_impl(PyObject *self); |
| 239 | |
| 240 | static PyObject * |
| 241 | int_as_integer_ratio(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 242 | { |
| 243 | return int_as_integer_ratio_impl(self); |
| 244 | } |
| 245 | |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 246 | PyDoc_STRVAR(int_to_bytes__doc__, |
| 247 | "to_bytes($self, /, length, byteorder, *, signed=False)\n" |
| 248 | "--\n" |
| 249 | "\n" |
| 250 | "Return an array of bytes representing an integer.\n" |
| 251 | "\n" |
| 252 | " length\n" |
| 253 | " Length of bytes object to use. An OverflowError is raised if the\n" |
| 254 | " integer is not representable with the given number of bytes.\n" |
| 255 | " byteorder\n" |
| 256 | " The byte order used to represent the integer. If byteorder is \'big\',\n" |
| 257 | " the most significant byte is at the beginning of the byte array. If\n" |
| 258 | " byteorder is \'little\', the most significant byte is at the end of the\n" |
| 259 | " byte array. To request the native byte order of the host system, use\n" |
| 260 | " `sys.byteorder\' as the byte order value.\n" |
| 261 | " signed\n" |
| 262 | " Determines whether two\'s complement is used to represent the integer.\n" |
| 263 | " If signed is False and a negative integer is given, an OverflowError\n" |
| 264 | " is raised."); |
| 265 | |
| 266 | #define INT_TO_BYTES_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 267 | {"to_bytes", (PyCFunction)(void(*)(void))int_to_bytes, METH_FASTCALL|METH_KEYWORDS, int_to_bytes__doc__}, |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 268 | |
| 269 | static PyObject * |
| 270 | int_to_bytes_impl(PyObject *self, Py_ssize_t length, PyObject *byteorder, |
| 271 | int is_signed); |
| 272 | |
| 273 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 274 | int_to_bytes(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 275 | { |
| 276 | PyObject *return_value = NULL; |
| 277 | static const char * const _keywords[] = {"length", "byteorder", "signed", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 278 | static _PyArg_Parser _parser = {NULL, _keywords, "to_bytes", 0}; |
| 279 | PyObject *argsbuf[3]; |
| 280 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2; |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 281 | Py_ssize_t length; |
| 282 | PyObject *byteorder; |
| 283 | int is_signed = 0; |
| 284 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 285 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf); |
| 286 | if (!args) { |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 287 | goto exit; |
| 288 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 289 | { |
| 290 | Py_ssize_t ival = -1; |
Serhiy Storchaka | 5f4b229d | 2020-05-28 10:33:45 +0300 | [diff] [blame] | 291 | PyObject *iobj = _PyNumber_Index(args[0]); |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 292 | if (iobj != NULL) { |
| 293 | ival = PyLong_AsSsize_t(iobj); |
| 294 | Py_DECREF(iobj); |
| 295 | } |
| 296 | if (ival == -1 && PyErr_Occurred()) { |
| 297 | goto exit; |
| 298 | } |
| 299 | length = ival; |
| 300 | } |
| 301 | if (!PyUnicode_Check(args[1])) { |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame] | 302 | _PyArg_BadArgument("to_bytes", "argument 'byteorder'", "str", args[1]); |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 303 | goto exit; |
| 304 | } |
| 305 | if (PyUnicode_READY(args[1]) == -1) { |
| 306 | goto exit; |
| 307 | } |
| 308 | byteorder = args[1]; |
| 309 | if (!noptargs) { |
| 310 | goto skip_optional_kwonly; |
| 311 | } |
| 312 | is_signed = PyObject_IsTrue(args[2]); |
| 313 | if (is_signed < 0) { |
| 314 | goto exit; |
| 315 | } |
| 316 | skip_optional_kwonly: |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 317 | return_value = int_to_bytes_impl(self, length, byteorder, is_signed); |
| 318 | |
| 319 | exit: |
| 320 | return return_value; |
| 321 | } |
| 322 | |
| 323 | PyDoc_STRVAR(int_from_bytes__doc__, |
| 324 | "from_bytes($type, /, bytes, byteorder, *, signed=False)\n" |
| 325 | "--\n" |
| 326 | "\n" |
| 327 | "Return the integer represented by the given array of bytes.\n" |
| 328 | "\n" |
| 329 | " bytes\n" |
| 330 | " Holds the array of bytes to convert. The argument must either\n" |
| 331 | " support the buffer protocol or be an iterable object producing bytes.\n" |
| 332 | " Bytes and bytearray are examples of built-in objects that support the\n" |
| 333 | " buffer protocol.\n" |
| 334 | " byteorder\n" |
| 335 | " The byte order used to represent the integer. If byteorder is \'big\',\n" |
| 336 | " the most significant byte is at the beginning of the byte array. If\n" |
| 337 | " byteorder is \'little\', the most significant byte is at the end of the\n" |
| 338 | " byte array. To request the native byte order of the host system, use\n" |
| 339 | " `sys.byteorder\' as the byte order value.\n" |
| 340 | " signed\n" |
| 341 | " Indicates whether two\'s complement is used to represent the integer."); |
| 342 | |
| 343 | #define INT_FROM_BYTES_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 344 | {"from_bytes", (PyCFunction)(void(*)(void))int_from_bytes, METH_FASTCALL|METH_KEYWORDS|METH_CLASS, int_from_bytes__doc__}, |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 345 | |
| 346 | static PyObject * |
| 347 | int_from_bytes_impl(PyTypeObject *type, PyObject *bytes_obj, |
| 348 | PyObject *byteorder, int is_signed); |
| 349 | |
| 350 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 351 | int_from_bytes(PyTypeObject *type, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 352 | { |
| 353 | PyObject *return_value = NULL; |
| 354 | static const char * const _keywords[] = {"bytes", "byteorder", "signed", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 355 | static _PyArg_Parser _parser = {NULL, _keywords, "from_bytes", 0}; |
| 356 | PyObject *argsbuf[3]; |
| 357 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2; |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 358 | PyObject *bytes_obj; |
| 359 | PyObject *byteorder; |
| 360 | int is_signed = 0; |
| 361 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 362 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf); |
| 363 | if (!args) { |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 364 | goto exit; |
| 365 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 366 | bytes_obj = args[0]; |
| 367 | if (!PyUnicode_Check(args[1])) { |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame] | 368 | _PyArg_BadArgument("from_bytes", "argument 'byteorder'", "str", args[1]); |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 369 | goto exit; |
| 370 | } |
| 371 | if (PyUnicode_READY(args[1]) == -1) { |
| 372 | goto exit; |
| 373 | } |
| 374 | byteorder = args[1]; |
| 375 | if (!noptargs) { |
| 376 | goto skip_optional_kwonly; |
| 377 | } |
| 378 | is_signed = PyObject_IsTrue(args[2]); |
| 379 | if (is_signed < 0) { |
| 380 | goto exit; |
| 381 | } |
| 382 | skip_optional_kwonly: |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 383 | return_value = int_from_bytes_impl(type, bytes_obj, byteorder, is_signed); |
| 384 | |
| 385 | exit: |
| 386 | return return_value; |
| 387 | } |
Robert Smallshire | 58a7da9 | 2020-10-01 18:30:08 +0200 | [diff] [blame] | 388 | /*[clinic end generated code: output=022614978e2fcdf3 input=a9049054013a1b77]*/ |