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