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 | |
| 90 | PyDoc_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 | |
| 99 | static Py_ssize_t |
| 100 | int___sizeof___impl(PyObject *self); |
| 101 | |
| 102 | static PyObject * |
| 103 | int___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 | |
| 114 | exit: |
| 115 | return return_value; |
| 116 | } |
| 117 | |
| 118 | PyDoc_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 | |
| 132 | static PyObject * |
| 133 | int_bit_length_impl(PyObject *self); |
| 134 | |
| 135 | static PyObject * |
| 136 | int_bit_length(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 137 | { |
| 138 | return int_bit_length_impl(self); |
| 139 | } |
| 140 | |
Niklas Fiekas | 8bd216d | 2020-05-29 18:28:02 +0200 | [diff] [blame^] | 141 | PyDoc_STRVAR(int_bit_count__doc__, |
| 142 | "bit_count($self, /)\n" |
| 143 | "--\n" |
| 144 | "\n" |
| 145 | "Number of ones in the binary representation of the absolute value of self.\n" |
| 146 | "\n" |
| 147 | "Also known as the population count.\n" |
| 148 | "\n" |
| 149 | ">>> bin(13)\n" |
| 150 | "\'0b1101\'\n" |
| 151 | ">>> (13).bit_count()\n" |
| 152 | "3"); |
| 153 | |
| 154 | #define INT_BIT_COUNT_METHODDEF \ |
| 155 | {"bit_count", (PyCFunction)int_bit_count, METH_NOARGS, int_bit_count__doc__}, |
| 156 | |
| 157 | static PyObject * |
| 158 | int_bit_count_impl(PyObject *self); |
| 159 | |
| 160 | static PyObject * |
| 161 | int_bit_count(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 162 | { |
| 163 | return int_bit_count_impl(self); |
| 164 | } |
| 165 | |
Lisa Roach | 5ac7043 | 2018-09-13 23:56:23 -0700 | [diff] [blame] | 166 | PyDoc_STRVAR(int_as_integer_ratio__doc__, |
| 167 | "as_integer_ratio($self, /)\n" |
| 168 | "--\n" |
| 169 | "\n" |
| 170 | "Return integer ratio.\n" |
| 171 | "\n" |
| 172 | "Return a pair of integers, whose ratio is exactly equal to the original int\n" |
| 173 | "and with a positive denominator.\n" |
| 174 | "\n" |
| 175 | ">>> (10).as_integer_ratio()\n" |
| 176 | "(10, 1)\n" |
| 177 | ">>> (-10).as_integer_ratio()\n" |
| 178 | "(-10, 1)\n" |
| 179 | ">>> (0).as_integer_ratio()\n" |
| 180 | "(0, 1)"); |
| 181 | |
| 182 | #define INT_AS_INTEGER_RATIO_METHODDEF \ |
| 183 | {"as_integer_ratio", (PyCFunction)int_as_integer_ratio, METH_NOARGS, int_as_integer_ratio__doc__}, |
| 184 | |
| 185 | static PyObject * |
| 186 | int_as_integer_ratio_impl(PyObject *self); |
| 187 | |
| 188 | static PyObject * |
| 189 | int_as_integer_ratio(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 190 | { |
| 191 | return int_as_integer_ratio_impl(self); |
| 192 | } |
| 193 | |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 194 | PyDoc_STRVAR(int_to_bytes__doc__, |
| 195 | "to_bytes($self, /, length, byteorder, *, signed=False)\n" |
| 196 | "--\n" |
| 197 | "\n" |
| 198 | "Return an array of bytes representing an integer.\n" |
| 199 | "\n" |
| 200 | " length\n" |
| 201 | " Length of bytes object to use. An OverflowError is raised if the\n" |
| 202 | " integer is not representable with the given number of bytes.\n" |
| 203 | " byteorder\n" |
| 204 | " The byte order used to represent the integer. If byteorder is \'big\',\n" |
| 205 | " the most significant byte is at the beginning of the byte array. If\n" |
| 206 | " byteorder is \'little\', the most significant byte is at the end of the\n" |
| 207 | " byte array. To request the native byte order of the host system, use\n" |
| 208 | " `sys.byteorder\' as the byte order value.\n" |
| 209 | " signed\n" |
| 210 | " Determines whether two\'s complement is used to represent the integer.\n" |
| 211 | " If signed is False and a negative integer is given, an OverflowError\n" |
| 212 | " is raised."); |
| 213 | |
| 214 | #define INT_TO_BYTES_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 215 | {"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] | 216 | |
| 217 | static PyObject * |
| 218 | int_to_bytes_impl(PyObject *self, Py_ssize_t length, PyObject *byteorder, |
| 219 | int is_signed); |
| 220 | |
| 221 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 222 | 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] | 223 | { |
| 224 | PyObject *return_value = NULL; |
| 225 | static const char * const _keywords[] = {"length", "byteorder", "signed", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 226 | static _PyArg_Parser _parser = {NULL, _keywords, "to_bytes", 0}; |
| 227 | PyObject *argsbuf[3]; |
| 228 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2; |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 229 | Py_ssize_t length; |
| 230 | PyObject *byteorder; |
| 231 | int is_signed = 0; |
| 232 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 233 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf); |
| 234 | if (!args) { |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 235 | goto exit; |
| 236 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 237 | { |
| 238 | Py_ssize_t ival = -1; |
Serhiy Storchaka | 5f4b229d | 2020-05-28 10:33:45 +0300 | [diff] [blame] | 239 | PyObject *iobj = _PyNumber_Index(args[0]); |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 240 | if (iobj != NULL) { |
| 241 | ival = PyLong_AsSsize_t(iobj); |
| 242 | Py_DECREF(iobj); |
| 243 | } |
| 244 | if (ival == -1 && PyErr_Occurred()) { |
| 245 | goto exit; |
| 246 | } |
| 247 | length = ival; |
| 248 | } |
| 249 | if (!PyUnicode_Check(args[1])) { |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame] | 250 | _PyArg_BadArgument("to_bytes", "argument 'byteorder'", "str", args[1]); |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 251 | goto exit; |
| 252 | } |
| 253 | if (PyUnicode_READY(args[1]) == -1) { |
| 254 | goto exit; |
| 255 | } |
| 256 | byteorder = args[1]; |
| 257 | if (!noptargs) { |
| 258 | goto skip_optional_kwonly; |
| 259 | } |
| 260 | is_signed = PyObject_IsTrue(args[2]); |
| 261 | if (is_signed < 0) { |
| 262 | goto exit; |
| 263 | } |
| 264 | skip_optional_kwonly: |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 265 | return_value = int_to_bytes_impl(self, length, byteorder, is_signed); |
| 266 | |
| 267 | exit: |
| 268 | return return_value; |
| 269 | } |
| 270 | |
| 271 | PyDoc_STRVAR(int_from_bytes__doc__, |
| 272 | "from_bytes($type, /, bytes, byteorder, *, signed=False)\n" |
| 273 | "--\n" |
| 274 | "\n" |
| 275 | "Return the integer represented by the given array of bytes.\n" |
| 276 | "\n" |
| 277 | " bytes\n" |
| 278 | " Holds the array of bytes to convert. The argument must either\n" |
| 279 | " support the buffer protocol or be an iterable object producing bytes.\n" |
| 280 | " Bytes and bytearray are examples of built-in objects that support the\n" |
| 281 | " buffer protocol.\n" |
| 282 | " byteorder\n" |
| 283 | " The byte order used to represent the integer. If byteorder is \'big\',\n" |
| 284 | " the most significant byte is at the beginning of the byte array. If\n" |
| 285 | " byteorder is \'little\', the most significant byte is at the end of the\n" |
| 286 | " byte array. To request the native byte order of the host system, use\n" |
| 287 | " `sys.byteorder\' as the byte order value.\n" |
| 288 | " signed\n" |
| 289 | " Indicates whether two\'s complement is used to represent the integer."); |
| 290 | |
| 291 | #define INT_FROM_BYTES_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 292 | {"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] | 293 | |
| 294 | static PyObject * |
| 295 | int_from_bytes_impl(PyTypeObject *type, PyObject *bytes_obj, |
| 296 | PyObject *byteorder, int is_signed); |
| 297 | |
| 298 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 299 | 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] | 300 | { |
| 301 | PyObject *return_value = NULL; |
| 302 | static const char * const _keywords[] = {"bytes", "byteorder", "signed", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 303 | static _PyArg_Parser _parser = {NULL, _keywords, "from_bytes", 0}; |
| 304 | PyObject *argsbuf[3]; |
| 305 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2; |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 306 | PyObject *bytes_obj; |
| 307 | PyObject *byteorder; |
| 308 | int is_signed = 0; |
| 309 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 310 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf); |
| 311 | if (!args) { |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 312 | goto exit; |
| 313 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 314 | bytes_obj = args[0]; |
| 315 | if (!PyUnicode_Check(args[1])) { |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame] | 316 | _PyArg_BadArgument("from_bytes", "argument 'byteorder'", "str", args[1]); |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 317 | goto exit; |
| 318 | } |
| 319 | if (PyUnicode_READY(args[1]) == -1) { |
| 320 | goto exit; |
| 321 | } |
| 322 | byteorder = args[1]; |
| 323 | if (!noptargs) { |
| 324 | goto skip_optional_kwonly; |
| 325 | } |
| 326 | is_signed = PyObject_IsTrue(args[2]); |
| 327 | if (is_signed < 0) { |
| 328 | goto exit; |
| 329 | } |
| 330 | skip_optional_kwonly: |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 331 | return_value = int_from_bytes_impl(type, bytes_obj, byteorder, is_signed); |
| 332 | |
| 333 | exit: |
| 334 | return return_value; |
| 335 | } |
Niklas Fiekas | 8bd216d | 2020-05-29 18:28:02 +0200 | [diff] [blame^] | 336 | /*[clinic end generated code: output=4257cfdb155efd00 input=a9049054013a1b77]*/ |