Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 1 | /*[clinic input] |
| 2 | preserve |
| 3 | [clinic start generated code]*/ |
| 4 | |
| 5 | PyDoc_STRVAR(builtin_abs__doc__, |
| 6 | "abs($module, x, /)\n" |
| 7 | "--\n" |
| 8 | "\n" |
| 9 | "Return the absolute value of the argument."); |
| 10 | |
| 11 | #define BUILTIN_ABS_METHODDEF \ |
| 12 | {"abs", (PyCFunction)builtin_abs, METH_O, builtin_abs__doc__}, |
| 13 | |
| 14 | PyDoc_STRVAR(builtin_all__doc__, |
| 15 | "all($module, iterable, /)\n" |
| 16 | "--\n" |
| 17 | "\n" |
| 18 | "Return True if bool(x) is True for all values x in the iterable.\n" |
| 19 | "\n" |
| 20 | "If the iterable is empty, return True."); |
| 21 | |
| 22 | #define BUILTIN_ALL_METHODDEF \ |
| 23 | {"all", (PyCFunction)builtin_all, METH_O, builtin_all__doc__}, |
| 24 | |
| 25 | PyDoc_STRVAR(builtin_any__doc__, |
| 26 | "any($module, iterable, /)\n" |
| 27 | "--\n" |
| 28 | "\n" |
| 29 | "Return True if bool(x) is True for any x in the iterable.\n" |
| 30 | "\n" |
| 31 | "If the iterable is empty, return False."); |
| 32 | |
| 33 | #define BUILTIN_ANY_METHODDEF \ |
| 34 | {"any", (PyCFunction)builtin_any, METH_O, builtin_any__doc__}, |
| 35 | |
| 36 | PyDoc_STRVAR(builtin_ascii__doc__, |
| 37 | "ascii($module, obj, /)\n" |
| 38 | "--\n" |
| 39 | "\n" |
| 40 | "Return an ASCII-only representation of an object.\n" |
| 41 | "\n" |
| 42 | "As repr(), return a string containing a printable representation of an\n" |
| 43 | "object, but escape the non-ASCII characters in the string returned by\n" |
| 44 | "repr() using \\\\x, \\\\u or \\\\U escapes. This generates a string similar\n" |
| 45 | "to that returned by repr() in Python 2."); |
| 46 | |
| 47 | #define BUILTIN_ASCII_METHODDEF \ |
| 48 | {"ascii", (PyCFunction)builtin_ascii, METH_O, builtin_ascii__doc__}, |
| 49 | |
| 50 | PyDoc_STRVAR(builtin_bin__doc__, |
| 51 | "bin($module, number, /)\n" |
| 52 | "--\n" |
| 53 | "\n" |
| 54 | "Return the binary representation of an integer.\n" |
| 55 | "\n" |
| 56 | " >>> bin(2796202)\n" |
| 57 | " \'0b1010101010101010101010\'"); |
| 58 | |
| 59 | #define BUILTIN_BIN_METHODDEF \ |
| 60 | {"bin", (PyCFunction)builtin_bin, METH_O, builtin_bin__doc__}, |
| 61 | |
| 62 | PyDoc_STRVAR(builtin_callable__doc__, |
| 63 | "callable($module, obj, /)\n" |
| 64 | "--\n" |
| 65 | "\n" |
| 66 | "Return whether the object is callable (i.e., some kind of function).\n" |
| 67 | "\n" |
| 68 | "Note that classes are callable, as are instances of classes with a\n" |
| 69 | "__call__() method."); |
| 70 | |
| 71 | #define BUILTIN_CALLABLE_METHODDEF \ |
| 72 | {"callable", (PyCFunction)builtin_callable, METH_O, builtin_callable__doc__}, |
| 73 | |
| 74 | PyDoc_STRVAR(builtin_format__doc__, |
| 75 | "format($module, value, format_spec=\'\', /)\n" |
| 76 | "--\n" |
| 77 | "\n" |
| 78 | "Return value.__format__(format_spec)\n" |
| 79 | "\n" |
Amit Kumar | 2e6bb44 | 2017-05-29 06:32:26 +0530 | [diff] [blame] | 80 | "format_spec defaults to the empty string.\n" |
| 81 | "See the Format Specification Mini-Language section of help(\'FORMATTING\') for\n" |
| 82 | "details."); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 83 | |
| 84 | #define BUILTIN_FORMAT_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 85 | {"format", (PyCFunction)(void(*)(void))builtin_format, METH_FASTCALL, builtin_format__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 86 | |
| 87 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 88 | builtin_format_impl(PyObject *module, PyObject *value, PyObject *format_spec); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 89 | |
| 90 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 91 | builtin_format(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 92 | { |
| 93 | PyObject *return_value = NULL; |
| 94 | PyObject *value; |
| 95 | PyObject *format_spec = NULL; |
| 96 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 97 | if (!_PyArg_CheckPositional("format", nargs, 1, 2)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 98 | goto exit; |
| 99 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 100 | value = args[0]; |
| 101 | if (nargs < 2) { |
| 102 | goto skip_optional; |
| 103 | } |
| 104 | if (!PyUnicode_Check(args[1])) { |
Serhiy Storchaka | 96631dc | 2019-08-29 18:29:59 +0300 | [diff] [blame] | 105 | _PyArg_BadArgument("format", "argument 2", "str", args[1]); |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 106 | goto exit; |
| 107 | } |
| 108 | if (PyUnicode_READY(args[1]) == -1) { |
| 109 | goto exit; |
| 110 | } |
| 111 | format_spec = args[1]; |
| 112 | skip_optional: |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 113 | return_value = builtin_format_impl(module, value, format_spec); |
| 114 | |
| 115 | exit: |
| 116 | return return_value; |
| 117 | } |
| 118 | |
| 119 | PyDoc_STRVAR(builtin_chr__doc__, |
| 120 | "chr($module, i, /)\n" |
| 121 | "--\n" |
| 122 | "\n" |
| 123 | "Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff."); |
| 124 | |
| 125 | #define BUILTIN_CHR_METHODDEF \ |
Serhiy Storchaka | 92e8af6 | 2015-04-04 00:12:11 +0300 | [diff] [blame] | 126 | {"chr", (PyCFunction)builtin_chr, METH_O, builtin_chr__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 127 | |
| 128 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 129 | builtin_chr_impl(PyObject *module, int i); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 130 | |
| 131 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 132 | builtin_chr(PyObject *module, PyObject *arg) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 133 | { |
| 134 | PyObject *return_value = NULL; |
| 135 | int i; |
| 136 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 137 | if (PyFloat_Check(arg)) { |
| 138 | PyErr_SetString(PyExc_TypeError, |
| 139 | "integer argument expected, got float" ); |
| 140 | goto exit; |
| 141 | } |
| 142 | i = _PyLong_AsInt(arg); |
| 143 | if (i == -1 && PyErr_Occurred()) { |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 144 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 145 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 146 | return_value = builtin_chr_impl(module, i); |
| 147 | |
| 148 | exit: |
| 149 | return return_value; |
| 150 | } |
| 151 | |
| 152 | PyDoc_STRVAR(builtin_compile__doc__, |
Serhiy Storchaka | 8b2e8b6 | 2015-05-30 11:30:39 +0300 | [diff] [blame] | 153 | "compile($module, /, source, filename, mode, flags=0,\n" |
Victor Stinner | b2fd32b | 2019-06-12 16:17:05 +0200 | [diff] [blame] | 154 | " dont_inherit=False, optimize=-1, *, _feature_version=-1)\n" |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 155 | "--\n" |
| 156 | "\n" |
| 157 | "Compile source into a code object that can be executed by exec() or eval().\n" |
| 158 | "\n" |
| 159 | "The source code may represent a Python module, statement or expression.\n" |
| 160 | "The filename will be used for run-time error messages.\n" |
| 161 | "The mode must be \'exec\' to compile a module, \'single\' to compile a\n" |
| 162 | "single (interactive) statement, or \'eval\' to compile an expression.\n" |
| 163 | "The flags argument, if present, controls which future statements influence\n" |
| 164 | "the compilation of the code.\n" |
Serhiy Storchaka | 8b2e8b6 | 2015-05-30 11:30:39 +0300 | [diff] [blame] | 165 | "The dont_inherit argument, if true, stops the compilation inheriting\n" |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 166 | "the effects of any future statements in effect in the code calling\n" |
Serhiy Storchaka | 8b2e8b6 | 2015-05-30 11:30:39 +0300 | [diff] [blame] | 167 | "compile; if absent or false these statements do influence the compilation,\n" |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 168 | "in addition to any features explicitly specified."); |
| 169 | |
| 170 | #define BUILTIN_COMPILE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 171 | {"compile", (PyCFunction)(void(*)(void))builtin_compile, METH_FASTCALL|METH_KEYWORDS, builtin_compile__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 172 | |
| 173 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 174 | builtin_compile_impl(PyObject *module, PyObject *source, PyObject *filename, |
| 175 | const char *mode, int flags, int dont_inherit, |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 176 | int optimize, int feature_version); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 177 | |
| 178 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 179 | builtin_compile(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 180 | { |
| 181 | PyObject *return_value = NULL; |
Victor Stinner | b2fd32b | 2019-06-12 16:17:05 +0200 | [diff] [blame] | 182 | static const char * const _keywords[] = {"source", "filename", "mode", "flags", "dont_inherit", "optimize", "_feature_version", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 183 | static _PyArg_Parser _parser = {NULL, _keywords, "compile", 0}; |
| 184 | PyObject *argsbuf[7]; |
| 185 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 3; |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 186 | PyObject *source; |
| 187 | PyObject *filename; |
| 188 | const char *mode; |
| 189 | int flags = 0; |
| 190 | int dont_inherit = 0; |
| 191 | int optimize = -1; |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 192 | int feature_version = -1; |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 193 | |
Victor Stinner | b2fd32b | 2019-06-12 16:17:05 +0200 | [diff] [blame] | 194 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 3, 6, 0, argsbuf); |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 195 | if (!args) { |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 196 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 197 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 198 | source = args[0]; |
| 199 | if (!PyUnicode_FSDecoder(args[1], &filename)) { |
| 200 | goto exit; |
| 201 | } |
| 202 | if (!PyUnicode_Check(args[2])) { |
Serhiy Storchaka | 96631dc | 2019-08-29 18:29:59 +0300 | [diff] [blame] | 203 | _PyArg_BadArgument("compile", "argument 'mode'", "str", args[2]); |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 204 | goto exit; |
| 205 | } |
| 206 | Py_ssize_t mode_length; |
| 207 | mode = PyUnicode_AsUTF8AndSize(args[2], &mode_length); |
| 208 | if (mode == NULL) { |
| 209 | goto exit; |
| 210 | } |
| 211 | if (strlen(mode) != (size_t)mode_length) { |
| 212 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 213 | goto exit; |
| 214 | } |
| 215 | if (!noptargs) { |
| 216 | goto skip_optional_pos; |
| 217 | } |
| 218 | if (args[3]) { |
| 219 | if (PyFloat_Check(args[3])) { |
| 220 | PyErr_SetString(PyExc_TypeError, |
| 221 | "integer argument expected, got float" ); |
| 222 | goto exit; |
| 223 | } |
| 224 | flags = _PyLong_AsInt(args[3]); |
| 225 | if (flags == -1 && PyErr_Occurred()) { |
| 226 | goto exit; |
| 227 | } |
| 228 | if (!--noptargs) { |
| 229 | goto skip_optional_pos; |
| 230 | } |
| 231 | } |
| 232 | if (args[4]) { |
| 233 | if (PyFloat_Check(args[4])) { |
| 234 | PyErr_SetString(PyExc_TypeError, |
| 235 | "integer argument expected, got float" ); |
| 236 | goto exit; |
| 237 | } |
| 238 | dont_inherit = _PyLong_AsInt(args[4]); |
| 239 | if (dont_inherit == -1 && PyErr_Occurred()) { |
| 240 | goto exit; |
| 241 | } |
| 242 | if (!--noptargs) { |
| 243 | goto skip_optional_pos; |
| 244 | } |
| 245 | } |
| 246 | if (args[5]) { |
| 247 | if (PyFloat_Check(args[5])) { |
| 248 | PyErr_SetString(PyExc_TypeError, |
| 249 | "integer argument expected, got float" ); |
| 250 | goto exit; |
| 251 | } |
| 252 | optimize = _PyLong_AsInt(args[5]); |
| 253 | if (optimize == -1 && PyErr_Occurred()) { |
| 254 | goto exit; |
| 255 | } |
| 256 | if (!--noptargs) { |
| 257 | goto skip_optional_pos; |
| 258 | } |
| 259 | } |
Victor Stinner | b2fd32b | 2019-06-12 16:17:05 +0200 | [diff] [blame] | 260 | skip_optional_pos: |
| 261 | if (!noptargs) { |
| 262 | goto skip_optional_kwonly; |
| 263 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 264 | if (PyFloat_Check(args[6])) { |
| 265 | PyErr_SetString(PyExc_TypeError, |
| 266 | "integer argument expected, got float" ); |
| 267 | goto exit; |
| 268 | } |
| 269 | feature_version = _PyLong_AsInt(args[6]); |
| 270 | if (feature_version == -1 && PyErr_Occurred()) { |
| 271 | goto exit; |
| 272 | } |
Victor Stinner | b2fd32b | 2019-06-12 16:17:05 +0200 | [diff] [blame] | 273 | skip_optional_kwonly: |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 274 | return_value = builtin_compile_impl(module, source, filename, mode, flags, dont_inherit, optimize, feature_version); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 275 | |
| 276 | exit: |
| 277 | return return_value; |
| 278 | } |
| 279 | |
| 280 | PyDoc_STRVAR(builtin_divmod__doc__, |
| 281 | "divmod($module, x, y, /)\n" |
| 282 | "--\n" |
| 283 | "\n" |
Serhiy Storchaka | df07173 | 2016-05-01 20:33:24 +0300 | [diff] [blame] | 284 | "Return the tuple (x//y, x%y). Invariant: div*y + mod == x."); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 285 | |
| 286 | #define BUILTIN_DIVMOD_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 287 | {"divmod", (PyCFunction)(void(*)(void))builtin_divmod, METH_FASTCALL, builtin_divmod__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 288 | |
| 289 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 290 | builtin_divmod_impl(PyObject *module, PyObject *x, PyObject *y); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 291 | |
| 292 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 293 | builtin_divmod(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 294 | { |
| 295 | PyObject *return_value = NULL; |
| 296 | PyObject *x; |
| 297 | PyObject *y; |
| 298 | |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 299 | if (!_PyArg_CheckPositional("divmod", nargs, 2, 2)) { |
Victor Stinner | 0c4a828 | 2017-01-17 02:21:47 +0100 | [diff] [blame] | 300 | goto exit; |
| 301 | } |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 302 | x = args[0]; |
| 303 | y = args[1]; |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 304 | return_value = builtin_divmod_impl(module, x, y); |
| 305 | |
| 306 | exit: |
| 307 | return return_value; |
| 308 | } |
| 309 | |
| 310 | PyDoc_STRVAR(builtin_eval__doc__, |
| 311 | "eval($module, source, globals=None, locals=None, /)\n" |
| 312 | "--\n" |
| 313 | "\n" |
| 314 | "Evaluate the given source in the context of globals and locals.\n" |
| 315 | "\n" |
| 316 | "The source may be a string representing a Python expression\n" |
| 317 | "or a code object as returned by compile().\n" |
| 318 | "The globals must be a dictionary and locals can be any mapping,\n" |
| 319 | "defaulting to the current globals and locals.\n" |
| 320 | "If only globals is given, locals defaults to it."); |
| 321 | |
| 322 | #define BUILTIN_EVAL_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 323 | {"eval", (PyCFunction)(void(*)(void))builtin_eval, METH_FASTCALL, builtin_eval__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 324 | |
| 325 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 326 | builtin_eval_impl(PyObject *module, PyObject *source, PyObject *globals, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 327 | PyObject *locals); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 328 | |
| 329 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 330 | builtin_eval(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 331 | { |
| 332 | PyObject *return_value = NULL; |
| 333 | PyObject *source; |
| 334 | PyObject *globals = Py_None; |
| 335 | PyObject *locals = Py_None; |
| 336 | |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 337 | if (!_PyArg_CheckPositional("eval", nargs, 1, 3)) { |
Victor Stinner | 0c4a828 | 2017-01-17 02:21:47 +0100 | [diff] [blame] | 338 | goto exit; |
| 339 | } |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 340 | source = args[0]; |
| 341 | if (nargs < 2) { |
| 342 | goto skip_optional; |
| 343 | } |
| 344 | globals = args[1]; |
| 345 | if (nargs < 3) { |
| 346 | goto skip_optional; |
| 347 | } |
| 348 | locals = args[2]; |
| 349 | skip_optional: |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 350 | return_value = builtin_eval_impl(module, source, globals, locals); |
| 351 | |
| 352 | exit: |
| 353 | return return_value; |
| 354 | } |
| 355 | |
| 356 | PyDoc_STRVAR(builtin_exec__doc__, |
| 357 | "exec($module, source, globals=None, locals=None, /)\n" |
| 358 | "--\n" |
| 359 | "\n" |
| 360 | "Execute the given source in the context of globals and locals.\n" |
| 361 | "\n" |
| 362 | "The source may be a string representing one or more Python statements\n" |
| 363 | "or a code object as returned by compile().\n" |
| 364 | "The globals must be a dictionary and locals can be any mapping,\n" |
| 365 | "defaulting to the current globals and locals.\n" |
| 366 | "If only globals is given, locals defaults to it."); |
| 367 | |
| 368 | #define BUILTIN_EXEC_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 369 | {"exec", (PyCFunction)(void(*)(void))builtin_exec, METH_FASTCALL, builtin_exec__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 370 | |
| 371 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 372 | builtin_exec_impl(PyObject *module, PyObject *source, PyObject *globals, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 373 | PyObject *locals); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 374 | |
| 375 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 376 | builtin_exec(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 377 | { |
| 378 | PyObject *return_value = NULL; |
| 379 | PyObject *source; |
| 380 | PyObject *globals = Py_None; |
| 381 | PyObject *locals = Py_None; |
| 382 | |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 383 | if (!_PyArg_CheckPositional("exec", nargs, 1, 3)) { |
Victor Stinner | 0c4a828 | 2017-01-17 02:21:47 +0100 | [diff] [blame] | 384 | goto exit; |
| 385 | } |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 386 | source = args[0]; |
| 387 | if (nargs < 2) { |
| 388 | goto skip_optional; |
| 389 | } |
| 390 | globals = args[1]; |
| 391 | if (nargs < 3) { |
| 392 | goto skip_optional; |
| 393 | } |
| 394 | locals = args[2]; |
| 395 | skip_optional: |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 396 | return_value = builtin_exec_impl(module, source, globals, locals); |
| 397 | |
| 398 | exit: |
| 399 | return return_value; |
| 400 | } |
| 401 | |
| 402 | PyDoc_STRVAR(builtin_globals__doc__, |
| 403 | "globals($module, /)\n" |
| 404 | "--\n" |
| 405 | "\n" |
| 406 | "Return the dictionary containing the current scope\'s global variables.\n" |
| 407 | "\n" |
| 408 | "NOTE: Updates to this dictionary *will* affect name lookups in the current\n" |
| 409 | "global scope and vice-versa."); |
| 410 | |
| 411 | #define BUILTIN_GLOBALS_METHODDEF \ |
| 412 | {"globals", (PyCFunction)builtin_globals, METH_NOARGS, builtin_globals__doc__}, |
| 413 | |
| 414 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 415 | builtin_globals_impl(PyObject *module); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 416 | |
| 417 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 418 | builtin_globals(PyObject *module, PyObject *Py_UNUSED(ignored)) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 419 | { |
| 420 | return builtin_globals_impl(module); |
| 421 | } |
| 422 | |
| 423 | PyDoc_STRVAR(builtin_hasattr__doc__, |
| 424 | "hasattr($module, obj, name, /)\n" |
| 425 | "--\n" |
| 426 | "\n" |
| 427 | "Return whether the object has an attribute with the given name.\n" |
| 428 | "\n" |
| 429 | "This is done by calling getattr(obj, name) and catching AttributeError."); |
| 430 | |
| 431 | #define BUILTIN_HASATTR_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 432 | {"hasattr", (PyCFunction)(void(*)(void))builtin_hasattr, METH_FASTCALL, builtin_hasattr__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 433 | |
| 434 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 435 | builtin_hasattr_impl(PyObject *module, PyObject *obj, PyObject *name); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 436 | |
| 437 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 438 | builtin_hasattr(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 439 | { |
| 440 | PyObject *return_value = NULL; |
| 441 | PyObject *obj; |
| 442 | PyObject *name; |
| 443 | |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 444 | if (!_PyArg_CheckPositional("hasattr", nargs, 2, 2)) { |
Victor Stinner | 0c4a828 | 2017-01-17 02:21:47 +0100 | [diff] [blame] | 445 | goto exit; |
| 446 | } |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 447 | obj = args[0]; |
| 448 | name = args[1]; |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 449 | return_value = builtin_hasattr_impl(module, obj, name); |
| 450 | |
| 451 | exit: |
| 452 | return return_value; |
| 453 | } |
| 454 | |
| 455 | PyDoc_STRVAR(builtin_id__doc__, |
| 456 | "id($module, obj, /)\n" |
| 457 | "--\n" |
| 458 | "\n" |
| 459 | "Return the identity of an object.\n" |
| 460 | "\n" |
| 461 | "This is guaranteed to be unique among simultaneously existing objects.\n" |
| 462 | "(CPython uses the object\'s memory address.)"); |
| 463 | |
| 464 | #define BUILTIN_ID_METHODDEF \ |
| 465 | {"id", (PyCFunction)builtin_id, METH_O, builtin_id__doc__}, |
| 466 | |
| 467 | PyDoc_STRVAR(builtin_setattr__doc__, |
| 468 | "setattr($module, obj, name, value, /)\n" |
| 469 | "--\n" |
| 470 | "\n" |
| 471 | "Sets the named attribute on the given object to the specified value.\n" |
| 472 | "\n" |
| 473 | "setattr(x, \'y\', v) is equivalent to ``x.y = v\'\'"); |
| 474 | |
| 475 | #define BUILTIN_SETATTR_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 476 | {"setattr", (PyCFunction)(void(*)(void))builtin_setattr, METH_FASTCALL, builtin_setattr__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 477 | |
| 478 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 479 | builtin_setattr_impl(PyObject *module, PyObject *obj, PyObject *name, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 480 | PyObject *value); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 481 | |
| 482 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 483 | builtin_setattr(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 484 | { |
| 485 | PyObject *return_value = NULL; |
| 486 | PyObject *obj; |
| 487 | PyObject *name; |
| 488 | PyObject *value; |
| 489 | |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 490 | if (!_PyArg_CheckPositional("setattr", nargs, 3, 3)) { |
Victor Stinner | 0c4a828 | 2017-01-17 02:21:47 +0100 | [diff] [blame] | 491 | goto exit; |
| 492 | } |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 493 | obj = args[0]; |
| 494 | name = args[1]; |
| 495 | value = args[2]; |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 496 | return_value = builtin_setattr_impl(module, obj, name, value); |
| 497 | |
| 498 | exit: |
| 499 | return return_value; |
| 500 | } |
| 501 | |
| 502 | PyDoc_STRVAR(builtin_delattr__doc__, |
| 503 | "delattr($module, obj, name, /)\n" |
| 504 | "--\n" |
| 505 | "\n" |
| 506 | "Deletes the named attribute from the given object.\n" |
| 507 | "\n" |
| 508 | "delattr(x, \'y\') is equivalent to ``del x.y\'\'"); |
| 509 | |
| 510 | #define BUILTIN_DELATTR_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 511 | {"delattr", (PyCFunction)(void(*)(void))builtin_delattr, METH_FASTCALL, builtin_delattr__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 512 | |
| 513 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 514 | builtin_delattr_impl(PyObject *module, PyObject *obj, PyObject *name); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 515 | |
| 516 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 517 | builtin_delattr(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 518 | { |
| 519 | PyObject *return_value = NULL; |
| 520 | PyObject *obj; |
| 521 | PyObject *name; |
| 522 | |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 523 | if (!_PyArg_CheckPositional("delattr", nargs, 2, 2)) { |
Victor Stinner | 0c4a828 | 2017-01-17 02:21:47 +0100 | [diff] [blame] | 524 | goto exit; |
| 525 | } |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 526 | obj = args[0]; |
| 527 | name = args[1]; |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 528 | return_value = builtin_delattr_impl(module, obj, name); |
| 529 | |
| 530 | exit: |
| 531 | return return_value; |
| 532 | } |
| 533 | |
| 534 | PyDoc_STRVAR(builtin_hash__doc__, |
| 535 | "hash($module, obj, /)\n" |
| 536 | "--\n" |
| 537 | "\n" |
| 538 | "Return the hash value for the given object.\n" |
| 539 | "\n" |
| 540 | "Two objects that compare equal must also have the same hash value, but the\n" |
| 541 | "reverse is not necessarily true."); |
| 542 | |
| 543 | #define BUILTIN_HASH_METHODDEF \ |
| 544 | {"hash", (PyCFunction)builtin_hash, METH_O, builtin_hash__doc__}, |
| 545 | |
| 546 | PyDoc_STRVAR(builtin_hex__doc__, |
| 547 | "hex($module, number, /)\n" |
| 548 | "--\n" |
| 549 | "\n" |
| 550 | "Return the hexadecimal representation of an integer.\n" |
| 551 | "\n" |
| 552 | " >>> hex(12648430)\n" |
| 553 | " \'0xc0ffee\'"); |
| 554 | |
| 555 | #define BUILTIN_HEX_METHODDEF \ |
| 556 | {"hex", (PyCFunction)builtin_hex, METH_O, builtin_hex__doc__}, |
| 557 | |
| 558 | PyDoc_STRVAR(builtin_len__doc__, |
| 559 | "len($module, obj, /)\n" |
| 560 | "--\n" |
| 561 | "\n" |
| 562 | "Return the number of items in a container."); |
| 563 | |
| 564 | #define BUILTIN_LEN_METHODDEF \ |
| 565 | {"len", (PyCFunction)builtin_len, METH_O, builtin_len__doc__}, |
| 566 | |
| 567 | PyDoc_STRVAR(builtin_locals__doc__, |
| 568 | "locals($module, /)\n" |
| 569 | "--\n" |
| 570 | "\n" |
| 571 | "Return a dictionary containing the current scope\'s local variables.\n" |
| 572 | "\n" |
| 573 | "NOTE: Whether or not updates to this dictionary will affect name lookups in\n" |
| 574 | "the local scope and vice-versa is *implementation dependent* and not\n" |
| 575 | "covered by any backwards compatibility guarantees."); |
| 576 | |
| 577 | #define BUILTIN_LOCALS_METHODDEF \ |
| 578 | {"locals", (PyCFunction)builtin_locals, METH_NOARGS, builtin_locals__doc__}, |
| 579 | |
| 580 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 581 | builtin_locals_impl(PyObject *module); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 582 | |
| 583 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 584 | builtin_locals(PyObject *module, PyObject *Py_UNUSED(ignored)) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 585 | { |
| 586 | return builtin_locals_impl(module); |
| 587 | } |
| 588 | |
| 589 | PyDoc_STRVAR(builtin_oct__doc__, |
| 590 | "oct($module, number, /)\n" |
| 591 | "--\n" |
| 592 | "\n" |
| 593 | "Return the octal representation of an integer.\n" |
| 594 | "\n" |
| 595 | " >>> oct(342391)\n" |
| 596 | " \'0o1234567\'"); |
| 597 | |
| 598 | #define BUILTIN_OCT_METHODDEF \ |
| 599 | {"oct", (PyCFunction)builtin_oct, METH_O, builtin_oct__doc__}, |
| 600 | |
| 601 | PyDoc_STRVAR(builtin_ord__doc__, |
| 602 | "ord($module, c, /)\n" |
| 603 | "--\n" |
| 604 | "\n" |
| 605 | "Return the Unicode code point for a one-character string."); |
| 606 | |
| 607 | #define BUILTIN_ORD_METHODDEF \ |
| 608 | {"ord", (PyCFunction)builtin_ord, METH_O, builtin_ord__doc__}, |
| 609 | |
| 610 | PyDoc_STRVAR(builtin_pow__doc__, |
| 611 | "pow($module, x, y, z=None, /)\n" |
| 612 | "--\n" |
| 613 | "\n" |
| 614 | "Equivalent to x**y (with two arguments) or x**y % z (with three arguments)\n" |
| 615 | "\n" |
| 616 | "Some types, such as ints, are able to use a more efficient algorithm when\n" |
| 617 | "invoked using the three argument form."); |
| 618 | |
| 619 | #define BUILTIN_POW_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 620 | {"pow", (PyCFunction)(void(*)(void))builtin_pow, METH_FASTCALL, builtin_pow__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 621 | |
| 622 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 623 | builtin_pow_impl(PyObject *module, PyObject *x, PyObject *y, PyObject *z); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 624 | |
| 625 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 626 | builtin_pow(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 627 | { |
| 628 | PyObject *return_value = NULL; |
| 629 | PyObject *x; |
| 630 | PyObject *y; |
| 631 | PyObject *z = Py_None; |
| 632 | |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 633 | if (!_PyArg_CheckPositional("pow", nargs, 2, 3)) { |
Victor Stinner | 0c4a828 | 2017-01-17 02:21:47 +0100 | [diff] [blame] | 634 | goto exit; |
| 635 | } |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 636 | x = args[0]; |
| 637 | y = args[1]; |
| 638 | if (nargs < 3) { |
| 639 | goto skip_optional; |
| 640 | } |
| 641 | z = args[2]; |
| 642 | skip_optional: |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 643 | return_value = builtin_pow_impl(module, x, y, z); |
| 644 | |
| 645 | exit: |
| 646 | return return_value; |
| 647 | } |
| 648 | |
| 649 | PyDoc_STRVAR(builtin_input__doc__, |
| 650 | "input($module, prompt=None, /)\n" |
| 651 | "--\n" |
| 652 | "\n" |
| 653 | "Read a string from standard input. The trailing newline is stripped.\n" |
| 654 | "\n" |
| 655 | "The prompt string, if given, is printed to standard output without a\n" |
| 656 | "trailing newline before reading input.\n" |
| 657 | "\n" |
| 658 | "If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError.\n" |
| 659 | "On *nix systems, readline is used if available."); |
| 660 | |
| 661 | #define BUILTIN_INPUT_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 662 | {"input", (PyCFunction)(void(*)(void))builtin_input, METH_FASTCALL, builtin_input__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 663 | |
| 664 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 665 | builtin_input_impl(PyObject *module, PyObject *prompt); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 666 | |
| 667 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 668 | builtin_input(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 669 | { |
| 670 | PyObject *return_value = NULL; |
| 671 | PyObject *prompt = NULL; |
| 672 | |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 673 | if (!_PyArg_CheckPositional("input", nargs, 0, 1)) { |
Victor Stinner | 0c4a828 | 2017-01-17 02:21:47 +0100 | [diff] [blame] | 674 | goto exit; |
| 675 | } |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 676 | if (nargs < 1) { |
| 677 | goto skip_optional; |
| 678 | } |
| 679 | prompt = args[0]; |
| 680 | skip_optional: |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 681 | return_value = builtin_input_impl(module, prompt); |
| 682 | |
| 683 | exit: |
| 684 | return return_value; |
| 685 | } |
| 686 | |
| 687 | PyDoc_STRVAR(builtin_repr__doc__, |
| 688 | "repr($module, obj, /)\n" |
| 689 | "--\n" |
| 690 | "\n" |
| 691 | "Return the canonical string representation of the object.\n" |
| 692 | "\n" |
| 693 | "For many object types, including most builtins, eval(repr(obj)) == obj."); |
| 694 | |
| 695 | #define BUILTIN_REPR_METHODDEF \ |
| 696 | {"repr", (PyCFunction)builtin_repr, METH_O, builtin_repr__doc__}, |
| 697 | |
Serhiy Storchaka | aca7f57 | 2017-11-15 17:51:14 +0200 | [diff] [blame] | 698 | PyDoc_STRVAR(builtin_round__doc__, |
| 699 | "round($module, /, number, ndigits=None)\n" |
| 700 | "--\n" |
| 701 | "\n" |
| 702 | "Round a number to a given precision in decimal digits.\n" |
| 703 | "\n" |
| 704 | "The return value is an integer if ndigits is omitted or None. Otherwise\n" |
| 705 | "the return value has the same type as the number. ndigits may be negative."); |
| 706 | |
| 707 | #define BUILTIN_ROUND_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 708 | {"round", (PyCFunction)(void(*)(void))builtin_round, METH_FASTCALL|METH_KEYWORDS, builtin_round__doc__}, |
Serhiy Storchaka | aca7f57 | 2017-11-15 17:51:14 +0200 | [diff] [blame] | 709 | |
| 710 | static PyObject * |
| 711 | builtin_round_impl(PyObject *module, PyObject *number, PyObject *ndigits); |
| 712 | |
| 713 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 714 | builtin_round(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
Serhiy Storchaka | aca7f57 | 2017-11-15 17:51:14 +0200 | [diff] [blame] | 715 | { |
| 716 | PyObject *return_value = NULL; |
| 717 | static const char * const _keywords[] = {"number", "ndigits", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 718 | static _PyArg_Parser _parser = {NULL, _keywords, "round", 0}; |
| 719 | PyObject *argsbuf[2]; |
| 720 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1; |
Serhiy Storchaka | aca7f57 | 2017-11-15 17:51:14 +0200 | [diff] [blame] | 721 | PyObject *number; |
Serhiy Storchaka | d322abb | 2019-09-14 13:31:50 +0300 | [diff] [blame] | 722 | PyObject *ndigits = Py_None; |
Serhiy Storchaka | aca7f57 | 2017-11-15 17:51:14 +0200 | [diff] [blame] | 723 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 724 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf); |
| 725 | if (!args) { |
Serhiy Storchaka | aca7f57 | 2017-11-15 17:51:14 +0200 | [diff] [blame] | 726 | goto exit; |
| 727 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 728 | number = args[0]; |
| 729 | if (!noptargs) { |
| 730 | goto skip_optional_pos; |
| 731 | } |
| 732 | ndigits = args[1]; |
| 733 | skip_optional_pos: |
Serhiy Storchaka | aca7f57 | 2017-11-15 17:51:14 +0200 | [diff] [blame] | 734 | return_value = builtin_round_impl(module, number, ndigits); |
| 735 | |
| 736 | exit: |
| 737 | return return_value; |
| 738 | } |
| 739 | |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 740 | PyDoc_STRVAR(builtin_sum__doc__, |
Raymond Hettinger | 9dfa0fe | 2018-09-12 10:54:06 -0700 | [diff] [blame] | 741 | "sum($module, iterable, /, start=0)\n" |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 742 | "--\n" |
| 743 | "\n" |
| 744 | "Return the sum of a \'start\' value (default: 0) plus an iterable of numbers\n" |
| 745 | "\n" |
| 746 | "When the iterable is empty, return the start value.\n" |
| 747 | "This function is intended specifically for use with numeric values and may\n" |
| 748 | "reject non-numeric types."); |
| 749 | |
| 750 | #define BUILTIN_SUM_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 751 | {"sum", (PyCFunction)(void(*)(void))builtin_sum, METH_FASTCALL|METH_KEYWORDS, builtin_sum__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 752 | |
| 753 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 754 | builtin_sum_impl(PyObject *module, PyObject *iterable, PyObject *start); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 755 | |
| 756 | static PyObject * |
Raymond Hettinger | 9dfa0fe | 2018-09-12 10:54:06 -0700 | [diff] [blame] | 757 | builtin_sum(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 758 | { |
| 759 | PyObject *return_value = NULL; |
Raymond Hettinger | 9dfa0fe | 2018-09-12 10:54:06 -0700 | [diff] [blame] | 760 | static const char * const _keywords[] = {"", "start", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 761 | static _PyArg_Parser _parser = {NULL, _keywords, "sum", 0}; |
| 762 | PyObject *argsbuf[2]; |
| 763 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1; |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 764 | PyObject *iterable; |
| 765 | PyObject *start = NULL; |
| 766 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 767 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf); |
| 768 | if (!args) { |
Victor Stinner | 0c4a828 | 2017-01-17 02:21:47 +0100 | [diff] [blame] | 769 | goto exit; |
| 770 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 771 | iterable = args[0]; |
| 772 | if (!noptargs) { |
| 773 | goto skip_optional_pos; |
| 774 | } |
| 775 | start = args[1]; |
| 776 | skip_optional_pos: |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 777 | return_value = builtin_sum_impl(module, iterable, start); |
| 778 | |
| 779 | exit: |
| 780 | return return_value; |
| 781 | } |
| 782 | |
| 783 | PyDoc_STRVAR(builtin_isinstance__doc__, |
| 784 | "isinstance($module, obj, class_or_tuple, /)\n" |
| 785 | "--\n" |
| 786 | "\n" |
| 787 | "Return whether an object is an instance of a class or of a subclass thereof.\n" |
| 788 | "\n" |
| 789 | "A tuple, as in ``isinstance(x, (A, B, ...))``, may be given as the target to\n" |
| 790 | "check against. This is equivalent to ``isinstance(x, A) or isinstance(x, B)\n" |
| 791 | "or ...`` etc."); |
| 792 | |
| 793 | #define BUILTIN_ISINSTANCE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 794 | {"isinstance", (PyCFunction)(void(*)(void))builtin_isinstance, METH_FASTCALL, builtin_isinstance__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 795 | |
| 796 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 797 | builtin_isinstance_impl(PyObject *module, PyObject *obj, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 798 | PyObject *class_or_tuple); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 799 | |
| 800 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 801 | builtin_isinstance(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 802 | { |
| 803 | PyObject *return_value = NULL; |
| 804 | PyObject *obj; |
| 805 | PyObject *class_or_tuple; |
| 806 | |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 807 | if (!_PyArg_CheckPositional("isinstance", nargs, 2, 2)) { |
Victor Stinner | 0c4a828 | 2017-01-17 02:21:47 +0100 | [diff] [blame] | 808 | goto exit; |
| 809 | } |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 810 | obj = args[0]; |
| 811 | class_or_tuple = args[1]; |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 812 | return_value = builtin_isinstance_impl(module, obj, class_or_tuple); |
| 813 | |
| 814 | exit: |
| 815 | return return_value; |
| 816 | } |
| 817 | |
| 818 | PyDoc_STRVAR(builtin_issubclass__doc__, |
| 819 | "issubclass($module, cls, class_or_tuple, /)\n" |
| 820 | "--\n" |
| 821 | "\n" |
| 822 | "Return whether \'cls\' is a derived from another class or is the same class.\n" |
| 823 | "\n" |
| 824 | "A tuple, as in ``issubclass(x, (A, B, ...))``, may be given as the target to\n" |
| 825 | "check against. This is equivalent to ``issubclass(x, A) or issubclass(x, B)\n" |
| 826 | "or ...`` etc."); |
| 827 | |
| 828 | #define BUILTIN_ISSUBCLASS_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 829 | {"issubclass", (PyCFunction)(void(*)(void))builtin_issubclass, METH_FASTCALL, builtin_issubclass__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 830 | |
| 831 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 832 | builtin_issubclass_impl(PyObject *module, PyObject *cls, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 833 | PyObject *class_or_tuple); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 834 | |
| 835 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 836 | builtin_issubclass(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 837 | { |
| 838 | PyObject *return_value = NULL; |
| 839 | PyObject *cls; |
| 840 | PyObject *class_or_tuple; |
| 841 | |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 842 | if (!_PyArg_CheckPositional("issubclass", nargs, 2, 2)) { |
Victor Stinner | 0c4a828 | 2017-01-17 02:21:47 +0100 | [diff] [blame] | 843 | goto exit; |
| 844 | } |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 845 | cls = args[0]; |
| 846 | class_or_tuple = args[1]; |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 847 | return_value = builtin_issubclass_impl(module, cls, class_or_tuple); |
| 848 | |
| 849 | exit: |
| 850 | return return_value; |
| 851 | } |
Serhiy Storchaka | d322abb | 2019-09-14 13:31:50 +0300 | [diff] [blame] | 852 | /*[clinic end generated code: output=4e118c2cd2cd98f3 input=a9049054013a1b77]*/ |