longobject.c.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /*[clinic input]
  2. preserve
  3. [clinic start generated code]*/
  4. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  5. # include "pycore_gc.h" // PyGC_Head
  6. # include "pycore_runtime.h" // _Py_ID()
  7. #endif
  8. static PyObject *
  9. long_new_impl(PyTypeObject *type, PyObject *x, PyObject *obase);
  10. static PyObject *
  11. long_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
  12. {
  13. PyObject *return_value = NULL;
  14. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  15. #define NUM_KEYWORDS 1
  16. static struct {
  17. PyGC_Head _this_is_not_used;
  18. PyObject_VAR_HEAD
  19. PyObject *ob_item[NUM_KEYWORDS];
  20. } _kwtuple = {
  21. .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
  22. .ob_item = { &_Py_ID(base), },
  23. };
  24. #undef NUM_KEYWORDS
  25. #define KWTUPLE (&_kwtuple.ob_base.ob_base)
  26. #else // !Py_BUILD_CORE
  27. # define KWTUPLE NULL
  28. #endif // !Py_BUILD_CORE
  29. static const char * const _keywords[] = {"", "base", NULL};
  30. static _PyArg_Parser _parser = {
  31. .keywords = _keywords,
  32. .fname = "int",
  33. .kwtuple = KWTUPLE,
  34. };
  35. #undef KWTUPLE
  36. PyObject *argsbuf[2];
  37. PyObject * const *fastargs;
  38. Py_ssize_t nargs = PyTuple_GET_SIZE(args);
  39. Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0;
  40. PyObject *x = NULL;
  41. PyObject *obase = NULL;
  42. fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 0, 2, 0, argsbuf);
  43. if (!fastargs) {
  44. goto exit;
  45. }
  46. if (nargs < 1) {
  47. goto skip_optional_posonly;
  48. }
  49. noptargs--;
  50. x = fastargs[0];
  51. skip_optional_posonly:
  52. if (!noptargs) {
  53. goto skip_optional_pos;
  54. }
  55. obase = fastargs[1];
  56. skip_optional_pos:
  57. return_value = long_new_impl(type, x, obase);
  58. exit:
  59. return return_value;
  60. }
  61. PyDoc_STRVAR(int___getnewargs____doc__,
  62. "__getnewargs__($self, /)\n"
  63. "--\n"
  64. "\n");
  65. #define INT___GETNEWARGS___METHODDEF \
  66. {"__getnewargs__", (PyCFunction)int___getnewargs__, METH_NOARGS, int___getnewargs____doc__},
  67. static PyObject *
  68. int___getnewargs___impl(PyObject *self);
  69. static PyObject *
  70. int___getnewargs__(PyObject *self, PyObject *Py_UNUSED(ignored))
  71. {
  72. return int___getnewargs___impl(self);
  73. }
  74. PyDoc_STRVAR(int___format____doc__,
  75. "__format__($self, format_spec, /)\n"
  76. "--\n"
  77. "\n"
  78. "Convert to a string according to format_spec.");
  79. #define INT___FORMAT___METHODDEF \
  80. {"__format__", (PyCFunction)int___format__, METH_O, int___format____doc__},
  81. static PyObject *
  82. int___format___impl(PyObject *self, PyObject *format_spec);
  83. static PyObject *
  84. int___format__(PyObject *self, PyObject *arg)
  85. {
  86. PyObject *return_value = NULL;
  87. PyObject *format_spec;
  88. if (!PyUnicode_Check(arg)) {
  89. _PyArg_BadArgument("__format__", "argument", "str", arg);
  90. goto exit;
  91. }
  92. if (PyUnicode_READY(arg) == -1) {
  93. goto exit;
  94. }
  95. format_spec = arg;
  96. return_value = int___format___impl(self, format_spec);
  97. exit:
  98. return return_value;
  99. }
  100. PyDoc_STRVAR(int___round____doc__,
  101. "__round__($self, ndigits=<unrepresentable>, /)\n"
  102. "--\n"
  103. "\n"
  104. "Rounding an Integral returns itself.\n"
  105. "\n"
  106. "Rounding with an ndigits argument also returns an integer.");
  107. #define INT___ROUND___METHODDEF \
  108. {"__round__", _PyCFunction_CAST(int___round__), METH_FASTCALL, int___round____doc__},
  109. static PyObject *
  110. int___round___impl(PyObject *self, PyObject *o_ndigits);
  111. static PyObject *
  112. int___round__(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
  113. {
  114. PyObject *return_value = NULL;
  115. PyObject *o_ndigits = NULL;
  116. if (!_PyArg_CheckPositional("__round__", nargs, 0, 1)) {
  117. goto exit;
  118. }
  119. if (nargs < 1) {
  120. goto skip_optional;
  121. }
  122. o_ndigits = args[0];
  123. skip_optional:
  124. return_value = int___round___impl(self, o_ndigits);
  125. exit:
  126. return return_value;
  127. }
  128. PyDoc_STRVAR(int___sizeof____doc__,
  129. "__sizeof__($self, /)\n"
  130. "--\n"
  131. "\n"
  132. "Returns size in memory, in bytes.");
  133. #define INT___SIZEOF___METHODDEF \
  134. {"__sizeof__", (PyCFunction)int___sizeof__, METH_NOARGS, int___sizeof____doc__},
  135. static Py_ssize_t
  136. int___sizeof___impl(PyObject *self);
  137. static PyObject *
  138. int___sizeof__(PyObject *self, PyObject *Py_UNUSED(ignored))
  139. {
  140. PyObject *return_value = NULL;
  141. Py_ssize_t _return_value;
  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. exit:
  148. return return_value;
  149. }
  150. PyDoc_STRVAR(int_bit_length__doc__,
  151. "bit_length($self, /)\n"
  152. "--\n"
  153. "\n"
  154. "Number of bits necessary to represent self in binary.\n"
  155. "\n"
  156. ">>> bin(37)\n"
  157. "\'0b100101\'\n"
  158. ">>> (37).bit_length()\n"
  159. "6");
  160. #define INT_BIT_LENGTH_METHODDEF \
  161. {"bit_length", (PyCFunction)int_bit_length, METH_NOARGS, int_bit_length__doc__},
  162. static PyObject *
  163. int_bit_length_impl(PyObject *self);
  164. static PyObject *
  165. int_bit_length(PyObject *self, PyObject *Py_UNUSED(ignored))
  166. {
  167. return int_bit_length_impl(self);
  168. }
  169. PyDoc_STRVAR(int_bit_count__doc__,
  170. "bit_count($self, /)\n"
  171. "--\n"
  172. "\n"
  173. "Number of ones in the binary representation of the absolute value of self.\n"
  174. "\n"
  175. "Also known as the population count.\n"
  176. "\n"
  177. ">>> bin(13)\n"
  178. "\'0b1101\'\n"
  179. ">>> (13).bit_count()\n"
  180. "3");
  181. #define INT_BIT_COUNT_METHODDEF \
  182. {"bit_count", (PyCFunction)int_bit_count, METH_NOARGS, int_bit_count__doc__},
  183. static PyObject *
  184. int_bit_count_impl(PyObject *self);
  185. static PyObject *
  186. int_bit_count(PyObject *self, PyObject *Py_UNUSED(ignored))
  187. {
  188. return int_bit_count_impl(self);
  189. }
  190. PyDoc_STRVAR(int_as_integer_ratio__doc__,
  191. "as_integer_ratio($self, /)\n"
  192. "--\n"
  193. "\n"
  194. "Return a pair of integers, whose ratio is equal to the original int.\n"
  195. "\n"
  196. "The ratio is in lowest terms and has a positive denominator.\n"
  197. "\n"
  198. ">>> (10).as_integer_ratio()\n"
  199. "(10, 1)\n"
  200. ">>> (-10).as_integer_ratio()\n"
  201. "(-10, 1)\n"
  202. ">>> (0).as_integer_ratio()\n"
  203. "(0, 1)");
  204. #define INT_AS_INTEGER_RATIO_METHODDEF \
  205. {"as_integer_ratio", (PyCFunction)int_as_integer_ratio, METH_NOARGS, int_as_integer_ratio__doc__},
  206. static PyObject *
  207. int_as_integer_ratio_impl(PyObject *self);
  208. static PyObject *
  209. int_as_integer_ratio(PyObject *self, PyObject *Py_UNUSED(ignored))
  210. {
  211. return int_as_integer_ratio_impl(self);
  212. }
  213. PyDoc_STRVAR(int_to_bytes__doc__,
  214. "to_bytes($self, /, length=1, byteorder=\'big\', *, signed=False)\n"
  215. "--\n"
  216. "\n"
  217. "Return an array of bytes representing an integer.\n"
  218. "\n"
  219. " length\n"
  220. " Length of bytes object to use. An OverflowError is raised if the\n"
  221. " integer is not representable with the given number of bytes. Default\n"
  222. " is length 1.\n"
  223. " byteorder\n"
  224. " The byte order used to represent the integer. If byteorder is \'big\',\n"
  225. " the most significant byte is at the beginning of the byte array. If\n"
  226. " byteorder is \'little\', the most significant byte is at the end of the\n"
  227. " byte array. To request the native byte order of the host system, use\n"
  228. " `sys.byteorder\' as the byte order value. Default is to use \'big\'.\n"
  229. " signed\n"
  230. " Determines whether two\'s complement is used to represent the integer.\n"
  231. " If signed is False and a negative integer is given, an OverflowError\n"
  232. " is raised.");
  233. #define INT_TO_BYTES_METHODDEF \
  234. {"to_bytes", _PyCFunction_CAST(int_to_bytes), METH_FASTCALL|METH_KEYWORDS, int_to_bytes__doc__},
  235. static PyObject *
  236. int_to_bytes_impl(PyObject *self, Py_ssize_t length, PyObject *byteorder,
  237. int is_signed);
  238. static PyObject *
  239. int_to_bytes(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  240. {
  241. PyObject *return_value = NULL;
  242. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  243. #define NUM_KEYWORDS 3
  244. static struct {
  245. PyGC_Head _this_is_not_used;
  246. PyObject_VAR_HEAD
  247. PyObject *ob_item[NUM_KEYWORDS];
  248. } _kwtuple = {
  249. .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
  250. .ob_item = { &_Py_ID(length), &_Py_ID(byteorder), &_Py_ID(signed), },
  251. };
  252. #undef NUM_KEYWORDS
  253. #define KWTUPLE (&_kwtuple.ob_base.ob_base)
  254. #else // !Py_BUILD_CORE
  255. # define KWTUPLE NULL
  256. #endif // !Py_BUILD_CORE
  257. static const char * const _keywords[] = {"length", "byteorder", "signed", NULL};
  258. static _PyArg_Parser _parser = {
  259. .keywords = _keywords,
  260. .fname = "to_bytes",
  261. .kwtuple = KWTUPLE,
  262. };
  263. #undef KWTUPLE
  264. PyObject *argsbuf[3];
  265. Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
  266. Py_ssize_t length = 1;
  267. PyObject *byteorder = NULL;
  268. int is_signed = 0;
  269. args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
  270. if (!args) {
  271. goto exit;
  272. }
  273. if (!noptargs) {
  274. goto skip_optional_pos;
  275. }
  276. if (args[0]) {
  277. {
  278. Py_ssize_t ival = -1;
  279. PyObject *iobj = _PyNumber_Index(args[0]);
  280. if (iobj != NULL) {
  281. ival = PyLong_AsSsize_t(iobj);
  282. Py_DECREF(iobj);
  283. }
  284. if (ival == -1 && PyErr_Occurred()) {
  285. goto exit;
  286. }
  287. length = ival;
  288. }
  289. if (!--noptargs) {
  290. goto skip_optional_pos;
  291. }
  292. }
  293. if (args[1]) {
  294. if (!PyUnicode_Check(args[1])) {
  295. _PyArg_BadArgument("to_bytes", "argument 'byteorder'", "str", args[1]);
  296. goto exit;
  297. }
  298. if (PyUnicode_READY(args[1]) == -1) {
  299. goto exit;
  300. }
  301. byteorder = args[1];
  302. if (!--noptargs) {
  303. goto skip_optional_pos;
  304. }
  305. }
  306. skip_optional_pos:
  307. if (!noptargs) {
  308. goto skip_optional_kwonly;
  309. }
  310. is_signed = PyObject_IsTrue(args[2]);
  311. if (is_signed < 0) {
  312. goto exit;
  313. }
  314. skip_optional_kwonly:
  315. return_value = int_to_bytes_impl(self, length, byteorder, is_signed);
  316. exit:
  317. return return_value;
  318. }
  319. PyDoc_STRVAR(int_from_bytes__doc__,
  320. "from_bytes($type, /, bytes, byteorder=\'big\', *, signed=False)\n"
  321. "--\n"
  322. "\n"
  323. "Return the integer represented by the given array of bytes.\n"
  324. "\n"
  325. " bytes\n"
  326. " Holds the array of bytes to convert. The argument must either\n"
  327. " support the buffer protocol or be an iterable object producing bytes.\n"
  328. " Bytes and bytearray are examples of built-in objects that support the\n"
  329. " buffer protocol.\n"
  330. " byteorder\n"
  331. " The byte order used to represent the integer. If byteorder is \'big\',\n"
  332. " the most significant byte is at the beginning of the byte array. If\n"
  333. " byteorder is \'little\', the most significant byte is at the end of the\n"
  334. " byte array. To request the native byte order of the host system, use\n"
  335. " `sys.byteorder\' as the byte order value. Default is to use \'big\'.\n"
  336. " signed\n"
  337. " Indicates whether two\'s complement is used to represent the integer.");
  338. #define INT_FROM_BYTES_METHODDEF \
  339. {"from_bytes", _PyCFunction_CAST(int_from_bytes), METH_FASTCALL|METH_KEYWORDS|METH_CLASS, int_from_bytes__doc__},
  340. static PyObject *
  341. int_from_bytes_impl(PyTypeObject *type, PyObject *bytes_obj,
  342. PyObject *byteorder, int is_signed);
  343. static PyObject *
  344. int_from_bytes(PyTypeObject *type, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  345. {
  346. PyObject *return_value = NULL;
  347. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  348. #define NUM_KEYWORDS 3
  349. static struct {
  350. PyGC_Head _this_is_not_used;
  351. PyObject_VAR_HEAD
  352. PyObject *ob_item[NUM_KEYWORDS];
  353. } _kwtuple = {
  354. .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
  355. .ob_item = { &_Py_ID(bytes), &_Py_ID(byteorder), &_Py_ID(signed), },
  356. };
  357. #undef NUM_KEYWORDS
  358. #define KWTUPLE (&_kwtuple.ob_base.ob_base)
  359. #else // !Py_BUILD_CORE
  360. # define KWTUPLE NULL
  361. #endif // !Py_BUILD_CORE
  362. static const char * const _keywords[] = {"bytes", "byteorder", "signed", NULL};
  363. static _PyArg_Parser _parser = {
  364. .keywords = _keywords,
  365. .fname = "from_bytes",
  366. .kwtuple = KWTUPLE,
  367. };
  368. #undef KWTUPLE
  369. PyObject *argsbuf[3];
  370. Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
  371. PyObject *bytes_obj;
  372. PyObject *byteorder = NULL;
  373. int is_signed = 0;
  374. args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
  375. if (!args) {
  376. goto exit;
  377. }
  378. bytes_obj = args[0];
  379. if (!noptargs) {
  380. goto skip_optional_pos;
  381. }
  382. if (args[1]) {
  383. if (!PyUnicode_Check(args[1])) {
  384. _PyArg_BadArgument("from_bytes", "argument 'byteorder'", "str", args[1]);
  385. goto exit;
  386. }
  387. if (PyUnicode_READY(args[1]) == -1) {
  388. goto exit;
  389. }
  390. byteorder = args[1];
  391. if (!--noptargs) {
  392. goto skip_optional_pos;
  393. }
  394. }
  395. skip_optional_pos:
  396. if (!noptargs) {
  397. goto skip_optional_kwonly;
  398. }
  399. is_signed = PyObject_IsTrue(args[2]);
  400. if (is_signed < 0) {
  401. goto exit;
  402. }
  403. skip_optional_kwonly:
  404. return_value = int_from_bytes_impl(type, bytes_obj, byteorder, is_signed);
  405. exit:
  406. return return_value;
  407. }
  408. PyDoc_STRVAR(int_is_integer__doc__,
  409. "is_integer($self, /)\n"
  410. "--\n"
  411. "\n"
  412. "Returns True. Exists for duck type compatibility with float.is_integer.");
  413. #define INT_IS_INTEGER_METHODDEF \
  414. {"is_integer", (PyCFunction)int_is_integer, METH_NOARGS, int_is_integer__doc__},
  415. static PyObject *
  416. int_is_integer_impl(PyObject *self);
  417. static PyObject *
  418. int_is_integer(PyObject *self, PyObject *Py_UNUSED(ignored))
  419. {
  420. return int_is_integer_impl(self);
  421. }
  422. /*[clinic end generated code: output=cfdf35d916158d4f input=a9049054013a1b77]*/