Showing the changes to sys/src/cmd/python/Python (71 of 3706 files). Show all
Unified · Split
| @@ -0,0 +1,3202 @@ | ||
| 1 | +/* File automatically generated by Parser/asdl_c.py */ | |
| 2 | + | |
| 3 | +#include "Python.h" | |
| 4 | +#include "Python-ast.h" | |
| 5 | + | |
| 6 | +static PyTypeObject* AST_type; | |
| 7 | +static PyTypeObject *mod_type; | |
| 8 | +static PyObject* ast2obj_mod(void*); | |
| 9 | +static PyTypeObject *Module_type; | |
| 10 | +static char *Module_fields[]={ | |
| 11 | + "body", | |
| 12 | +}; | |
| 13 | +static PyTypeObject *Interactive_type; | |
| 14 | +static char *Interactive_fields[]={ | |
| 15 | + "body", | |
| 16 | +}; | |
| 17 | +static PyTypeObject *Expression_type; | |
| 18 | +static char *Expression_fields[]={ | |
| 19 | + "body", | |
| 20 | +}; | |
| 21 | +static PyTypeObject *Suite_type; | |
| 22 | +static char *Suite_fields[]={ | |
| 23 | + "body", | |
| 24 | +}; | |
| 25 | +static PyTypeObject *stmt_type; | |
| 26 | +static char *stmt_attributes[] = { | |
| 27 | + "lineno", | |
| 28 | + "col_offset", | |
| 29 | +}; | |
| 30 | +static PyObject* ast2obj_stmt(void*); | |
| 31 | +static PyTypeObject *FunctionDef_type; | |
| 32 | +static char *FunctionDef_fields[]={ | |
| 33 | + "name", | |
| 34 | + "args", | |
| 35 | + "body", | |
| 36 | + "decorators", | |
| 37 | +}; | |
| 38 | +static PyTypeObject *ClassDef_type; | |
| 39 | +static char *ClassDef_fields[]={ | |
| 40 | + "name", | |
| 41 | + "bases", | |
| 42 | + "body", | |
| 43 | +}; | |
| 44 | +static PyTypeObject *Return_type; | |
| 45 | +static char *Return_fields[]={ | |
| 46 | + "value", | |
| 47 | +}; | |
| 48 | +static PyTypeObject *Delete_type; | |
| 49 | +static char *Delete_fields[]={ | |
| 50 | + "targets", | |
| 51 | +}; | |
| 52 | +static PyTypeObject *Assign_type; | |
| 53 | +static char *Assign_fields[]={ | |
| 54 | + "targets", | |
| 55 | + "value", | |
| 56 | +}; | |
| 57 | +static PyTypeObject *AugAssign_type; | |
| 58 | +static char *AugAssign_fields[]={ | |
| 59 | + "target", | |
| 60 | + "op", | |
| 61 | + "value", | |
| 62 | +}; | |
| 63 | +static PyTypeObject *Print_type; | |
| 64 | +static char *Print_fields[]={ | |
| 65 | + "dest", | |
| 66 | + "values", | |
| 67 | + "nl", | |
| 68 | +}; | |
| 69 | +static PyTypeObject *For_type; | |
| 70 | +static char *For_fields[]={ | |
| 71 | + "target", | |
| 72 | + "iter", | |
| 73 | + "body", | |
| 74 | + "orelse", | |
| 75 | +}; | |
| 76 | +static PyTypeObject *While_type; | |
| 77 | +static char *While_fields[]={ | |
| 78 | + "test", | |
| 79 | + "body", | |
| 80 | + "orelse", | |
| 81 | +}; | |
| 82 | +static PyTypeObject *If_type; | |
| 83 | +static char *If_fields[]={ | |
| 84 | + "test", | |
| 85 | + "body", | |
| 86 | + "orelse", | |
| 87 | +}; | |
| 88 | +static PyTypeObject *With_type; | |
| 89 | +static char *With_fields[]={ | |
| 90 | + "context_expr", | |
| 91 | + "optional_vars", | |
| 92 | + "body", | |
| 93 | +}; | |
| 94 | +static PyTypeObject *Raise_type; | |
| 95 | +static char *Raise_fields[]={ | |
| 96 | + "type", | |
| 97 | + "inst", | |
| 98 | + "tback", | |
| 99 | +}; | |
| 100 | +static PyTypeObject *TryExcept_type; | |
| 101 | +static char *TryExcept_fields[]={ | |
| 102 | + "body", | |
| 103 | + "handlers", | |
| 104 | + "orelse", | |
| 105 | +}; | |
| 106 | +static PyTypeObject *TryFinally_type; | |
| 107 | +static char *TryFinally_fields[]={ | |
| 108 | + "body", | |
| 109 | + "finalbody", | |
| 110 | +}; | |
| 111 | +static PyTypeObject *Assert_type; | |
| 112 | +static char *Assert_fields[]={ | |
| 113 | + "test", | |
| 114 | + "msg", | |
| 115 | +}; | |
| 116 | +static PyTypeObject *Import_type; | |
| 117 | +static char *Import_fields[]={ | |
| 118 | + "names", | |
| 119 | +}; | |
| 120 | +static PyTypeObject *ImportFrom_type; | |
| 121 | +static char *ImportFrom_fields[]={ | |
| 122 | + "module", | |
| 123 | + "names", | |
| 124 | + "level", | |
| 125 | +}; | |
| 126 | +static PyTypeObject *Exec_type; | |
| 127 | +static char *Exec_fields[]={ | |
| 128 | + "body", | |
| 129 | + "globals", | |
| 130 | + "locals", | |
| 131 | +}; | |
| 132 | +static PyTypeObject *Global_type; | |
| 133 | +static char *Global_fields[]={ | |
| 134 | + "names", | |
| 135 | +}; | |
| 136 | +static PyTypeObject *Expr_type; | |
| 137 | +static char *Expr_fields[]={ | |
| 138 | + "value", | |
| 139 | +}; | |
| 140 | +static PyTypeObject *Pass_type; | |
| 141 | +static PyTypeObject *Break_type; | |
| 142 | +static PyTypeObject *Continue_type; | |
| 143 | +static PyTypeObject *expr_type; | |
| 144 | +static char *expr_attributes[] = { | |
| 145 | + "lineno", | |
| 146 | + "col_offset", | |
| 147 | +}; | |
| 148 | +static PyObject* ast2obj_expr(void*); | |
| 149 | +static PyTypeObject *BoolOp_type; | |
| 150 | +static char *BoolOp_fields[]={ | |
| 151 | + "op", | |
| 152 | + "values", | |
| 153 | +}; | |
| 154 | +static PyTypeObject *BinOp_type; | |
| 155 | +static char *BinOp_fields[]={ | |
| 156 | + "left", | |
| 157 | + "op", | |
| 158 | + "right", | |
| 159 | +}; | |
| 160 | +static PyTypeObject *UnaryOp_type; | |
| 161 | +static char *UnaryOp_fields[]={ | |
| 162 | + "op", | |
| 163 | + "operand", | |
| 164 | +}; | |
| 165 | +static PyTypeObject *Lambda_type; | |
| 166 | +static char *Lambda_fields[]={ | |
| 167 | + "args", | |
| 168 | + "body", | |
| 169 | +}; | |
| 170 | +static PyTypeObject *IfExp_type; | |
| 171 | +static char *IfExp_fields[]={ | |
| 172 | + "test", | |
| 173 | + "body", | |
| 174 | + "orelse", | |
| 175 | +}; | |
| 176 | +static PyTypeObject *Dict_type; | |
| 177 | +static char *Dict_fields[]={ | |
| 178 | + "keys", | |
| 179 | + "values", | |
| 180 | +}; | |
| 181 | +static PyTypeObject *ListComp_type; | |
| 182 | +static char *ListComp_fields[]={ | |
| 183 | + "elt", | |
| 184 | + "generators", | |
| 185 | +}; | |
| 186 | +static PyTypeObject *GeneratorExp_type; | |
| 187 | +static char *GeneratorExp_fields[]={ | |
| 188 | + "elt", | |
| 189 | + "generators", | |
| 190 | +}; | |
| 191 | +static PyTypeObject *Yield_type; | |
| 192 | +static char *Yield_fields[]={ | |
| 193 | + "value", | |
| 194 | +}; | |
| 195 | +static PyTypeObject *Compare_type; | |
| 196 | +static char *Compare_fields[]={ | |
| 197 | + "left", | |
| 198 | + "ops", | |
| 199 | + "comparators", | |
| 200 | +}; | |
| 201 | +static PyTypeObject *Call_type; | |
| 202 | +static char *Call_fields[]={ | |
| 203 | + "func", | |
| 204 | + "args", | |
| 205 | + "keywords", | |
| 206 | + "starargs", | |
| 207 | + "kwargs", | |
| 208 | +}; | |
| 209 | +static PyTypeObject *Repr_type; | |
| 210 | +static char *Repr_fields[]={ | |
| 211 | + "value", | |
| 212 | +}; | |
| 213 | +static PyTypeObject *Num_type; | |
| 214 | +static char *Num_fields[]={ | |
| 215 | + "n", | |
| 216 | +}; | |
| 217 | +static PyTypeObject *Str_type; | |
| 218 | +static char *Str_fields[]={ | |
| 219 | + "s", | |
| 220 | +}; | |
| 221 | +static PyTypeObject *Attribute_type; | |
| 222 | +static char *Attribute_fields[]={ | |
| 223 | + "value", | |
| 224 | + "attr", | |
| 225 | + "ctx", | |
| 226 | +}; | |
| 227 | +static PyTypeObject *Subscript_type; | |
| 228 | +static char *Subscript_fields[]={ | |
| 229 | + "value", | |
| 230 | + "slice", | |
| 231 | + "ctx", | |
| 232 | +}; | |
| 233 | +static PyTypeObject *Name_type; | |
| 234 | +static char *Name_fields[]={ | |
| 235 | + "id", | |
| 236 | + "ctx", | |
| 237 | +}; | |
| 238 | +static PyTypeObject *List_type; | |
| 239 | +static char *List_fields[]={ | |
| 240 | + "elts", | |
| 241 | + "ctx", | |
| 242 | +}; | |
| 243 | +static PyTypeObject *Tuple_type; | |
| 244 | +static char *Tuple_fields[]={ | |
| 245 | + "elts", | |
| 246 | + "ctx", | |
| 247 | +}; | |
| 248 | +static PyTypeObject *expr_context_type; | |
| 249 | +static PyObject *Load_singleton, *Store_singleton, *Del_singleton, | |
| 250 | +*AugLoad_singleton, *AugStore_singleton, *Param_singleton; | |
| 251 | +static PyObject* ast2obj_expr_context(expr_context_ty); | |
| 252 | +static PyTypeObject *Load_type; | |
| 253 | +static PyTypeObject *Store_type; | |
| 254 | +static PyTypeObject *Del_type; | |
| 255 | +static PyTypeObject *AugLoad_type; | |
| 256 | +static PyTypeObject *AugStore_type; | |
| 257 | +static PyTypeObject *Param_type; | |
| 258 | +static PyTypeObject *slice_type; | |
| 259 | +static PyObject* ast2obj_slice(void*); | |
| 260 | +static PyTypeObject *Ellipsis_type; | |
| 261 | +static PyTypeObject *Slice_type; | |
| 262 | +static char *Slice_fields[]={ | |
| 263 | + "lower", | |
| 264 | + "upper", | |
| 265 | + "step", | |
| 266 | +}; | |
| 267 | +static PyTypeObject *ExtSlice_type; | |
| 268 | +static char *ExtSlice_fields[]={ | |
| 269 | + "dims", | |
| 270 | +}; | |
| 271 | +static PyTypeObject *Index_type; | |
| 272 | +static char *Index_fields[]={ | |
| 273 | + "value", | |
| 274 | +}; | |
| 275 | +static PyTypeObject *boolop_type; | |
| 276 | +static PyObject *And_singleton, *Or_singleton; | |
| 277 | +static PyObject* ast2obj_boolop(boolop_ty); | |
| 278 | +static PyTypeObject *And_type; | |
| 279 | +static PyTypeObject *Or_type; | |
| 280 | +static PyTypeObject *operator_type; | |
| 281 | +static PyObject *Add_singleton, *Sub_singleton, *Mult_singleton, | |
| 282 | +*Div_singleton, *Mod_singleton, *Pow_singleton, *LShift_singleton, | |
| 283 | +*RShift_singleton, *BitOr_singleton, *BitXor_singleton, *BitAnd_singleton, | |
| 284 | +*FloorDiv_singleton; | |
| 285 | +static PyObject* ast2obj_operator(operator_ty); | |
| 286 | +static PyTypeObject *Add_type; | |
| 287 | +static PyTypeObject *Sub_type; | |
| 288 | +static PyTypeObject *Mult_type; | |
| 289 | +static PyTypeObject *Div_type; | |
| 290 | +static PyTypeObject *Mod_type; | |
| 291 | +static PyTypeObject *Pow_type; | |
| 292 | +static PyTypeObject *LShift_type; | |
| 293 | +static PyTypeObject *RShift_type; | |
| 294 | +static PyTypeObject *BitOr_type; | |
| 295 | +static PyTypeObject *BitXor_type; | |
| 296 | +static PyTypeObject *BitAnd_type; | |
| 297 | +static PyTypeObject *FloorDiv_type; | |
| 298 | +static PyTypeObject *unaryop_type; | |
| 299 | +static PyObject *Invert_singleton, *Not_singleton, *UAdd_singleton, | |
| 300 | +*USub_singleton; | |
| 301 | +static PyObject* ast2obj_unaryop(unaryop_ty); | |
| 302 | +static PyTypeObject *Invert_type; | |
| 303 | +static PyTypeObject *Not_type; | |
| 304 | +static PyTypeObject *UAdd_type; | |
| 305 | +static PyTypeObject *USub_type; | |
| 306 | +static PyTypeObject *cmpop_type; | |
| 307 | +static PyObject *Eq_singleton, *NotEq_singleton, *Lt_singleton, *LtE_singleton, | |
| 308 | +*Gt_singleton, *GtE_singleton, *Is_singleton, *IsNot_singleton, *In_singleton, | |
| 309 | +*NotIn_singleton; | |
| 310 | +static PyObject* ast2obj_cmpop(cmpop_ty); | |
| 311 | +static PyTypeObject *Eq_type; | |
| 312 | +static PyTypeObject *NotEq_type; | |
| 313 | +static PyTypeObject *Lt_type; | |
| 314 | +static PyTypeObject *LtE_type; | |
| 315 | +static PyTypeObject *Gt_type; | |
| 316 | +static PyTypeObject *GtE_type; | |
| 317 | +static PyTypeObject *Is_type; | |
| 318 | +static PyTypeObject *IsNot_type; | |
| 319 | +static PyTypeObject *In_type; | |
| 320 | +static PyTypeObject *NotIn_type; | |
| 321 | +static PyTypeObject *comprehension_type; | |
| 322 | +static PyObject* ast2obj_comprehension(void*); | |
| 323 | +static char *comprehension_fields[]={ | |
| 324 | + "target", | |
| 325 | + "iter", | |
| 326 | + "ifs", | |
| 327 | +}; | |
| 328 | +static PyTypeObject *excepthandler_type; | |
| 329 | +static PyObject* ast2obj_excepthandler(void*); | |
| 330 | +static char *excepthandler_fields[]={ | |
| 331 | + "type", | |
| 332 | + "name", | |
| 333 | + "body", | |
| 334 | + "lineno", | |
| 335 | + "col_offset", | |
| 336 | +}; | |
| 337 | +static PyTypeObject *arguments_type; | |
| 338 | +static PyObject* ast2obj_arguments(void*); | |
| 339 | +static char *arguments_fields[]={ | |
| 340 | + "args", | |
| 341 | + "vararg", | |
| 342 | + "kwarg", | |
| 343 | + "defaults", | |
| 344 | +}; | |
| 345 | +static PyTypeObject *keyword_type; | |
| 346 | +static PyObject* ast2obj_keyword(void*); | |
| 347 | +static char *keyword_fields[]={ | |
| 348 | + "arg", | |
| 349 | + "value", | |
| 350 | +}; | |
| 351 | +static PyTypeObject *alias_type; | |
| 352 | +static PyObject* ast2obj_alias(void*); | |
| 353 | +static char *alias_fields[]={ | |
| 354 | + "name", | |
| 355 | + "asname", | |
| 356 | +}; | |
| 357 | + | |
| 358 | + | |
| 359 | +static PyTypeObject* make_type(char *type, PyTypeObject* base, char**fields, int num_fields) | |
| 360 | +{ | |
| 361 | + PyObject *fnames, *result; | |
| 362 | + int i; | |
| 363 | + if (num_fields) { | |
| 364 | + fnames = PyTuple_New(num_fields); | |
| 365 | + if (!fnames) return NULL; | |
| 366 | + } else { | |
| 367 | + fnames = Py_None; | |
| 368 | + Py_INCREF(Py_None); | |
| 369 | + } | |
| 370 | + for(i=0; i < num_fields; i++) { | |
| 371 | + PyObject *field = PyString_FromString(fields[i]); | |
| 372 | + if (!field) { | |
| 373 | + Py_DECREF(fnames); | |
| 374 | + return NULL; | |
| 375 | + } | |
| 376 | + PyTuple_SET_ITEM(fnames, i, field); | |
| 377 | + } | |
| 378 | + result = PyObject_CallFunction((PyObject*)&PyType_Type, "s(O){sOss}", | |
| 379 | + type, base, "_fields", fnames, "__module__", "_ast"); | |
| 380 | + Py_DECREF(fnames); | |
| 381 | + return (PyTypeObject*)result; | |
| 382 | +} | |
| 383 | + | |
| 384 | +static int add_attributes(PyTypeObject* type, char**attrs, int num_fields) | |
| 385 | +{ | |
| 386 | + int i, result; | |
| 387 | + PyObject *s, *l = PyList_New(num_fields); | |
| 388 | + if (!l) return 0; | |
| 389 | + for(i = 0; i < num_fields; i++) { | |
| 390 | + s = PyString_FromString(attrs[i]); | |
| 391 | + if (!s) { | |
| 392 | + Py_DECREF(l); | |
| 393 | + return 0; | |
| 394 | + } | |
| 395 | + PyList_SET_ITEM(l, i, s); | |
| 396 | + } | |
| 397 | + result = PyObject_SetAttrString((PyObject*)type, "_attributes", l) >= 0; | |
| 398 | + Py_DECREF(l); | |
| 399 | + return result; | |
| 400 | +} | |
| 401 | + | |
| 402 | +static PyObject* ast2obj_list(asdl_seq *seq, PyObject* (*func)(void*)) | |
| 403 | +{ | |
| 404 | + int i, n = asdl_seq_LEN(seq); | |
| 405 | + PyObject *result = PyList_New(n); | |
| 406 | + PyObject *value; | |
| 407 | + if (!result) | |
| 408 | + return NULL; | |
| 409 | + for (i = 0; i < n; i++) { | |
| 410 | + value = func(asdl_seq_GET(seq, i)); | |
| 411 | + if (!value) { | |
| 412 | + Py_DECREF(result); | |
| 413 | + return NULL; | |
| 414 | + } | |
| 415 | + PyList_SET_ITEM(result, i, value); | |
| 416 | + } | |
| 417 | + return result; | |
| 418 | +} | |
| 419 | + | |
| 420 | +static PyObject* ast2obj_object(void *o) | |
| 421 | +{ | |
| 422 | + if (!o) | |
| 423 | + o = Py_None; | |
| 424 | + Py_INCREF((PyObject*)o); | |
| 425 | + return (PyObject*)o; | |
| 426 | +} | |
| 427 | +#define ast2obj_identifier ast2obj_object | |
| 428 | +#define ast2obj_string ast2obj_object | |
| 429 | +static PyObject* ast2obj_bool(bool b) | |
| 430 | +{ | |
| 431 | + return PyBool_FromLong(b); | |
| 432 | +} | |
| 433 | + | |
| 434 | +static PyObject* ast2obj_int(bool b) | |
| 435 | +{ | |
| 436 | + return PyInt_FromLong(b); | |
| 437 | +} | |
| 438 | + | |
| 439 | +static int init_types(void) | |
| 440 | +{ | |
| 441 | + static int initialized; | |
| 442 | + if (initialized) return 1; | |
| 443 | + AST_type = make_type("AST", &PyBaseObject_Type, NULL, 0); | |
| 444 | + mod_type = make_type("mod", AST_type, NULL, 0); | |
| 445 | + if (!mod_type) return 0; | |
| 446 | + if (!add_attributes(mod_type, NULL, 0)) return 0; | |
| 447 | + Module_type = make_type("Module", mod_type, Module_fields, 1); | |
| 448 | + if (!Module_type) return 0; | |
| 449 | + Interactive_type = make_type("Interactive", mod_type, | |
| 450 | + Interactive_fields, 1); | |
| 451 | + if (!Interactive_type) return 0; | |
| 452 | + Expression_type = make_type("Expression", mod_type, Expression_fields, | |
| 453 | + 1); | |
| 454 | + if (!Expression_type) return 0; | |
| 455 | + Suite_type = make_type("Suite", mod_type, Suite_fields, 1); | |
| 456 | + if (!Suite_type) return 0; | |
| 457 | + stmt_type = make_type("stmt", AST_type, NULL, 0); | |
| 458 | + if (!stmt_type) return 0; | |
| 459 | + if (!add_attributes(stmt_type, stmt_attributes, 2)) return 0; | |
| 460 | + FunctionDef_type = make_type("FunctionDef", stmt_type, | |
| 461 | + FunctionDef_fields, 4); | |
| 462 | + if (!FunctionDef_type) return 0; | |
| 463 | + ClassDef_type = make_type("ClassDef", stmt_type, ClassDef_fields, 3); | |
| 464 | + if (!ClassDef_type) return 0; | |
| 465 | + Return_type = make_type("Return", stmt_type, Return_fields, 1); | |
| 466 | + if (!Return_type) return 0; | |
| 467 | + Delete_type = make_type("Delete", stmt_type, Delete_fields, 1); | |
| 468 | + if (!Delete_type) return 0; | |
| 469 | + Assign_type = make_type("Assign", stmt_type, Assign_fields, 2); | |
| 470 | + if (!Assign_type) return 0; | |
| 471 | + AugAssign_type = make_type("AugAssign", stmt_type, AugAssign_fields, 3); | |
| 472 | + if (!AugAssign_type) return 0; | |
| 473 | + Print_type = make_type("Print", stmt_type, Print_fields, 3); | |
| 474 | + if (!Print_type) return 0; | |
| 475 | + For_type = make_type("For", stmt_type, For_fields, 4); | |
| 476 | + if (!For_type) return 0; | |
| 477 | + While_type = make_type("While", stmt_type, While_fields, 3); | |
| 478 | + if (!While_type) return 0; | |
| 479 | + If_type = make_type("If", stmt_type, If_fields, 3); | |
| 480 | + if (!If_type) return 0; | |
| 481 | + With_type = make_type("With", stmt_type, With_fields, 3); | |
| 482 | + if (!With_type) return 0; | |
| 483 | + Raise_type = make_type("Raise", stmt_type, Raise_fields, 3); | |
| 484 | + if (!Raise_type) return 0; | |
| 485 | + TryExcept_type = make_type("TryExcept", stmt_type, TryExcept_fields, 3); | |
| 486 | + if (!TryExcept_type) return 0; | |
| 487 | + TryFinally_type = make_type("TryFinally", stmt_type, TryFinally_fields, | |
| 488 | + 2); | |
| 489 | + if (!TryFinally_type) return 0; | |
| 490 | + Assert_type = make_type("Assert", stmt_type, Assert_fields, 2); | |
| 491 | + if (!Assert_type) return 0; | |
| 492 | + Import_type = make_type("Import", stmt_type, Import_fields, 1); | |
| 493 | + if (!Import_type) return 0; | |
| 494 | + ImportFrom_type = make_type("ImportFrom", stmt_type, ImportFrom_fields, | |
| 495 | + 3); | |
| 496 | + if (!ImportFrom_type) return 0; | |
| 497 | + Exec_type = make_type("Exec", stmt_type, Exec_fields, 3); | |
| 498 | + if (!Exec_type) return 0; | |
| 499 | + Global_type = make_type("Global", stmt_type, Global_fields, 1); | |
| 500 | + if (!Global_type) return 0; | |
| 501 | + Expr_type = make_type("Expr", stmt_type, Expr_fields, 1); | |
| 502 | + if (!Expr_type) return 0; | |
| 503 | + Pass_type = make_type("Pass", stmt_type, NULL, 0); | |
| 504 | + if (!Pass_type) return 0; | |
| 505 | + Break_type = make_type("Break", stmt_type, NULL, 0); | |
| 506 | + if (!Break_type) return 0; | |
| 507 | + Continue_type = make_type("Continue", stmt_type, NULL, 0); | |
| 508 | + if (!Continue_type) return 0; | |
| 509 | + expr_type = make_type("expr", AST_type, NULL, 0); | |
| 510 | + if (!expr_type) return 0; | |
| 511 | + if (!add_attributes(expr_type, expr_attributes, 2)) return 0; | |
| 512 | + BoolOp_type = make_type("BoolOp", expr_type, BoolOp_fields, 2); | |
| 513 | + if (!BoolOp_type) return 0; | |
| 514 | + BinOp_type = make_type("BinOp", expr_type, BinOp_fields, 3); | |
| 515 | + if (!BinOp_type) return 0; | |
| 516 | + UnaryOp_type = make_type("UnaryOp", expr_type, UnaryOp_fields, 2); | |
| 517 | + if (!UnaryOp_type) return 0; | |
| 518 | + Lambda_type = make_type("Lambda", expr_type, Lambda_fields, 2); | |
| 519 | + if (!Lambda_type) return 0; | |
| 520 | + IfExp_type = make_type("IfExp", expr_type, IfExp_fields, 3); | |
| 521 | + if (!IfExp_type) return 0; | |
| 522 | + Dict_type = make_type("Dict", expr_type, Dict_fields, 2); | |
| 523 | + if (!Dict_type) return 0; | |
| 524 | + ListComp_type = make_type("ListComp", expr_type, ListComp_fields, 2); | |
| 525 | + if (!ListComp_type) return 0; | |
| 526 | + GeneratorExp_type = make_type("GeneratorExp", expr_type, | |
| 527 | + GeneratorExp_fields, 2); | |
| 528 | + if (!GeneratorExp_type) return 0; | |
| 529 | + Yield_type = make_type("Yield", expr_type, Yield_fields, 1); | |
| 530 | + if (!Yield_type) return 0; | |
| 531 | + Compare_type = make_type("Compare", expr_type, Compare_fields, 3); | |
| 532 | + if (!Compare_type) return 0; | |
| 533 | + Call_type = make_type("Call", expr_type, Call_fields, 5); | |
| 534 | + if (!Call_type) return 0; | |
| 535 | + Repr_type = make_type("Repr", expr_type, Repr_fields, 1); | |
| 536 | + if (!Repr_type) return 0; | |
| 537 | + Num_type = make_type("Num", expr_type, Num_fields, 1); | |
| 538 | + if (!Num_type) return 0; | |
| 539 | + Str_type = make_type("Str", expr_type, Str_fields, 1); | |
| 540 | + if (!Str_type) return 0; | |
| 541 | + Attribute_type = make_type("Attribute", expr_type, Attribute_fields, 3); | |
| 542 | + if (!Attribute_type) return 0; | |
| 543 | + Subscript_type = make_type("Subscript", expr_type, Subscript_fields, 3); | |
| 544 | + if (!Subscript_type) return 0; | |
| 545 | + Name_type = make_type("Name", expr_type, Name_fields, 2); | |
| 546 | + if (!Name_type) return 0; | |
| 547 | + List_type = make_type("List", expr_type, List_fields, 2); | |
| 548 | + if (!List_type) return 0; | |
| 549 | + Tuple_type = make_type("Tuple", expr_type, Tuple_fields, 2); | |
| 550 | + if (!Tuple_type) return 0; | |
| 551 | + expr_context_type = make_type("expr_context", AST_type, NULL, 0); | |
| 552 | + if (!expr_context_type) return 0; | |
| 553 | + if (!add_attributes(expr_context_type, NULL, 0)) return 0; | |
| 554 | + Load_type = make_type("Load", expr_context_type, NULL, 0); | |
| 555 | + if (!Load_type) return 0; | |
| 556 | + Load_singleton = PyType_GenericNew(Load_type, NULL, NULL); | |
| 557 | + if (!Load_singleton) return 0; | |
| 558 | + Store_type = make_type("Store", expr_context_type, NULL, 0); | |
| 559 | + if (!Store_type) return 0; | |
| 560 | + Store_singleton = PyType_GenericNew(Store_type, NULL, NULL); | |
| 561 | + if (!Store_singleton) return 0; | |
| 562 | + Del_type = make_type("Del", expr_context_type, NULL, 0); | |
| 563 | + if (!Del_type) return 0; | |
| 564 | + Del_singleton = PyType_GenericNew(Del_type, NULL, NULL); | |
| 565 | + if (!Del_singleton) return 0; | |
| 566 | + AugLoad_type = make_type("AugLoad", expr_context_type, NULL, 0); | |
| 567 | + if (!AugLoad_type) return 0; | |
| 568 | + AugLoad_singleton = PyType_GenericNew(AugLoad_type, NULL, NULL); | |
| 569 | + if (!AugLoad_singleton) return 0; | |
| 570 | + AugStore_type = make_type("AugStore", expr_context_type, NULL, 0); | |
| 571 | + if (!AugStore_type) return 0; | |
| 572 | + AugStore_singleton = PyType_GenericNew(AugStore_type, NULL, NULL); | |
| 573 | + if (!AugStore_singleton) return 0; | |
| 574 | + Param_type = make_type("Param", expr_context_type, NULL, 0); | |
| 575 | + if (!Param_type) return 0; | |
| 576 | + Param_singleton = PyType_GenericNew(Param_type, NULL, NULL); | |
| 577 | + if (!Param_singleton) return 0; | |
| 578 | + slice_type = make_type("slice", AST_type, NULL, 0); | |
| 579 | + if (!slice_type) return 0; | |
| 580 | + if (!add_attributes(slice_type, NULL, 0)) return 0; | |
| 581 | + Ellipsis_type = make_type("Ellipsis", slice_type, NULL, 0); | |
| 582 | + if (!Ellipsis_type) return 0; | |
| 583 | + Slice_type = make_type("Slice", slice_type, Slice_fields, 3); | |
| 584 | + if (!Slice_type) return 0; | |
| 585 | + ExtSlice_type = make_type("ExtSlice", slice_type, ExtSlice_fields, 1); | |
| 586 | + if (!ExtSlice_type) return 0; | |
| 587 | + Index_type = make_type("Index", slice_type, Index_fields, 1); | |
| 588 | + if (!Index_type) return 0; | |
| 589 | + boolop_type = make_type("boolop", AST_type, NULL, 0); | |
| 590 | + if (!boolop_type) return 0; | |
| 591 | + if (!add_attributes(boolop_type, NULL, 0)) return 0; | |
| 592 | + And_type = make_type("And", boolop_type, NULL, 0); | |
| 593 | + if (!And_type) return 0; | |
| 594 | + And_singleton = PyType_GenericNew(And_type, NULL, NULL); | |
| 595 | + if (!And_singleton) return 0; | |
| 596 | + Or_type = make_type("Or", boolop_type, NULL, 0); | |
| 597 | + if (!Or_type) return 0; | |
| 598 | + Or_singleton = PyType_GenericNew(Or_type, NULL, NULL); | |
| 599 | + if (!Or_singleton) return 0; | |
| 600 | + operator_type = make_type("operator", AST_type, NULL, 0); | |
| 601 | + if (!operator_type) return 0; | |
| 602 | + if (!add_attributes(operator_type, NULL, 0)) return 0; | |
| 603 | + Add_type = make_type("Add", operator_type, NULL, 0); | |
| 604 | + if (!Add_type) return 0; | |
| 605 | + Add_singleton = PyType_GenericNew(Add_type, NULL, NULL); | |
| 606 | + if (!Add_singleton) return 0; | |
| 607 | + Sub_type = make_type("Sub", operator_type, NULL, 0); | |
| 608 | + if (!Sub_type) return 0; | |
| 609 | + Sub_singleton = PyType_GenericNew(Sub_type, NULL, NULL); | |
| 610 | + if (!Sub_singleton) return 0; | |
| 611 | + Mult_type = make_type("Mult", operator_type, NULL, 0); | |
| 612 | + if (!Mult_type) return 0; | |
| 613 | + Mult_singleton = PyType_GenericNew(Mult_type, NULL, NULL); | |
| 614 | + if (!Mult_singleton) return 0; | |
| 615 | + Div_type = make_type("Div", operator_type, NULL, 0); | |
| 616 | + if (!Div_type) return 0; | |
| 617 | + Div_singleton = PyType_GenericNew(Div_type, NULL, NULL); | |
| 618 | + if (!Div_singleton) return 0; | |
| 619 | + Mod_type = make_type("Mod", operator_type, NULL, 0); | |
| 620 | + if (!Mod_type) return 0; | |
| 621 | + Mod_singleton = PyType_GenericNew(Mod_type, NULL, NULL); | |
| 622 | + if (!Mod_singleton) return 0; | |
| 623 | + Pow_type = make_type("Pow", operator_type, NULL, 0); | |
| 624 | + if (!Pow_type) return 0; | |
| 625 | + Pow_singleton = PyType_GenericNew(Pow_type, NULL, NULL); | |
| 626 | + if (!Pow_singleton) return 0; | |
| 627 | + LShift_type = make_type("LShift", operator_type, NULL, 0); | |
| 628 | + if (!LShift_type) return 0; | |
| 629 | + LShift_singleton = PyType_GenericNew(LShift_type, NULL, NULL); | |
| 630 | + if (!LShift_singleton) return 0; | |
| 631 | + RShift_type = make_type("RShift", operator_type, NULL, 0); | |
| 632 | + if (!RShift_type) return 0; | |
| 633 | + RShift_singleton = PyType_GenericNew(RShift_type, NULL, NULL); | |
| 634 | + if (!RShift_singleton) return 0; | |
| 635 | + BitOr_type = make_type("BitOr", operator_type, NULL, 0); | |
| 636 | + if (!BitOr_type) return 0; | |
| 637 | + BitOr_singleton = PyType_GenericNew(BitOr_type, NULL, NULL); | |
| 638 | + if (!BitOr_singleton) return 0; | |
| 639 | + BitXor_type = make_type("BitXor", operator_type, NULL, 0); | |
| 640 | + if (!BitXor_type) return 0; | |
| 641 | + BitXor_singleton = PyType_GenericNew(BitXor_type, NULL, NULL); | |
| 642 | + if (!BitXor_singleton) return 0; | |
| 643 | + BitAnd_type = make_type("BitAnd", operator_type, NULL, 0); | |
| 644 | + if (!BitAnd_type) return 0; | |
| 645 | + BitAnd_singleton = PyType_GenericNew(BitAnd_type, NULL, NULL); | |
| 646 | + if (!BitAnd_singleton) return 0; | |
| 647 | + FloorDiv_type = make_type("FloorDiv", operator_type, NULL, 0); | |
| 648 | + if (!FloorDiv_type) return 0; | |
| 649 | + FloorDiv_singleton = PyType_GenericNew(FloorDiv_type, NULL, NULL); | |
| 650 | + if (!FloorDiv_singleton) return 0; | |
| 651 | + unaryop_type = make_type("unaryop", AST_type, NULL, 0); | |
| 652 | + if (!unaryop_type) return 0; | |
| 653 | + if (!add_attributes(unaryop_type, NULL, 0)) return 0; | |
| 654 | + Invert_type = make_type("Invert", unaryop_type, NULL, 0); | |
| 655 | + if (!Invert_type) return 0; | |
| 656 | + Invert_singleton = PyType_GenericNew(Invert_type, NULL, NULL); | |
| 657 | + if (!Invert_singleton) return 0; | |
| 658 | + Not_type = make_type("Not", unaryop_type, NULL, 0); | |
| 659 | + if (!Not_type) return 0; | |
| 660 | + Not_singleton = PyType_GenericNew(Not_type, NULL, NULL); | |
| 661 | + if (!Not_singleton) return 0; | |
| 662 | + UAdd_type = make_type("UAdd", unaryop_type, NULL, 0); | |
| 663 | + if (!UAdd_type) return 0; | |
| 664 | + UAdd_singleton = PyType_GenericNew(UAdd_type, NULL, NULL); | |
| 665 | + if (!UAdd_singleton) return 0; | |
| 666 | + USub_type = make_type("USub", unaryop_type, NULL, 0); | |
| 667 | + if (!USub_type) return 0; | |
| 668 | + USub_singleton = PyType_GenericNew(USub_type, NULL, NULL); | |
| 669 | + if (!USub_singleton) return 0; | |
| 670 | + cmpop_type = make_type("cmpop", AST_type, NULL, 0); | |
| 671 | + if (!cmpop_type) return 0; | |
| 672 | + if (!add_attributes(cmpop_type, NULL, 0)) return 0; | |
| 673 | + Eq_type = make_type("Eq", cmpop_type, NULL, 0); | |
| 674 | + if (!Eq_type) return 0; | |
| 675 | + Eq_singleton = PyType_GenericNew(Eq_type, NULL, NULL); | |
| 676 | + if (!Eq_singleton) return 0; | |
| 677 | + NotEq_type = make_type("NotEq", cmpop_type, NULL, 0); | |
| 678 | + if (!NotEq_type) return 0; | |
| 679 | + NotEq_singleton = PyType_GenericNew(NotEq_type, NULL, NULL); | |
| 680 | + if (!NotEq_singleton) return 0; | |
| 681 | + Lt_type = make_type("Lt", cmpop_type, NULL, 0); | |
| 682 | + if (!Lt_type) return 0; | |
| 683 | + Lt_singleton = PyType_GenericNew(Lt_type, NULL, NULL); | |
| 684 | + if (!Lt_singleton) return 0; | |
| 685 | + LtE_type = make_type("LtE", cmpop_type, NULL, 0); | |
| 686 | + if (!LtE_type) return 0; | |
| 687 | + LtE_singleton = PyType_GenericNew(LtE_type, NULL, NULL); | |
| 688 | + if (!LtE_singleton) return 0; | |
| 689 | + Gt_type = make_type("Gt", cmpop_type, NULL, 0); | |
| 690 | + if (!Gt_type) return 0; | |
| 691 | + Gt_singleton = PyType_GenericNew(Gt_type, NULL, NULL); | |
| 692 | + if (!Gt_singleton) return 0; | |
| 693 | + GtE_type = make_type("GtE", cmpop_type, NULL, 0); | |
| 694 | + if (!GtE_type) return 0; | |
| 695 | + GtE_singleton = PyType_GenericNew(GtE_type, NULL, NULL); | |
| 696 | + if (!GtE_singleton) return 0; | |
| 697 | + Is_type = make_type("Is", cmpop_type, NULL, 0); | |
| 698 | + if (!Is_type) return 0; | |
| 699 | + Is_singleton = PyType_GenericNew(Is_type, NULL, NULL); | |
| 700 | + if (!Is_singleton) return 0; | |
| 701 | + IsNot_type = make_type("IsNot", cmpop_type, NULL, 0); | |
| 702 | + if (!IsNot_type) return 0; | |
| 703 | + IsNot_singleton = PyType_GenericNew(IsNot_type, NULL, NULL); | |
| 704 | + if (!IsNot_singleton) return 0; | |
| 705 | + In_type = make_type("In", cmpop_type, NULL, 0); | |
| 706 | + if (!In_type) return 0; | |
| 707 | + In_singleton = PyType_GenericNew(In_type, NULL, NULL); | |
| 708 | + if (!In_singleton) return 0; | |
| 709 | + NotIn_type = make_type("NotIn", cmpop_type, NULL, 0); | |
| 710 | + if (!NotIn_type) return 0; | |
| 711 | + NotIn_singleton = PyType_GenericNew(NotIn_type, NULL, NULL); | |
| 712 | + if (!NotIn_singleton) return 0; | |
| 713 | + comprehension_type = make_type("comprehension", AST_type, | |
| 714 | + comprehension_fields, 3); | |
| 715 | + if (!comprehension_type) return 0; | |
| 716 | + excepthandler_type = make_type("excepthandler", AST_type, | |
| 717 | + excepthandler_fields, 5); | |
| 718 | + if (!excepthandler_type) return 0; | |
| 719 | + arguments_type = make_type("arguments", AST_type, arguments_fields, 4); | |
| 720 | + if (!arguments_type) return 0; | |
| 721 | + keyword_type = make_type("keyword", AST_type, keyword_fields, 2); | |
| 722 | + if (!keyword_type) return 0; | |
| 723 | + alias_type = make_type("alias", AST_type, alias_fields, 2); | |
| 724 | + if (!alias_type) return 0; | |
| 725 | + initialized = 1; | |
| 726 | + return 1; | |
| 727 | +} | |
| 728 | + | |
| 729 | +mod_ty | |
| 730 | +Module(asdl_seq * body, PyArena *arena) | |
| 731 | +{ | |
| 732 | + mod_ty p; | |
| 733 | + p = (mod_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 734 | + if (!p) { | |
| 735 | + PyErr_NoMemory(); | |
| 736 | + return NULL; | |
| 737 | + } | |
| 738 | + p->kind = Module_kind; | |
| 739 | + p->v.Module.body = body; | |
| 740 | + return p; | |
| 741 | +} | |
| 742 | + | |
| 743 | +mod_ty | |
| 744 | +Interactive(asdl_seq * body, PyArena *arena) | |
| 745 | +{ | |
| 746 | + mod_ty p; | |
| 747 | + p = (mod_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 748 | + if (!p) { | |
| 749 | + PyErr_NoMemory(); | |
| 750 | + return NULL; | |
| 751 | + } | |
| 752 | + p->kind = Interactive_kind; | |
| 753 | + p->v.Interactive.body = body; | |
| 754 | + return p; | |
| 755 | +} | |
| 756 | + | |
| 757 | +mod_ty | |
| 758 | +Expression(expr_ty body, PyArena *arena) | |
| 759 | +{ | |
| 760 | + mod_ty p; | |
| 761 | + if (!body) { | |
| 762 | + PyErr_SetString(PyExc_ValueError, | |
| 763 | + "field body is required for Expression"); | |
| 764 | + return NULL; | |
| 765 | + } | |
| 766 | + p = (mod_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 767 | + if (!p) { | |
| 768 | + PyErr_NoMemory(); | |
| 769 | + return NULL; | |
| 770 | + } | |
| 771 | + p->kind = Expression_kind; | |
| 772 | + p->v.Expression.body = body; | |
| 773 | + return p; | |
| 774 | +} | |
| 775 | + | |
| 776 | +mod_ty | |
| 777 | +Suite(asdl_seq * body, PyArena *arena) | |
| 778 | +{ | |
| 779 | + mod_ty p; | |
| 780 | + p = (mod_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 781 | + if (!p) { | |
| 782 | + PyErr_NoMemory(); | |
| 783 | + return NULL; | |
| 784 | + } | |
| 785 | + p->kind = Suite_kind; | |
| 786 | + p->v.Suite.body = body; | |
| 787 | + return p; | |
| 788 | +} | |
| 789 | + | |
| 790 | +stmt_ty | |
| 791 | +FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq * | |
| 792 | + decorators, int lineno, int col_offset, PyArena *arena) | |
| 793 | +{ | |
| 794 | + stmt_ty p; | |
| 795 | + if (!name) { | |
| 796 | + PyErr_SetString(PyExc_ValueError, | |
| 797 | + "field name is required for FunctionDef"); | |
| 798 | + return NULL; | |
| 799 | + } | |
| 800 | + if (!args) { | |
| 801 | + PyErr_SetString(PyExc_ValueError, | |
| 802 | + "field args is required for FunctionDef"); | |
| 803 | + return NULL; | |
| 804 | + } | |
| 805 | + p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 806 | + if (!p) { | |
| 807 | + PyErr_NoMemory(); | |
| 808 | + return NULL; | |
| 809 | + } | |
| 810 | + p->kind = FunctionDef_kind; | |
| 811 | + p->v.FunctionDef.name = name; | |
| 812 | + p->v.FunctionDef.args = args; | |
| 813 | + p->v.FunctionDef.body = body; | |
| 814 | + p->v.FunctionDef.decorators = decorators; | |
| 815 | + p->lineno = lineno; | |
| 816 | + p->col_offset = col_offset; | |
| 817 | + return p; | |
| 818 | +} | |
| 819 | + | |
| 820 | +stmt_ty | |
| 821 | +ClassDef(identifier name, asdl_seq * bases, asdl_seq * body, int lineno, int | |
| 822 | + col_offset, PyArena *arena) | |
| 823 | +{ | |
| 824 | + stmt_ty p; | |
| 825 | + if (!name) { | |
| 826 | + PyErr_SetString(PyExc_ValueError, | |
| 827 | + "field name is required for ClassDef"); | |
| 828 | + return NULL; | |
| 829 | + } | |
| 830 | + p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 831 | + if (!p) { | |
| 832 | + PyErr_NoMemory(); | |
| 833 | + return NULL; | |
| 834 | + } | |
| 835 | + p->kind = ClassDef_kind; | |
| 836 | + p->v.ClassDef.name = name; | |
| 837 | + p->v.ClassDef.bases = bases; | |
| 838 | + p->v.ClassDef.body = body; | |
| 839 | + p->lineno = lineno; | |
| 840 | + p->col_offset = col_offset; | |
| 841 | + return p; | |
| 842 | +} | |
| 843 | + | |
| 844 | +stmt_ty | |
| 845 | +Return(expr_ty value, int lineno, int col_offset, PyArena *arena) | |
| 846 | +{ | |
| 847 | + stmt_ty p; | |
| 848 | + p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 849 | + if (!p) { | |
| 850 | + PyErr_NoMemory(); | |
| 851 | + return NULL; | |
| 852 | + } | |
| 853 | + p->kind = Return_kind; | |
| 854 | + p->v.Return.value = value; | |
| 855 | + p->lineno = lineno; | |
| 856 | + p->col_offset = col_offset; | |
| 857 | + return p; | |
| 858 | +} | |
| 859 | + | |
| 860 | +stmt_ty | |
| 861 | +Delete(asdl_seq * targets, int lineno, int col_offset, PyArena *arena) | |
| 862 | +{ | |
| 863 | + stmt_ty p; | |
| 864 | + p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 865 | + if (!p) { | |
| 866 | + PyErr_NoMemory(); | |
| 867 | + return NULL; | |
| 868 | + } | |
| 869 | + p->kind = Delete_kind; | |
| 870 | + p->v.Delete.targets = targets; | |
| 871 | + p->lineno = lineno; | |
| 872 | + p->col_offset = col_offset; | |
| 873 | + return p; | |
| 874 | +} | |
| 875 | + | |
| 876 | +stmt_ty | |
| 877 | +Assign(asdl_seq * targets, expr_ty value, int lineno, int col_offset, PyArena | |
| 878 | + *arena) | |
| 879 | +{ | |
| 880 | + stmt_ty p; | |
| 881 | + if (!value) { | |
| 882 | + PyErr_SetString(PyExc_ValueError, | |
| 883 | + "field value is required for Assign"); | |
| 884 | + return NULL; | |
| 885 | + } | |
| 886 | + p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 887 | + if (!p) { | |
| 888 | + PyErr_NoMemory(); | |
| 889 | + return NULL; | |
| 890 | + } | |
| 891 | + p->kind = Assign_kind; | |
| 892 | + p->v.Assign.targets = targets; | |
| 893 | + p->v.Assign.value = value; | |
| 894 | + p->lineno = lineno; | |
| 895 | + p->col_offset = col_offset; | |
| 896 | + return p; | |
| 897 | +} | |
| 898 | + | |
| 899 | +stmt_ty | |
| 900 | +AugAssign(expr_ty target, operator_ty op, expr_ty value, int lineno, int | |
| 901 | + col_offset, PyArena *arena) | |
| 902 | +{ | |
| 903 | + stmt_ty p; | |
| 904 | + if (!target) { | |
| 905 | + PyErr_SetString(PyExc_ValueError, | |
| 906 | + "field target is required for AugAssign"); | |
| 907 | + return NULL; | |
| 908 | + } | |
| 909 | + if (!op) { | |
| 910 | + PyErr_SetString(PyExc_ValueError, | |
| 911 | + "field op is required for AugAssign"); | |
| 912 | + return NULL; | |
| 913 | + } | |
| 914 | + if (!value) { | |
| 915 | + PyErr_SetString(PyExc_ValueError, | |
| 916 | + "field value is required for AugAssign"); | |
| 917 | + return NULL; | |
| 918 | + } | |
| 919 | + p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 920 | + if (!p) { | |
| 921 | + PyErr_NoMemory(); | |
| 922 | + return NULL; | |
| 923 | + } | |
| 924 | + p->kind = AugAssign_kind; | |
| 925 | + p->v.AugAssign.target = target; | |
| 926 | + p->v.AugAssign.op = op; | |
| 927 | + p->v.AugAssign.value = value; | |
| 928 | + p->lineno = lineno; | |
| 929 | + p->col_offset = col_offset; | |
| 930 | + return p; | |
| 931 | +} | |
| 932 | + | |
| 933 | +stmt_ty | |
| 934 | +Print(expr_ty dest, asdl_seq * values, bool nl, int lineno, int col_offset, | |
| 935 | + PyArena *arena) | |
| 936 | +{ | |
| 937 | + stmt_ty p; | |
| 938 | + p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 939 | + if (!p) { | |
| 940 | + PyErr_NoMemory(); | |
| 941 | + return NULL; | |
| 942 | + } | |
| 943 | + p->kind = Print_kind; | |
| 944 | + p->v.Print.dest = dest; | |
| 945 | + p->v.Print.values = values; | |
| 946 | + p->v.Print.nl = nl; | |
| 947 | + p->lineno = lineno; | |
| 948 | + p->col_offset = col_offset; | |
| 949 | + return p; | |
| 950 | +} | |
| 951 | + | |
| 952 | +stmt_ty | |
| 953 | +For(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int | |
| 954 | + lineno, int col_offset, PyArena *arena) | |
| 955 | +{ | |
| 956 | + stmt_ty p; | |
| 957 | + if (!target) { | |
| 958 | + PyErr_SetString(PyExc_ValueError, | |
| 959 | + "field target is required for For"); | |
| 960 | + return NULL; | |
| 961 | + } | |
| 962 | + if (!iter) { | |
| 963 | + PyErr_SetString(PyExc_ValueError, | |
| 964 | + "field iter is required for For"); | |
| 965 | + return NULL; | |
| 966 | + } | |
| 967 | + p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 968 | + if (!p) { | |
| 969 | + PyErr_NoMemory(); | |
| 970 | + return NULL; | |
| 971 | + } | |
| 972 | + p->kind = For_kind; | |
| 973 | + p->v.For.target = target; | |
| 974 | + p->v.For.iter = iter; | |
| 975 | + p->v.For.body = body; | |
| 976 | + p->v.For.orelse = orelse; | |
| 977 | + p->lineno = lineno; | |
| 978 | + p->col_offset = col_offset; | |
| 979 | + return p; | |
| 980 | +} | |
| 981 | + | |
| 982 | +stmt_ty | |
| 983 | +While(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int | |
| 984 | + col_offset, PyArena *arena) | |
| 985 | +{ | |
| 986 | + stmt_ty p; | |
| 987 | + if (!test) { | |
| 988 | + PyErr_SetString(PyExc_ValueError, | |
| 989 | + "field test is required for While"); | |
| 990 | + return NULL; | |
| 991 | + } | |
| 992 | + p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 993 | + if (!p) { | |
| 994 | + PyErr_NoMemory(); | |
| 995 | + return NULL; | |
| 996 | + } | |
| 997 | + p->kind = While_kind; | |
| 998 | + p->v.While.test = test; | |
| 999 | + p->v.While.body = body; | |
| 1000 | + p->v.While.orelse = orelse; | |
| 1001 | + p->lineno = lineno; | |
| 1002 | + p->col_offset = col_offset; | |
| 1003 | + return p; | |
| 1004 | +} | |
| 1005 | + | |
| 1006 | +stmt_ty | |
| 1007 | +If(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int | |
| 1008 | + col_offset, PyArena *arena) | |
| 1009 | +{ | |
| 1010 | + stmt_ty p; | |
| 1011 | + if (!test) { | |
| 1012 | + PyErr_SetString(PyExc_ValueError, | |
| 1013 | + "field test is required for If"); | |
| 1014 | + return NULL; | |
| 1015 | + } | |
| 1016 | + p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 1017 | + if (!p) { | |
| 1018 | + PyErr_NoMemory(); | |
| 1019 | + return NULL; | |
| 1020 | + } | |
| 1021 | + p->kind = If_kind; | |
| 1022 | + p->v.If.test = test; | |
| 1023 | + p->v.If.body = body; | |
| 1024 | + p->v.If.orelse = orelse; | |
| 1025 | + p->lineno = lineno; | |
| 1026 | + p->col_offset = col_offset; | |
| 1027 | + return p; | |
| 1028 | +} | |
| 1029 | + | |
| 1030 | +stmt_ty | |
| 1031 | +With(expr_ty context_expr, expr_ty optional_vars, asdl_seq * body, int lineno, | |
| 1032 | + int col_offset, PyArena *arena) | |
| 1033 | +{ | |
| 1034 | + stmt_ty p; | |
| 1035 | + if (!context_expr) { | |
| 1036 | + PyErr_SetString(PyExc_ValueError, | |
| 1037 | + "field context_expr is required for With"); | |
| 1038 | + return NULL; | |
| 1039 | + } | |
| 1040 | + p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 1041 | + if (!p) { | |
| 1042 | + PyErr_NoMemory(); | |
| 1043 | + return NULL; | |
| 1044 | + } | |
| 1045 | + p->kind = With_kind; | |
| 1046 | + p->v.With.context_expr = context_expr; | |
| 1047 | + p->v.With.optional_vars = optional_vars; | |
| 1048 | + p->v.With.body = body; | |
| 1049 | + p->lineno = lineno; | |
| 1050 | + p->col_offset = col_offset; | |
| 1051 | + return p; | |
| 1052 | +} | |
| 1053 | + | |
| 1054 | +stmt_ty | |
| 1055 | +Raise(expr_ty type, expr_ty inst, expr_ty tback, int lineno, int col_offset, | |
| 1056 | + PyArena *arena) | |
| 1057 | +{ | |
| 1058 | + stmt_ty p; | |
| 1059 | + p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 1060 | + if (!p) { | |
| 1061 | + PyErr_NoMemory(); | |
| 1062 | + return NULL; | |
| 1063 | + } | |
| 1064 | + p->kind = Raise_kind; | |
| 1065 | + p->v.Raise.type = type; | |
| 1066 | + p->v.Raise.inst = inst; | |
| 1067 | + p->v.Raise.tback = tback; | |
| 1068 | + p->lineno = lineno; | |
| 1069 | + p->col_offset = col_offset; | |
| 1070 | + return p; | |
| 1071 | +} | |
| 1072 | + | |
| 1073 | +stmt_ty | |
| 1074 | +TryExcept(asdl_seq * body, asdl_seq * handlers, asdl_seq * orelse, int lineno, | |
| 1075 | + int col_offset, PyArena *arena) | |
| 1076 | +{ | |
| 1077 | + stmt_ty p; | |
| 1078 | + p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 1079 | + if (!p) { | |
| 1080 | + PyErr_NoMemory(); | |
| 1081 | + return NULL; | |
| 1082 | + } | |
| 1083 | + p->kind = TryExcept_kind; | |
| 1084 | + p->v.TryExcept.body = body; | |
| 1085 | + p->v.TryExcept.handlers = handlers; | |
| 1086 | + p->v.TryExcept.orelse = orelse; | |
| 1087 | + p->lineno = lineno; | |
| 1088 | + p->col_offset = col_offset; | |
| 1089 | + return p; | |
| 1090 | +} | |
| 1091 | + | |
| 1092 | +stmt_ty | |
| 1093 | +TryFinally(asdl_seq * body, asdl_seq * finalbody, int lineno, int col_offset, | |
| 1094 | + PyArena *arena) | |
| 1095 | +{ | |
| 1096 | + stmt_ty p; | |
| 1097 | + p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 1098 | + if (!p) { | |
| 1099 | + PyErr_NoMemory(); | |
| 1100 | + return NULL; | |
| 1101 | + } | |
| 1102 | + p->kind = TryFinally_kind; | |
| 1103 | + p->v.TryFinally.body = body; | |
| 1104 | + p->v.TryFinally.finalbody = finalbody; | |
| 1105 | + p->lineno = lineno; | |
| 1106 | + p->col_offset = col_offset; | |
| 1107 | + return p; | |
| 1108 | +} | |
| 1109 | + | |
| 1110 | +stmt_ty | |
| 1111 | +Assert(expr_ty test, expr_ty msg, int lineno, int col_offset, PyArena *arena) | |
| 1112 | +{ | |
| 1113 | + stmt_ty p; | |
| 1114 | + if (!test) { | |
| 1115 | + PyErr_SetString(PyExc_ValueError, | |
| 1116 | + "field test is required for Assert"); | |
| 1117 | + return NULL; | |
| 1118 | + } | |
| 1119 | + p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 1120 | + if (!p) { | |
| 1121 | + PyErr_NoMemory(); | |
| 1122 | + return NULL; | |
| 1123 | + } | |
| 1124 | + p->kind = Assert_kind; | |
| 1125 | + p->v.Assert.test = test; | |
| 1126 | + p->v.Assert.msg = msg; | |
| 1127 | + p->lineno = lineno; | |
| 1128 | + p->col_offset = col_offset; | |
| 1129 | + return p; | |
| 1130 | +} | |
| 1131 | + | |
| 1132 | +stmt_ty | |
| 1133 | +Import(asdl_seq * names, int lineno, int col_offset, PyArena *arena) | |
| 1134 | +{ | |
| 1135 | + stmt_ty p; | |
| 1136 | + p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 1137 | + if (!p) { | |
| 1138 | + PyErr_NoMemory(); | |
| 1139 | + return NULL; | |
| 1140 | + } | |
| 1141 | + p->kind = Import_kind; | |
| 1142 | + p->v.Import.names = names; | |
| 1143 | + p->lineno = lineno; | |
| 1144 | + p->col_offset = col_offset; | |
| 1145 | + return p; | |
| 1146 | +} | |
| 1147 | + | |
| 1148 | +stmt_ty | |
| 1149 | +ImportFrom(identifier module, asdl_seq * names, int level, int lineno, int | |
| 1150 | + col_offset, PyArena *arena) | |
| 1151 | +{ | |
| 1152 | + stmt_ty p; | |
| 1153 | + if (!module) { | |
| 1154 | + PyErr_SetString(PyExc_ValueError, | |
| 1155 | + "field module is required for ImportFrom"); | |
| 1156 | + return NULL; | |
| 1157 | + } | |
| 1158 | + p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 1159 | + if (!p) { | |
| 1160 | + PyErr_NoMemory(); | |
| 1161 | + return NULL; | |
| 1162 | + } | |
| 1163 | + p->kind = ImportFrom_kind; | |
| 1164 | + p->v.ImportFrom.module = module; | |
| 1165 | + p->v.ImportFrom.names = names; | |
| 1166 | + p->v.ImportFrom.level = level; | |
| 1167 | + p->lineno = lineno; | |
| 1168 | + p->col_offset = col_offset; | |
| 1169 | + return p; | |
| 1170 | +} | |
| 1171 | + | |
| 1172 | +stmt_ty | |
| 1173 | +Exec(expr_ty body, expr_ty globals, expr_ty locals, int lineno, int col_offset, | |
| 1174 | + PyArena *arena) | |
| 1175 | +{ | |
| 1176 | + stmt_ty p; | |
| 1177 | + if (!body) { | |
| 1178 | + PyErr_SetString(PyExc_ValueError, | |
| 1179 | + "field body is required for Exec"); | |
| 1180 | + return NULL; | |
| 1181 | + } | |
| 1182 | + p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 1183 | + if (!p) { | |
| 1184 | + PyErr_NoMemory(); | |
| 1185 | + return NULL; | |
| 1186 | + } | |
| 1187 | + p->kind = Exec_kind; | |
| 1188 | + p->v.Exec.body = body; | |
| 1189 | + p->v.Exec.globals = globals; | |
| 1190 | + p->v.Exec.locals = locals; | |
| 1191 | + p->lineno = lineno; | |
| 1192 | + p->col_offset = col_offset; | |
| 1193 | + return p; | |
| 1194 | +} | |
| 1195 | + | |
| 1196 | +stmt_ty | |
| 1197 | +Global(asdl_seq * names, int lineno, int col_offset, PyArena *arena) | |
| 1198 | +{ | |
| 1199 | + stmt_ty p; | |
| 1200 | + p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 1201 | + if (!p) { | |
| 1202 | + PyErr_NoMemory(); | |
| 1203 | + return NULL; | |
| 1204 | + } | |
| 1205 | + p->kind = Global_kind; | |
| 1206 | + p->v.Global.names = names; | |
| 1207 | + p->lineno = lineno; | |
| 1208 | + p->col_offset = col_offset; | |
| 1209 | + return p; | |
| 1210 | +} | |
| 1211 | + | |
| 1212 | +stmt_ty | |
| 1213 | +Expr(expr_ty value, int lineno, int col_offset, PyArena *arena) | |
| 1214 | +{ | |
| 1215 | + stmt_ty p; | |
| 1216 | + if (!value) { | |
| 1217 | + PyErr_SetString(PyExc_ValueError, | |
| 1218 | + "field value is required for Expr"); | |
| 1219 | + return NULL; | |
| 1220 | + } | |
| 1221 | + p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 1222 | + if (!p) { | |
| 1223 | + PyErr_NoMemory(); | |
| 1224 | + return NULL; | |
| 1225 | + } | |
| 1226 | + p->kind = Expr_kind; | |
| 1227 | + p->v.Expr.value = value; | |
| 1228 | + p->lineno = lineno; | |
| 1229 | + p->col_offset = col_offset; | |
| 1230 | + return p; | |
| 1231 | +} | |
| 1232 | + | |
| 1233 | +stmt_ty | |
| 1234 | +Pass(int lineno, int col_offset, PyArena *arena) | |
| 1235 | +{ | |
| 1236 | + stmt_ty p; | |
| 1237 | + p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 1238 | + if (!p) { | |
| 1239 | + PyErr_NoMemory(); | |
| 1240 | + return NULL; | |
| 1241 | + } | |
| 1242 | + p->kind = Pass_kind; | |
| 1243 | + p->lineno = lineno; | |
| 1244 | + p->col_offset = col_offset; | |
| 1245 | + return p; | |
| 1246 | +} | |
| 1247 | + | |
| 1248 | +stmt_ty | |
| 1249 | +Break(int lineno, int col_offset, PyArena *arena) | |
| 1250 | +{ | |
| 1251 | + stmt_ty p; | |
| 1252 | + p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 1253 | + if (!p) { | |
| 1254 | + PyErr_NoMemory(); | |
| 1255 | + return NULL; | |
| 1256 | + } | |
| 1257 | + p->kind = Break_kind; | |
| 1258 | + p->lineno = lineno; | |
| 1259 | + p->col_offset = col_offset; | |
| 1260 | + return p; | |
| 1261 | +} | |
| 1262 | + | |
| 1263 | +stmt_ty | |
| 1264 | +Continue(int lineno, int col_offset, PyArena *arena) | |
| 1265 | +{ | |
| 1266 | + stmt_ty p; | |
| 1267 | + p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 1268 | + if (!p) { | |
| 1269 | + PyErr_NoMemory(); | |
| 1270 | + return NULL; | |
| 1271 | + } | |
| 1272 | + p->kind = Continue_kind; | |
| 1273 | + p->lineno = lineno; | |
| 1274 | + p->col_offset = col_offset; | |
| 1275 | + return p; | |
| 1276 | +} | |
| 1277 | + | |
| 1278 | +expr_ty | |
| 1279 | +BoolOp(boolop_ty op, asdl_seq * values, int lineno, int col_offset, PyArena | |
| 1280 | + *arena) | |
| 1281 | +{ | |
| 1282 | + expr_ty p; | |
| 1283 | + if (!op) { | |
| 1284 | + PyErr_SetString(PyExc_ValueError, | |
| 1285 | + "field op is required for BoolOp"); | |
| 1286 | + return NULL; | |
| 1287 | + } | |
| 1288 | + p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 1289 | + if (!p) { | |
| 1290 | + PyErr_NoMemory(); | |
| 1291 | + return NULL; | |
| 1292 | + } | |
| 1293 | + p->kind = BoolOp_kind; | |
| 1294 | + p->v.BoolOp.op = op; | |
| 1295 | + p->v.BoolOp.values = values; | |
| 1296 | + p->lineno = lineno; | |
| 1297 | + p->col_offset = col_offset; | |
| 1298 | + return p; | |
| 1299 | +} | |
| 1300 | + | |
| 1301 | +expr_ty | |
| 1302 | +BinOp(expr_ty left, operator_ty op, expr_ty right, int lineno, int col_offset, | |
| 1303 | + PyArena *arena) | |
| 1304 | +{ | |
| 1305 | + expr_ty p; | |
| 1306 | + if (!left) { | |
| 1307 | + PyErr_SetString(PyExc_ValueError, | |
| 1308 | + "field left is required for BinOp"); | |
| 1309 | + return NULL; | |
| 1310 | + } | |
| 1311 | + if (!op) { | |
| 1312 | + PyErr_SetString(PyExc_ValueError, | |
| 1313 | + "field op is required for BinOp"); | |
| 1314 | + return NULL; | |
| 1315 | + } | |
| 1316 | + if (!right) { | |
| 1317 | + PyErr_SetString(PyExc_ValueError, | |
| 1318 | + "field right is required for BinOp"); | |
| 1319 | + return NULL; | |
| 1320 | + } | |
| 1321 | + p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 1322 | + if (!p) { | |
| 1323 | + PyErr_NoMemory(); | |
| 1324 | + return NULL; | |
| 1325 | + } | |
| 1326 | + p->kind = BinOp_kind; | |
| 1327 | + p->v.BinOp.left = left; | |
| 1328 | + p->v.BinOp.op = op; | |
| 1329 | + p->v.BinOp.right = right; | |
| 1330 | + p->lineno = lineno; | |
| 1331 | + p->col_offset = col_offset; | |
| 1332 | + return p; | |
| 1333 | +} | |
| 1334 | + | |
| 1335 | +expr_ty | |
| 1336 | +UnaryOp(unaryop_ty op, expr_ty operand, int lineno, int col_offset, PyArena | |
| 1337 | + *arena) | |
| 1338 | +{ | |
| 1339 | + expr_ty p; | |
| 1340 | + if (!op) { | |
| 1341 | + PyErr_SetString(PyExc_ValueError, | |
| 1342 | + "field op is required for UnaryOp"); | |
| 1343 | + return NULL; | |
| 1344 | + } | |
| 1345 | + if (!operand) { | |
| 1346 | + PyErr_SetString(PyExc_ValueError, | |
| 1347 | + "field operand is required for UnaryOp"); | |
| 1348 | + return NULL; | |
| 1349 | + } | |
| 1350 | + p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 1351 | + if (!p) { | |
| 1352 | + PyErr_NoMemory(); | |
| 1353 | + return NULL; | |
| 1354 | + } | |
| 1355 | + p->kind = UnaryOp_kind; | |
| 1356 | + p->v.UnaryOp.op = op; | |
| 1357 | + p->v.UnaryOp.operand = operand; | |
| 1358 | + p->lineno = lineno; | |
| 1359 | + p->col_offset = col_offset; | |
| 1360 | + return p; | |
| 1361 | +} | |
| 1362 | + | |
| 1363 | +expr_ty | |
| 1364 | +Lambda(arguments_ty args, expr_ty body, int lineno, int col_offset, PyArena | |
| 1365 | + *arena) | |
| 1366 | +{ | |
| 1367 | + expr_ty p; | |
| 1368 | + if (!args) { | |
| 1369 | + PyErr_SetString(PyExc_ValueError, | |
| 1370 | + "field args is required for Lambda"); | |
| 1371 | + return NULL; | |
| 1372 | + } | |
| 1373 | + if (!body) { | |
| 1374 | + PyErr_SetString(PyExc_ValueError, | |
| 1375 | + "field body is required for Lambda"); | |
| 1376 | + return NULL; | |
| 1377 | + } | |
| 1378 | + p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 1379 | + if (!p) { | |
| 1380 | + PyErr_NoMemory(); | |
| 1381 | + return NULL; | |
| 1382 | + } | |
| 1383 | + p->kind = Lambda_kind; | |
| 1384 | + p->v.Lambda.args = args; | |
| 1385 | + p->v.Lambda.body = body; | |
| 1386 | + p->lineno = lineno; | |
| 1387 | + p->col_offset = col_offset; | |
| 1388 | + return p; | |
| 1389 | +} | |
| 1390 | + | |
| 1391 | +expr_ty | |
| 1392 | +IfExp(expr_ty test, expr_ty body, expr_ty orelse, int lineno, int col_offset, | |
| 1393 | + PyArena *arena) | |
| 1394 | +{ | |
| 1395 | + expr_ty p; | |
| 1396 | + if (!test) { | |
| 1397 | + PyErr_SetString(PyExc_ValueError, | |
| 1398 | + "field test is required for IfExp"); | |
| 1399 | + return NULL; | |
| 1400 | + } | |
| 1401 | + if (!body) { | |
| 1402 | + PyErr_SetString(PyExc_ValueError, | |
| 1403 | + "field body is required for IfExp"); | |
| 1404 | + return NULL; | |
| 1405 | + } | |
| 1406 | + if (!orelse) { | |
| 1407 | + PyErr_SetString(PyExc_ValueError, | |
| 1408 | + "field orelse is required for IfExp"); | |
| 1409 | + return NULL; | |
| 1410 | + } | |
| 1411 | + p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 1412 | + if (!p) { | |
| 1413 | + PyErr_NoMemory(); | |
| 1414 | + return NULL; | |
| 1415 | + } | |
| 1416 | + p->kind = IfExp_kind; | |
| 1417 | + p->v.IfExp.test = test; | |
| 1418 | + p->v.IfExp.body = body; | |
| 1419 | + p->v.IfExp.orelse = orelse; | |
| 1420 | + p->lineno = lineno; | |
| 1421 | + p->col_offset = col_offset; | |
| 1422 | + return p; | |
| 1423 | +} | |
| 1424 | + | |
| 1425 | +expr_ty | |
| 1426 | +Dict(asdl_seq * keys, asdl_seq * values, int lineno, int col_offset, PyArena | |
| 1427 | + *arena) | |
| 1428 | +{ | |
| 1429 | + expr_ty p; | |
| 1430 | + p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 1431 | + if (!p) { | |
| 1432 | + PyErr_NoMemory(); | |
| 1433 | + return NULL; | |
| 1434 | + } | |
| 1435 | + p->kind = Dict_kind; | |
| 1436 | + p->v.Dict.keys = keys; | |
| 1437 | + p->v.Dict.values = values; | |
| 1438 | + p->lineno = lineno; | |
| 1439 | + p->col_offset = col_offset; | |
| 1440 | + return p; | |
| 1441 | +} | |
| 1442 | + | |
| 1443 | +expr_ty | |
| 1444 | +ListComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset, | |
| 1445 | + PyArena *arena) | |
| 1446 | +{ | |
| 1447 | + expr_ty p; | |
| 1448 | + if (!elt) { | |
| 1449 | + PyErr_SetString(PyExc_ValueError, | |
| 1450 | + "field elt is required for ListComp"); | |
| 1451 | + return NULL; | |
| 1452 | + } | |
| 1453 | + p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 1454 | + if (!p) { | |
| 1455 | + PyErr_NoMemory(); | |
| 1456 | + return NULL; | |
| 1457 | + } | |
| 1458 | + p->kind = ListComp_kind; | |
| 1459 | + p->v.ListComp.elt = elt; | |
| 1460 | + p->v.ListComp.generators = generators; | |
| 1461 | + p->lineno = lineno; | |
| 1462 | + p->col_offset = col_offset; | |
| 1463 | + return p; | |
| 1464 | +} | |
| 1465 | + | |
| 1466 | +expr_ty | |
| 1467 | +GeneratorExp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset, | |
| 1468 | + PyArena *arena) | |
| 1469 | +{ | |
| 1470 | + expr_ty p; | |
| 1471 | + if (!elt) { | |
| 1472 | + PyErr_SetString(PyExc_ValueError, | |
| 1473 | + "field elt is required for GeneratorExp"); | |
| 1474 | + return NULL; | |
| 1475 | + } | |
| 1476 | + p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 1477 | + if (!p) { | |
| 1478 | + PyErr_NoMemory(); | |
| 1479 | + return NULL; | |
| 1480 | + } | |
| 1481 | + p->kind = GeneratorExp_kind; | |
| 1482 | + p->v.GeneratorExp.elt = elt; | |
| 1483 | + p->v.GeneratorExp.generators = generators; | |
| 1484 | + p->lineno = lineno; | |
| 1485 | + p->col_offset = col_offset; | |
| 1486 | + return p; | |
| 1487 | +} | |
| 1488 | + | |
| 1489 | +expr_ty | |
| 1490 | +Yield(expr_ty value, int lineno, int col_offset, PyArena *arena) | |
| 1491 | +{ | |
| 1492 | + expr_ty p; | |
| 1493 | + p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 1494 | + if (!p) { | |
| 1495 | + PyErr_NoMemory(); | |
| 1496 | + return NULL; | |
| 1497 | + } | |
| 1498 | + p->kind = Yield_kind; | |
| 1499 | + p->v.Yield.value = value; | |
| 1500 | + p->lineno = lineno; | |
| 1501 | + p->col_offset = col_offset; | |
| 1502 | + return p; | |
| 1503 | +} | |
| 1504 | + | |
| 1505 | +expr_ty | |
| 1506 | +Compare(expr_ty left, asdl_int_seq * ops, asdl_seq * comparators, int lineno, | |
| 1507 | + int col_offset, PyArena *arena) | |
| 1508 | +{ | |
| 1509 | + expr_ty p; | |
| 1510 | + if (!left) { | |
| 1511 | + PyErr_SetString(PyExc_ValueError, | |
| 1512 | + "field left is required for Compare"); | |
| 1513 | + return NULL; | |
| 1514 | + } | |
| 1515 | + p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 1516 | + if (!p) { | |
| 1517 | + PyErr_NoMemory(); | |
| 1518 | + return NULL; | |
| 1519 | + } | |
| 1520 | + p->kind = Compare_kind; | |
| 1521 | + p->v.Compare.left = left; | |
| 1522 | + p->v.Compare.ops = ops; | |
| 1523 | + p->v.Compare.comparators = comparators; | |
| 1524 | + p->lineno = lineno; | |
| 1525 | + p->col_offset = col_offset; | |
| 1526 | + return p; | |
| 1527 | +} | |
| 1528 | + | |
| 1529 | +expr_ty | |
| 1530 | +Call(expr_ty func, asdl_seq * args, asdl_seq * keywords, expr_ty starargs, | |
| 1531 | + expr_ty kwargs, int lineno, int col_offset, PyArena *arena) | |
| 1532 | +{ | |
| 1533 | + expr_ty p; | |
| 1534 | + if (!func) { | |
| 1535 | + PyErr_SetString(PyExc_ValueError, | |
| 1536 | + "field func is required for Call"); | |
| 1537 | + return NULL; | |
| 1538 | + } | |
| 1539 | + p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 1540 | + if (!p) { | |
| 1541 | + PyErr_NoMemory(); | |
| 1542 | + return NULL; | |
| 1543 | + } | |
| 1544 | + p->kind = Call_kind; | |
| 1545 | + p->v.Call.func = func; | |
| 1546 | + p->v.Call.args = args; | |
| 1547 | + p->v.Call.keywords = keywords; | |
| 1548 | + p->v.Call.starargs = starargs; | |
| 1549 | + p->v.Call.kwargs = kwargs; | |
| 1550 | + p->lineno = lineno; | |
| 1551 | + p->col_offset = col_offset; | |
| 1552 | + return p; | |
| 1553 | +} | |
| 1554 | + | |
| 1555 | +expr_ty | |
| 1556 | +Repr(expr_ty value, int lineno, int col_offset, PyArena *arena) | |
| 1557 | +{ | |
| 1558 | + expr_ty p; | |
| 1559 | + if (!value) { | |
| 1560 | + PyErr_SetString(PyExc_ValueError, | |
| 1561 | + "field value is required for Repr"); | |
| 1562 | + return NULL; | |
| 1563 | + } | |
| 1564 | + p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 1565 | + if (!p) { | |
| 1566 | + PyErr_NoMemory(); | |
| 1567 | + return NULL; | |
| 1568 | + } | |
| 1569 | + p->kind = Repr_kind; | |
| 1570 | + p->v.Repr.value = value; | |
| 1571 | + p->lineno = lineno; | |
| 1572 | + p->col_offset = col_offset; | |
| 1573 | + return p; | |
| 1574 | +} | |
| 1575 | + | |
| 1576 | +expr_ty | |
| 1577 | +Num(object n, int lineno, int col_offset, PyArena *arena) | |
| 1578 | +{ | |
| 1579 | + expr_ty p; | |
| 1580 | + if (!n) { | |
| 1581 | + PyErr_SetString(PyExc_ValueError, | |
| 1582 | + "field n is required for Num"); | |
| 1583 | + return NULL; | |
| 1584 | + } | |
| 1585 | + p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 1586 | + if (!p) { | |
| 1587 | + PyErr_NoMemory(); | |
| 1588 | + return NULL; | |
| 1589 | + } | |
| 1590 | + p->kind = Num_kind; | |
| 1591 | + p->v.Num.n = n; | |
| 1592 | + p->lineno = lineno; | |
| 1593 | + p->col_offset = col_offset; | |
| 1594 | + return p; | |
| 1595 | +} | |
| 1596 | + | |
| 1597 | +expr_ty | |
| 1598 | +Str(string s, int lineno, int col_offset, PyArena *arena) | |
| 1599 | +{ | |
| 1600 | + expr_ty p; | |
| 1601 | + if (!s) { | |
| 1602 | + PyErr_SetString(PyExc_ValueError, | |
| 1603 | + "field s is required for Str"); | |
| 1604 | + return NULL; | |
| 1605 | + } | |
| 1606 | + p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 1607 | + if (!p) { | |
| 1608 | + PyErr_NoMemory(); | |
| 1609 | + return NULL; | |
| 1610 | + } | |
| 1611 | + p->kind = Str_kind; | |
| 1612 | + p->v.Str.s = s; | |
| 1613 | + p->lineno = lineno; | |
| 1614 | + p->col_offset = col_offset; | |
| 1615 | + return p; | |
| 1616 | +} | |
| 1617 | + | |
| 1618 | +expr_ty | |
| 1619 | +Attribute(expr_ty value, identifier attr, expr_context_ty ctx, int lineno, int | |
| 1620 | + col_offset, PyArena *arena) | |
| 1621 | +{ | |
| 1622 | + expr_ty p; | |
| 1623 | + if (!value) { | |
| 1624 | + PyErr_SetString(PyExc_ValueError, | |
| 1625 | + "field value is required for Attribute"); | |
| 1626 | + return NULL; | |
| 1627 | + } | |
| 1628 | + if (!attr) { | |
| 1629 | + PyErr_SetString(PyExc_ValueError, | |
| 1630 | + "field attr is required for Attribute"); | |
| 1631 | + return NULL; | |
| 1632 | + } | |
| 1633 | + if (!ctx) { | |
| 1634 | + PyErr_SetString(PyExc_ValueError, | |
| 1635 | + "field ctx is required for Attribute"); | |
| 1636 | + return NULL; | |
| 1637 | + } | |
| 1638 | + p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 1639 | + if (!p) { | |
| 1640 | + PyErr_NoMemory(); | |
| 1641 | + return NULL; | |
| 1642 | + } | |
| 1643 | + p->kind = Attribute_kind; | |
| 1644 | + p->v.Attribute.value = value; | |
| 1645 | + p->v.Attribute.attr = attr; | |
| 1646 | + p->v.Attribute.ctx = ctx; | |
| 1647 | + p->lineno = lineno; | |
| 1648 | + p->col_offset = col_offset; | |
| 1649 | + return p; | |
| 1650 | +} | |
| 1651 | + | |
| 1652 | +expr_ty | |
| 1653 | +Subscript(expr_ty value, slice_ty slice, expr_context_ty ctx, int lineno, int | |
| 1654 | + col_offset, PyArena *arena) | |
| 1655 | +{ | |
| 1656 | + expr_ty p; | |
| 1657 | + if (!value) { | |
| 1658 | + PyErr_SetString(PyExc_ValueError, | |
| 1659 | + "field value is required for Subscript"); | |
| 1660 | + return NULL; | |
| 1661 | + } | |
| 1662 | + if (!slice) { | |
| 1663 | + PyErr_SetString(PyExc_ValueError, | |
| 1664 | + "field slice is required for Subscript"); | |
| 1665 | + return NULL; | |
| 1666 | + } | |
| 1667 | + if (!ctx) { | |
| 1668 | + PyErr_SetString(PyExc_ValueError, | |
| 1669 | + "field ctx is required for Subscript"); | |
| 1670 | + return NULL; | |
| 1671 | + } | |
| 1672 | + p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 1673 | + if (!p) { | |
| 1674 | + PyErr_NoMemory(); | |
| 1675 | + return NULL; | |
| 1676 | + } | |
| 1677 | + p->kind = Subscript_kind; | |
| 1678 | + p->v.Subscript.value = value; | |
| 1679 | + p->v.Subscript.slice = slice; | |
| 1680 | + p->v.Subscript.ctx = ctx; | |
| 1681 | + p->lineno = lineno; | |
| 1682 | + p->col_offset = col_offset; | |
| 1683 | + return p; | |
| 1684 | +} | |
| 1685 | + | |
| 1686 | +expr_ty | |
| 1687 | +Name(identifier id, expr_context_ty ctx, int lineno, int col_offset, PyArena | |
| 1688 | + *arena) | |
| 1689 | +{ | |
| 1690 | + expr_ty p; | |
| 1691 | + if (!id) { | |
| 1692 | + PyErr_SetString(PyExc_ValueError, | |
| 1693 | + "field id is required for Name"); | |
| 1694 | + return NULL; | |
| 1695 | + } | |
| 1696 | + if (!ctx) { | |
| 1697 | + PyErr_SetString(PyExc_ValueError, | |
| 1698 | + "field ctx is required for Name"); | |
| 1699 | + return NULL; | |
| 1700 | + } | |
| 1701 | + p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 1702 | + if (!p) { | |
| 1703 | + PyErr_NoMemory(); | |
| 1704 | + return NULL; | |
| 1705 | + } | |
| 1706 | + p->kind = Name_kind; | |
| 1707 | + p->v.Name.id = id; | |
| 1708 | + p->v.Name.ctx = ctx; | |
| 1709 | + p->lineno = lineno; | |
| 1710 | + p->col_offset = col_offset; | |
| 1711 | + return p; | |
| 1712 | +} | |
| 1713 | + | |
| 1714 | +expr_ty | |
| 1715 | +List(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena | |
| 1716 | + *arena) | |
| 1717 | +{ | |
| 1718 | + expr_ty p; | |
| 1719 | + if (!ctx) { | |
| 1720 | + PyErr_SetString(PyExc_ValueError, | |
| 1721 | + "field ctx is required for List"); | |
| 1722 | + return NULL; | |
| 1723 | + } | |
| 1724 | + p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 1725 | + if (!p) { | |
| 1726 | + PyErr_NoMemory(); | |
| 1727 | + return NULL; | |
| 1728 | + } | |
| 1729 | + p->kind = List_kind; | |
| 1730 | + p->v.List.elts = elts; | |
| 1731 | + p->v.List.ctx = ctx; | |
| 1732 | + p->lineno = lineno; | |
| 1733 | + p->col_offset = col_offset; | |
| 1734 | + return p; | |
| 1735 | +} | |
| 1736 | + | |
| 1737 | +expr_ty | |
| 1738 | +Tuple(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena | |
| 1739 | + *arena) | |
| 1740 | +{ | |
| 1741 | + expr_ty p; | |
| 1742 | + if (!ctx) { | |
| 1743 | + PyErr_SetString(PyExc_ValueError, | |
| 1744 | + "field ctx is required for Tuple"); | |
| 1745 | + return NULL; | |
| 1746 | + } | |
| 1747 | + p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 1748 | + if (!p) { | |
| 1749 | + PyErr_NoMemory(); | |
| 1750 | + return NULL; | |
| 1751 | + } | |
| 1752 | + p->kind = Tuple_kind; | |
| 1753 | + p->v.Tuple.elts = elts; | |
| 1754 | + p->v.Tuple.ctx = ctx; | |
| 1755 | + p->lineno = lineno; | |
| 1756 | + p->col_offset = col_offset; | |
| 1757 | + return p; | |
| 1758 | +} | |
| 1759 | + | |
| 1760 | +slice_ty | |
| 1761 | +Ellipsis(PyArena *arena) | |
| 1762 | +{ | |
| 1763 | + slice_ty p; | |
| 1764 | + p = (slice_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 1765 | + if (!p) { | |
| 1766 | + PyErr_NoMemory(); | |
| 1767 | + return NULL; | |
| 1768 | + } | |
| 1769 | + p->kind = Ellipsis_kind; | |
| 1770 | + return p; | |
| 1771 | +} | |
| 1772 | + | |
| 1773 | +slice_ty | |
| 1774 | +Slice(expr_ty lower, expr_ty upper, expr_ty step, PyArena *arena) | |
| 1775 | +{ | |
| 1776 | + slice_ty p; | |
| 1777 | + p = (slice_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 1778 | + if (!p) { | |
| 1779 | + PyErr_NoMemory(); | |
| 1780 | + return NULL; | |
| 1781 | + } | |
| 1782 | + p->kind = Slice_kind; | |
| 1783 | + p->v.Slice.lower = lower; | |
| 1784 | + p->v.Slice.upper = upper; | |
| 1785 | + p->v.Slice.step = step; | |
| 1786 | + return p; | |
| 1787 | +} | |
| 1788 | + | |
| 1789 | +slice_ty | |
| 1790 | +ExtSlice(asdl_seq * dims, PyArena *arena) | |
| 1791 | +{ | |
| 1792 | + slice_ty p; | |
| 1793 | + p = (slice_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 1794 | + if (!p) { | |
| 1795 | + PyErr_NoMemory(); | |
| 1796 | + return NULL; | |
| 1797 | + } | |
| 1798 | + p->kind = ExtSlice_kind; | |
| 1799 | + p->v.ExtSlice.dims = dims; | |
| 1800 | + return p; | |
| 1801 | +} | |
| 1802 | + | |
| 1803 | +slice_ty | |
| 1804 | +Index(expr_ty value, PyArena *arena) | |
| 1805 | +{ | |
| 1806 | + slice_ty p; | |
| 1807 | + if (!value) { | |
| 1808 | + PyErr_SetString(PyExc_ValueError, | |
| 1809 | + "field value is required for Index"); | |
| 1810 | + return NULL; | |
| 1811 | + } | |
| 1812 | + p = (slice_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 1813 | + if (!p) { | |
| 1814 | + PyErr_NoMemory(); | |
| 1815 | + return NULL; | |
| 1816 | + } | |
| 1817 | + p->kind = Index_kind; | |
| 1818 | + p->v.Index.value = value; | |
| 1819 | + return p; | |
| 1820 | +} | |
| 1821 | + | |
| 1822 | +comprehension_ty | |
| 1823 | +comprehension(expr_ty target, expr_ty iter, asdl_seq * ifs, PyArena *arena) | |
| 1824 | +{ | |
| 1825 | + comprehension_ty p; | |
| 1826 | + if (!target) { | |
| 1827 | + PyErr_SetString(PyExc_ValueError, | |
| 1828 | + "field target is required for comprehension"); | |
| 1829 | + return NULL; | |
| 1830 | + } | |
| 1831 | + if (!iter) { | |
| 1832 | + PyErr_SetString(PyExc_ValueError, | |
| 1833 | + "field iter is required for comprehension"); | |
| 1834 | + return NULL; | |
| 1835 | + } | |
| 1836 | + p = (comprehension_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 1837 | + if (!p) { | |
| 1838 | + PyErr_NoMemory(); | |
| 1839 | + return NULL; | |
| 1840 | + } | |
| 1841 | + p->target = target; | |
| 1842 | + p->iter = iter; | |
| 1843 | + p->ifs = ifs; | |
| 1844 | + return p; | |
| 1845 | +} | |
| 1846 | + | |
| 1847 | +excepthandler_ty | |
| 1848 | +excepthandler(expr_ty type, expr_ty name, asdl_seq * body, int lineno, int | |
| 1849 | + col_offset, PyArena *arena) | |
| 1850 | +{ | |
| 1851 | + excepthandler_ty p; | |
| 1852 | + p = (excepthandler_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 1853 | + if (!p) { | |
| 1854 | + PyErr_NoMemory(); | |
| 1855 | + return NULL; | |
| 1856 | + } | |
| 1857 | + p->type = type; | |
| 1858 | + p->name = name; | |
| 1859 | + p->body = body; | |
| 1860 | + p->lineno = lineno; | |
| 1861 | + p->col_offset = col_offset; | |
| 1862 | + return p; | |
| 1863 | +} | |
| 1864 | + | |
| 1865 | +arguments_ty | |
| 1866 | +arguments(asdl_seq * args, identifier vararg, identifier kwarg, asdl_seq * | |
| 1867 | + defaults, PyArena *arena) | |
| 1868 | +{ | |
| 1869 | + arguments_ty p; | |
| 1870 | + p = (arguments_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 1871 | + if (!p) { | |
| 1872 | + PyErr_NoMemory(); | |
| 1873 | + return NULL; | |
| 1874 | + } | |
| 1875 | + p->args = args; | |
| 1876 | + p->vararg = vararg; | |
| 1877 | + p->kwarg = kwarg; | |
| 1878 | + p->defaults = defaults; | |
| 1879 | + return p; | |
| 1880 | +} | |
| 1881 | + | |
| 1882 | +keyword_ty | |
| 1883 | +keyword(identifier arg, expr_ty value, PyArena *arena) | |
| 1884 | +{ | |
| 1885 | + keyword_ty p; | |
| 1886 | + if (!arg) { | |
| 1887 | + PyErr_SetString(PyExc_ValueError, | |
| 1888 | + "field arg is required for keyword"); | |
| 1889 | + return NULL; | |
| 1890 | + } | |
| 1891 | + if (!value) { | |
| 1892 | + PyErr_SetString(PyExc_ValueError, | |
| 1893 | + "field value is required for keyword"); | |
| 1894 | + return NULL; | |
| 1895 | + } | |
| 1896 | + p = (keyword_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 1897 | + if (!p) { | |
| 1898 | + PyErr_NoMemory(); | |
| 1899 | + return NULL; | |
| 1900 | + } | |
| 1901 | + p->arg = arg; | |
| 1902 | + p->value = value; | |
| 1903 | + return p; | |
| 1904 | +} | |
| 1905 | + | |
| 1906 | +alias_ty | |
| 1907 | +alias(identifier name, identifier asname, PyArena *arena) | |
| 1908 | +{ | |
| 1909 | + alias_ty p; | |
| 1910 | + if (!name) { | |
| 1911 | + PyErr_SetString(PyExc_ValueError, | |
| 1912 | + "field name is required for alias"); | |
| 1913 | + return NULL; | |
| 1914 | + } | |
| 1915 | + p = (alias_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 1916 | + if (!p) { | |
| 1917 | + PyErr_NoMemory(); | |
| 1918 | + return NULL; | |
| 1919 | + } | |
| 1920 | + p->name = name; | |
| 1921 | + p->asname = asname; | |
| 1922 | + return p; | |
| 1923 | +} | |
| 1924 | + | |
| 1925 | + | |
| 1926 | +PyObject* | |
| 1927 | +ast2obj_mod(void* _o) | |
| 1928 | +{ | |
| 1929 | + mod_ty o = (mod_ty)_o; | |
| 1930 | + PyObject *result = NULL, *value = NULL; | |
| 1931 | + if (!o) { | |
| 1932 | + Py_INCREF(Py_None); | |
| 1933 | + return Py_None; | |
| 1934 | + } | |
| 1935 | + | |
| 1936 | + switch (o->kind) { | |
| 1937 | + case Module_kind: | |
| 1938 | + result = PyType_GenericNew(Module_type, NULL, NULL); | |
| 1939 | + if (!result) goto failed; | |
| 1940 | + value = ast2obj_list(o->v.Module.body, ast2obj_stmt); | |
| 1941 | + if (!value) goto failed; | |
| 1942 | + if (PyObject_SetAttrString(result, "body", value) == -1) | |
| 1943 | + goto failed; | |
| 1944 | + Py_DECREF(value); | |
| 1945 | + break; | |
| 1946 | + case Interactive_kind: | |
| 1947 | + result = PyType_GenericNew(Interactive_type, NULL, NULL); | |
| 1948 | + if (!result) goto failed; | |
| 1949 | + value = ast2obj_list(o->v.Interactive.body, ast2obj_stmt); | |
| 1950 | + if (!value) goto failed; | |
| 1951 | + if (PyObject_SetAttrString(result, "body", value) == -1) | |
| 1952 | + goto failed; | |
| 1953 | + Py_DECREF(value); | |
| 1954 | + break; | |
| 1955 | + case Expression_kind: | |
| 1956 | + result = PyType_GenericNew(Expression_type, NULL, NULL); | |
| 1957 | + if (!result) goto failed; | |
| 1958 | + value = ast2obj_expr(o->v.Expression.body); | |
| 1959 | + if (!value) goto failed; | |
| 1960 | + if (PyObject_SetAttrString(result, "body", value) == -1) | |
| 1961 | + goto failed; | |
| 1962 | + Py_DECREF(value); | |
| 1963 | + break; | |
| 1964 | + case Suite_kind: | |
| 1965 | + result = PyType_GenericNew(Suite_type, NULL, NULL); | |
| 1966 | + if (!result) goto failed; | |
| 1967 | + value = ast2obj_list(o->v.Suite.body, ast2obj_stmt); | |
| 1968 | + if (!value) goto failed; | |
| 1969 | + if (PyObject_SetAttrString(result, "body", value) == -1) | |
| 1970 | + goto failed; | |
| 1971 | + Py_DECREF(value); | |
| 1972 | + break; | |
| 1973 | + } | |
| 1974 | + return result; | |
| 1975 | +failed: | |
| 1976 | + Py_XDECREF(value); | |
| 1977 | + Py_XDECREF(result); | |
| 1978 | + return NULL; | |
| 1979 | +} | |
| 1980 | + | |
| 1981 | +PyObject* | |
| 1982 | +ast2obj_stmt(void* _o) | |
| 1983 | +{ | |
| 1984 | + stmt_ty o = (stmt_ty)_o; | |
| 1985 | + PyObject *result = NULL, *value = NULL; | |
| 1986 | + if (!o) { | |
| 1987 | + Py_INCREF(Py_None); | |
| 1988 | + return Py_None; | |
| 1989 | + } | |
| 1990 | + | |
| 1991 | + switch (o->kind) { | |
| 1992 | + case FunctionDef_kind: | |
| 1993 | + result = PyType_GenericNew(FunctionDef_type, NULL, NULL); | |
| 1994 | + if (!result) goto failed; | |
| 1995 | + value = ast2obj_identifier(o->v.FunctionDef.name); | |
| 1996 | + if (!value) goto failed; | |
| 1997 | + if (PyObject_SetAttrString(result, "name", value) == -1) | |
| 1998 | + goto failed; | |
| 1999 | + Py_DECREF(value); | |
| 2000 | + value = ast2obj_arguments(o->v.FunctionDef.args); | |
| 2001 | + if (!value) goto failed; | |
| 2002 | + if (PyObject_SetAttrString(result, "args", value) == -1) | |
| 2003 | + goto failed; | |
| 2004 | + Py_DECREF(value); | |
| 2005 | + value = ast2obj_list(o->v.FunctionDef.body, ast2obj_stmt); | |
| 2006 | + if (!value) goto failed; | |
| 2007 | + if (PyObject_SetAttrString(result, "body", value) == -1) | |
| 2008 | + goto failed; | |
| 2009 | + Py_DECREF(value); | |
| 2010 | + value = ast2obj_list(o->v.FunctionDef.decorators, ast2obj_expr); | |
| 2011 | + if (!value) goto failed; | |
| 2012 | + if (PyObject_SetAttrString(result, "decorators", value) == -1) | |
| 2013 | + goto failed; | |
| 2014 | + Py_DECREF(value); | |
| 2015 | + break; | |
| 2016 | + case ClassDef_kind: | |
| 2017 | + result = PyType_GenericNew(ClassDef_type, NULL, NULL); | |
| 2018 | + if (!result) goto failed; | |
| 2019 | + value = ast2obj_identifier(o->v.ClassDef.name); | |
| 2020 | + if (!value) goto failed; | |
| 2021 | + if (PyObject_SetAttrString(result, "name", value) == -1) | |
| 2022 | + goto failed; | |
| 2023 | + Py_DECREF(value); | |
| 2024 | + value = ast2obj_list(o->v.ClassDef.bases, ast2obj_expr); | |
| 2025 | + if (!value) goto failed; | |
| 2026 | + if (PyObject_SetAttrString(result, "bases", value) == -1) | |
| 2027 | + goto failed; | |
| 2028 | + Py_DECREF(value); | |
| 2029 | + value = ast2obj_list(o->v.ClassDef.body, ast2obj_stmt); | |
| 2030 | + if (!value) goto failed; | |
| 2031 | + if (PyObject_SetAttrString(result, "body", value) == -1) | |
| 2032 | + goto failed; | |
| 2033 | + Py_DECREF(value); | |
| 2034 | + break; | |
| 2035 | + case Return_kind: | |
| 2036 | + result = PyType_GenericNew(Return_type, NULL, NULL); | |
| 2037 | + if (!result) goto failed; | |
| 2038 | + value = ast2obj_expr(o->v.Return.value); | |
| 2039 | + if (!value) goto failed; | |
| 2040 | + if (PyObject_SetAttrString(result, "value", value) == -1) | |
| 2041 | + goto failed; | |
| 2042 | + Py_DECREF(value); | |
| 2043 | + break; | |
| 2044 | + case Delete_kind: | |
| 2045 | + result = PyType_GenericNew(Delete_type, NULL, NULL); | |
| 2046 | + if (!result) goto failed; | |
| 2047 | + value = ast2obj_list(o->v.Delete.targets, ast2obj_expr); | |
| 2048 | + if (!value) goto failed; | |
| 2049 | + if (PyObject_SetAttrString(result, "targets", value) == -1) | |
| 2050 | + goto failed; | |
| 2051 | + Py_DECREF(value); | |
| 2052 | + break; | |
| 2053 | + case Assign_kind: | |
| 2054 | + result = PyType_GenericNew(Assign_type, NULL, NULL); | |
| 2055 | + if (!result) goto failed; | |
| 2056 | + value = ast2obj_list(o->v.Assign.targets, ast2obj_expr); | |
| 2057 | + if (!value) goto failed; | |
| 2058 | + if (PyObject_SetAttrString(result, "targets", value) == -1) | |
| 2059 | + goto failed; | |
| 2060 | + Py_DECREF(value); | |
| 2061 | + value = ast2obj_expr(o->v.Assign.value); | |
| 2062 | + if (!value) goto failed; | |
| 2063 | + if (PyObject_SetAttrString(result, "value", value) == -1) | |
| 2064 | + goto failed; | |
| 2065 | + Py_DECREF(value); | |
| 2066 | + break; | |
| 2067 | + case AugAssign_kind: | |
| 2068 | + result = PyType_GenericNew(AugAssign_type, NULL, NULL); | |
| 2069 | + if (!result) goto failed; | |
| 2070 | + value = ast2obj_expr(o->v.AugAssign.target); | |
| 2071 | + if (!value) goto failed; | |
| 2072 | + if (PyObject_SetAttrString(result, "target", value) == -1) | |
| 2073 | + goto failed; | |
| 2074 | + Py_DECREF(value); | |
| 2075 | + value = ast2obj_operator(o->v.AugAssign.op); | |
| 2076 | + if (!value) goto failed; | |
| 2077 | + if (PyObject_SetAttrString(result, "op", value) == -1) | |
| 2078 | + goto failed; | |
| 2079 | + Py_DECREF(value); | |
| 2080 | + value = ast2obj_expr(o->v.AugAssign.value); | |
| 2081 | + if (!value) goto failed; | |
| 2082 | + if (PyObject_SetAttrString(result, "value", value) == -1) | |
| 2083 | + goto failed; | |
| 2084 | + Py_DECREF(value); | |
| 2085 | + break; | |
| 2086 | + case Print_kind: | |
| 2087 | + result = PyType_GenericNew(Print_type, NULL, NULL); | |
| 2088 | + if (!result) goto failed; | |
| 2089 | + value = ast2obj_expr(o->v.Print.dest); | |
| 2090 | + if (!value) goto failed; | |
| 2091 | + if (PyObject_SetAttrString(result, "dest", value) == -1) | |
| 2092 | + goto failed; | |
| 2093 | + Py_DECREF(value); | |
| 2094 | + value = ast2obj_list(o->v.Print.values, ast2obj_expr); | |
| 2095 | + if (!value) goto failed; | |
| 2096 | + if (PyObject_SetAttrString(result, "values", value) == -1) | |
| 2097 | + goto failed; | |
| 2098 | + Py_DECREF(value); | |
| 2099 | + value = ast2obj_bool(o->v.Print.nl); | |
| 2100 | + if (!value) goto failed; | |
| 2101 | + if (PyObject_SetAttrString(result, "nl", value) == -1) | |
| 2102 | + goto failed; | |
| 2103 | + Py_DECREF(value); | |
| 2104 | + break; | |
| 2105 | + case For_kind: | |
| 2106 | + result = PyType_GenericNew(For_type, NULL, NULL); | |
| 2107 | + if (!result) goto failed; | |
| 2108 | + value = ast2obj_expr(o->v.For.target); | |
| 2109 | + if (!value) goto failed; | |
| 2110 | + if (PyObject_SetAttrString(result, "target", value) == -1) | |
| 2111 | + goto failed; | |
| 2112 | + Py_DECREF(value); | |
| 2113 | + value = ast2obj_expr(o->v.For.iter); | |
| 2114 | + if (!value) goto failed; | |
| 2115 | + if (PyObject_SetAttrString(result, "iter", value) == -1) | |
| 2116 | + goto failed; | |
| 2117 | + Py_DECREF(value); | |
| 2118 | + value = ast2obj_list(o->v.For.body, ast2obj_stmt); | |
| 2119 | + if (!value) goto failed; | |
| 2120 | + if (PyObject_SetAttrString(result, "body", value) == -1) | |
| 2121 | + goto failed; | |
| 2122 | + Py_DECREF(value); | |
| 2123 | + value = ast2obj_list(o->v.For.orelse, ast2obj_stmt); | |
| 2124 | + if (!value) goto failed; | |
| 2125 | + if (PyObject_SetAttrString(result, "orelse", value) == -1) | |
| 2126 | + goto failed; | |
| 2127 | + Py_DECREF(value); | |
| 2128 | + break; | |
| 2129 | + case While_kind: | |
| 2130 | + result = PyType_GenericNew(While_type, NULL, NULL); | |
| 2131 | + if (!result) goto failed; | |
| 2132 | + value = ast2obj_expr(o->v.While.test); | |
| 2133 | + if (!value) goto failed; | |
| 2134 | + if (PyObject_SetAttrString(result, "test", value) == -1) | |
| 2135 | + goto failed; | |
| 2136 | + Py_DECREF(value); | |
| 2137 | + value = ast2obj_list(o->v.While.body, ast2obj_stmt); | |
| 2138 | + if (!value) goto failed; | |
| 2139 | + if (PyObject_SetAttrString(result, "body", value) == -1) | |
| 2140 | + goto failed; | |
| 2141 | + Py_DECREF(value); | |
| 2142 | + value = ast2obj_list(o->v.While.orelse, ast2obj_stmt); | |
| 2143 | + if (!value) goto failed; | |
| 2144 | + if (PyObject_SetAttrString(result, "orelse", value) == -1) | |
| 2145 | + goto failed; | |
| 2146 | + Py_DECREF(value); | |
| 2147 | + break; | |
| 2148 | + case If_kind: | |
| 2149 | + result = PyType_GenericNew(If_type, NULL, NULL); | |
| 2150 | + if (!result) goto failed; | |
| 2151 | + value = ast2obj_expr(o->v.If.test); | |
| 2152 | + if (!value) goto failed; | |
| 2153 | + if (PyObject_SetAttrString(result, "test", value) == -1) | |
| 2154 | + goto failed; | |
| 2155 | + Py_DECREF(value); | |
| 2156 | + value = ast2obj_list(o->v.If.body, ast2obj_stmt); | |
| 2157 | + if (!value) goto failed; | |
| 2158 | + if (PyObject_SetAttrString(result, "body", value) == -1) | |
| 2159 | + goto failed; | |
| 2160 | + Py_DECREF(value); | |
| 2161 | + value = ast2obj_list(o->v.If.orelse, ast2obj_stmt); | |
| 2162 | + if (!value) goto failed; | |
| 2163 | + if (PyObject_SetAttrString(result, "orelse", value) == -1) | |
| 2164 | + goto failed; | |
| 2165 | + Py_DECREF(value); | |
| 2166 | + break; | |
| 2167 | + case With_kind: | |
| 2168 | + result = PyType_GenericNew(With_type, NULL, NULL); | |
| 2169 | + if (!result) goto failed; | |
| 2170 | + value = ast2obj_expr(o->v.With.context_expr); | |
| 2171 | + if (!value) goto failed; | |
| 2172 | + if (PyObject_SetAttrString(result, "context_expr", value) == -1) | |
| 2173 | + goto failed; | |
| 2174 | + Py_DECREF(value); | |
| 2175 | + value = ast2obj_expr(o->v.With.optional_vars); | |
| 2176 | + if (!value) goto failed; | |
| 2177 | + if (PyObject_SetAttrString(result, "optional_vars", value) == | |
| 2178 | + -1) | |
| 2179 | + goto failed; | |
| 2180 | + Py_DECREF(value); | |
| 2181 | + value = ast2obj_list(o->v.With.body, ast2obj_stmt); | |
| 2182 | + if (!value) goto failed; | |
| 2183 | + if (PyObject_SetAttrString(result, "body", value) == -1) | |
| 2184 | + goto failed; | |
| 2185 | + Py_DECREF(value); | |
| 2186 | + break; | |
| 2187 | + case Raise_kind: | |
| 2188 | + result = PyType_GenericNew(Raise_type, NULL, NULL); | |
| 2189 | + if (!result) goto failed; | |
| 2190 | + value = ast2obj_expr(o->v.Raise.type); | |
| 2191 | + if (!value) goto failed; | |
| 2192 | + if (PyObject_SetAttrString(result, "type", value) == -1) | |
| 2193 | + goto failed; | |
| 2194 | + Py_DECREF(value); | |
| 2195 | + value = ast2obj_expr(o->v.Raise.inst); | |
| 2196 | + if (!value) goto failed; | |
| 2197 | + if (PyObject_SetAttrString(result, "inst", value) == -1) | |
| 2198 | + goto failed; | |
| 2199 | + Py_DECREF(value); | |
| 2200 | + value = ast2obj_expr(o->v.Raise.tback); | |
| 2201 | + if (!value) goto failed; | |
| 2202 | + if (PyObject_SetAttrString(result, "tback", value) == -1) | |
| 2203 | + goto failed; | |
| 2204 | + Py_DECREF(value); | |
| 2205 | + break; | |
| 2206 | + case TryExcept_kind: | |
| 2207 | + result = PyType_GenericNew(TryExcept_type, NULL, NULL); | |
| 2208 | + if (!result) goto failed; | |
| 2209 | + value = ast2obj_list(o->v.TryExcept.body, ast2obj_stmt); | |
| 2210 | + if (!value) goto failed; | |
| 2211 | + if (PyObject_SetAttrString(result, "body", value) == -1) | |
| 2212 | + goto failed; | |
| 2213 | + Py_DECREF(value); | |
| 2214 | + value = ast2obj_list(o->v.TryExcept.handlers, | |
| 2215 | + ast2obj_excepthandler); | |
| 2216 | + if (!value) goto failed; | |
| 2217 | + if (PyObject_SetAttrString(result, "handlers", value) == -1) | |
| 2218 | + goto failed; | |
| 2219 | + Py_DECREF(value); | |
| 2220 | + value = ast2obj_list(o->v.TryExcept.orelse, ast2obj_stmt); | |
| 2221 | + if (!value) goto failed; | |
| 2222 | + if (PyObject_SetAttrString(result, "orelse", value) == -1) | |
| 2223 | + goto failed; | |
| 2224 | + Py_DECREF(value); | |
| 2225 | + break; | |
| 2226 | + case TryFinally_kind: | |
| 2227 | + result = PyType_GenericNew(TryFinally_type, NULL, NULL); | |
| 2228 | + if (!result) goto failed; | |
| 2229 | + value = ast2obj_list(o->v.TryFinally.body, ast2obj_stmt); | |
| 2230 | + if (!value) goto failed; | |
| 2231 | + if (PyObject_SetAttrString(result, "body", value) == -1) | |
| 2232 | + goto failed; | |
| 2233 | + Py_DECREF(value); | |
| 2234 | + value = ast2obj_list(o->v.TryFinally.finalbody, ast2obj_stmt); | |
| 2235 | + if (!value) goto failed; | |
| 2236 | + if (PyObject_SetAttrString(result, "finalbody", value) == -1) | |
| 2237 | + goto failed; | |
| 2238 | + Py_DECREF(value); | |
| 2239 | + break; | |
| 2240 | + case Assert_kind: | |
| 2241 | + result = PyType_GenericNew(Assert_type, NULL, NULL); | |
| 2242 | + if (!result) goto failed; | |
| 2243 | + value = ast2obj_expr(o->v.Assert.test); | |
| 2244 | + if (!value) goto failed; | |
| 2245 | + if (PyObject_SetAttrString(result, "test", value) == -1) | |
| 2246 | + goto failed; | |
| 2247 | + Py_DECREF(value); | |
| 2248 | + value = ast2obj_expr(o->v.Assert.msg); | |
| 2249 | + if (!value) goto failed; | |
| 2250 | + if (PyObject_SetAttrString(result, "msg", value) == -1) | |
| 2251 | + goto failed; | |
| 2252 | + Py_DECREF(value); | |
| 2253 | + break; | |
| 2254 | + case Import_kind: | |
| 2255 | + result = PyType_GenericNew(Import_type, NULL, NULL); | |
| 2256 | + if (!result) goto failed; | |
| 2257 | + value = ast2obj_list(o->v.Import.names, ast2obj_alias); | |
| 2258 | + if (!value) goto failed; | |
| 2259 | + if (PyObject_SetAttrString(result, "names", value) == -1) | |
| 2260 | + goto failed; | |
| 2261 | + Py_DECREF(value); | |
| 2262 | + break; | |
| 2263 | + case ImportFrom_kind: | |
| 2264 | + result = PyType_GenericNew(ImportFrom_type, NULL, NULL); | |
| 2265 | + if (!result) goto failed; | |
| 2266 | + value = ast2obj_identifier(o->v.ImportFrom.module); | |
| 2267 | + if (!value) goto failed; | |
| 2268 | + if (PyObject_SetAttrString(result, "module", value) == -1) | |
| 2269 | + goto failed; | |
| 2270 | + Py_DECREF(value); | |
| 2271 | + value = ast2obj_list(o->v.ImportFrom.names, ast2obj_alias); | |
| 2272 | + if (!value) goto failed; | |
| 2273 | + if (PyObject_SetAttrString(result, "names", value) == -1) | |
| 2274 | + goto failed; | |
| 2275 | + Py_DECREF(value); | |
| 2276 | + value = ast2obj_int(o->v.ImportFrom.level); | |
| 2277 | + if (!value) goto failed; | |
| 2278 | + if (PyObject_SetAttrString(result, "level", value) == -1) | |
| 2279 | + goto failed; | |
| 2280 | + Py_DECREF(value); | |
| 2281 | + break; | |
| 2282 | + case Exec_kind: | |
| 2283 | + result = PyType_GenericNew(Exec_type, NULL, NULL); | |
| 2284 | + if (!result) goto failed; | |
| 2285 | + value = ast2obj_expr(o->v.Exec.body); | |
| 2286 | + if (!value) goto failed; | |
| 2287 | + if (PyObject_SetAttrString(result, "body", value) == -1) | |
| 2288 | + goto failed; | |
| 2289 | + Py_DECREF(value); | |
| 2290 | + value = ast2obj_expr(o->v.Exec.globals); | |
| 2291 | + if (!value) goto failed; | |
| 2292 | + if (PyObject_SetAttrString(result, "globals", value) == -1) | |
| 2293 | + goto failed; | |
| 2294 | + Py_DECREF(value); | |
| 2295 | + value = ast2obj_expr(o->v.Exec.locals); | |
| 2296 | + if (!value) goto failed; | |
| 2297 | + if (PyObject_SetAttrString(result, "locals", value) == -1) | |
| 2298 | + goto failed; | |
| 2299 | + Py_DECREF(value); | |
| 2300 | + break; | |
| 2301 | + case Global_kind: | |
| 2302 | + result = PyType_GenericNew(Global_type, NULL, NULL); | |
| 2303 | + if (!result) goto failed; | |
| 2304 | + value = ast2obj_list(o->v.Global.names, ast2obj_identifier); | |
| 2305 | + if (!value) goto failed; | |
| 2306 | + if (PyObject_SetAttrString(result, "names", value) == -1) | |
| 2307 | + goto failed; | |
| 2308 | + Py_DECREF(value); | |
| 2309 | + break; | |
| 2310 | + case Expr_kind: | |
| 2311 | + result = PyType_GenericNew(Expr_type, NULL, NULL); | |
| 2312 | + if (!result) goto failed; | |
| 2313 | + value = ast2obj_expr(o->v.Expr.value); | |
| 2314 | + if (!value) goto failed; | |
| 2315 | + if (PyObject_SetAttrString(result, "value", value) == -1) | |
| 2316 | + goto failed; | |
| 2317 | + Py_DECREF(value); | |
| 2318 | + break; | |
| 2319 | + case Pass_kind: | |
| 2320 | + result = PyType_GenericNew(Pass_type, NULL, NULL); | |
| 2321 | + if (!result) goto failed; | |
| 2322 | + break; | |
| 2323 | + case Break_kind: | |
| 2324 | + result = PyType_GenericNew(Break_type, NULL, NULL); | |
| 2325 | + if (!result) goto failed; | |
| 2326 | + break; | |
| 2327 | + case Continue_kind: | |
| 2328 | + result = PyType_GenericNew(Continue_type, NULL, NULL); | |
| 2329 | + if (!result) goto failed; | |
| 2330 | + break; | |
| 2331 | + } | |
| 2332 | + value = ast2obj_int(o->lineno); | |
| 2333 | + if (!value) goto failed; | |
| 2334 | + if (PyObject_SetAttrString(result, "lineno", value) < 0) | |
| 2335 | + goto failed; | |
| 2336 | + Py_DECREF(value); | |
| 2337 | + value = ast2obj_int(o->col_offset); | |
| 2338 | + if (!value) goto failed; | |
| 2339 | + if (PyObject_SetAttrString(result, "col_offset", value) < 0) | |
| 2340 | + goto failed; | |
| 2341 | + Py_DECREF(value); | |
| 2342 | + return result; | |
| 2343 | +failed: | |
| 2344 | + Py_XDECREF(value); | |
| 2345 | + Py_XDECREF(result); | |
| 2346 | + return NULL; | |
| 2347 | +} | |
| 2348 | + | |
| 2349 | +PyObject* | |
| 2350 | +ast2obj_expr(void* _o) | |
| 2351 | +{ | |
| 2352 | + expr_ty o = (expr_ty)_o; | |
| 2353 | + PyObject *result = NULL, *value = NULL; | |
| 2354 | + if (!o) { | |
| 2355 | + Py_INCREF(Py_None); | |
| 2356 | + return Py_None; | |
| 2357 | + } | |
| 2358 | + | |
| 2359 | + switch (o->kind) { | |
| 2360 | + case BoolOp_kind: | |
| 2361 | + result = PyType_GenericNew(BoolOp_type, NULL, NULL); | |
| 2362 | + if (!result) goto failed; | |
| 2363 | + value = ast2obj_boolop(o->v.BoolOp.op); | |
| 2364 | + if (!value) goto failed; | |
| 2365 | + if (PyObject_SetAttrString(result, "op", value) == -1) | |
| 2366 | + goto failed; | |
| 2367 | + Py_DECREF(value); | |
| 2368 | + value = ast2obj_list(o->v.BoolOp.values, ast2obj_expr); | |
| 2369 | + if (!value) goto failed; | |
| 2370 | + if (PyObject_SetAttrString(result, "values", value) == -1) | |
| 2371 | + goto failed; | |
| 2372 | + Py_DECREF(value); | |
| 2373 | + break; | |
| 2374 | + case BinOp_kind: | |
| 2375 | + result = PyType_GenericNew(BinOp_type, NULL, NULL); | |
| 2376 | + if (!result) goto failed; | |
| 2377 | + value = ast2obj_expr(o->v.BinOp.left); | |
| 2378 | + if (!value) goto failed; | |
| 2379 | + if (PyObject_SetAttrString(result, "left", value) == -1) | |
| 2380 | + goto failed; | |
| 2381 | + Py_DECREF(value); | |
| 2382 | + value = ast2obj_operator(o->v.BinOp.op); | |
| 2383 | + if (!value) goto failed; | |
| 2384 | + if (PyObject_SetAttrString(result, "op", value) == -1) | |
| 2385 | + goto failed; | |
| 2386 | + Py_DECREF(value); | |
| 2387 | + value = ast2obj_expr(o->v.BinOp.right); | |
| 2388 | + if (!value) goto failed; | |
| 2389 | + if (PyObject_SetAttrString(result, "right", value) == -1) | |
| 2390 | + goto failed; | |
| 2391 | + Py_DECREF(value); | |
| 2392 | + break; | |
| 2393 | + case UnaryOp_kind: | |
| 2394 | + result = PyType_GenericNew(UnaryOp_type, NULL, NULL); | |
| 2395 | + if (!result) goto failed; | |
| 2396 | + value = ast2obj_unaryop(o->v.UnaryOp.op); | |
| 2397 | + if (!value) goto failed; | |
| 2398 | + if (PyObject_SetAttrString(result, "op", value) == -1) | |
| 2399 | + goto failed; | |
| 2400 | + Py_DECREF(value); | |
| 2401 | + value = ast2obj_expr(o->v.UnaryOp.operand); | |
| 2402 | + if (!value) goto failed; | |
| 2403 | + if (PyObject_SetAttrString(result, "operand", value) == -1) | |
| 2404 | + goto failed; | |
| 2405 | + Py_DECREF(value); | |
| 2406 | + break; | |
| 2407 | + case Lambda_kind: | |
| 2408 | + result = PyType_GenericNew(Lambda_type, NULL, NULL); | |
| 2409 | + if (!result) goto failed; | |
| 2410 | + value = ast2obj_arguments(o->v.Lambda.args); | |
| 2411 | + if (!value) goto failed; | |
| 2412 | + if (PyObject_SetAttrString(result, "args", value) == -1) | |
| 2413 | + goto failed; | |
| 2414 | + Py_DECREF(value); | |
| 2415 | + value = ast2obj_expr(o->v.Lambda.body); | |
| 2416 | + if (!value) goto failed; | |
| 2417 | + if (PyObject_SetAttrString(result, "body", value) == -1) | |
| 2418 | + goto failed; | |
| 2419 | + Py_DECREF(value); | |
| 2420 | + break; | |
| 2421 | + case IfExp_kind: | |
| 2422 | + result = PyType_GenericNew(IfExp_type, NULL, NULL); | |
| 2423 | + if (!result) goto failed; | |
| 2424 | + value = ast2obj_expr(o->v.IfExp.test); | |
| 2425 | + if (!value) goto failed; | |
| 2426 | + if (PyObject_SetAttrString(result, "test", value) == -1) | |
| 2427 | + goto failed; | |
| 2428 | + Py_DECREF(value); | |
| 2429 | + value = ast2obj_expr(o->v.IfExp.body); | |
| 2430 | + if (!value) goto failed; | |
| 2431 | + if (PyObject_SetAttrString(result, "body", value) == -1) | |
| 2432 | + goto failed; | |
| 2433 | + Py_DECREF(value); | |
| 2434 | + value = ast2obj_expr(o->v.IfExp.orelse); | |
| 2435 | + if (!value) goto failed; | |
| 2436 | + if (PyObject_SetAttrString(result, "orelse", value) == -1) | |
| 2437 | + goto failed; | |
| 2438 | + Py_DECREF(value); | |
| 2439 | + break; | |
| 2440 | + case Dict_kind: | |
| 2441 | + result = PyType_GenericNew(Dict_type, NULL, NULL); | |
| 2442 | + if (!result) goto failed; | |
| 2443 | + value = ast2obj_list(o->v.Dict.keys, ast2obj_expr); | |
| 2444 | + if (!value) goto failed; | |
| 2445 | + if (PyObject_SetAttrString(result, "keys", value) == -1) | |
| 2446 | + goto failed; | |
| 2447 | + Py_DECREF(value); | |
| 2448 | + value = ast2obj_list(o->v.Dict.values, ast2obj_expr); | |
| 2449 | + if (!value) goto failed; | |
| 2450 | + if (PyObject_SetAttrString(result, "values", value) == -1) | |
| 2451 | + goto failed; | |
| 2452 | + Py_DECREF(value); | |
| 2453 | + break; | |
| 2454 | + case ListComp_kind: | |
| 2455 | + result = PyType_GenericNew(ListComp_type, NULL, NULL); | |
| 2456 | + if (!result) goto failed; | |
| 2457 | + value = ast2obj_expr(o->v.ListComp.elt); | |
| 2458 | + if (!value) goto failed; | |
| 2459 | + if (PyObject_SetAttrString(result, "elt", value) == -1) | |
| 2460 | + goto failed; | |
| 2461 | + Py_DECREF(value); | |
| 2462 | + value = ast2obj_list(o->v.ListComp.generators, | |
| 2463 | + ast2obj_comprehension); | |
| 2464 | + if (!value) goto failed; | |
| 2465 | + if (PyObject_SetAttrString(result, "generators", value) == -1) | |
| 2466 | + goto failed; | |
| 2467 | + Py_DECREF(value); | |
| 2468 | + break; | |
| 2469 | + case GeneratorExp_kind: | |
| 2470 | + result = PyType_GenericNew(GeneratorExp_type, NULL, NULL); | |
| 2471 | + if (!result) goto failed; | |
| 2472 | + value = ast2obj_expr(o->v.GeneratorExp.elt); | |
| 2473 | + if (!value) goto failed; | |
| 2474 | + if (PyObject_SetAttrString(result, "elt", value) == -1) | |
| 2475 | + goto failed; | |
| 2476 | + Py_DECREF(value); | |
| 2477 | + value = ast2obj_list(o->v.GeneratorExp.generators, | |
| 2478 | + ast2obj_comprehension); | |
| 2479 | + if (!value) goto failed; | |
| 2480 | + if (PyObject_SetAttrString(result, "generators", value) == -1) | |
| 2481 | + goto failed; | |
| 2482 | + Py_DECREF(value); | |
| 2483 | + break; | |
| 2484 | + case Yield_kind: | |
| 2485 | + result = PyType_GenericNew(Yield_type, NULL, NULL); | |
| 2486 | + if (!result) goto failed; | |
| 2487 | + value = ast2obj_expr(o->v.Yield.value); | |
| 2488 | + if (!value) goto failed; | |
| 2489 | + if (PyObject_SetAttrString(result, "value", value) == -1) | |
| 2490 | + goto failed; | |
| 2491 | + Py_DECREF(value); | |
| 2492 | + break; | |
| 2493 | + case Compare_kind: | |
| 2494 | + result = PyType_GenericNew(Compare_type, NULL, NULL); | |
| 2495 | + if (!result) goto failed; | |
| 2496 | + value = ast2obj_expr(o->v.Compare.left); | |
| 2497 | + if (!value) goto failed; | |
| 2498 | + if (PyObject_SetAttrString(result, "left", value) == -1) | |
| 2499 | + goto failed; | |
| 2500 | + Py_DECREF(value); | |
| 2501 | + { | |
| 2502 | + int i, n = asdl_seq_LEN(o->v.Compare.ops); | |
| 2503 | + value = PyList_New(n); | |
| 2504 | + if (!value) goto failed; | |
| 2505 | + for(i = 0; i < n; i++) | |
| 2506 | + PyList_SET_ITEM(value, i, ast2obj_cmpop((cmpop_ty)asdl_seq_GET(o->v.Compare.ops, i))); | |
| 2507 | + } | |
| 2508 | + if (!value) goto failed; | |
| 2509 | + if (PyObject_SetAttrString(result, "ops", value) == -1) | |
| 2510 | + goto failed; | |
| 2511 | + Py_DECREF(value); | |
| 2512 | + value = ast2obj_list(o->v.Compare.comparators, ast2obj_expr); | |
| 2513 | + if (!value) goto failed; | |
| 2514 | + if (PyObject_SetAttrString(result, "comparators", value) == -1) | |
| 2515 | + goto failed; | |
| 2516 | + Py_DECREF(value); | |
| 2517 | + break; | |
| 2518 | + case Call_kind: | |
| 2519 | + result = PyType_GenericNew(Call_type, NULL, NULL); | |
| 2520 | + if (!result) goto failed; | |
| 2521 | + value = ast2obj_expr(o->v.Call.func); | |
| 2522 | + if (!value) goto failed; | |
| 2523 | + if (PyObject_SetAttrString(result, "func", value) == -1) | |
| 2524 | + goto failed; | |
| 2525 | + Py_DECREF(value); | |
| 2526 | + value = ast2obj_list(o->v.Call.args, ast2obj_expr); | |
| 2527 | + if (!value) goto failed; | |
| 2528 | + if (PyObject_SetAttrString(result, "args", value) == -1) | |
| 2529 | + goto failed; | |
| 2530 | + Py_DECREF(value); | |
| 2531 | + value = ast2obj_list(o->v.Call.keywords, ast2obj_keyword); | |
| 2532 | + if (!value) goto failed; | |
| 2533 | + if (PyObject_SetAttrString(result, "keywords", value) == -1) | |
| 2534 | + goto failed; | |
| 2535 | + Py_DECREF(value); | |
| 2536 | + value = ast2obj_expr(o->v.Call.starargs); | |
| 2537 | + if (!value) goto failed; | |
| 2538 | + if (PyObject_SetAttrString(result, "starargs", value) == -1) | |
| 2539 | + goto failed; | |
| 2540 | + Py_DECREF(value); | |
| 2541 | + value = ast2obj_expr(o->v.Call.kwargs); | |
| 2542 | + if (!value) goto failed; | |
| 2543 | + if (PyObject_SetAttrString(result, "kwargs", value) == -1) | |
| 2544 | + goto failed; | |
| 2545 | + Py_DECREF(value); | |
| 2546 | + break; | |
| 2547 | + case Repr_kind: | |
| 2548 | + result = PyType_GenericNew(Repr_type, NULL, NULL); | |
| 2549 | + if (!result) goto failed; | |
| 2550 | + value = ast2obj_expr(o->v.Repr.value); | |
| 2551 | + if (!value) goto failed; | |
| 2552 | + if (PyObject_SetAttrString(result, "value", value) == -1) | |
| 2553 | + goto failed; | |
| 2554 | + Py_DECREF(value); | |
| 2555 | + break; | |
| 2556 | + case Num_kind: | |
| 2557 | + result = PyType_GenericNew(Num_type, NULL, NULL); | |
| 2558 | + if (!result) goto failed; | |
| 2559 | + value = ast2obj_object(o->v.Num.n); | |
| 2560 | + if (!value) goto failed; | |
| 2561 | + if (PyObject_SetAttrString(result, "n", value) == -1) | |
| 2562 | + goto failed; | |
| 2563 | + Py_DECREF(value); | |
| 2564 | + break; | |
| 2565 | + case Str_kind: | |
| 2566 | + result = PyType_GenericNew(Str_type, NULL, NULL); | |
| 2567 | + if (!result) goto failed; | |
| 2568 | + value = ast2obj_string(o->v.Str.s); | |
| 2569 | + if (!value) goto failed; | |
| 2570 | + if (PyObject_SetAttrString(result, "s", value) == -1) | |
| 2571 | + goto failed; | |
| 2572 | + Py_DECREF(value); | |
| 2573 | + break; | |
| 2574 | + case Attribute_kind: | |
| 2575 | + result = PyType_GenericNew(Attribute_type, NULL, NULL); | |
| 2576 | + if (!result) goto failed; | |
| 2577 | + value = ast2obj_expr(o->v.Attribute.value); | |
| 2578 | + if (!value) goto failed; | |
| 2579 | + if (PyObject_SetAttrString(result, "value", value) == -1) | |
| 2580 | + goto failed; | |
| 2581 | + Py_DECREF(value); | |
| 2582 | + value = ast2obj_identifier(o->v.Attribute.attr); | |
| 2583 | + if (!value) goto failed; | |
| 2584 | + if (PyObject_SetAttrString(result, "attr", value) == -1) | |
| 2585 | + goto failed; | |
| 2586 | + Py_DECREF(value); | |
| 2587 | + value = ast2obj_expr_context(o->v.Attribute.ctx); | |
| 2588 | + if (!value) goto failed; | |
| 2589 | + if (PyObject_SetAttrString(result, "ctx", value) == -1) | |
| 2590 | + goto failed; | |
| 2591 | + Py_DECREF(value); | |
| 2592 | + break; | |
| 2593 | + case Subscript_kind: | |
| 2594 | + result = PyType_GenericNew(Subscript_type, NULL, NULL); | |
| 2595 | + if (!result) goto failed; | |
| 2596 | + value = ast2obj_expr(o->v.Subscript.value); | |
| 2597 | + if (!value) goto failed; | |
| 2598 | + if (PyObject_SetAttrString(result, "value", value) == -1) | |
| 2599 | + goto failed; | |
| 2600 | + Py_DECREF(value); | |
| 2601 | + value = ast2obj_slice(o->v.Subscript.slice); | |
| 2602 | + if (!value) goto failed; | |
| 2603 | + if (PyObject_SetAttrString(result, "slice", value) == -1) | |
| 2604 | + goto failed; | |
| 2605 | + Py_DECREF(value); | |
| 2606 | + value = ast2obj_expr_context(o->v.Subscript.ctx); | |
| 2607 | + if (!value) goto failed; | |
| 2608 | + if (PyObject_SetAttrString(result, "ctx", value) == -1) | |
| 2609 | + goto failed; | |
| 2610 | + Py_DECREF(value); | |
| 2611 | + break; | |
| 2612 | + case Name_kind: | |
| 2613 | + result = PyType_GenericNew(Name_type, NULL, NULL); | |
| 2614 | + if (!result) goto failed; | |
| 2615 | + value = ast2obj_identifier(o->v.Name.id); | |
| 2616 | + if (!value) goto failed; | |
| 2617 | + if (PyObject_SetAttrString(result, "id", value) == -1) | |
| 2618 | + goto failed; | |
| 2619 | + Py_DECREF(value); | |
| 2620 | + value = ast2obj_expr_context(o->v.Name.ctx); | |
| 2621 | + if (!value) goto failed; | |
| 2622 | + if (PyObject_SetAttrString(result, "ctx", value) == -1) | |
| 2623 | + goto failed; | |
| 2624 | + Py_DECREF(value); | |
| 2625 | + break; | |
| 2626 | + case List_kind: | |
| 2627 | + result = PyType_GenericNew(List_type, NULL, NULL); | |
| 2628 | + if (!result) goto failed; | |
| 2629 | + value = ast2obj_list(o->v.List.elts, ast2obj_expr); | |
| 2630 | + if (!value) goto failed; | |
| 2631 | + if (PyObject_SetAttrString(result, "elts", value) == -1) | |
| 2632 | + goto failed; | |
| 2633 | + Py_DECREF(value); | |
| 2634 | + value = ast2obj_expr_context(o->v.List.ctx); | |
| 2635 | + if (!value) goto failed; | |
| 2636 | + if (PyObject_SetAttrString(result, "ctx", value) == -1) | |
| 2637 | + goto failed; | |
| 2638 | + Py_DECREF(value); | |
| 2639 | + break; | |
| 2640 | + case Tuple_kind: | |
| 2641 | + result = PyType_GenericNew(Tuple_type, NULL, NULL); | |
| 2642 | + if (!result) goto failed; | |
| 2643 | + value = ast2obj_list(o->v.Tuple.elts, ast2obj_expr); | |
| 2644 | + if (!value) goto failed; | |
| 2645 | + if (PyObject_SetAttrString(result, "elts", value) == -1) | |
| 2646 | + goto failed; | |
| 2647 | + Py_DECREF(value); | |
| 2648 | + value = ast2obj_expr_context(o->v.Tuple.ctx); | |
| 2649 | + if (!value) goto failed; | |
| 2650 | + if (PyObject_SetAttrString(result, "ctx", value) == -1) | |
| 2651 | + goto failed; | |
| 2652 | + Py_DECREF(value); | |
| 2653 | + break; | |
| 2654 | + } | |
| 2655 | + value = ast2obj_int(o->lineno); | |
| 2656 | + if (!value) goto failed; | |
| 2657 | + if (PyObject_SetAttrString(result, "lineno", value) < 0) | |
| 2658 | + goto failed; | |
| 2659 | + Py_DECREF(value); | |
| 2660 | + value = ast2obj_int(o->col_offset); | |
| 2661 | + if (!value) goto failed; | |
| 2662 | + if (PyObject_SetAttrString(result, "col_offset", value) < 0) | |
| 2663 | + goto failed; | |
| 2664 | + Py_DECREF(value); | |
| 2665 | + return result; | |
| 2666 | +failed: | |
| 2667 | + Py_XDECREF(value); | |
| 2668 | + Py_XDECREF(result); | |
| 2669 | + return NULL; | |
| 2670 | +} | |
| 2671 | + | |
| 2672 | +PyObject* ast2obj_expr_context(expr_context_ty o) | |
| 2673 | +{ | |
| 2674 | + switch(o) { | |
| 2675 | + case Load: | |
| 2676 | + Py_INCREF(Load_singleton); | |
| 2677 | + return Load_singleton; | |
| 2678 | + case Store: | |
| 2679 | + Py_INCREF(Store_singleton); | |
| 2680 | + return Store_singleton; | |
| 2681 | + case Del: | |
| 2682 | + Py_INCREF(Del_singleton); | |
| 2683 | + return Del_singleton; | |
| 2684 | + case AugLoad: | |
| 2685 | + Py_INCREF(AugLoad_singleton); | |
| 2686 | + return AugLoad_singleton; | |
| 2687 | + case AugStore: | |
| 2688 | + Py_INCREF(AugStore_singleton); | |
| 2689 | + return AugStore_singleton; | |
| 2690 | + case Param: | |
| 2691 | + Py_INCREF(Param_singleton); | |
| 2692 | + return Param_singleton; | |
| 2693 | + } | |
| 2694 | + return NULL; /* cannot happen */ | |
| 2695 | +} | |
| 2696 | +PyObject* | |
| 2697 | +ast2obj_slice(void* _o) | |
| 2698 | +{ | |
| 2699 | + slice_ty o = (slice_ty)_o; | |
| 2700 | + PyObject *result = NULL, *value = NULL; | |
| 2701 | + if (!o) { | |
| 2702 | + Py_INCREF(Py_None); | |
| 2703 | + return Py_None; | |
| 2704 | + } | |
| 2705 | + | |
| 2706 | + switch (o->kind) { | |
| 2707 | + case Ellipsis_kind: | |
| 2708 | + result = PyType_GenericNew(Ellipsis_type, NULL, NULL); | |
| 2709 | + if (!result) goto failed; | |
| 2710 | + break; | |
| 2711 | + case Slice_kind: | |
| 2712 | + result = PyType_GenericNew(Slice_type, NULL, NULL); | |
| 2713 | + if (!result) goto failed; | |
| 2714 | + value = ast2obj_expr(o->v.Slice.lower); | |
| 2715 | + if (!value) goto failed; | |
| 2716 | + if (PyObject_SetAttrString(result, "lower", value) == -1) | |
| 2717 | + goto failed; | |
| 2718 | + Py_DECREF(value); | |
| 2719 | + value = ast2obj_expr(o->v.Slice.upper); | |
| 2720 | + if (!value) goto failed; | |
| 2721 | + if (PyObject_SetAttrString(result, "upper", value) == -1) | |
| 2722 | + goto failed; | |
| 2723 | + Py_DECREF(value); | |
| 2724 | + value = ast2obj_expr(o->v.Slice.step); | |
| 2725 | + if (!value) goto failed; | |
| 2726 | + if (PyObject_SetAttrString(result, "step", value) == -1) | |
| 2727 | + goto failed; | |
| 2728 | + Py_DECREF(value); | |
| 2729 | + break; | |
| 2730 | + case ExtSlice_kind: | |
| 2731 | + result = PyType_GenericNew(ExtSlice_type, NULL, NULL); | |
| 2732 | + if (!result) goto failed; | |
| 2733 | + value = ast2obj_list(o->v.ExtSlice.dims, ast2obj_slice); | |
| 2734 | + if (!value) goto failed; | |
| 2735 | + if (PyObject_SetAttrString(result, "dims", value) == -1) | |
| 2736 | + goto failed; | |
| 2737 | + Py_DECREF(value); | |
| 2738 | + break; | |
| 2739 | + case Index_kind: | |
| 2740 | + result = PyType_GenericNew(Index_type, NULL, NULL); | |
| 2741 | + if (!result) goto failed; | |
| 2742 | + value = ast2obj_expr(o->v.Index.value); | |
| 2743 | + if (!value) goto failed; | |
| 2744 | + if (PyObject_SetAttrString(result, "value", value) == -1) | |
| 2745 | + goto failed; | |
| 2746 | + Py_DECREF(value); | |
| 2747 | + break; | |
| 2748 | + } | |
| 2749 | + return result; | |
| 2750 | +failed: | |
| 2751 | + Py_XDECREF(value); | |
| 2752 | + Py_XDECREF(result); | |
| 2753 | + return NULL; | |
| 2754 | +} | |
| 2755 | + | |
| 2756 | +PyObject* ast2obj_boolop(boolop_ty o) | |
| 2757 | +{ | |
| 2758 | + switch(o) { | |
| 2759 | + case And: | |
| 2760 | + Py_INCREF(And_singleton); | |
| 2761 | + return And_singleton; | |
| 2762 | + case Or: | |
| 2763 | + Py_INCREF(Or_singleton); | |
| 2764 | + return Or_singleton; | |
| 2765 | + } | |
| 2766 | + return NULL; /* cannot happen */ | |
| 2767 | +} | |
| 2768 | +PyObject* ast2obj_operator(operator_ty o) | |
| 2769 | +{ | |
| 2770 | + switch(o) { | |
| 2771 | + case Add: | |
| 2772 | + Py_INCREF(Add_singleton); | |
| 2773 | + return Add_singleton; | |
| 2774 | + case Sub: | |
| 2775 | + Py_INCREF(Sub_singleton); | |
| 2776 | + return Sub_singleton; | |
| 2777 | + case Mult: | |
| 2778 | + Py_INCREF(Mult_singleton); | |
| 2779 | + return Mult_singleton; | |
| 2780 | + case Div: | |
| 2781 | + Py_INCREF(Div_singleton); | |
| 2782 | + return Div_singleton; | |
| 2783 | + case Mod: | |
| 2784 | + Py_INCREF(Mod_singleton); | |
| 2785 | + return Mod_singleton; | |
| 2786 | + case Pow: | |
| 2787 | + Py_INCREF(Pow_singleton); | |
| 2788 | + return Pow_singleton; | |
| 2789 | + case LShift: | |
| 2790 | + Py_INCREF(LShift_singleton); | |
| 2791 | + return LShift_singleton; | |
| 2792 | + case RShift: | |
| 2793 | + Py_INCREF(RShift_singleton); | |
| 2794 | + return RShift_singleton; | |
| 2795 | + case BitOr: | |
| 2796 | + Py_INCREF(BitOr_singleton); | |
| 2797 | + return BitOr_singleton; | |
| 2798 | + case BitXor: | |
| 2799 | + Py_INCREF(BitXor_singleton); | |
| 2800 | + return BitXor_singleton; | |
| 2801 | + case BitAnd: | |
| 2802 | + Py_INCREF(BitAnd_singleton); | |
| 2803 | + return BitAnd_singleton; | |
| 2804 | + case FloorDiv: | |
| 2805 | + Py_INCREF(FloorDiv_singleton); | |
| 2806 | + return FloorDiv_singleton; | |
| 2807 | + } | |
| 2808 | + return NULL; /* cannot happen */ | |
| 2809 | +} | |
| 2810 | +PyObject* ast2obj_unaryop(unaryop_ty o) | |
| 2811 | +{ | |
| 2812 | + switch(o) { | |
| 2813 | + case Invert: | |
| 2814 | + Py_INCREF(Invert_singleton); | |
| 2815 | + return Invert_singleton; | |
| 2816 | + case Not: | |
| 2817 | + Py_INCREF(Not_singleton); | |
| 2818 | + return Not_singleton; | |
| 2819 | + case UAdd: | |
| 2820 | + Py_INCREF(UAdd_singleton); | |
| 2821 | + return UAdd_singleton; | |
| 2822 | + case USub: | |
| 2823 | + Py_INCREF(USub_singleton); | |
| 2824 | + return USub_singleton; | |
| 2825 | + } | |
| 2826 | + return NULL; /* cannot happen */ | |
| 2827 | +} | |
| 2828 | +PyObject* ast2obj_cmpop(cmpop_ty o) | |
| 2829 | +{ | |
| 2830 | + switch(o) { | |
| 2831 | + case Eq: | |
| 2832 | + Py_INCREF(Eq_singleton); | |
| 2833 | + return Eq_singleton; | |
| 2834 | + case NotEq: | |
| 2835 | + Py_INCREF(NotEq_singleton); | |
| 2836 | + return NotEq_singleton; | |
| 2837 | + case Lt: | |
| 2838 | + Py_INCREF(Lt_singleton); | |
| 2839 | + return Lt_singleton; | |
| 2840 | + case LtE: | |
| 2841 | + Py_INCREF(LtE_singleton); | |
| 2842 | + return LtE_singleton; | |
| 2843 | + case Gt: | |
| 2844 | + Py_INCREF(Gt_singleton); | |
| 2845 | + return Gt_singleton; | |
| 2846 | + case GtE: | |
| 2847 | + Py_INCREF(GtE_singleton); | |
| 2848 | + return GtE_singleton; | |
| 2849 | + case Is: | |
| 2850 | + Py_INCREF(Is_singleton); | |
| 2851 | + return Is_singleton; | |
| 2852 | + case IsNot: | |
| 2853 | + Py_INCREF(IsNot_singleton); | |
| 2854 | + return IsNot_singleton; | |
| 2855 | + case In: | |
| 2856 | + Py_INCREF(In_singleton); | |
| 2857 | + return In_singleton; | |
| 2858 | + case NotIn: | |
| 2859 | + Py_INCREF(NotIn_singleton); | |
| 2860 | + return NotIn_singleton; | |
| 2861 | + } | |
| 2862 | + return NULL; /* cannot happen */ | |
| 2863 | +} | |
| 2864 | +PyObject* | |
| 2865 | +ast2obj_comprehension(void* _o) | |
| 2866 | +{ | |
| 2867 | + comprehension_ty o = (comprehension_ty)_o; | |
| 2868 | + PyObject *result = NULL, *value = NULL; | |
| 2869 | + if (!o) { | |
| 2870 | + Py_INCREF(Py_None); | |
| 2871 | + return Py_None; | |
| 2872 | + } | |
| 2873 | + | |
| 2874 | + result = PyType_GenericNew(comprehension_type, NULL, NULL); | |
| 2875 | + if (!result) return NULL; | |
| 2876 | + value = ast2obj_expr(o->target); | |
| 2877 | + if (!value) goto failed; | |
| 2878 | + if (PyObject_SetAttrString(result, "target", value) == -1) | |
| 2879 | + goto failed; | |
| 2880 | + Py_DECREF(value); | |
| 2881 | + value = ast2obj_expr(o->iter); | |
| 2882 | + if (!value) goto failed; | |
| 2883 | + if (PyObject_SetAttrString(result, "iter", value) == -1) | |
| 2884 | + goto failed; | |
| 2885 | + Py_DECREF(value); | |
| 2886 | + value = ast2obj_list(o->ifs, ast2obj_expr); | |
| 2887 | + if (!value) goto failed; | |
| 2888 | + if (PyObject_SetAttrString(result, "ifs", value) == -1) | |
| 2889 | + goto failed; | |
| 2890 | + Py_DECREF(value); | |
| 2891 | + return result; | |
| 2892 | +failed: | |
| 2893 | + Py_XDECREF(value); | |
| 2894 | + Py_XDECREF(result); | |
| 2895 | + return NULL; | |
| 2896 | +} | |
| 2897 | + | |
| 2898 | +PyObject* | |
| 2899 | +ast2obj_excepthandler(void* _o) | |
| 2900 | +{ | |
| 2901 | + excepthandler_ty o = (excepthandler_ty)_o; | |
| 2902 | + PyObject *result = NULL, *value = NULL; | |
| 2903 | + if (!o) { | |
| 2904 | + Py_INCREF(Py_None); | |
| 2905 | + return Py_None; | |
| 2906 | + } | |
| 2907 | + | |
| 2908 | + result = PyType_GenericNew(excepthandler_type, NULL, NULL); | |
| 2909 | + if (!result) return NULL; | |
| 2910 | + value = ast2obj_expr(o->type); | |
| 2911 | + if (!value) goto failed; | |
| 2912 | + if (PyObject_SetAttrString(result, "type", value) == -1) | |
| 2913 | + goto failed; | |
| 2914 | + Py_DECREF(value); | |
| 2915 | + value = ast2obj_expr(o->name); | |
| 2916 | + if (!value) goto failed; | |
| 2917 | + if (PyObject_SetAttrString(result, "name", value) == -1) | |
| 2918 | + goto failed; | |
| 2919 | + Py_DECREF(value); | |
| 2920 | + value = ast2obj_list(o->body, ast2obj_stmt); | |
| 2921 | + if (!value) goto failed; | |
| 2922 | + if (PyObject_SetAttrString(result, "body", value) == -1) | |
| 2923 | + goto failed; | |
| 2924 | + Py_DECREF(value); | |
| 2925 | + value = ast2obj_int(o->lineno); | |
| 2926 | + if (!value) goto failed; | |
| 2927 | + if (PyObject_SetAttrString(result, "lineno", value) == -1) | |
| 2928 | + goto failed; | |
| 2929 | + Py_DECREF(value); | |
| 2930 | + value = ast2obj_int(o->col_offset); | |
| 2931 | + if (!value) goto failed; | |
| 2932 | + if (PyObject_SetAttrString(result, "col_offset", value) == -1) | |
| 2933 | + goto failed; | |
| 2934 | + Py_DECREF(value); | |
| 2935 | + return result; | |
| 2936 | +failed: | |
| 2937 | + Py_XDECREF(value); | |
| 2938 | + Py_XDECREF(result); | |
| 2939 | + return NULL; | |
| 2940 | +} | |
| 2941 | + | |
| 2942 | +PyObject* | |
| 2943 | +ast2obj_arguments(void* _o) | |
| 2944 | +{ | |
| 2945 | + arguments_ty o = (arguments_ty)_o; | |
| 2946 | + PyObject *result = NULL, *value = NULL; | |
| 2947 | + if (!o) { | |
| 2948 | + Py_INCREF(Py_None); | |
| 2949 | + return Py_None; | |
| 2950 | + } | |
| 2951 | + | |
| 2952 | + result = PyType_GenericNew(arguments_type, NULL, NULL); | |
| 2953 | + if (!result) return NULL; | |
| 2954 | + value = ast2obj_list(o->args, ast2obj_expr); | |
| 2955 | + if (!value) goto failed; | |
| 2956 | + if (PyObject_SetAttrString(result, "args", value) == -1) | |
| 2957 | + goto failed; | |
| 2958 | + Py_DECREF(value); | |
| 2959 | + value = ast2obj_identifier(o->vararg); | |
| 2960 | + if (!value) goto failed; | |
| 2961 | + if (PyObject_SetAttrString(result, "vararg", value) == -1) | |
| 2962 | + goto failed; | |
| 2963 | + Py_DECREF(value); | |
| 2964 | + value = ast2obj_identifier(o->kwarg); | |
| 2965 | + if (!value) goto failed; | |
| 2966 | + if (PyObject_SetAttrString(result, "kwarg", value) == -1) | |
| 2967 | + goto failed; | |
| 2968 | + Py_DECREF(value); | |
| 2969 | + value = ast2obj_list(o->defaults, ast2obj_expr); | |
| 2970 | + if (!value) goto failed; | |
| 2971 | + if (PyObject_SetAttrString(result, "defaults", value) == -1) | |
| 2972 | + goto failed; | |
| 2973 | + Py_DECREF(value); | |
| 2974 | + return result; | |
| 2975 | +failed: | |
| 2976 | + Py_XDECREF(value); | |
| 2977 | + Py_XDECREF(result); | |
| 2978 | + return NULL; | |
| 2979 | +} | |
| 2980 | + | |
| 2981 | +PyObject* | |
| 2982 | +ast2obj_keyword(void* _o) | |
| 2983 | +{ | |
| 2984 | + keyword_ty o = (keyword_ty)_o; | |
| 2985 | + PyObject *result = NULL, *value = NULL; | |
| 2986 | + if (!o) { | |
| 2987 | + Py_INCREF(Py_None); | |
| 2988 | + return Py_None; | |
| 2989 | + } | |
| 2990 | + | |
| 2991 | + result = PyType_GenericNew(keyword_type, NULL, NULL); | |
| 2992 | + if (!result) return NULL; | |
| 2993 | + value = ast2obj_identifier(o->arg); | |
| 2994 | + if (!value) goto failed; | |
| 2995 | + if (PyObject_SetAttrString(result, "arg", value) == -1) | |
| 2996 | + goto failed; | |
| 2997 | + Py_DECREF(value); | |
| 2998 | + value = ast2obj_expr(o->value); | |
| 2999 | + if (!value) goto failed; | |
| 3000 | + if (PyObject_SetAttrString(result, "value", value) == -1) | |
| 3001 | + goto failed; | |
| 3002 | + Py_DECREF(value); | |
| 3003 | + return result; | |
| 3004 | +failed: | |
| 3005 | + Py_XDECREF(value); | |
| 3006 | + Py_XDECREF(result); | |
| 3007 | + return NULL; | |
| 3008 | +} | |
| 3009 | + | |
| 3010 | +PyObject* | |
| 3011 | +ast2obj_alias(void* _o) | |
| 3012 | +{ | |
| 3013 | + alias_ty o = (alias_ty)_o; | |
| 3014 | + PyObject *result = NULL, *value = NULL; | |
| 3015 | + if (!o) { | |
| 3016 | + Py_INCREF(Py_None); | |
| 3017 | + return Py_None; | |
| 3018 | + } | |
| 3019 | + | |
| 3020 | + result = PyType_GenericNew(alias_type, NULL, NULL); | |
| 3021 | + if (!result) return NULL; | |
| 3022 | + value = ast2obj_identifier(o->name); | |
| 3023 | + if (!value) goto failed; | |
| 3024 | + if (PyObject_SetAttrString(result, "name", value) == -1) | |
| 3025 | + goto failed; | |
| 3026 | + Py_DECREF(value); | |
| 3027 | + value = ast2obj_identifier(o->asname); | |
| 3028 | + if (!value) goto failed; | |
| 3029 | + if (PyObject_SetAttrString(result, "asname", value) == -1) | |
| 3030 | + goto failed; | |
| 3031 | + Py_DECREF(value); | |
| 3032 | + return result; | |
| 3033 | +failed: | |
| 3034 | + Py_XDECREF(value); | |
| 3035 | + Py_XDECREF(result); | |
| 3036 | + return NULL; | |
| 3037 | +} | |
| 3038 | + | |
| 3039 | + | |
| 3040 | +PyMODINIT_FUNC | |
| 3041 | +init_ast(void) | |
| 3042 | +{ | |
| 3043 | + PyObject *m, *d; | |
| 3044 | + if (!init_types()) return; | |
| 3045 | + m = Py_InitModule3("_ast", NULL, NULL); | |
| 3046 | + if (!m) return; | |
| 3047 | + d = PyModule_GetDict(m); | |
| 3048 | + if (PyDict_SetItemString(d, "AST", (PyObject*)AST_type) < 0) return; | |
| 3049 | + if (PyModule_AddIntConstant(m, "PyCF_ONLY_AST", PyCF_ONLY_AST) < 0) | |
| 3050 | + return; | |
| 3051 | + if (PyModule_AddStringConstant(m, "__version__", "43614") < 0) | |
| 3052 | + return; | |
| 3053 | + if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return; | |
| 3054 | + if (PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0) | |
| 3055 | + return; | |
| 3056 | + if (PyDict_SetItemString(d, "Interactive", (PyObject*)Interactive_type) | |
| 3057 | + < 0) return; | |
| 3058 | + if (PyDict_SetItemString(d, "Expression", (PyObject*)Expression_type) < | |
| 3059 | + 0) return; | |
| 3060 | + if (PyDict_SetItemString(d, "Suite", (PyObject*)Suite_type) < 0) return; | |
| 3061 | + if (PyDict_SetItemString(d, "stmt", (PyObject*)stmt_type) < 0) return; | |
| 3062 | + if (PyDict_SetItemString(d, "FunctionDef", (PyObject*)FunctionDef_type) | |
| 3063 | + < 0) return; | |
| 3064 | + if (PyDict_SetItemString(d, "ClassDef", (PyObject*)ClassDef_type) < 0) | |
| 3065 | + return; | |
| 3066 | + if (PyDict_SetItemString(d, "Return", (PyObject*)Return_type) < 0) | |
| 3067 | + return; | |
| 3068 | + if (PyDict_SetItemString(d, "Delete", (PyObject*)Delete_type) < 0) | |
| 3069 | + return; | |
| 3070 | + if (PyDict_SetItemString(d, "Assign", (PyObject*)Assign_type) < 0) | |
| 3071 | + return; | |
| 3072 | + if (PyDict_SetItemString(d, "AugAssign", (PyObject*)AugAssign_type) < | |
| 3073 | + 0) return; | |
| 3074 | + if (PyDict_SetItemString(d, "Print", (PyObject*)Print_type) < 0) return; | |
| 3075 | + if (PyDict_SetItemString(d, "For", (PyObject*)For_type) < 0) return; | |
| 3076 | + if (PyDict_SetItemString(d, "While", (PyObject*)While_type) < 0) return; | |
| 3077 | + if (PyDict_SetItemString(d, "If", (PyObject*)If_type) < 0) return; | |
| 3078 | + if (PyDict_SetItemString(d, "With", (PyObject*)With_type) < 0) return; | |
| 3079 | + if (PyDict_SetItemString(d, "Raise", (PyObject*)Raise_type) < 0) return; | |
| 3080 | + if (PyDict_SetItemString(d, "TryExcept", (PyObject*)TryExcept_type) < | |
| 3081 | + 0) return; | |
| 3082 | + if (PyDict_SetItemString(d, "TryFinally", (PyObject*)TryFinally_type) < | |
| 3083 | + 0) return; | |
| 3084 | + if (PyDict_SetItemString(d, "Assert", (PyObject*)Assert_type) < 0) | |
| 3085 | + return; | |
| 3086 | + if (PyDict_SetItemString(d, "Import", (PyObject*)Import_type) < 0) | |
| 3087 | + return; | |
| 3088 | + if (PyDict_SetItemString(d, "ImportFrom", (PyObject*)ImportFrom_type) < | |
| 3089 | + 0) return; | |
| 3090 | + if (PyDict_SetItemString(d, "Exec", (PyObject*)Exec_type) < 0) return; | |
| 3091 | + if (PyDict_SetItemString(d, "Global", (PyObject*)Global_type) < 0) | |
| 3092 | + return; | |
| 3093 | + if (PyDict_SetItemString(d, "Expr", (PyObject*)Expr_type) < 0) return; | |
| 3094 | + if (PyDict_SetItemString(d, "Pass", (PyObject*)Pass_type) < 0) return; | |
| 3095 | + if (PyDict_SetItemString(d, "Break", (PyObject*)Break_type) < 0) return; | |
| 3096 | + if (PyDict_SetItemString(d, "Continue", (PyObject*)Continue_type) < 0) | |
| 3097 | + return; | |
| 3098 | + if (PyDict_SetItemString(d, "expr", (PyObject*)expr_type) < 0) return; | |
| 3099 | + if (PyDict_SetItemString(d, "BoolOp", (PyObject*)BoolOp_type) < 0) | |
| 3100 | + return; | |
| 3101 | + if (PyDict_SetItemString(d, "BinOp", (PyObject*)BinOp_type) < 0) return; | |
| 3102 | + if (PyDict_SetItemString(d, "UnaryOp", (PyObject*)UnaryOp_type) < 0) | |
| 3103 | + return; | |
| 3104 | + if (PyDict_SetItemString(d, "Lambda", (PyObject*)Lambda_type) < 0) | |
| 3105 | + return; | |
| 3106 | + if (PyDict_SetItemString(d, "IfExp", (PyObject*)IfExp_type) < 0) return; | |
| 3107 | + if (PyDict_SetItemString(d, "Dict", (PyObject*)Dict_type) < 0) return; | |
| 3108 | + if (PyDict_SetItemString(d, "ListComp", (PyObject*)ListComp_type) < 0) | |
| 3109 | + return; | |
| 3110 | + if (PyDict_SetItemString(d, "GeneratorExp", | |
| 3111 | + (PyObject*)GeneratorExp_type) < 0) return; | |
| 3112 | + if (PyDict_SetItemString(d, "Yield", (PyObject*)Yield_type) < 0) return; | |
| 3113 | + if (PyDict_SetItemString(d, "Compare", (PyObject*)Compare_type) < 0) | |
| 3114 | + return; | |
| 3115 | + if (PyDict_SetItemString(d, "Call", (PyObject*)Call_type) < 0) return; | |
| 3116 | + if (PyDict_SetItemString(d, "Repr", (PyObject*)Repr_type) < 0) return; | |
| 3117 | + if (PyDict_SetItemString(d, "Num", (PyObject*)Num_type) < 0) return; | |
| 3118 | + if (PyDict_SetItemString(d, "Str", (PyObject*)Str_type) < 0) return; | |
| 3119 | + if (PyDict_SetItemString(d, "Attribute", (PyObject*)Attribute_type) < | |
| 3120 | + 0) return; | |
| 3121 | + if (PyDict_SetItemString(d, "Subscript", (PyObject*)Subscript_type) < | |
| 3122 | + 0) return; | |
| 3123 | + if (PyDict_SetItemString(d, "Name", (PyObject*)Name_type) < 0) return; | |
| 3124 | + if (PyDict_SetItemString(d, "List", (PyObject*)List_type) < 0) return; | |
| 3125 | + if (PyDict_SetItemString(d, "Tuple", (PyObject*)Tuple_type) < 0) return; | |
| 3126 | + if (PyDict_SetItemString(d, "expr_context", | |
| 3127 | + (PyObject*)expr_context_type) < 0) return; | |
| 3128 | + if (PyDict_SetItemString(d, "Load", (PyObject*)Load_type) < 0) return; | |
| 3129 | + if (PyDict_SetItemString(d, "Store", (PyObject*)Store_type) < 0) return; | |
| 3130 | + if (PyDict_SetItemString(d, "Del", (PyObject*)Del_type) < 0) return; | |
| 3131 | + if (PyDict_SetItemString(d, "AugLoad", (PyObject*)AugLoad_type) < 0) | |
| 3132 | + return; | |
| 3133 | + if (PyDict_SetItemString(d, "AugStore", (PyObject*)AugStore_type) < 0) | |
| 3134 | + return; | |
| 3135 | + if (PyDict_SetItemString(d, "Param", (PyObject*)Param_type) < 0) return; | |
| 3136 | + if (PyDict_SetItemString(d, "slice", (PyObject*)slice_type) < 0) return; | |
| 3137 | + if (PyDict_SetItemString(d, "Ellipsis", (PyObject*)Ellipsis_type) < 0) | |
| 3138 | + return; | |
| 3139 | + if (PyDict_SetItemString(d, "Slice", (PyObject*)Slice_type) < 0) return; | |
| 3140 | + if (PyDict_SetItemString(d, "ExtSlice", (PyObject*)ExtSlice_type) < 0) | |
| 3141 | + return; | |
| 3142 | + if (PyDict_SetItemString(d, "Index", (PyObject*)Index_type) < 0) return; | |
| 3143 | + if (PyDict_SetItemString(d, "boolop", (PyObject*)boolop_type) < 0) | |
| 3144 | + return; | |
| 3145 | + if (PyDict_SetItemString(d, "And", (PyObject*)And_type) < 0) return; | |
| 3146 | + if (PyDict_SetItemString(d, "Or", (PyObject*)Or_type) < 0) return; | |
| 3147 | + if (PyDict_SetItemString(d, "operator", (PyObject*)operator_type) < 0) | |
| 3148 | + return; | |
| 3149 | + if (PyDict_SetItemString(d, "Add", (PyObject*)Add_type) < 0) return; | |
| 3150 | + if (PyDict_SetItemString(d, "Sub", (PyObject*)Sub_type) < 0) return; | |
| 3151 | + if (PyDict_SetItemString(d, "Mult", (PyObject*)Mult_type) < 0) return; | |
| 3152 | + if (PyDict_SetItemString(d, "Div", (PyObject*)Div_type) < 0) return; | |
| 3153 | + if (PyDict_SetItemString(d, "Mod", (PyObject*)Mod_type) < 0) return; | |
| 3154 | + if (PyDict_SetItemString(d, "Pow", (PyObject*)Pow_type) < 0) return; | |
| 3155 | + if (PyDict_SetItemString(d, "LShift", (PyObject*)LShift_type) < 0) | |
| 3156 | + return; | |
| 3157 | + if (PyDict_SetItemString(d, "RShift", (PyObject*)RShift_type) < 0) | |
| 3158 | + return; | |
| 3159 | + if (PyDict_SetItemString(d, "BitOr", (PyObject*)BitOr_type) < 0) return; | |
| 3160 | + if (PyDict_SetItemString(d, "BitXor", (PyObject*)BitXor_type) < 0) | |
| 3161 | + return; | |
| 3162 | + if (PyDict_SetItemString(d, "BitAnd", (PyObject*)BitAnd_type) < 0) | |
| 3163 | + return; | |
| 3164 | + if (PyDict_SetItemString(d, "FloorDiv", (PyObject*)FloorDiv_type) < 0) | |
| 3165 | + return; | |
| 3166 | + if (PyDict_SetItemString(d, "unaryop", (PyObject*)unaryop_type) < 0) | |
| 3167 | + return; | |
| 3168 | + if (PyDict_SetItemString(d, "Invert", (PyObject*)Invert_type) < 0) | |
| 3169 | + return; | |
| 3170 | + if (PyDict_SetItemString(d, "Not", (PyObject*)Not_type) < 0) return; | |
| 3171 | + if (PyDict_SetItemString(d, "UAdd", (PyObject*)UAdd_type) < 0) return; | |
| 3172 | + if (PyDict_SetItemString(d, "USub", (PyObject*)USub_type) < 0) return; | |
| 3173 | + if (PyDict_SetItemString(d, "cmpop", (PyObject*)cmpop_type) < 0) return; | |
| 3174 | + if (PyDict_SetItemString(d, "Eq", (PyObject*)Eq_type) < 0) return; | |
| 3175 | + if (PyDict_SetItemString(d, "NotEq", (PyObject*)NotEq_type) < 0) return; | |
| 3176 | + if (PyDict_SetItemString(d, "Lt", (PyObject*)Lt_type) < 0) return; | |
| 3177 | + if (PyDict_SetItemString(d, "LtE", (PyObject*)LtE_type) < 0) return; | |
| 3178 | + if (PyDict_SetItemString(d, "Gt", (PyObject*)Gt_type) < 0) return; | |
| 3179 | + if (PyDict_SetItemString(d, "GtE", (PyObject*)GtE_type) < 0) return; | |
| 3180 | + if (PyDict_SetItemString(d, "Is", (PyObject*)Is_type) < 0) return; | |
| 3181 | + if (PyDict_SetItemString(d, "IsNot", (PyObject*)IsNot_type) < 0) return; | |
| 3182 | + if (PyDict_SetItemString(d, "In", (PyObject*)In_type) < 0) return; | |
| 3183 | + if (PyDict_SetItemString(d, "NotIn", (PyObject*)NotIn_type) < 0) return; | |
| 3184 | + if (PyDict_SetItemString(d, "comprehension", | |
| 3185 | + (PyObject*)comprehension_type) < 0) return; | |
| 3186 | + if (PyDict_SetItemString(d, "excepthandler", | |
| 3187 | + (PyObject*)excepthandler_type) < 0) return; | |
| 3188 | + if (PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) < | |
| 3189 | + 0) return; | |
| 3190 | + if (PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0) | |
| 3191 | + return; | |
| 3192 | + if (PyDict_SetItemString(d, "alias", (PyObject*)alias_type) < 0) return; | |
| 3193 | +} | |
| 3194 | + | |
| 3195 | + | |
| 3196 | +PyObject* PyAST_mod2obj(mod_ty t) | |
| 3197 | +{ | |
| 3198 | + init_types(); | |
| 3199 | + return ast2obj_mod(t); | |
| 3200 | +} | |
| 3201 | + | |
| 3202 | + | |
| @@ -0,0 +1,36 @@ | ||
| 1 | +#include "Python.h" | |
| 2 | +#include "asdl.h" | |
| 3 | + | |
| 4 | +asdl_seq * | |
| 5 | +asdl_seq_new(int size, PyArena *arena) | |
| 6 | +{ | |
| 7 | + asdl_seq *seq = NULL; | |
| 8 | + size_t n = sizeof(asdl_seq) + | |
| 9 | + (size ? (sizeof(void *) * (size - 1)) : 0); | |
| 10 | + | |
| 11 | + seq = (asdl_seq *)PyArena_Malloc(arena, n); | |
| 12 | + if (!seq) { | |
| 13 | + PyErr_NoMemory(); | |
| 14 | + return NULL; | |
| 15 | + } | |
| 16 | + memset(seq, 0, n); | |
| 17 | + seq->size = size; | |
| 18 | + return seq; | |
| 19 | +} | |
| 20 | + | |
| 21 | +asdl_int_seq * | |
| 22 | +asdl_int_seq_new(int size, PyArena *arena) | |
| 23 | +{ | |
| 24 | + asdl_int_seq *seq = NULL; | |
| 25 | + size_t n = sizeof(asdl_seq) + | |
| 26 | + (size ? (sizeof(int) * (size - 1)) : 0); | |
| 27 | + | |
| 28 | + seq = (asdl_int_seq *)PyArena_Malloc(arena, n); | |
| 29 | + if (!seq) { | |
| 30 | + PyErr_NoMemory(); | |
| 31 | + return NULL; | |
| 32 | + } | |
| 33 | + memset(seq, 0, n); | |
| 34 | + seq->size = size; | |
| 35 | + return seq; | |
| 36 | +} | |
| @@ -0,0 +1,3292 @@ | ||
| 1 | +/* | |
| 2 | + * This file includes functions to transform a concrete syntax tree (CST) to | |
| 3 | + * an abstract syntax tree (AST). The main function is PyAST_FromNode(). | |
| 4 | + * | |
| 5 | + */ | |
| 6 | +#include "Python.h" | |
| 7 | +#include "Python-ast.h" | |
| 8 | +#include "grammar.h" | |
| 9 | +#include "node.h" | |
| 10 | +#include "pyarena.h" | |
| 11 | +#include "ast.h" | |
| 12 | +#include "token.h" | |
| 13 | +#include "parsetok.h" | |
| 14 | +#include "graminit.h" | |
| 15 | + | |
| 16 | +#include <assert.h> | |
| 17 | + | |
| 18 | +/* XXX TO DO | |
| 19 | + - re-indent this file (should be done) | |
| 20 | + - internal error checking (freeing memory, etc.) | |
| 21 | + - syntax errors | |
| 22 | +*/ | |
| 23 | + | |
| 24 | +/* Data structure used internally */ | |
| 25 | +struct compiling { | |
| 26 | + char *c_encoding; /* source encoding */ | |
| 27 | + PyArena *c_arena; /* arena for allocating memeory */ | |
| 28 | +}; | |
| 29 | + | |
| 30 | +static asdl_seq *seq_for_testlist(struct compiling *, const node *); | |
| 31 | +static expr_ty ast_for_expr(struct compiling *, const node *); | |
| 32 | +static stmt_ty ast_for_stmt(struct compiling *, const node *); | |
| 33 | +static asdl_seq *ast_for_suite(struct compiling *, const node *); | |
| 34 | +static asdl_seq *ast_for_exprlist(struct compiling *, const node *, expr_context_ty); | |
| 35 | +static expr_ty ast_for_testlist(struct compiling *, const node *); | |
| 36 | +static expr_ty ast_for_testlist_gexp(struct compiling *, const node *); | |
| 37 | + | |
| 38 | +/* Note different signature for ast_for_call */ | |
| 39 | +static expr_ty ast_for_call(struct compiling *, const node *, expr_ty); | |
| 40 | + | |
| 41 | +static PyObject *parsenumber(const char *); | |
| 42 | +static PyObject *parsestr(const char *s, const char *encoding); | |
| 43 | +static PyObject *parsestrplus(struct compiling *, const node *n); | |
| 44 | + | |
| 45 | +#ifndef LINENO | |
| 46 | +#define LINENO(n) ((n)->n_lineno) | |
| 47 | +#endif | |
| 48 | + | |
| 49 | +static identifier | |
| 50 | +new_identifier(const char* n, PyArena *arena) { | |
| 51 | + PyObject* id = PyString_InternFromString(n); | |
| 52 | + PyArena_AddPyObject(arena, id); | |
| 53 | + return id; | |
| 54 | +} | |
| 55 | + | |
| 56 | +#define NEW_IDENTIFIER(n) new_identifier(STR(n), c->c_arena) | |
| 57 | + | |
| 58 | +/* This routine provides an invalid object for the syntax error. | |
| 59 | + The outermost routine must unpack this error and create the | |
| 60 | + proper object. We do this so that we don't have to pass | |
| 61 | + the filename to everything function. | |
| 62 | + | |
| 63 | + XXX Maybe we should just pass the filename... | |
| 64 | +*/ | |
| 65 | + | |
| 66 | +static int | |
| 67 | +ast_error(const node *n, const char *errstr) | |
| 68 | +{ | |
| 69 | + PyObject *u = Py_BuildValue("zi", errstr, LINENO(n)); | |
| 70 | + if (!u) | |
| 71 | + return 0; | |
| 72 | + PyErr_SetObject(PyExc_SyntaxError, u); | |
| 73 | + Py_DECREF(u); | |
| 74 | + return 0; | |
| 75 | +} | |
| 76 | + | |
| 77 | +static void | |
| 78 | +ast_error_finish(const char *filename) | |
| 79 | +{ | |
| 80 | + PyObject *type, *value, *tback, *errstr, *loc, *tmp; | |
| 81 | + long lineno; | |
| 82 | + | |
| 83 | + assert(PyErr_Occurred()); | |
| 84 | + if (!PyErr_ExceptionMatches(PyExc_SyntaxError)) | |
| 85 | + return; | |
| 86 | + | |
| 87 | + PyErr_Fetch(&type, &value, &tback); | |
| 88 | + errstr = PyTuple_GetItem(value, 0); | |
| 89 | + if (!errstr) | |
| 90 | + return; | |
| 91 | + Py_INCREF(errstr); | |
| 92 | + lineno = PyInt_AsLong(PyTuple_GetItem(value, 1)); | |
| 93 | + if (lineno == -1) { | |
| 94 | + Py_DECREF(errstr); | |
| 95 | + return; | |
| 96 | + } | |
| 97 | + Py_DECREF(value); | |
| 98 | + | |
| 99 | + loc = PyErr_ProgramText(filename, lineno); | |
| 100 | + if (!loc) { | |
| 101 | + Py_INCREF(Py_None); | |
| 102 | + loc = Py_None; | |
| 103 | + } | |
| 104 | + tmp = Py_BuildValue("(zlOO)", filename, lineno, Py_None, loc); | |
| 105 | + Py_DECREF(loc); | |
| 106 | + if (!tmp) { | |
| 107 | + Py_DECREF(errstr); | |
| 108 | + return; | |
| 109 | + } | |
| 110 | + value = PyTuple_Pack(2, errstr, tmp); | |
| 111 | + Py_DECREF(errstr); | |
| 112 | + Py_DECREF(tmp); | |
| 113 | + if (!value) | |
| 114 | + return; | |
| 115 | + PyErr_Restore(type, value, tback); | |
| 116 | +} | |
| 117 | + | |
| 118 | +/* num_stmts() returns number of contained statements. | |
| 119 | + | |
| 120 | + Use this routine to determine how big a sequence is needed for | |
| 121 | + the statements in a parse tree. Its raison d'etre is this bit of | |
| 122 | + grammar: | |
| 123 | + | |
| 124 | + stmt: simple_stmt | compound_stmt | |
| 125 | + simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE | |
| 126 | + | |
| 127 | + A simple_stmt can contain multiple small_stmt elements joined | |
| 128 | + by semicolons. If the arg is a simple_stmt, the number of | |
| 129 | + small_stmt elements is returned. | |
| 130 | +*/ | |
| 131 | + | |
| 132 | +static int | |
| 133 | +num_stmts(const node *n) | |
| 134 | +{ | |
| 135 | + int i, l; | |
| 136 | + node *ch; | |
| 137 | + | |
| 138 | + switch (TYPE(n)) { | |
| 139 | + case single_input: | |
| 140 | + if (TYPE(CHILD(n, 0)) == NEWLINE) | |
| 141 | + return 0; | |
| 142 | + else | |
| 143 | + return num_stmts(CHILD(n, 0)); | |
| 144 | + case file_input: | |
| 145 | + l = 0; | |
| 146 | + for (i = 0; i < NCH(n); i++) { | |
| 147 | + ch = CHILD(n, i); | |
| 148 | + if (TYPE(ch) == stmt) | |
| 149 | + l += num_stmts(ch); | |
| 150 | + } | |
| 151 | + return l; | |
| 152 | + case stmt: | |
| 153 | + return num_stmts(CHILD(n, 0)); | |
| 154 | + case compound_stmt: | |
| 155 | + return 1; | |
| 156 | + case simple_stmt: | |
| 157 | + return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */ | |
| 158 | + case suite: | |
| 159 | + if (NCH(n) == 1) | |
| 160 | + return num_stmts(CHILD(n, 0)); | |
| 161 | + else { | |
| 162 | + l = 0; | |
| 163 | + for (i = 2; i < (NCH(n) - 1); i++) | |
| 164 | + l += num_stmts(CHILD(n, i)); | |
| 165 | + return l; | |
| 166 | + } | |
| 167 | + default: { | |
| 168 | + char buf[128]; | |
| 169 | + | |
| 170 | + sprintf(buf, "Non-statement found: %d %d\n", | |
| 171 | + TYPE(n), NCH(n)); | |
| 172 | + Py_FatalError(buf); | |
| 173 | + } | |
| 174 | + } | |
| 175 | + assert(0); | |
| 176 | + return 0; | |
| 177 | +} | |
| 178 | + | |
| 179 | +/* Transform the CST rooted at node * to the appropriate AST | |
| 180 | +*/ | |
| 181 | + | |
| 182 | +mod_ty | |
| 183 | +PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename, | |
| 184 | + PyArena *arena) | |
| 185 | +{ | |
| 186 | + int i, j, k, num; | |
| 187 | + asdl_seq *stmts = NULL; | |
| 188 | + stmt_ty s; | |
| 189 | + node *ch; | |
| 190 | + struct compiling c; | |
| 191 | + | |
| 192 | + if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) { | |
| 193 | + c.c_encoding = "utf-8"; | |
| 194 | + if (TYPE(n) == encoding_decl) { | |
| 195 | + ast_error(n, "encoding declaration in Unicode string"); | |
| 196 | + goto error; | |
| 197 | + } | |
| 198 | + } else if (TYPE(n) == encoding_decl) { | |
| 199 | + c.c_encoding = STR(n); | |
| 200 | + n = CHILD(n, 0); | |
| 201 | + } else { | |
| 202 | + c.c_encoding = NULL; | |
| 203 | + } | |
| 204 | + c.c_arena = arena; | |
| 205 | + | |
| 206 | + k = 0; | |
| 207 | + switch (TYPE(n)) { | |
| 208 | + case file_input: | |
| 209 | + stmts = asdl_seq_new(num_stmts(n), arena); | |
| 210 | + if (!stmts) | |
| 211 | + return NULL; | |
| 212 | + for (i = 0; i < NCH(n) - 1; i++) { | |
| 213 | + ch = CHILD(n, i); | |
| 214 | + if (TYPE(ch) == NEWLINE) | |
| 215 | + continue; | |
| 216 | + REQ(ch, stmt); | |
| 217 | + num = num_stmts(ch); | |
| 218 | + if (num == 1) { | |
| 219 | + s = ast_for_stmt(&c, ch); | |
| 220 | + if (!s) | |
| 221 | + goto error; | |
| 222 | + asdl_seq_SET(stmts, k++, s); | |
| 223 | + } | |
| 224 | + else { | |
| 225 | + ch = CHILD(ch, 0); | |
| 226 | + REQ(ch, simple_stmt); | |
| 227 | + for (j = 0; j < num; j++) { | |
| 228 | + s = ast_for_stmt(&c, CHILD(ch, j * 2)); | |
| 229 | + if (!s) | |
| 230 | + goto error; | |
| 231 | + asdl_seq_SET(stmts, k++, s); | |
| 232 | + } | |
| 233 | + } | |
| 234 | + } | |
| 235 | + return Module(stmts, arena); | |
| 236 | + case eval_input: { | |
| 237 | + expr_ty testlist_ast; | |
| 238 | + | |
| 239 | + /* XXX Why not gen_for here? */ | |
| 240 | + testlist_ast = ast_for_testlist(&c, CHILD(n, 0)); | |
| 241 | + if (!testlist_ast) | |
| 242 | + goto error; | |
| 243 | + return Expression(testlist_ast, arena); | |
| 244 | + } | |
| 245 | + case single_input: | |
| 246 | + if (TYPE(CHILD(n, 0)) == NEWLINE) { | |
| 247 | + stmts = asdl_seq_new(1, arena); | |
| 248 | + if (!stmts) | |
| 249 | + goto error; | |
| 250 | + asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset, | |
| 251 | + arena)); | |
| 252 | + return Interactive(stmts, arena); | |
| 253 | + } | |
| 254 | + else { | |
| 255 | + n = CHILD(n, 0); | |
| 256 | + num = num_stmts(n); | |
| 257 | + stmts = asdl_seq_new(num, arena); | |
| 258 | + if (!stmts) | |
| 259 | + goto error; | |
| 260 | + if (num == 1) { | |
| 261 | + s = ast_for_stmt(&c, n); | |
| 262 | + if (!s) | |
| 263 | + goto error; | |
| 264 | + asdl_seq_SET(stmts, 0, s); | |
| 265 | + } | |
| 266 | + else { | |
| 267 | + /* Only a simple_stmt can contain multiple statements. */ | |
| 268 | + REQ(n, simple_stmt); | |
| 269 | + for (i = 0; i < NCH(n); i += 2) { | |
| 270 | + if (TYPE(CHILD(n, i)) == NEWLINE) | |
| 271 | + break; | |
| 272 | + s = ast_for_stmt(&c, CHILD(n, i)); | |
| 273 | + if (!s) | |
| 274 | + goto error; | |
| 275 | + asdl_seq_SET(stmts, i / 2, s); | |
| 276 | + } | |
| 277 | + } | |
| 278 | + | |
| 279 | + return Interactive(stmts, arena); | |
| 280 | + } | |
| 281 | + default: | |
| 282 | + goto error; | |
| 283 | + } | |
| 284 | + error: | |
| 285 | + ast_error_finish(filename); | |
| 286 | + return NULL; | |
| 287 | +} | |
| 288 | + | |
| 289 | +/* Return the AST repr. of the operator represented as syntax (|, ^, etc.) | |
| 290 | +*/ | |
| 291 | + | |
| 292 | +static operator_ty | |
| 293 | +get_operator(const node *n) | |
| 294 | +{ | |
| 295 | + switch (TYPE(n)) { | |
| 296 | + case VBAR: | |
| 297 | + return BitOr; | |
| 298 | + case CIRCUMFLEX: | |
| 299 | + return BitXor; | |
| 300 | + case AMPER: | |
| 301 | + return BitAnd; | |
| 302 | + case LEFTSHIFT: | |
| 303 | + return LShift; | |
| 304 | + case RIGHTSHIFT: | |
| 305 | + return RShift; | |
| 306 | + case PLUS: | |
| 307 | + return Add; | |
| 308 | + case MINUS: | |
| 309 | + return Sub; | |
| 310 | + case STAR: | |
| 311 | + return Mult; | |
| 312 | + case SLASH: | |
| 313 | + return Div; | |
| 314 | + case DOUBLESLASH: | |
| 315 | + return FloorDiv; | |
| 316 | + case PERCENT: | |
| 317 | + return Mod; | |
| 318 | + default: | |
| 319 | + return (operator_ty)0; | |
| 320 | + } | |
| 321 | +} | |
| 322 | + | |
| 323 | +/* Set the context ctx for expr_ty e, recursively traversing e. | |
| 324 | + | |
| 325 | + Only sets context for expr kinds that "can appear in assignment context" | |
| 326 | + (according to ../Parser/Python.asdl). For other expr kinds, it sets | |
| 327 | + an appropriate syntax error and returns false. | |
| 328 | +*/ | |
| 329 | + | |
| 330 | +static int | |
| 331 | +set_context(expr_ty e, expr_context_ty ctx, const node *n) | |
| 332 | +{ | |
| 333 | + asdl_seq *s = NULL; | |
| 334 | + /* If a particular expression type can't be used for assign / delete, | |
| 335 | + set expr_name to its name and an error message will be generated. | |
| 336 | + */ | |
| 337 | + const char* expr_name = NULL; | |
| 338 | + | |
| 339 | + /* The ast defines augmented store and load contexts, but the | |
| 340 | + implementation here doesn't actually use them. The code may be | |
| 341 | + a little more complex than necessary as a result. It also means | |
| 342 | + that expressions in an augmented assignment have a Store context. | |
| 343 | + Consider restructuring so that augmented assignment uses | |
| 344 | + set_context(), too. | |
| 345 | + */ | |
| 346 | + assert(ctx != AugStore && ctx != AugLoad); | |
| 347 | + | |
| 348 | + switch (e->kind) { | |
| 349 | + case Attribute_kind: | |
| 350 | + if (ctx == Store && | |
| 351 | + !strcmp(PyString_AS_STRING(e->v.Attribute.attr), "None")) { | |
| 352 | + return ast_error(n, "assignment to None"); | |
| 353 | + } | |
| 354 | + e->v.Attribute.ctx = ctx; | |
| 355 | + break; | |
| 356 | + case Subscript_kind: | |
| 357 | + e->v.Subscript.ctx = ctx; | |
| 358 | + break; | |
| 359 | + case Name_kind: | |
| 360 | + if (ctx == Store && | |
| 361 | + !strcmp(PyString_AS_STRING(e->v.Name.id), "None")) { | |
| 362 | + return ast_error(n, "assignment to None"); | |
| 363 | + } | |
| 364 | + e->v.Name.ctx = ctx; | |
| 365 | + break; | |
| 366 | + case List_kind: | |
| 367 | + e->v.List.ctx = ctx; | |
| 368 | + s = e->v.List.elts; | |
| 369 | + break; | |
| 370 | + case Tuple_kind: | |
| 371 | + if (asdl_seq_LEN(e->v.Tuple.elts) == 0) | |
| 372 | + return ast_error(n, "can't assign to ()"); | |
| 373 | + e->v.Tuple.ctx = ctx; | |
| 374 | + s = e->v.Tuple.elts; | |
| 375 | + break; | |
| 376 | + case Lambda_kind: | |
| 377 | + expr_name = "lambda"; | |
| 378 | + break; | |
| 379 | + case Call_kind: | |
| 380 | + expr_name = "function call"; | |
| 381 | + break; | |
| 382 | + case BoolOp_kind: | |
| 383 | + case BinOp_kind: | |
| 384 | + case UnaryOp_kind: | |
| 385 | + expr_name = "operator"; | |
| 386 | + break; | |
| 387 | + case GeneratorExp_kind: | |
| 388 | + expr_name = "generator expression"; | |
| 389 | + break; | |
| 390 | + case Yield_kind: | |
| 391 | + expr_name = "yield expression"; | |
| 392 | + break; | |
| 393 | + case ListComp_kind: | |
| 394 | + expr_name = "list comprehension"; | |
| 395 | + break; | |
| 396 | + case Dict_kind: | |
| 397 | + case Num_kind: | |
| 398 | + case Str_kind: | |
| 399 | + expr_name = "literal"; | |
| 400 | + break; | |
| 401 | + case Compare_kind: | |
| 402 | + expr_name = "comparison"; | |
| 403 | + break; | |
| 404 | + case Repr_kind: | |
| 405 | + expr_name = "repr"; | |
| 406 | + break; | |
| 407 | + case IfExp_kind: | |
| 408 | + expr_name = "conditional expression"; | |
| 409 | + break; | |
| 410 | + default: | |
| 411 | + PyErr_Format(PyExc_SystemError, | |
| 412 | + "unexpected expression in assignment %d (line %d)", | |
| 413 | + e->kind, e->lineno); | |
| 414 | + return 0; | |
| 415 | + } | |
| 416 | + /* Check for error string set by switch */ | |
| 417 | + if (expr_name) { | |
| 418 | + char buf[300]; | |
| 419 | + PyOS_snprintf(buf, sizeof(buf), | |
| 420 | + "can't %s %s", | |
| 421 | + ctx == Store ? "assign to" : "delete", | |
| 422 | + expr_name); | |
| 423 | + return ast_error(n, buf); | |
| 424 | + } | |
| 425 | + | |
| 426 | + /* If the LHS is a list or tuple, we need to set the assignment | |
| 427 | + context for all the contained elements. | |
| 428 | + */ | |
| 429 | + if (s) { | |
| 430 | + int i; | |
| 431 | + | |
| 432 | + for (i = 0; i < asdl_seq_LEN(s); i++) { | |
| 433 | + if (!set_context((expr_ty)asdl_seq_GET(s, i), ctx, n)) | |
| 434 | + return 0; | |
| 435 | + } | |
| 436 | + } | |
| 437 | + return 1; | |
| 438 | +} | |
| 439 | + | |
| 440 | +static operator_ty | |
| 441 | +ast_for_augassign(const node *n) | |
| 442 | +{ | |
| 443 | + REQ(n, augassign); | |
| 444 | + n = CHILD(n, 0); | |
| 445 | + switch (STR(n)[0]) { | |
| 446 | + case '+': | |
| 447 | + return Add; | |
| 448 | + case '-': | |
| 449 | + return Sub; | |
| 450 | + case '/': | |
| 451 | + if (STR(n)[1] == '/') | |
| 452 | + return FloorDiv; | |
| 453 | + else | |
| 454 | + return Div; | |
| 455 | + case '%': | |
| 456 | + return Mod; | |
| 457 | + case '<': | |
| 458 | + return LShift; | |
| 459 | + case '>': | |
| 460 | + return RShift; | |
| 461 | + case '&': | |
| 462 | + return BitAnd; | |
| 463 | + case '^': | |
| 464 | + return BitXor; | |
| 465 | + case '|': | |
| 466 | + return BitOr; | |
| 467 | + case '*': | |
| 468 | + if (STR(n)[1] == '*') | |
| 469 | + return Pow; | |
| 470 | + else | |
| 471 | + return Mult; | |
| 472 | + default: | |
| 473 | + PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n)); | |
| 474 | + return (operator_ty)0; | |
| 475 | + } | |
| 476 | +} | |
| 477 | + | |
| 478 | +static cmpop_ty | |
| 479 | +ast_for_comp_op(const node *n) | |
| 480 | +{ | |
| 481 | + /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is' | |
| 482 | + |'is' 'not' | |
| 483 | + */ | |
| 484 | + REQ(n, comp_op); | |
| 485 | + if (NCH(n) == 1) { | |
| 486 | + n = CHILD(n, 0); | |
| 487 | + switch (TYPE(n)) { | |
| 488 | + case LESS: | |
| 489 | + return Lt; | |
| 490 | + case GREATER: | |
| 491 | + return Gt; | |
| 492 | + case EQEQUAL: /* == */ | |
| 493 | + return Eq; | |
| 494 | + case LESSEQUAL: | |
| 495 | + return LtE; | |
| 496 | + case GREATEREQUAL: | |
| 497 | + return GtE; | |
| 498 | + case NOTEQUAL: | |
| 499 | + return NotEq; | |
| 500 | + case NAME: | |
| 501 | + if (strcmp(STR(n), "in") == 0) | |
| 502 | + return In; | |
| 503 | + if (strcmp(STR(n), "is") == 0) | |
| 504 | + return Is; | |
| 505 | + default: | |
| 506 | + PyErr_Format(PyExc_SystemError, "invalid comp_op: %s", | |
| 507 | + STR(n)); | |
| 508 | + return (cmpop_ty)0; | |
| 509 | + } | |
| 510 | + } | |
| 511 | + else if (NCH(n) == 2) { | |
| 512 | + /* handle "not in" and "is not" */ | |
| 513 | + switch (TYPE(CHILD(n, 0))) { | |
| 514 | + case NAME: | |
| 515 | + if (strcmp(STR(CHILD(n, 1)), "in") == 0) | |
| 516 | + return NotIn; | |
| 517 | + if (strcmp(STR(CHILD(n, 0)), "is") == 0) | |
| 518 | + return IsNot; | |
| 519 | + default: | |
| 520 | + PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s", | |
| 521 | + STR(CHILD(n, 0)), STR(CHILD(n, 1))); | |
| 522 | + return (cmpop_ty)0; | |
| 523 | + } | |
| 524 | + } | |
| 525 | + PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children", | |
| 526 | + NCH(n)); | |
| 527 | + return (cmpop_ty)0; | |
| 528 | +} | |
| 529 | + | |
| 530 | +static asdl_seq * | |
| 531 | +seq_for_testlist(struct compiling *c, const node *n) | |
| 532 | +{ | |
| 533 | + /* testlist: test (',' test)* [','] */ | |
| 534 | + asdl_seq *seq; | |
| 535 | + expr_ty expression; | |
| 536 | + int i; | |
| 537 | + assert(TYPE(n) == testlist | |
| 538 | + || TYPE(n) == listmaker | |
| 539 | + || TYPE(n) == testlist_gexp | |
| 540 | + || TYPE(n) == testlist_safe | |
| 541 | + || TYPE(n) == testlist1 | |
| 542 | + ); | |
| 543 | + | |
| 544 | + seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); | |
| 545 | + if (!seq) | |
| 546 | + return NULL; | |
| 547 | + | |
| 548 | + for (i = 0; i < NCH(n); i += 2) { | |
| 549 | + assert(TYPE(CHILD(n, i)) == test || TYPE(CHILD(n, i)) == old_test); | |
| 550 | + | |
| 551 | + expression = ast_for_expr(c, CHILD(n, i)); | |
| 552 | + if (!expression) | |
| 553 | + return NULL; | |
| 554 | + | |
| 555 | + assert(i / 2 < seq->size); | |
| 556 | + asdl_seq_SET(seq, i / 2, expression); | |
| 557 | + } | |
| 558 | + return seq; | |
| 559 | +} | |
| 560 | + | |
| 561 | +static expr_ty | |
| 562 | +compiler_complex_args(struct compiling *c, const node *n) | |
| 563 | +{ | |
| 564 | + int i, len = (NCH(n) + 1) / 2; | |
| 565 | + expr_ty result; | |
| 566 | + asdl_seq *args = asdl_seq_new(len, c->c_arena); | |
| 567 | + if (!args) | |
| 568 | + return NULL; | |
| 569 | + | |
| 570 | + /* fpdef: NAME | '(' fplist ')' | |
| 571 | + fplist: fpdef (',' fpdef)* [','] | |
| 572 | + */ | |
| 573 | + REQ(n, fplist); | |
| 574 | + for (i = 0; i < len; i++) { | |
| 575 | + const node *fpdef_node = CHILD(n, 2*i); | |
| 576 | + const node *child; | |
| 577 | + expr_ty arg; | |
| 578 | +set_name: | |
| 579 | + /* fpdef_node is either a NAME or an fplist */ | |
| 580 | + child = CHILD(fpdef_node, 0); | |
| 581 | + if (TYPE(child) == NAME) { | |
| 582 | + if (!strcmp(STR(child), "None")) { | |
| 583 | + ast_error(child, "assignment to None"); | |
| 584 | + return NULL; | |
| 585 | + } | |
| 586 | + arg = Name(NEW_IDENTIFIER(child), Store, LINENO(child), | |
| 587 | + child->n_col_offset, c->c_arena); | |
| 588 | + } | |
| 589 | + else { | |
| 590 | + assert(TYPE(fpdef_node) == fpdef); | |
| 591 | + /* fpdef_node[0] is not a name, so it must be a '(', get CHILD[1] */ | |
| 592 | + child = CHILD(fpdef_node, 1); | |
| 593 | + assert(TYPE(child) == fplist); | |
| 594 | + /* NCH == 1 means we have (x), we need to elide the extra parens */ | |
| 595 | + if (NCH(child) == 1) { | |
| 596 | + fpdef_node = CHILD(child, 0); | |
| 597 | + assert(TYPE(fpdef_node) == fpdef); | |
| 598 | + goto set_name; | |
| 599 | + } | |
| 600 | + arg = compiler_complex_args(c, child); | |
| 601 | + } | |
| 602 | + asdl_seq_SET(args, i, arg); | |
| 603 | + } | |
| 604 | + | |
| 605 | + result = Tuple(args, Store, LINENO(n), n->n_col_offset, c->c_arena); | |
| 606 | + if (!set_context(result, Store, n)) | |
| 607 | + return NULL; | |
| 608 | + return result; | |
| 609 | +} | |
| 610 | + | |
| 611 | + | |
| 612 | +/* Create AST for argument list. */ | |
| 613 | + | |
| 614 | +static arguments_ty | |
| 615 | +ast_for_arguments(struct compiling *c, const node *n) | |
| 616 | +{ | |
| 617 | + /* parameters: '(' [varargslist] ')' | |
| 618 | + varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] | |
| 619 | + | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [','] | |
| 620 | + */ | |
| 621 | + int i, j, k, n_args = 0, n_defaults = 0, found_default = 0; | |
| 622 | + asdl_seq *args, *defaults; | |
| 623 | + identifier vararg = NULL, kwarg = NULL; | |
| 624 | + node *ch; | |
| 625 | + | |
| 626 | + if (TYPE(n) == parameters) { | |
| 627 | + if (NCH(n) == 2) /* () as argument list */ | |
| 628 | + return arguments(NULL, NULL, NULL, NULL, c->c_arena); | |
| 629 | + n = CHILD(n, 1); | |
| 630 | + } | |
| 631 | + REQ(n, varargslist); | |
| 632 | + | |
| 633 | + /* first count the number of normal args & defaults */ | |
| 634 | + for (i = 0; i < NCH(n); i++) { | |
| 635 | + ch = CHILD(n, i); | |
| 636 | + if (TYPE(ch) == fpdef) | |
| 637 | + n_args++; | |
| 638 | + if (TYPE(ch) == EQUAL) | |
| 639 | + n_defaults++; | |
| 640 | + } | |
| 641 | + args = (n_args ? asdl_seq_new(n_args, c->c_arena) : NULL); | |
| 642 | + if (!args && n_args) | |
| 643 | + return NULL; /* Don't need to goto error; no objects allocated */ | |
| 644 | + defaults = (n_defaults ? asdl_seq_new(n_defaults, c->c_arena) : NULL); | |
| 645 | + if (!defaults && n_defaults) | |
| 646 | + return NULL; /* Don't need to goto error; no objects allocated */ | |
| 647 | + | |
| 648 | + /* fpdef: NAME | '(' fplist ')' | |
| 649 | + fplist: fpdef (',' fpdef)* [','] | |
| 650 | + */ | |
| 651 | + i = 0; | |
| 652 | + j = 0; /* index for defaults */ | |
| 653 | + k = 0; /* index for args */ | |
| 654 | + while (i < NCH(n)) { | |
| 655 | + ch = CHILD(n, i); | |
| 656 | + switch (TYPE(ch)) { | |
| 657 | + case fpdef: | |
| 658 | + handle_fpdef: | |
| 659 | + /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is | |
| 660 | + anything other than EQUAL or a comma? */ | |
| 661 | + /* XXX Should NCH(n) check be made a separate check? */ | |
| 662 | + if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) { | |
| 663 | + expr_ty expression = ast_for_expr(c, CHILD(n, i + 2)); | |
| 664 | + if (!expression) | |
| 665 | + goto error; | |
| 666 | + assert(defaults != NULL); | |
| 667 | + asdl_seq_SET(defaults, j++, expression); | |
| 668 | + i += 2; | |
| 669 | + found_default = 1; | |
| 670 | + } | |
| 671 | + else if (found_default) { | |
| 672 | + ast_error(n, | |
| 673 | + "non-default argument follows default argument"); | |
| 674 | + goto error; | |
| 675 | + } | |
| 676 | + if (NCH(ch) == 3) { | |
| 677 | + ch = CHILD(ch, 1); | |
| 678 | + /* def foo((x)): is not complex, special case. */ | |
| 679 | + if (NCH(ch) != 1) { | |
| 680 | + /* We have complex arguments, setup for unpacking. */ | |
| 681 | + asdl_seq_SET(args, k++, compiler_complex_args(c, ch)); | |
| 682 | + } else { | |
| 683 | + /* def foo((x)): setup for checking NAME below. */ | |
| 684 | + /* Loop because there can be many parens and tuple | |
| 685 | + upacking mixed in. */ | |
| 686 | + ch = CHILD(ch, 0); | |
| 687 | + assert(TYPE(ch) == fpdef); | |
| 688 | + goto handle_fpdef; | |
| 689 | + } | |
| 690 | + } | |
| 691 | + if (TYPE(CHILD(ch, 0)) == NAME) { | |
| 692 | + expr_ty name; | |
| 693 | + if (!strcmp(STR(CHILD(ch, 0)), "None")) { | |
| 694 | + ast_error(CHILD(ch, 0), "assignment to None"); | |
| 695 | + goto error; | |
| 696 | + } | |
| 697 | + name = Name(NEW_IDENTIFIER(CHILD(ch, 0)), | |
| 698 | + Param, LINENO(ch), ch->n_col_offset, | |
| 699 | + c->c_arena); | |
| 700 | + if (!name) | |
| 701 | + goto error; | |
| 702 | + asdl_seq_SET(args, k++, name); | |
| 703 | + | |
| 704 | + } | |
| 705 | + i += 2; /* the name and the comma */ | |
| 706 | + break; | |
| 707 | + case STAR: | |
| 708 | + if (!strcmp(STR(CHILD(n, i+1)), "None")) { | |
| 709 | + ast_error(CHILD(n, i+1), "assignment to None"); | |
| 710 | + goto error; | |
| 711 | + } | |
| 712 | + vararg = NEW_IDENTIFIER(CHILD(n, i+1)); | |
| 713 | + i += 3; | |
| 714 | + break; | |
| 715 | + case DOUBLESTAR: | |
| 716 | + if (!strcmp(STR(CHILD(n, i+1)), "None")) { | |
| 717 | + ast_error(CHILD(n, i+1), "assignment to None"); | |
| 718 | + goto error; | |
| 719 | + } | |
| 720 | + kwarg = NEW_IDENTIFIER(CHILD(n, i+1)); | |
| 721 | + i += 3; | |
| 722 | + break; | |
| 723 | + default: | |
| 724 | + PyErr_Format(PyExc_SystemError, | |
| 725 | + "unexpected node in varargslist: %d @ %d", | |
| 726 | + TYPE(ch), i); | |
| 727 | + goto error; | |
| 728 | + } | |
| 729 | + } | |
| 730 | + | |
| 731 | + return arguments(args, vararg, kwarg, defaults, c->c_arena); | |
| 732 | + | |
| 733 | + error: | |
| 734 | + Py_XDECREF(vararg); | |
| 735 | + Py_XDECREF(kwarg); | |
| 736 | + return NULL; | |
| 737 | +} | |
| 738 | + | |
| 739 | +static expr_ty | |
| 740 | +ast_for_dotted_name(struct compiling *c, const node *n) | |
| 741 | +{ | |
| 742 | + expr_ty e; | |
| 743 | + identifier id; | |
| 744 | + int lineno, col_offset; | |
| 745 | + int i; | |
| 746 | + | |
| 747 | + REQ(n, dotted_name); | |
| 748 | + | |
| 749 | + lineno = LINENO(n); | |
| 750 | + col_offset = n->n_col_offset; | |
| 751 | + | |
| 752 | + id = NEW_IDENTIFIER(CHILD(n, 0)); | |
| 753 | + if (!id) | |
| 754 | + return NULL; | |
| 755 | + e = Name(id, Load, lineno, col_offset, c->c_arena); | |
| 756 | + if (!e) | |
| 757 | + return NULL; | |
| 758 | + | |
| 759 | + for (i = 2; i < NCH(n); i+=2) { | |
| 760 | + id = NEW_IDENTIFIER(CHILD(n, i)); | |
| 761 | + if (!id) | |
| 762 | + return NULL; | |
| 763 | + e = Attribute(e, id, Load, lineno, col_offset, c->c_arena); | |
| 764 | + if (!e) | |
| 765 | + return NULL; | |
| 766 | + } | |
| 767 | + | |
| 768 | + return e; | |
| 769 | +} | |
| 770 | + | |
| 771 | +static expr_ty | |
| 772 | +ast_for_decorator(struct compiling *c, const node *n) | |
| 773 | +{ | |
| 774 | + /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */ | |
| 775 | + expr_ty d = NULL; | |
| 776 | + expr_ty name_expr; | |
| 777 | + | |
| 778 | + REQ(n, decorator); | |
| 779 | + REQ(CHILD(n, 0), AT); | |
| 780 | + REQ(RCHILD(n, -1), NEWLINE); | |
| 781 | + | |
| 782 | + name_expr = ast_for_dotted_name(c, CHILD(n, 1)); | |
| 783 | + if (!name_expr) | |
| 784 | + return NULL; | |
| 785 | + | |
| 786 | + if (NCH(n) == 3) { /* No arguments */ | |
| 787 | + d = name_expr; | |
| 788 | + name_expr = NULL; | |
| 789 | + } | |
| 790 | + else if (NCH(n) == 5) { /* Call with no arguments */ | |
| 791 | + d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n), | |
| 792 | + n->n_col_offset, c->c_arena); | |
| 793 | + if (!d) | |
| 794 | + return NULL; | |
| 795 | + name_expr = NULL; | |
| 796 | + } | |
| 797 | + else { | |
| 798 | + d = ast_for_call(c, CHILD(n, 3), name_expr); | |
| 799 | + if (!d) | |
| 800 | + return NULL; | |
| 801 | + name_expr = NULL; | |
| 802 | + } | |
| 803 | + | |
| 804 | + return d; | |
| 805 | +} | |
| 806 | + | |
| 807 | +static asdl_seq* | |
| 808 | +ast_for_decorators(struct compiling *c, const node *n) | |
| 809 | +{ | |
| 810 | + asdl_seq* decorator_seq; | |
| 811 | + expr_ty d; | |
| 812 | + int i; | |
| 813 | + | |
| 814 | + REQ(n, decorators); | |
| 815 | + decorator_seq = asdl_seq_new(NCH(n), c->c_arena); | |
| 816 | + if (!decorator_seq) | |
| 817 | + return NULL; | |
| 818 | + | |
| 819 | + for (i = 0; i < NCH(n); i++) { | |
| 820 | + d = ast_for_decorator(c, CHILD(n, i)); | |
| 821 | + if (!d) | |
| 822 | + return NULL; | |
| 823 | + asdl_seq_SET(decorator_seq, i, d); | |
| 824 | + } | |
| 825 | + return decorator_seq; | |
| 826 | +} | |
| 827 | + | |
| 828 | +static stmt_ty | |
| 829 | +ast_for_funcdef(struct compiling *c, const node *n) | |
| 830 | +{ | |
| 831 | + /* funcdef: 'def' [decorators] NAME parameters ':' suite */ | |
| 832 | + identifier name; | |
| 833 | + arguments_ty args; | |
| 834 | + asdl_seq *body; | |
| 835 | + asdl_seq *decorator_seq = NULL; | |
| 836 | + int name_i; | |
| 837 | + | |
| 838 | + REQ(n, funcdef); | |
| 839 | + | |
| 840 | + if (NCH(n) == 6) { /* decorators are present */ | |
| 841 | + decorator_seq = ast_for_decorators(c, CHILD(n, 0)); | |
| 842 | + if (!decorator_seq) | |
| 843 | + return NULL; | |
| 844 | + name_i = 2; | |
| 845 | + } | |
| 846 | + else { | |
| 847 | + name_i = 1; | |
| 848 | + } | |
| 849 | + | |
| 850 | + name = NEW_IDENTIFIER(CHILD(n, name_i)); | |
| 851 | + if (!name) | |
| 852 | + return NULL; | |
| 853 | + else if (!strcmp(STR(CHILD(n, name_i)), "None")) { | |
| 854 | + ast_error(CHILD(n, name_i), "assignment to None"); | |
| 855 | + return NULL; | |
| 856 | + } | |
| 857 | + args = ast_for_arguments(c, CHILD(n, name_i + 1)); | |
| 858 | + if (!args) | |
| 859 | + return NULL; | |
| 860 | + body = ast_for_suite(c, CHILD(n, name_i + 3)); | |
| 861 | + if (!body) | |
| 862 | + return NULL; | |
| 863 | + | |
| 864 | + return FunctionDef(name, args, body, decorator_seq, LINENO(n), | |
| 865 | + n->n_col_offset, c->c_arena); | |
| 866 | +} | |
| 867 | + | |
| 868 | +static expr_ty | |
| 869 | +ast_for_lambdef(struct compiling *c, const node *n) | |
| 870 | +{ | |
| 871 | + /* lambdef: 'lambda' [varargslist] ':' test */ | |
| 872 | + arguments_ty args; | |
| 873 | + expr_ty expression; | |
| 874 | + | |
| 875 | + if (NCH(n) == 3) { | |
| 876 | + args = arguments(NULL, NULL, NULL, NULL, c->c_arena); | |
| 877 | + if (!args) | |
| 878 | + return NULL; | |
| 879 | + expression = ast_for_expr(c, CHILD(n, 2)); | |
| 880 | + if (!expression) | |
| 881 | + return NULL; | |
| 882 | + } | |
| 883 | + else { | |
| 884 | + args = ast_for_arguments(c, CHILD(n, 1)); | |
| 885 | + if (!args) | |
| 886 | + return NULL; | |
| 887 | + expression = ast_for_expr(c, CHILD(n, 3)); | |
| 888 | + if (!expression) | |
| 889 | + return NULL; | |
| 890 | + } | |
| 891 | + | |
| 892 | + return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena); | |
| 893 | +} | |
| 894 | + | |
| 895 | +static expr_ty | |
| 896 | +ast_for_ifexpr(struct compiling *c, const node *n) | |
| 897 | +{ | |
| 898 | + /* test: or_test 'if' or_test 'else' test */ | |
| 899 | + expr_ty expression, body, orelse; | |
| 900 | + | |
| 901 | + assert(NCH(n) == 5); | |
| 902 | + body = ast_for_expr(c, CHILD(n, 0)); | |
| 903 | + if (!body) | |
| 904 | + return NULL; | |
| 905 | + expression = ast_for_expr(c, CHILD(n, 2)); | |
| 906 | + if (!expression) | |
| 907 | + return NULL; | |
| 908 | + orelse = ast_for_expr(c, CHILD(n, 4)); | |
| 909 | + if (!orelse) | |
| 910 | + return NULL; | |
| 911 | + return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset, | |
| 912 | + c->c_arena); | |
| 913 | +} | |
| 914 | + | |
| 915 | +/* Count the number of 'for' loop in a list comprehension. | |
| 916 | + | |
| 917 | + Helper for ast_for_listcomp(). | |
| 918 | +*/ | |
| 919 | + | |
| 920 | +static int | |
| 921 | +count_list_fors(const node *n) | |
| 922 | +{ | |
| 923 | + int n_fors = 0; | |
| 924 | + node *ch = CHILD(n, 1); | |
| 925 | + | |
| 926 | + count_list_for: | |
| 927 | + n_fors++; | |
| 928 | + REQ(ch, list_for); | |
| 929 | + if (NCH(ch) == 5) | |
| 930 | + ch = CHILD(ch, 4); | |
| 931 | + else | |
| 932 | + return n_fors; | |
| 933 | + count_list_iter: | |
| 934 | + REQ(ch, list_iter); | |
| 935 | + ch = CHILD(ch, 0); | |
| 936 | + if (TYPE(ch) == list_for) | |
| 937 | + goto count_list_for; | |
| 938 | + else if (TYPE(ch) == list_if) { | |
| 939 | + if (NCH(ch) == 3) { | |
| 940 | + ch = CHILD(ch, 2); | |
| 941 | + goto count_list_iter; | |
| 942 | + } | |
| 943 | + else | |
| 944 | + return n_fors; | |
| 945 | + } | |
| 946 | + | |
| 947 | + /* Should never be reached */ | |
| 948 | + PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors"); | |
| 949 | + return -1; | |
| 950 | +} | |
| 951 | + | |
| 952 | +/* Count the number of 'if' statements in a list comprehension. | |
| 953 | + | |
| 954 | + Helper for ast_for_listcomp(). | |
| 955 | +*/ | |
| 956 | + | |
| 957 | +static int | |
| 958 | +count_list_ifs(const node *n) | |
| 959 | +{ | |
| 960 | + int n_ifs = 0; | |
| 961 | + | |
| 962 | + count_list_iter: | |
| 963 | + REQ(n, list_iter); | |
| 964 | + if (TYPE(CHILD(n, 0)) == list_for) | |
| 965 | + return n_ifs; | |
| 966 | + n = CHILD(n, 0); | |
| 967 | + REQ(n, list_if); | |
| 968 | + n_ifs++; | |
| 969 | + if (NCH(n) == 2) | |
| 970 | + return n_ifs; | |
| 971 | + n = CHILD(n, 2); | |
| 972 | + goto count_list_iter; | |
| 973 | +} | |
| 974 | + | |
| 975 | +static expr_ty | |
| 976 | +ast_for_listcomp(struct compiling *c, const node *n) | |
| 977 | +{ | |
| 978 | + /* listmaker: test ( list_for | (',' test)* [','] ) | |
| 979 | + list_for: 'for' exprlist 'in' testlist_safe [list_iter] | |
| 980 | + list_iter: list_for | list_if | |
| 981 | + list_if: 'if' test [list_iter] | |
| 982 | + testlist_safe: test [(',' test)+ [',']] | |
| 983 | + */ | |
| 984 | + expr_ty elt; | |
| 985 | + asdl_seq *listcomps; | |
| 986 | + int i, n_fors; | |
| 987 | + node *ch; | |
| 988 | + | |
| 989 | + REQ(n, listmaker); | |
| 990 | + assert(NCH(n) > 1); | |
| 991 | + | |
| 992 | + elt = ast_for_expr(c, CHILD(n, 0)); | |
| 993 | + if (!elt) | |
| 994 | + return NULL; | |
| 995 | + | |
| 996 | + n_fors = count_list_fors(n); | |
| 997 | + if (n_fors == -1) | |
| 998 | + return NULL; | |
| 999 | + | |
| 1000 | + listcomps = asdl_seq_new(n_fors, c->c_arena); | |
| 1001 | + if (!listcomps) | |
| 1002 | + return NULL; | |
| 1003 | + | |
| 1004 | + ch = CHILD(n, 1); | |
| 1005 | + for (i = 0; i < n_fors; i++) { | |
| 1006 | + comprehension_ty lc; | |
| 1007 | + asdl_seq *t; | |
| 1008 | + expr_ty expression; | |
| 1009 | + node *for_ch; | |
| 1010 | + | |
| 1011 | + REQ(ch, list_for); | |
| 1012 | + | |
| 1013 | + for_ch = CHILD(ch, 1); | |
| 1014 | + t = ast_for_exprlist(c, for_ch, Store); | |
| 1015 | + if (!t) | |
| 1016 | + return NULL; | |
| 1017 | + expression = ast_for_testlist(c, CHILD(ch, 3)); | |
| 1018 | + if (!expression) | |
| 1019 | + return NULL; | |
| 1020 | + | |
| 1021 | + /* Check the # of children rather than the length of t, since | |
| 1022 | + [x for x, in ... ] has 1 element in t, but still requires a Tuple. */ | |
| 1023 | + if (NCH(for_ch) == 1) | |
| 1024 | + lc = comprehension((expr_ty)asdl_seq_GET(t, 0), expression, NULL, | |
| 1025 | + c->c_arena); | |
| 1026 | + else | |
| 1027 | + lc = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset, | |
| 1028 | + c->c_arena), | |
| 1029 | + expression, NULL, c->c_arena); | |
| 1030 | + if (!lc) | |
| 1031 | + return NULL; | |
| 1032 | + | |
| 1033 | + if (NCH(ch) == 5) { | |
| 1034 | + int j, n_ifs; | |
| 1035 | + asdl_seq *ifs; | |
| 1036 | + expr_ty list_for_expr; | |
| 1037 | + | |
| 1038 | + ch = CHILD(ch, 4); | |
| 1039 | + n_ifs = count_list_ifs(ch); | |
| 1040 | + if (n_ifs == -1) | |
| 1041 | + return NULL; | |
| 1042 | + | |
| 1043 | + ifs = asdl_seq_new(n_ifs, c->c_arena); | |
| 1044 | + if (!ifs) | |
| 1045 | + return NULL; | |
| 1046 | + | |
| 1047 | + for (j = 0; j < n_ifs; j++) { | |
| 1048 | + REQ(ch, list_iter); | |
| 1049 | + ch = CHILD(ch, 0); | |
| 1050 | + REQ(ch, list_if); | |
| 1051 | + | |
| 1052 | + list_for_expr = ast_for_expr(c, CHILD(ch, 1)); | |
| 1053 | + if (!list_for_expr) | |
| 1054 | + return NULL; | |
| 1055 | + | |
| 1056 | + asdl_seq_SET(ifs, j, list_for_expr); | |
| 1057 | + if (NCH(ch) == 3) | |
| 1058 | + ch = CHILD(ch, 2); | |
| 1059 | + } | |
| 1060 | + /* on exit, must guarantee that ch is a list_for */ | |
| 1061 | + if (TYPE(ch) == list_iter) | |
| 1062 | + ch = CHILD(ch, 0); | |
| 1063 | + lc->ifs = ifs; | |
| 1064 | + } | |
| 1065 | + asdl_seq_SET(listcomps, i, lc); | |
| 1066 | + } | |
| 1067 | + | |
| 1068 | + return ListComp(elt, listcomps, LINENO(n), n->n_col_offset, c->c_arena); | |
| 1069 | +} | |
| 1070 | + | |
| 1071 | +/* | |
| 1072 | + Count the number of 'for' loops in a generator expression. | |
| 1073 | + | |
| 1074 | + Helper for ast_for_genexp(). | |
| 1075 | +*/ | |
| 1076 | + | |
| 1077 | +static int | |
| 1078 | +count_gen_fors(const node *n) | |
| 1079 | +{ | |
| 1080 | + int n_fors = 0; | |
| 1081 | + node *ch = CHILD(n, 1); | |
| 1082 | + | |
| 1083 | + count_gen_for: | |
| 1084 | + n_fors++; | |
| 1085 | + REQ(ch, gen_for); | |
| 1086 | + if (NCH(ch) == 5) | |
| 1087 | + ch = CHILD(ch, 4); | |
| 1088 | + else | |
| 1089 | + return n_fors; | |
| 1090 | + count_gen_iter: | |
| 1091 | + REQ(ch, gen_iter); | |
| 1092 | + ch = CHILD(ch, 0); | |
| 1093 | + if (TYPE(ch) == gen_for) | |
| 1094 | + goto count_gen_for; | |
| 1095 | + else if (TYPE(ch) == gen_if) { | |
| 1096 | + if (NCH(ch) == 3) { | |
| 1097 | + ch = CHILD(ch, 2); | |
| 1098 | + goto count_gen_iter; | |
| 1099 | + } | |
| 1100 | + else | |
| 1101 | + return n_fors; | |
| 1102 | + } | |
| 1103 | + | |
| 1104 | + /* Should never be reached */ | |
| 1105 | + PyErr_SetString(PyExc_SystemError, | |
| 1106 | + "logic error in count_gen_fors"); | |
| 1107 | + return -1; | |
| 1108 | +} | |
| 1109 | + | |
| 1110 | +/* Count the number of 'if' statements in a generator expression. | |
| 1111 | + | |
| 1112 | + Helper for ast_for_genexp(). | |
| 1113 | +*/ | |
| 1114 | + | |
| 1115 | +static int | |
| 1116 | +count_gen_ifs(const node *n) | |
| 1117 | +{ | |
| 1118 | + int n_ifs = 0; | |
| 1119 | + | |
| 1120 | + while (1) { | |
| 1121 | + REQ(n, gen_iter); | |
| 1122 | + if (TYPE(CHILD(n, 0)) == gen_for) | |
| 1123 | + return n_ifs; | |
| 1124 | + n = CHILD(n, 0); | |
| 1125 | + REQ(n, gen_if); | |
| 1126 | + n_ifs++; | |
| 1127 | + if (NCH(n) == 2) | |
| 1128 | + return n_ifs; | |
| 1129 | + n = CHILD(n, 2); | |
| 1130 | + } | |
| 1131 | +} | |
| 1132 | + | |
| 1133 | +/* TODO(jhylton): Combine with list comprehension code? */ | |
| 1134 | +static expr_ty | |
| 1135 | +ast_for_genexp(struct compiling *c, const node *n) | |
| 1136 | +{ | |
| 1137 | + /* testlist_gexp: test ( gen_for | (',' test)* [','] ) | |
| 1138 | + argument: [test '='] test [gen_for] # Really [keyword '='] test */ | |
| 1139 | + expr_ty elt; | |
| 1140 | + asdl_seq *genexps; | |
| 1141 | + int i, n_fors; | |
| 1142 | + node *ch; | |
| 1143 | + | |
| 1144 | + assert(TYPE(n) == (testlist_gexp) || TYPE(n) == (argument)); | |
| 1145 | + assert(NCH(n) > 1); | |
| 1146 | + | |
| 1147 | + elt = ast_for_expr(c, CHILD(n, 0)); | |
| 1148 | + if (!elt) | |
| 1149 | + return NULL; | |
| 1150 | + | |
| 1151 | + n_fors = count_gen_fors(n); | |
| 1152 | + if (n_fors == -1) | |
| 1153 | + return NULL; | |
| 1154 | + | |
| 1155 | + genexps = asdl_seq_new(n_fors, c->c_arena); | |
| 1156 | + if (!genexps) | |
| 1157 | + return NULL; | |
| 1158 | + | |
| 1159 | + ch = CHILD(n, 1); | |
| 1160 | + for (i = 0; i < n_fors; i++) { | |
| 1161 | + comprehension_ty ge; | |
| 1162 | + asdl_seq *t; | |
| 1163 | + expr_ty expression; | |
| 1164 | + node *for_ch; | |
| 1165 | + | |
| 1166 | + REQ(ch, gen_for); | |
| 1167 | + | |
| 1168 | + for_ch = CHILD(ch, 1); | |
| 1169 | + t = ast_for_exprlist(c, for_ch, Store); | |
| 1170 | + if (!t) | |
| 1171 | + return NULL; | |
| 1172 | + expression = ast_for_expr(c, CHILD(ch, 3)); | |
| 1173 | + if (!expression) | |
| 1174 | + return NULL; | |
| 1175 | + | |
| 1176 | + /* Check the # of children rather than the length of t, since | |
| 1177 | + (x for x, in ...) has 1 element in t, but still requires a Tuple. */ | |
| 1178 | + if (NCH(for_ch) == 1) | |
| 1179 | + ge = comprehension((expr_ty)asdl_seq_GET(t, 0), expression, | |
| 1180 | + NULL, c->c_arena); | |
| 1181 | + else | |
| 1182 | + ge = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset, | |
| 1183 | + c->c_arena), | |
| 1184 | + expression, NULL, c->c_arena); | |
| 1185 | + | |
| 1186 | + if (!ge) | |
| 1187 | + return NULL; | |
| 1188 | + | |
| 1189 | + if (NCH(ch) == 5) { | |
| 1190 | + int j, n_ifs; | |
| 1191 | + asdl_seq *ifs; | |
| 1192 | + | |
| 1193 | + ch = CHILD(ch, 4); | |
| 1194 | + n_ifs = count_gen_ifs(ch); | |
| 1195 | + if (n_ifs == -1) | |
| 1196 | + return NULL; | |
| 1197 | + | |
| 1198 | + ifs = asdl_seq_new(n_ifs, c->c_arena); | |
| 1199 | + if (!ifs) | |
| 1200 | + return NULL; | |
| 1201 | + | |
| 1202 | + for (j = 0; j < n_ifs; j++) { | |
| 1203 | + REQ(ch, gen_iter); | |
| 1204 | + ch = CHILD(ch, 0); | |
| 1205 | + REQ(ch, gen_if); | |
| 1206 | + | |
| 1207 | + expression = ast_for_expr(c, CHILD(ch, 1)); | |
| 1208 | + if (!expression) | |
| 1209 | + return NULL; | |
| 1210 | + asdl_seq_SET(ifs, j, expression); | |
| 1211 | + if (NCH(ch) == 3) | |
| 1212 | + ch = CHILD(ch, 2); | |
| 1213 | + } | |
| 1214 | + /* on exit, must guarantee that ch is a gen_for */ | |
| 1215 | + if (TYPE(ch) == gen_iter) | |
| 1216 | + ch = CHILD(ch, 0); | |
| 1217 | + ge->ifs = ifs; | |
| 1218 | + } | |
| 1219 | + asdl_seq_SET(genexps, i, ge); | |
| 1220 | + } | |
| 1221 | + | |
| 1222 | + return GeneratorExp(elt, genexps, LINENO(n), n->n_col_offset, c->c_arena); | |
| 1223 | +} | |
| 1224 | + | |
| 1225 | +static expr_ty | |
| 1226 | +ast_for_atom(struct compiling *c, const node *n) | |
| 1227 | +{ | |
| 1228 | + /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']' | |
| 1229 | + | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+ | |
| 1230 | + */ | |
| 1231 | + node *ch = CHILD(n, 0); | |
| 1232 | + | |
| 1233 | + switch (TYPE(ch)) { | |
| 1234 | + case NAME: | |
| 1235 | + /* All names start in Load context, but may later be | |
| 1236 | + changed. */ | |
| 1237 | + return Name(NEW_IDENTIFIER(ch), Load, LINENO(n), n->n_col_offset, c->c_arena); | |
| 1238 | + case STRING: { | |
| 1239 | + PyObject *str = parsestrplus(c, n); | |
| 1240 | + if (!str) | |
| 1241 | + return NULL; | |
| 1242 | + | |
| 1243 | + PyArena_AddPyObject(c->c_arena, str); | |
| 1244 | + return Str(str, LINENO(n), n->n_col_offset, c->c_arena); | |
| 1245 | + } | |
| 1246 | + case NUMBER: { | |
| 1247 | + PyObject *pynum = parsenumber(STR(ch)); | |
| 1248 | + if (!pynum) | |
| 1249 | + return NULL; | |
| 1250 | + | |
| 1251 | + PyArena_AddPyObject(c->c_arena, pynum); | |
| 1252 | + return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena); | |
| 1253 | + } | |
| 1254 | + case LPAR: /* some parenthesized expressions */ | |
| 1255 | + ch = CHILD(n, 1); | |
| 1256 | + | |
| 1257 | + if (TYPE(ch) == RPAR) | |
| 1258 | + return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena); | |
| 1259 | + | |
| 1260 | + if (TYPE(ch) == yield_expr) | |
| 1261 | + return ast_for_expr(c, ch); | |
| 1262 | + | |
| 1263 | + if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == gen_for)) | |
| 1264 | + return ast_for_genexp(c, ch); | |
| 1265 | + | |
| 1266 | + return ast_for_testlist_gexp(c, ch); | |
| 1267 | + case LSQB: /* list (or list comprehension) */ | |
| 1268 | + ch = CHILD(n, 1); | |
| 1269 | + | |
| 1270 | + if (TYPE(ch) == RSQB) | |
| 1271 | + return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena); | |
| 1272 | + | |
| 1273 | + REQ(ch, listmaker); | |
| 1274 | + if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) { | |
| 1275 | + asdl_seq *elts = seq_for_testlist(c, ch); | |
| 1276 | + if (!elts) | |
| 1277 | + return NULL; | |
| 1278 | + | |
| 1279 | + return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena); | |
| 1280 | + } | |
| 1281 | + else | |
| 1282 | + return ast_for_listcomp(c, ch); | |
| 1283 | + case LBRACE: { | |
| 1284 | + /* dictmaker: test ':' test (',' test ':' test)* [','] */ | |
| 1285 | + int i, size; | |
| 1286 | + asdl_seq *keys, *values; | |
| 1287 | + | |
| 1288 | + ch = CHILD(n, 1); | |
| 1289 | + size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */ | |
| 1290 | + keys = asdl_seq_new(size, c->c_arena); | |
| 1291 | + if (!keys) | |
| 1292 | + return NULL; | |
| 1293 | + | |
| 1294 | + values = asdl_seq_new(size, c->c_arena); | |
| 1295 | + if (!values) | |
| 1296 | + return NULL; | |
| 1297 | + | |
| 1298 | + for (i = 0; i < NCH(ch); i += 4) { | |
| 1299 | + expr_ty expression; | |
| 1300 | + | |
| 1301 | + expression = ast_for_expr(c, CHILD(ch, i)); | |
| 1302 | + if (!expression) | |
| 1303 | + return NULL; | |
| 1304 | + | |
| 1305 | + asdl_seq_SET(keys, i / 4, expression); | |
| 1306 | + | |
| 1307 | + expression = ast_for_expr(c, CHILD(ch, i + 2)); | |
| 1308 | + if (!expression) | |
| 1309 | + return NULL; | |
| 1310 | + | |
| 1311 | + asdl_seq_SET(values, i / 4, expression); | |
| 1312 | + } | |
| 1313 | + return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena); | |
| 1314 | + } | |
| 1315 | + case BACKQUOTE: { /* repr */ | |
| 1316 | + expr_ty expression = ast_for_testlist(c, CHILD(n, 1)); | |
| 1317 | + if (!expression) | |
| 1318 | + return NULL; | |
| 1319 | + | |
| 1320 | + return Repr(expression, LINENO(n), n->n_col_offset, c->c_arena); | |
| 1321 | + } | |
| 1322 | + default: | |
| 1323 | + PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch)); | |
| 1324 | + return NULL; | |
| 1325 | + } | |
| 1326 | +} | |
| 1327 | + | |
| 1328 | +static slice_ty | |
| 1329 | +ast_for_slice(struct compiling *c, const node *n) | |
| 1330 | +{ | |
| 1331 | + node *ch; | |
| 1332 | + expr_ty lower = NULL, upper = NULL, step = NULL; | |
| 1333 | + | |
| 1334 | + REQ(n, subscript); | |
| 1335 | + | |
| 1336 | + /* | |
| 1337 | + subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop] | |
| 1338 | + sliceop: ':' [test] | |
| 1339 | + */ | |
| 1340 | + ch = CHILD(n, 0); | |
| 1341 | + if (TYPE(ch) == DOT) | |
| 1342 | + return Ellipsis(c->c_arena); | |
| 1343 | + | |
| 1344 | + if (NCH(n) == 1 && TYPE(ch) == test) { | |
| 1345 | + /* 'step' variable hold no significance in terms of being used over | |
| 1346 | + other vars */ | |
| 1347 | + step = ast_for_expr(c, ch); | |
| 1348 | + if (!step) | |
| 1349 | + return NULL; | |
| 1350 | + | |
| 1351 | + return Index(step, c->c_arena); | |
| 1352 | + } | |
| 1353 | + | |
| 1354 | + if (TYPE(ch) == test) { | |
| 1355 | + lower = ast_for_expr(c, ch); | |
| 1356 | + if (!lower) | |
| 1357 | + return NULL; | |
| 1358 | + } | |
| 1359 | + | |
| 1360 | + /* If there's an upper bound it's in the second or third position. */ | |
| 1361 | + if (TYPE(ch) == COLON) { | |
| 1362 | + if (NCH(n) > 1) { | |
| 1363 | + node *n2 = CHILD(n, 1); | |
| 1364 | + | |
| 1365 | + if (TYPE(n2) == test) { | |
| 1366 | + upper = ast_for_expr(c, n2); | |
| 1367 | + if (!upper) | |
| 1368 | + return NULL; | |
| 1369 | + } | |
| 1370 | + } | |
| 1371 | + } else if (NCH(n) > 2) { | |
| 1372 | + node *n2 = CHILD(n, 2); | |
| 1373 | + | |
| 1374 | + if (TYPE(n2) == test) { | |
| 1375 | + upper = ast_for_expr(c, n2); | |
| 1376 | + if (!upper) | |
| 1377 | + return NULL; | |
| 1378 | + } | |
| 1379 | + } | |
| 1380 | + | |
| 1381 | + ch = CHILD(n, NCH(n) - 1); | |
| 1382 | + if (TYPE(ch) == sliceop) { | |
| 1383 | + if (NCH(ch) == 1) { | |
| 1384 | + /* No expression, so step is None */ | |
| 1385 | + ch = CHILD(ch, 0); | |
| 1386 | + step = Name(new_identifier("None", c->c_arena), Load, | |
| 1387 | + LINENO(ch), ch->n_col_offset, c->c_arena); | |
| 1388 | + if (!step) | |
| 1389 | + return NULL; | |
| 1390 | + } else { | |
| 1391 | + ch = CHILD(ch, 1); | |
| 1392 | + if (TYPE(ch) == test) { | |
| 1393 | + step = ast_for_expr(c, ch); | |
| 1394 | + if (!step) | |
| 1395 | + return NULL; | |
| 1396 | + } | |
| 1397 | + } | |
| 1398 | + } | |
| 1399 | + | |
| 1400 | + return Slice(lower, upper, step, c->c_arena); | |
| 1401 | +} | |
| 1402 | + | |
| 1403 | +static expr_ty | |
| 1404 | +ast_for_binop(struct compiling *c, const node *n) | |
| 1405 | +{ | |
| 1406 | + /* Must account for a sequence of expressions. | |
| 1407 | + How should A op B op C by represented? | |
| 1408 | + BinOp(BinOp(A, op, B), op, C). | |
| 1409 | + */ | |
| 1410 | + | |
| 1411 | + int i, nops; | |
| 1412 | + expr_ty expr1, expr2, result; | |
| 1413 | + operator_ty newoperator; | |
| 1414 | + | |
| 1415 | + expr1 = ast_for_expr(c, CHILD(n, 0)); | |
| 1416 | + if (!expr1) | |
| 1417 | + return NULL; | |
| 1418 | + | |
| 1419 | + expr2 = ast_for_expr(c, CHILD(n, 2)); | |
| 1420 | + if (!expr2) | |
| 1421 | + return NULL; | |
| 1422 | + | |
| 1423 | + newoperator = get_operator(CHILD(n, 1)); | |
| 1424 | + if (!newoperator) | |
| 1425 | + return NULL; | |
| 1426 | + | |
| 1427 | + result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, | |
| 1428 | + c->c_arena); | |
| 1429 | + if (!result) | |
| 1430 | + return NULL; | |
| 1431 | + | |
| 1432 | + nops = (NCH(n) - 1) / 2; | |
| 1433 | + for (i = 1; i < nops; i++) { | |
| 1434 | + expr_ty tmp_result, tmp; | |
| 1435 | + const node* next_oper = CHILD(n, i * 2 + 1); | |
| 1436 | + | |
| 1437 | + newoperator = get_operator(next_oper); | |
| 1438 | + if (!newoperator) | |
| 1439 | + return NULL; | |
| 1440 | + | |
| 1441 | + tmp = ast_for_expr(c, CHILD(n, i * 2 + 2)); | |
| 1442 | + if (!tmp) | |
| 1443 | + return NULL; | |
| 1444 | + | |
| 1445 | + tmp_result = BinOp(result, newoperator, tmp, | |
| 1446 | + LINENO(next_oper), next_oper->n_col_offset, | |
| 1447 | + c->c_arena); | |
| 1448 | + if (!tmp) | |
| 1449 | + return NULL; | |
| 1450 | + result = tmp_result; | |
| 1451 | + } | |
| 1452 | + return result; | |
| 1453 | +} | |
| 1454 | + | |
| 1455 | +static expr_ty | |
| 1456 | +ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr) | |
| 1457 | +{ | |
| 1458 | + /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME | |
| 1459 | + subscriptlist: subscript (',' subscript)* [','] | |
| 1460 | + subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop] | |
| 1461 | + */ | |
| 1462 | + REQ(n, trailer); | |
| 1463 | + if (TYPE(CHILD(n, 0)) == LPAR) { | |
| 1464 | + if (NCH(n) == 2) | |
| 1465 | + return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n), | |
| 1466 | + n->n_col_offset, c->c_arena); | |
| 1467 | + else | |
| 1468 | + return ast_for_call(c, CHILD(n, 1), left_expr); | |
| 1469 | + } | |
| 1470 | + else if (TYPE(CHILD(n, 0)) == DOT ) { | |
| 1471 | + return Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load, | |
| 1472 | + LINENO(n), n->n_col_offset, c->c_arena); | |
| 1473 | + } | |
| 1474 | + else { | |
| 1475 | + REQ(CHILD(n, 0), LSQB); | |
| 1476 | + REQ(CHILD(n, 2), RSQB); | |
| 1477 | + n = CHILD(n, 1); | |
| 1478 | + if (NCH(n) == 1) { | |
| 1479 | + slice_ty slc = ast_for_slice(c, CHILD(n, 0)); | |
| 1480 | + if (!slc) | |
| 1481 | + return NULL; | |
| 1482 | + return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset, | |
| 1483 | + c->c_arena); | |
| 1484 | + } | |
| 1485 | + else { | |
| 1486 | + /* The grammar is ambiguous here. The ambiguity is resolved | |
| 1487 | + by treating the sequence as a tuple literal if there are | |
| 1488 | + no slice features. | |
| 1489 | + */ | |
| 1490 | + int j; | |
| 1491 | + slice_ty slc; | |
| 1492 | + expr_ty e; | |
| 1493 | + bool simple = true; | |
| 1494 | + asdl_seq *slices, *elts; | |
| 1495 | + slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); | |
| 1496 | + if (!slices) | |
| 1497 | + return NULL; | |
| 1498 | + for (j = 0; j < NCH(n); j += 2) { | |
| 1499 | + slc = ast_for_slice(c, CHILD(n, j)); | |
| 1500 | + if (!slc) | |
| 1501 | + return NULL; | |
| 1502 | + if (slc->kind != Index_kind) | |
| 1503 | + simple = false; | |
| 1504 | + asdl_seq_SET(slices, j / 2, slc); | |
| 1505 | + } | |
| 1506 | + if (!simple) { | |
| 1507 | + return Subscript(left_expr, ExtSlice(slices, c->c_arena), | |
| 1508 | + Load, LINENO(n), n->n_col_offset, c->c_arena); | |
| 1509 | + } | |
| 1510 | + /* extract Index values and put them in a Tuple */ | |
| 1511 | + elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena); | |
| 1512 | + if (!elts) | |
| 1513 | + return NULL; | |
| 1514 | + for (j = 0; j < asdl_seq_LEN(slices); ++j) { | |
| 1515 | + slc = (slice_ty)asdl_seq_GET(slices, j); | |
| 1516 | + assert(slc->kind == Index_kind && slc->v.Index.value); | |
| 1517 | + asdl_seq_SET(elts, j, slc->v.Index.value); | |
| 1518 | + } | |
| 1519 | + e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena); | |
| 1520 | + if (!e) | |
| 1521 | + return NULL; | |
| 1522 | + return Subscript(left_expr, Index(e, c->c_arena), | |
| 1523 | + Load, LINENO(n), n->n_col_offset, c->c_arena); | |
| 1524 | + } | |
| 1525 | + } | |
| 1526 | +} | |
| 1527 | + | |
| 1528 | +static expr_ty | |
| 1529 | +ast_for_factor(struct compiling *c, const node *n) | |
| 1530 | +{ | |
| 1531 | + node *pfactor, *ppower, *patom, *pnum; | |
| 1532 | + expr_ty expression; | |
| 1533 | + | |
| 1534 | + /* If the unary - operator is applied to a constant, don't generate | |
| 1535 | + a UNARY_NEGATIVE opcode. Just store the approriate value as a | |
| 1536 | + constant. The peephole optimizer already does something like | |
| 1537 | + this but it doesn't handle the case where the constant is | |
| 1538 | + (sys.maxint - 1). In that case, we want a PyIntObject, not a | |
| 1539 | + PyLongObject. | |
| 1540 | + */ | |
| 1541 | + if (TYPE(CHILD(n, 0)) == MINUS | |
| 1542 | + && NCH(n) == 2 | |
| 1543 | + && TYPE((pfactor = CHILD(n, 1))) == factor | |
| 1544 | + && NCH(pfactor) == 1 | |
| 1545 | + && TYPE((ppower = CHILD(pfactor, 0))) == power | |
| 1546 | + && NCH(ppower) == 1 | |
| 1547 | + && TYPE((patom = CHILD(ppower, 0))) == atom | |
| 1548 | + && TYPE((pnum = CHILD(patom, 0))) == NUMBER) { | |
| 1549 | + char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2); | |
| 1550 | + if (s == NULL) | |
| 1551 | + return NULL; | |
| 1552 | + s[0] = '-'; | |
| 1553 | + strcpy(s + 1, STR(pnum)); | |
| 1554 | + PyObject_FREE(STR(pnum)); | |
| 1555 | + STR(pnum) = s; | |
| 1556 | + return ast_for_atom(c, patom); | |
| 1557 | + } | |
| 1558 | + | |
| 1559 | + expression = ast_for_expr(c, CHILD(n, 1)); | |
| 1560 | + if (!expression) | |
| 1561 | + return NULL; | |
| 1562 | + | |
| 1563 | + switch (TYPE(CHILD(n, 0))) { | |
| 1564 | + case PLUS: | |
| 1565 | + return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset, | |
| 1566 | + c->c_arena); | |
| 1567 | + case MINUS: | |
| 1568 | + return UnaryOp(USub, expression, LINENO(n), n->n_col_offset, | |
| 1569 | + c->c_arena); | |
| 1570 | + case TILDE: | |
| 1571 | + return UnaryOp(Invert, expression, LINENO(n), | |
| 1572 | + n->n_col_offset, c->c_arena); | |
| 1573 | + } | |
| 1574 | + PyErr_Format(PyExc_SystemError, "unhandled factor: %d", | |
| 1575 | + TYPE(CHILD(n, 0))); | |
| 1576 | + return NULL; | |
| 1577 | +} | |
| 1578 | + | |
| 1579 | +static expr_ty | |
| 1580 | +ast_for_power(struct compiling *c, const node *n) | |
| 1581 | +{ | |
| 1582 | + /* power: atom trailer* ('**' factor)* | |
| 1583 | + */ | |
| 1584 | + int i; | |
| 1585 | + expr_ty e, tmp; | |
| 1586 | + REQ(n, power); | |
| 1587 | + e = ast_for_atom(c, CHILD(n, 0)); | |
| 1588 | + if (!e) | |
| 1589 | + return NULL; | |
| 1590 | + if (NCH(n) == 1) | |
| 1591 | + return e; | |
| 1592 | + for (i = 1; i < NCH(n); i++) { | |
| 1593 | + node *ch = CHILD(n, i); | |
| 1594 | + if (TYPE(ch) != trailer) | |
| 1595 | + break; | |
| 1596 | + tmp = ast_for_trailer(c, ch, e); | |
| 1597 | + if (!tmp) | |
| 1598 | + return NULL; | |
| 1599 | + tmp->lineno = e->lineno; | |
| 1600 | + tmp->col_offset = e->col_offset; | |
| 1601 | + e = tmp; | |
| 1602 | + } | |
| 1603 | + if (TYPE(CHILD(n, NCH(n) - 1)) == factor) { | |
| 1604 | + expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1)); | |
| 1605 | + if (!f) | |
| 1606 | + return NULL; | |
| 1607 | + tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena); | |
| 1608 | + if (!tmp) | |
| 1609 | + return NULL; | |
| 1610 | + e = tmp; | |
| 1611 | + } | |
| 1612 | + return e; | |
| 1613 | +} | |
| 1614 | + | |
| 1615 | +/* Do not name a variable 'expr'! Will cause a compile error. | |
| 1616 | +*/ | |
| 1617 | + | |
| 1618 | +static expr_ty | |
| 1619 | +ast_for_expr(struct compiling *c, const node *n) | |
| 1620 | +{ | |
| 1621 | + /* handle the full range of simple expressions | |
| 1622 | + test: or_test ['if' or_test 'else' test] | lambdef | |
| 1623 | + or_test: and_test ('or' and_test)* | |
| 1624 | + and_test: not_test ('and' not_test)* | |
| 1625 | + not_test: 'not' not_test | comparison | |
| 1626 | + comparison: expr (comp_op expr)* | |
| 1627 | + expr: xor_expr ('|' xor_expr)* | |
| 1628 | + xor_expr: and_expr ('^' and_expr)* | |
| 1629 | + and_expr: shift_expr ('&' shift_expr)* | |
| 1630 | + shift_expr: arith_expr (('<<'|'>>') arith_expr)* | |
| 1631 | + arith_expr: term (('+'|'-') term)* | |
| 1632 | + term: factor (('*'|'/'|'%'|'//') factor)* | |
| 1633 | + factor: ('+'|'-'|'~') factor | power | |
| 1634 | + power: atom trailer* ('**' factor)* | |
| 1635 | + | |
| 1636 | + As well as modified versions that exist for backward compatibility, | |
| 1637 | + to explicitly allow: | |
| 1638 | + [ x for x in lambda: 0, lambda: 1 ] | |
| 1639 | + (which would be ambiguous without these extra rules) | |
| 1640 | + | |
| 1641 | + old_test: or_test | old_lambdef | |
| 1642 | + old_lambdef: 'lambda' [vararglist] ':' old_test | |
| 1643 | + | |
| 1644 | + */ | |
| 1645 | + | |
| 1646 | + asdl_seq *seq; | |
| 1647 | + int i; | |
| 1648 | + | |
| 1649 | + loop: | |
| 1650 | + switch (TYPE(n)) { | |
| 1651 | + case test: | |
| 1652 | + case old_test: | |
| 1653 | + if (TYPE(CHILD(n, 0)) == lambdef || | |
| 1654 | + TYPE(CHILD(n, 0)) == old_lambdef) | |
| 1655 | + return ast_for_lambdef(c, CHILD(n, 0)); | |
| 1656 | + else if (NCH(n) > 1) | |
| 1657 | + return ast_for_ifexpr(c, n); | |
| 1658 | + /* Fallthrough */ | |
| 1659 | + case or_test: | |
| 1660 | + case and_test: | |
| 1661 | + if (NCH(n) == 1) { | |
| 1662 | + n = CHILD(n, 0); | |
| 1663 | + goto loop; | |
| 1664 | + } | |
| 1665 | + seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); | |
| 1666 | + if (!seq) | |
| 1667 | + return NULL; | |
| 1668 | + for (i = 0; i < NCH(n); i += 2) { | |
| 1669 | + expr_ty e = ast_for_expr(c, CHILD(n, i)); | |
| 1670 | + if (!e) | |
| 1671 | + return NULL; | |
| 1672 | + asdl_seq_SET(seq, i / 2, e); | |
| 1673 | + } | |
| 1674 | + if (!strcmp(STR(CHILD(n, 1)), "and")) | |
| 1675 | + return BoolOp(And, seq, LINENO(n), n->n_col_offset, | |
| 1676 | + c->c_arena); | |
| 1677 | + assert(!strcmp(STR(CHILD(n, 1)), "or")); | |
| 1678 | + return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena); | |
| 1679 | + case not_test: | |
| 1680 | + if (NCH(n) == 1) { | |
| 1681 | + n = CHILD(n, 0); | |
| 1682 | + goto loop; | |
| 1683 | + } | |
| 1684 | + else { | |
| 1685 | + expr_ty expression = ast_for_expr(c, CHILD(n, 1)); | |
| 1686 | + if (!expression) | |
| 1687 | + return NULL; | |
| 1688 | + | |
| 1689 | + return UnaryOp(Not, expression, LINENO(n), n->n_col_offset, | |
| 1690 | + c->c_arena); | |
| 1691 | + } | |
| 1692 | + case comparison: | |
| 1693 | + if (NCH(n) == 1) { | |
| 1694 | + n = CHILD(n, 0); | |
| 1695 | + goto loop; | |
| 1696 | + } | |
| 1697 | + else { | |
| 1698 | + expr_ty expression; | |
| 1699 | + asdl_int_seq *ops; | |
| 1700 | + asdl_seq *cmps; | |
| 1701 | + ops = asdl_int_seq_new(NCH(n) / 2, c->c_arena); | |
| 1702 | + if (!ops) | |
| 1703 | + return NULL; | |
| 1704 | + cmps = asdl_seq_new(NCH(n) / 2, c->c_arena); | |
| 1705 | + if (!cmps) { | |
| 1706 | + return NULL; | |
| 1707 | + } | |
| 1708 | + for (i = 1; i < NCH(n); i += 2) { | |
| 1709 | + cmpop_ty newoperator; | |
| 1710 | + | |
| 1711 | + newoperator = ast_for_comp_op(CHILD(n, i)); | |
| 1712 | + if (!newoperator) { | |
| 1713 | + return NULL; | |
| 1714 | + } | |
| 1715 | + | |
| 1716 | + expression = ast_for_expr(c, CHILD(n, i + 1)); | |
| 1717 | + if (!expression) { | |
| 1718 | + return NULL; | |
| 1719 | + } | |
| 1720 | + | |
| 1721 | + asdl_seq_SET(ops, i / 2, newoperator); | |
| 1722 | + asdl_seq_SET(cmps, i / 2, expression); | |
| 1723 | + } | |
| 1724 | + expression = ast_for_expr(c, CHILD(n, 0)); | |
| 1725 | + if (!expression) { | |
| 1726 | + return NULL; | |
| 1727 | + } | |
| 1728 | + | |
| 1729 | + return Compare(expression, ops, cmps, LINENO(n), | |
| 1730 | + n->n_col_offset, c->c_arena); | |
| 1731 | + } | |
| 1732 | + break; | |
| 1733 | + | |
| 1734 | + /* The next five cases all handle BinOps. The main body of code | |
| 1735 | + is the same in each case, but the switch turned inside out to | |
| 1736 | + reuse the code for each type of operator. | |
| 1737 | + */ | |
| 1738 | + case expr: | |
| 1739 | + case xor_expr: | |
| 1740 | + case and_expr: | |
| 1741 | + case shift_expr: | |
| 1742 | + case arith_expr: | |
| 1743 | + case term: | |
| 1744 | + if (NCH(n) == 1) { | |
| 1745 | + n = CHILD(n, 0); | |
| 1746 | + goto loop; | |
| 1747 | + } | |
| 1748 | + return ast_for_binop(c, n); | |
| 1749 | + case yield_expr: { | |
| 1750 | + expr_ty exp = NULL; | |
| 1751 | + if (NCH(n) == 2) { | |
| 1752 | + exp = ast_for_testlist(c, CHILD(n, 1)); | |
| 1753 | + if (!exp) | |
| 1754 | + return NULL; | |
| 1755 | + } | |
| 1756 | + return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena); | |
| 1757 | + } | |
| 1758 | + case factor: | |
| 1759 | + if (NCH(n) == 1) { | |
| 1760 | + n = CHILD(n, 0); | |
| 1761 | + goto loop; | |
| 1762 | + } | |
| 1763 | + return ast_for_factor(c, n); | |
| 1764 | + case power: | |
| 1765 | + return ast_for_power(c, n); | |
| 1766 | + default: | |
| 1767 | + PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n)); | |
| 1768 | + return NULL; | |
| 1769 | + } | |
| 1770 | + /* should never get here unless if error is set */ | |
| 1771 | + return NULL; | |
| 1772 | +} | |
| 1773 | + | |
| 1774 | +static expr_ty | |
| 1775 | +ast_for_call(struct compiling *c, const node *n, expr_ty func) | |
| 1776 | +{ | |
| 1777 | + /* | |
| 1778 | + arglist: (argument ',')* (argument [',']| '*' test [',' '**' test] | |
| 1779 | + | '**' test) | |
| 1780 | + argument: [test '='] test [gen_for] # Really [keyword '='] test | |
| 1781 | + */ | |
| 1782 | + | |
| 1783 | + int i, nargs, nkeywords, ngens; | |
| 1784 | + asdl_seq *args; | |
| 1785 | + asdl_seq *keywords; | |
| 1786 | + expr_ty vararg = NULL, kwarg = NULL; | |
| 1787 | + | |
| 1788 | + REQ(n, arglist); | |
| 1789 | + | |
| 1790 | + nargs = 0; | |
| 1791 | + nkeywords = 0; | |
| 1792 | + ngens = 0; | |
| 1793 | + for (i = 0; i < NCH(n); i++) { | |
| 1794 | + node *ch = CHILD(n, i); | |
| 1795 | + if (TYPE(ch) == argument) { | |
| 1796 | + if (NCH(ch) == 1) | |
| 1797 | + nargs++; | |
| 1798 | + else if (TYPE(CHILD(ch, 1)) == gen_for) | |
| 1799 | + ngens++; | |
| 1800 | + else | |
| 1801 | + nkeywords++; | |
| 1802 | + } | |
| 1803 | + } | |
| 1804 | + if (ngens > 1 || (ngens && (nargs || nkeywords))) { | |
| 1805 | + ast_error(n, "Generator expression must be parenthesized " | |
| 1806 | + "if not sole argument"); | |
| 1807 | + return NULL; | |
| 1808 | + } | |
| 1809 | + | |
| 1810 | + if (nargs + nkeywords + ngens > 255) { | |
| 1811 | + ast_error(n, "more than 255 arguments"); | |
| 1812 | + return NULL; | |
| 1813 | + } | |
| 1814 | + | |
| 1815 | + args = asdl_seq_new(nargs + ngens, c->c_arena); | |
| 1816 | + if (!args) | |
| 1817 | + return NULL; | |
| 1818 | + keywords = asdl_seq_new(nkeywords, c->c_arena); | |
| 1819 | + if (!keywords) | |
| 1820 | + return NULL; | |
| 1821 | + nargs = 0; | |
| 1822 | + nkeywords = 0; | |
| 1823 | + for (i = 0; i < NCH(n); i++) { | |
| 1824 | + node *ch = CHILD(n, i); | |
| 1825 | + if (TYPE(ch) == argument) { | |
| 1826 | + expr_ty e; | |
| 1827 | + if (NCH(ch) == 1) { | |
| 1828 | + if (nkeywords) { | |
| 1829 | + ast_error(CHILD(ch, 0), | |
| 1830 | + "non-keyword arg after keyword arg"); | |
| 1831 | + return NULL; | |
| 1832 | + } | |
| 1833 | + e = ast_for_expr(c, CHILD(ch, 0)); | |
| 1834 | + if (!e) | |
| 1835 | + return NULL; | |
| 1836 | + asdl_seq_SET(args, nargs++, e); | |
| 1837 | + } | |
| 1838 | + else if (TYPE(CHILD(ch, 1)) == gen_for) { | |
| 1839 | + e = ast_for_genexp(c, ch); | |
| 1840 | + if (!e) | |
| 1841 | + return NULL; | |
| 1842 | + asdl_seq_SET(args, nargs++, e); | |
| 1843 | + } | |
| 1844 | + else { | |
| 1845 | + keyword_ty kw; | |
| 1846 | + identifier key; | |
| 1847 | + | |
| 1848 | + /* CHILD(ch, 0) is test, but must be an identifier? */ | |
| 1849 | + e = ast_for_expr(c, CHILD(ch, 0)); | |
| 1850 | + if (!e) | |
| 1851 | + return NULL; | |
| 1852 | + /* f(lambda x: x[0] = 3) ends up getting parsed with | |
| 1853 | + * LHS test = lambda x: x[0], and RHS test = 3. | |
| 1854 | + * SF bug 132313 points out that complaining about a keyword | |
| 1855 | + * then is very confusing. | |
| 1856 | + */ | |
| 1857 | + if (e->kind == Lambda_kind) { | |
| 1858 | + ast_error(CHILD(ch, 0), "lambda cannot contain assignment"); | |
| 1859 | + return NULL; | |
| 1860 | + } else if (e->kind != Name_kind) { | |
| 1861 | + ast_error(CHILD(ch, 0), "keyword can't be an expression"); | |
| 1862 | + return NULL; | |
| 1863 | + } | |
| 1864 | + key = e->v.Name.id; | |
| 1865 | + e = ast_for_expr(c, CHILD(ch, 2)); | |
| 1866 | + if (!e) | |
| 1867 | + return NULL; | |
| 1868 | + kw = keyword(key, e, c->c_arena); | |
| 1869 | + if (!kw) | |
| 1870 | + return NULL; | |
| 1871 | + asdl_seq_SET(keywords, nkeywords++, kw); | |
| 1872 | + } | |
| 1873 | + } | |
| 1874 | + else if (TYPE(ch) == STAR) { | |
| 1875 | + vararg = ast_for_expr(c, CHILD(n, i+1)); | |
| 1876 | + i++; | |
| 1877 | + } | |
| 1878 | + else if (TYPE(ch) == DOUBLESTAR) { | |
| 1879 | + kwarg = ast_for_expr(c, CHILD(n, i+1)); | |
| 1880 | + i++; | |
| 1881 | + } | |
| 1882 | + } | |
| 1883 | + | |
| 1884 | + return Call(func, args, keywords, vararg, kwarg, func->lineno, func->col_offset, c->c_arena); | |
| 1885 | +} | |
| 1886 | + | |
| 1887 | +static expr_ty | |
| 1888 | +ast_for_testlist(struct compiling *c, const node* n) | |
| 1889 | +{ | |
| 1890 | + /* testlist_gexp: test (',' test)* [','] */ | |
| 1891 | + /* testlist: test (',' test)* [','] */ | |
| 1892 | + /* testlist_safe: test (',' test)+ [','] */ | |
| 1893 | + /* testlist1: test (',' test)* */ | |
| 1894 | + assert(NCH(n) > 0); | |
| 1895 | + if (TYPE(n) == testlist_gexp) { | |
| 1896 | + if (NCH(n) > 1) | |
| 1897 | + assert(TYPE(CHILD(n, 1)) != gen_for); | |
| 1898 | + } | |
| 1899 | + else { | |
| 1900 | + assert(TYPE(n) == testlist || | |
| 1901 | + TYPE(n) == testlist_safe || | |
| 1902 | + TYPE(n) == testlist1); | |
| 1903 | + } | |
| 1904 | + if (NCH(n) == 1) | |
| 1905 | + return ast_for_expr(c, CHILD(n, 0)); | |
| 1906 | + else { | |
| 1907 | + asdl_seq *tmp = seq_for_testlist(c, n); | |
| 1908 | + if (!tmp) | |
| 1909 | + return NULL; | |
| 1910 | + return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena); | |
| 1911 | + } | |
| 1912 | +} | |
| 1913 | + | |
| 1914 | +static expr_ty | |
| 1915 | +ast_for_testlist_gexp(struct compiling *c, const node* n) | |
| 1916 | +{ | |
| 1917 | + /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */ | |
| 1918 | + /* argument: test [ gen_for ] */ | |
| 1919 | + assert(TYPE(n) == testlist_gexp || TYPE(n) == argument); | |
| 1920 | + if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for) | |
| 1921 | + return ast_for_genexp(c, n); | |
| 1922 | + return ast_for_testlist(c, n); | |
| 1923 | +} | |
| 1924 | + | |
| 1925 | +/* like ast_for_testlist() but returns a sequence */ | |
| 1926 | +static asdl_seq* | |
| 1927 | +ast_for_class_bases(struct compiling *c, const node* n) | |
| 1928 | +{ | |
| 1929 | + /* testlist: test (',' test)* [','] */ | |
| 1930 | + assert(NCH(n) > 0); | |
| 1931 | + REQ(n, testlist); | |
| 1932 | + if (NCH(n) == 1) { | |
| 1933 | + expr_ty base; | |
| 1934 | + asdl_seq *bases = asdl_seq_new(1, c->c_arena); | |
| 1935 | + if (!bases) | |
| 1936 | + return NULL; | |
| 1937 | + base = ast_for_expr(c, CHILD(n, 0)); | |
| 1938 | + if (!base) | |
| 1939 | + return NULL; | |
| 1940 | + asdl_seq_SET(bases, 0, base); | |
| 1941 | + return bases; | |
| 1942 | + } | |
| 1943 | + | |
| 1944 | + return seq_for_testlist(c, n); | |
| 1945 | +} | |
| 1946 | + | |
| 1947 | +static stmt_ty | |
| 1948 | +ast_for_expr_stmt(struct compiling *c, const node *n) | |
| 1949 | +{ | |
| 1950 | + REQ(n, expr_stmt); | |
| 1951 | + /* expr_stmt: testlist (augassign (yield_expr|testlist) | |
| 1952 | + | ('=' (yield_expr|testlist))*) | |
| 1953 | + testlist: test (',' test)* [','] | |
| 1954 | + augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' | |
| 1955 | + | '<<=' | '>>=' | '**=' | '//=' | |
| 1956 | + test: ... here starts the operator precendence dance | |
| 1957 | + */ | |
| 1958 | + | |
| 1959 | + if (NCH(n) == 1) { | |
| 1960 | + expr_ty e = ast_for_testlist(c, CHILD(n, 0)); | |
| 1961 | + if (!e) | |
| 1962 | + return NULL; | |
| 1963 | + | |
| 1964 | + return Expr(e, LINENO(n), n->n_col_offset, c->c_arena); | |
| 1965 | + } | |
| 1966 | + else if (TYPE(CHILD(n, 1)) == augassign) { | |
| 1967 | + expr_ty expr1, expr2; | |
| 1968 | + operator_ty newoperator; | |
| 1969 | + node *ch = CHILD(n, 0); | |
| 1970 | + | |
| 1971 | + expr1 = ast_for_testlist(c, ch); | |
| 1972 | + if (!expr1) | |
| 1973 | + return NULL; | |
| 1974 | + /* TODO(nas): Remove duplicated error checks (set_context does it) */ | |
| 1975 | + switch (expr1->kind) { | |
| 1976 | + case GeneratorExp_kind: | |
| 1977 | + ast_error(ch, "augmented assignment to generator " | |
| 1978 | + "expression not possible"); | |
| 1979 | + return NULL; | |
| 1980 | + case Yield_kind: | |
| 1981 | + ast_error(ch, "augmented assignment to yield " | |
| 1982 | + "expression not possible"); | |
| 1983 | + return NULL; | |
| 1984 | + case Name_kind: { | |
| 1985 | + const char *var_name = PyString_AS_STRING(expr1->v.Name.id); | |
| 1986 | + if (var_name[0] == 'N' && !strcmp(var_name, "None")) { | |
| 1987 | + ast_error(ch, "assignment to None"); | |
| 1988 | + return NULL; | |
| 1989 | + } | |
| 1990 | + break; | |
| 1991 | + } | |
| 1992 | + case Attribute_kind: | |
| 1993 | + case Subscript_kind: | |
| 1994 | + break; | |
| 1995 | + default: | |
| 1996 | + ast_error(ch, "illegal expression for augmented " | |
| 1997 | + "assignment"); | |
| 1998 | + return NULL; | |
| 1999 | + } | |
| 2000 | + if (!set_context(expr1, Store, ch)) | |
| 2001 | + return NULL; | |
| 2002 | + | |
| 2003 | + ch = CHILD(n, 2); | |
| 2004 | + if (TYPE(ch) == testlist) | |
| 2005 | + expr2 = ast_for_testlist(c, ch); | |
| 2006 | + else | |
| 2007 | + expr2 = ast_for_expr(c, ch); | |
| 2008 | + if (!expr2) | |
| 2009 | + return NULL; | |
| 2010 | + | |
| 2011 | + newoperator = ast_for_augassign(CHILD(n, 1)); | |
| 2012 | + if (!newoperator) | |
| 2013 | + return NULL; | |
| 2014 | + | |
| 2015 | + return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena); | |
| 2016 | + } | |
| 2017 | + else { | |
| 2018 | + int i; | |
| 2019 | + asdl_seq *targets; | |
| 2020 | + node *value; | |
| 2021 | + expr_ty expression; | |
| 2022 | + | |
| 2023 | + /* a normal assignment */ | |
| 2024 | + REQ(CHILD(n, 1), EQUAL); | |
| 2025 | + targets = asdl_seq_new(NCH(n) / 2, c->c_arena); | |
| 2026 | + if (!targets) | |
| 2027 | + return NULL; | |
| 2028 | + for (i = 0; i < NCH(n) - 2; i += 2) { | |
| 2029 | + expr_ty e; | |
| 2030 | + node *ch = CHILD(n, i); | |
| 2031 | + if (TYPE(ch) == yield_expr) { | |
| 2032 | + ast_error(ch, "assignment to yield expression not possible"); | |
| 2033 | + return NULL; | |
| 2034 | + } | |
| 2035 | + e = ast_for_testlist(c, ch); | |
| 2036 | + | |
| 2037 | + /* set context to assign */ | |
| 2038 | + if (!e) | |
| 2039 | + return NULL; | |
| 2040 | + | |
| 2041 | + if (!set_context(e, Store, CHILD(n, i))) | |
| 2042 | + return NULL; | |
| 2043 | + | |
| 2044 | + asdl_seq_SET(targets, i / 2, e); | |
| 2045 | + } | |
| 2046 | + value = CHILD(n, NCH(n) - 1); | |
| 2047 | + if (TYPE(value) == testlist) | |
| 2048 | + expression = ast_for_testlist(c, value); | |
| 2049 | + else | |
| 2050 | + expression = ast_for_expr(c, value); | |
| 2051 | + if (!expression) | |
| 2052 | + return NULL; | |
| 2053 | + return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena); | |
| 2054 | + } | |
| 2055 | +} | |
| 2056 | + | |
| 2057 | +static stmt_ty | |
| 2058 | +ast_for_print_stmt(struct compiling *c, const node *n) | |
| 2059 | +{ | |
| 2060 | + /* print_stmt: 'print' ( [ test (',' test)* [','] ] | |
| 2061 | + | '>>' test [ (',' test)+ [','] ] ) | |
| 2062 | + */ | |
| 2063 | + expr_ty dest = NULL, expression; | |
| 2064 | + asdl_seq *seq; | |
| 2065 | + bool nl; | |
| 2066 | + int i, j, start = 1; | |
| 2067 | + | |
| 2068 | + REQ(n, print_stmt); | |
| 2069 | + if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) { | |
| 2070 | + dest = ast_for_expr(c, CHILD(n, 2)); | |
| 2071 | + if (!dest) | |
| 2072 | + return NULL; | |
| 2073 | + start = 4; | |
| 2074 | + } | |
| 2075 | + seq = asdl_seq_new((NCH(n) + 1 - start) / 2, c->c_arena); | |
| 2076 | + if (!seq) | |
| 2077 | + return NULL; | |
| 2078 | + for (i = start, j = 0; i < NCH(n); i += 2, ++j) { | |
| 2079 | + expression = ast_for_expr(c, CHILD(n, i)); | |
| 2080 | + if (!expression) | |
| 2081 | + return NULL; | |
| 2082 | + asdl_seq_SET(seq, j, expression); | |
| 2083 | + } | |
| 2084 | + nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true; | |
| 2085 | + return Print(dest, seq, nl, LINENO(n), n->n_col_offset, c->c_arena); | |
| 2086 | +} | |
| 2087 | + | |
| 2088 | +static asdl_seq * | |
| 2089 | +ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context) | |
| 2090 | +{ | |
| 2091 | + asdl_seq *seq; | |
| 2092 | + int i; | |
| 2093 | + expr_ty e; | |
| 2094 | + | |
| 2095 | + REQ(n, exprlist); | |
| 2096 | + | |
| 2097 | + seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); | |
| 2098 | + if (!seq) | |
| 2099 | + return NULL; | |
| 2100 | + for (i = 0; i < NCH(n); i += 2) { | |
| 2101 | + e = ast_for_expr(c, CHILD(n, i)); | |
| 2102 | + if (!e) | |
| 2103 | + return NULL; | |
| 2104 | + asdl_seq_SET(seq, i / 2, e); | |
| 2105 | + if (context && !set_context(e, context, CHILD(n, i))) | |
| 2106 | + return NULL; | |
| 2107 | + } | |
| 2108 | + return seq; | |
| 2109 | +} | |
| 2110 | + | |
| 2111 | +static stmt_ty | |
| 2112 | +ast_for_del_stmt(struct compiling *c, const node *n) | |
| 2113 | +{ | |
| 2114 | + asdl_seq *expr_list; | |
| 2115 | + | |
| 2116 | + /* del_stmt: 'del' exprlist */ | |
| 2117 | + REQ(n, del_stmt); | |
| 2118 | + | |
| 2119 | + expr_list = ast_for_exprlist(c, CHILD(n, 1), Del); | |
| 2120 | + if (!expr_list) | |
| 2121 | + return NULL; | |
| 2122 | + return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena); | |
| 2123 | +} | |
| 2124 | + | |
| 2125 | +static stmt_ty | |
| 2126 | +ast_for_flow_stmt(struct compiling *c, const node *n) | |
| 2127 | +{ | |
| 2128 | + /* | |
| 2129 | + flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | |
| 2130 | + | yield_stmt | |
| 2131 | + break_stmt: 'break' | |
| 2132 | + continue_stmt: 'continue' | |
| 2133 | + return_stmt: 'return' [testlist] | |
| 2134 | + yield_stmt: yield_expr | |
| 2135 | + yield_expr: 'yield' testlist | |
| 2136 | + raise_stmt: 'raise' [test [',' test [',' test]]] | |
| 2137 | + */ | |
| 2138 | + node *ch; | |
| 2139 | + | |
| 2140 | + REQ(n, flow_stmt); | |
| 2141 | + ch = CHILD(n, 0); | |
| 2142 | + switch (TYPE(ch)) { | |
| 2143 | + case break_stmt: | |
| 2144 | + return Break(LINENO(n), n->n_col_offset, c->c_arena); | |
| 2145 | + case continue_stmt: | |
| 2146 | + return Continue(LINENO(n), n->n_col_offset, c->c_arena); | |
| 2147 | + case yield_stmt: { /* will reduce to yield_expr */ | |
| 2148 | + expr_ty exp = ast_for_expr(c, CHILD(ch, 0)); | |
| 2149 | + if (!exp) | |
| 2150 | + return NULL; | |
| 2151 | + return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena); | |
| 2152 | + } | |
| 2153 | + case return_stmt: | |
| 2154 | + if (NCH(ch) == 1) | |
| 2155 | + return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena); | |
| 2156 | + else { | |
| 2157 | + expr_ty expression = ast_for_testlist(c, CHILD(ch, 1)); | |
| 2158 | + if (!expression) | |
| 2159 | + return NULL; | |
| 2160 | + return Return(expression, LINENO(n), n->n_col_offset, c->c_arena); | |
| 2161 | + } | |
| 2162 | + case raise_stmt: | |
| 2163 | + if (NCH(ch) == 1) | |
| 2164 | + return Raise(NULL, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena); | |
| 2165 | + else if (NCH(ch) == 2) { | |
| 2166 | + expr_ty expression = ast_for_expr(c, CHILD(ch, 1)); | |
| 2167 | + if (!expression) | |
| 2168 | + return NULL; | |
| 2169 | + return Raise(expression, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena); | |
| 2170 | + } | |
| 2171 | + else if (NCH(ch) == 4) { | |
| 2172 | + expr_ty expr1, expr2; | |
| 2173 | + | |
| 2174 | + expr1 = ast_for_expr(c, CHILD(ch, 1)); | |
| 2175 | + if (!expr1) | |
| 2176 | + return NULL; | |
| 2177 | + expr2 = ast_for_expr(c, CHILD(ch, 3)); | |
| 2178 | + if (!expr2) | |
| 2179 | + return NULL; | |
| 2180 | + | |
| 2181 | + return Raise(expr1, expr2, NULL, LINENO(n), n->n_col_offset, c->c_arena); | |
| 2182 | + } | |
| 2183 | + else if (NCH(ch) == 6) { | |
| 2184 | + expr_ty expr1, expr2, expr3; | |
| 2185 | + | |
| 2186 | + expr1 = ast_for_expr(c, CHILD(ch, 1)); | |
| 2187 | + if (!expr1) | |
| 2188 | + return NULL; | |
| 2189 | + expr2 = ast_for_expr(c, CHILD(ch, 3)); | |
| 2190 | + if (!expr2) | |
| 2191 | + return NULL; | |
| 2192 | + expr3 = ast_for_expr(c, CHILD(ch, 5)); | |
| 2193 | + if (!expr3) | |
| 2194 | + return NULL; | |
| 2195 | + | |
| 2196 | + return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset, c->c_arena); | |
| 2197 | + } | |
| 2198 | + default: | |
| 2199 | + PyErr_Format(PyExc_SystemError, | |
| 2200 | + "unexpected flow_stmt: %d", TYPE(ch)); | |
| 2201 | + return NULL; | |
| 2202 | + } | |
| 2203 | + | |
| 2204 | + PyErr_SetString(PyExc_SystemError, "unhandled flow statement"); | |
| 2205 | + return NULL; | |
| 2206 | +} | |
| 2207 | + | |
| 2208 | +static alias_ty | |
| 2209 | +alias_for_import_name(struct compiling *c, const node *n) | |
| 2210 | +{ | |
| 2211 | + /* | |
| 2212 | + import_as_name: NAME ['as' NAME] | |
| 2213 | + dotted_as_name: dotted_name ['as' NAME] | |
| 2214 | + dotted_name: NAME ('.' NAME)* | |
| 2215 | + */ | |
| 2216 | + PyObject *str; | |
| 2217 | + | |
| 2218 | + loop: | |
| 2219 | + switch (TYPE(n)) { | |
| 2220 | + case import_as_name: | |
| 2221 | + str = NULL; | |
| 2222 | + if (NCH(n) == 3) { | |
| 2223 | + if (strcmp(STR(CHILD(n, 1)), "as") != 0) { | |
| 2224 | + ast_error(n, "must use 'as' in import"); | |
| 2225 | + return NULL; | |
| 2226 | + } | |
| 2227 | + str = NEW_IDENTIFIER(CHILD(n, 2)); | |
| 2228 | + } | |
| 2229 | + return alias(NEW_IDENTIFIER(CHILD(n, 0)), str, c->c_arena); | |
| 2230 | + case dotted_as_name: | |
| 2231 | + if (NCH(n) == 1) { | |
| 2232 | + n = CHILD(n, 0); | |
| 2233 | + goto loop; | |
| 2234 | + } | |
| 2235 | + else { | |
| 2236 | + alias_ty a = alias_for_import_name(c, CHILD(n, 0)); | |
| 2237 | + if (!a) | |
| 2238 | + return NULL; | |
| 2239 | + if (strcmp(STR(CHILD(n, 1)), "as") != 0) { | |
| 2240 | + ast_error(n, "must use 'as' in import"); | |
| 2241 | + return NULL; | |
| 2242 | + } | |
| 2243 | + assert(!a->asname); | |
| 2244 | + a->asname = NEW_IDENTIFIER(CHILD(n, 2)); | |
| 2245 | + return a; | |
| 2246 | + } | |
| 2247 | + break; | |
| 2248 | + case dotted_name: | |
| 2249 | + if (NCH(n) == 1) | |
| 2250 | + return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL, c->c_arena); | |
| 2251 | + else { | |
| 2252 | + /* Create a string of the form "a.b.c" */ | |
| 2253 | + int i; | |
| 2254 | + size_t len; | |
| 2255 | + char *s; | |
| 2256 | + | |
| 2257 | + len = 0; | |
| 2258 | + for (i = 0; i < NCH(n); i += 2) | |
| 2259 | + /* length of string plus one for the dot */ | |
| 2260 | + len += strlen(STR(CHILD(n, i))) + 1; | |
| 2261 | + len--; /* the last name doesn't have a dot */ | |
| 2262 | + str = PyString_FromStringAndSize(NULL, len); | |
| 2263 | + if (!str) | |
| 2264 | + return NULL; | |
| 2265 | + s = PyString_AS_STRING(str); | |
| 2266 | + if (!s) | |
| 2267 | + return NULL; | |
| 2268 | + for (i = 0; i < NCH(n); i += 2) { | |
| 2269 | + char *sch = STR(CHILD(n, i)); | |
| 2270 | + strcpy(s, STR(CHILD(n, i))); | |
| 2271 | + s += strlen(sch); | |
| 2272 | + *s++ = '.'; | |
| 2273 | + } | |
| 2274 | + --s; | |
| 2275 | + *s = '\0'; | |
| 2276 | + PyString_InternInPlace(&str); | |
| 2277 | + PyArena_AddPyObject(c->c_arena, str); | |
| 2278 | + return alias(str, NULL, c->c_arena); | |
| 2279 | + } | |
| 2280 | + break; | |
| 2281 | + case STAR: | |
| 2282 | + str = PyString_InternFromString("*"); | |
| 2283 | + PyArena_AddPyObject(c->c_arena, str); | |
| 2284 | + return alias(str, NULL, c->c_arena); | |
| 2285 | + default: | |
| 2286 | + PyErr_Format(PyExc_SystemError, | |
| 2287 | + "unexpected import name: %d", TYPE(n)); | |
| 2288 | + return NULL; | |
| 2289 | + } | |
| 2290 | + | |
| 2291 | + PyErr_SetString(PyExc_SystemError, "unhandled import name condition"); | |
| 2292 | + return NULL; | |
| 2293 | +} | |
| 2294 | + | |
| 2295 | +static stmt_ty | |
| 2296 | +ast_for_import_stmt(struct compiling *c, const node *n) | |
| 2297 | +{ | |
| 2298 | + /* | |
| 2299 | + import_stmt: import_name | import_from | |
| 2300 | + import_name: 'import' dotted_as_names | |
| 2301 | + import_from: 'from' ('.'* dotted_name | '.') 'import' | |
| 2302 | + ('*' | '(' import_as_names ')' | import_as_names) | |
| 2303 | + */ | |
| 2304 | + int lineno; | |
| 2305 | + int col_offset; | |
| 2306 | + int i; | |
| 2307 | + asdl_seq *aliases; | |
| 2308 | + | |
| 2309 | + REQ(n, import_stmt); | |
| 2310 | + lineno = LINENO(n); | |
| 2311 | + col_offset = n->n_col_offset; | |
| 2312 | + n = CHILD(n, 0); | |
| 2313 | + if (TYPE(n) == import_name) { | |
| 2314 | + n = CHILD(n, 1); | |
| 2315 | + REQ(n, dotted_as_names); | |
| 2316 | + aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); | |
| 2317 | + if (!aliases) | |
| 2318 | + return NULL; | |
| 2319 | + for (i = 0; i < NCH(n); i += 2) { | |
| 2320 | + alias_ty import_alias = alias_for_import_name(c, CHILD(n, i)); | |
| 2321 | + if (!import_alias) | |
| 2322 | + return NULL; | |
| 2323 | + asdl_seq_SET(aliases, i / 2, import_alias); | |
| 2324 | + } | |
| 2325 | + return Import(aliases, lineno, col_offset, c->c_arena); | |
| 2326 | + } | |
| 2327 | + else if (TYPE(n) == import_from) { | |
| 2328 | + int n_children; | |
| 2329 | + int idx, ndots = 0; | |
| 2330 | + alias_ty mod = NULL; | |
| 2331 | + identifier modname; | |
| 2332 | + | |
| 2333 | + /* Count the number of dots (for relative imports) and check for the | |
| 2334 | + optional module name */ | |
| 2335 | + for (idx = 1; idx < NCH(n); idx++) { | |
| 2336 | + if (TYPE(CHILD(n, idx)) == dotted_name) { | |
| 2337 | + mod = alias_for_import_name(c, CHILD(n, idx)); | |
| 2338 | + idx++; | |
| 2339 | + break; | |
| 2340 | + } else if (TYPE(CHILD(n, idx)) != DOT) { | |
| 2341 | + break; | |
| 2342 | + } | |
| 2343 | + ndots++; | |
| 2344 | + } | |
| 2345 | + idx++; /* skip over the 'import' keyword */ | |
| 2346 | + switch (TYPE(CHILD(n, idx))) { | |
| 2347 | + case STAR: | |
| 2348 | + /* from ... import * */ | |
| 2349 | + n = CHILD(n, idx); | |
| 2350 | + n_children = 1; | |
| 2351 | + if (ndots) { | |
| 2352 | + ast_error(n, "'import *' not allowed with 'from .'"); | |
| 2353 | + return NULL; | |
| 2354 | + } | |
| 2355 | + break; | |
| 2356 | + case LPAR: | |
| 2357 | + /* from ... import (x, y, z) */ | |
| 2358 | + n = CHILD(n, idx + 1); | |
| 2359 | + n_children = NCH(n); | |
| 2360 | + break; | |
| 2361 | + case import_as_names: | |
| 2362 | + /* from ... import x, y, z */ | |
| 2363 | + n = CHILD(n, idx); | |
| 2364 | + n_children = NCH(n); | |
| 2365 | + if (n_children % 2 == 0) { | |
| 2366 | + ast_error(n, "trailing comma not allowed without" | |
| 2367 | + " surrounding parentheses"); | |
| 2368 | + return NULL; | |
| 2369 | + } | |
| 2370 | + break; | |
| 2371 | + default: | |
| 2372 | + ast_error(n, "Unexpected node-type in from-import"); | |
| 2373 | + return NULL; | |
| 2374 | + } | |
| 2375 | + | |
| 2376 | + aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena); | |
| 2377 | + if (!aliases) | |
| 2378 | + return NULL; | |
| 2379 | + | |
| 2380 | + /* handle "from ... import *" special b/c there's no children */ | |
| 2381 | + if (TYPE(n) == STAR) { | |
| 2382 | + alias_ty import_alias = alias_for_import_name(c, n); | |
| 2383 | + if (!import_alias) | |
| 2384 | + return NULL; | |
| 2385 | + asdl_seq_SET(aliases, 0, import_alias); | |
| 2386 | + } | |
| 2387 | + else { | |
| 2388 | + for (i = 0; i < NCH(n); i += 2) { | |
| 2389 | + alias_ty import_alias = alias_for_import_name(c, CHILD(n, i)); | |
| 2390 | + if (!import_alias) | |
| 2391 | + return NULL; | |
| 2392 | + asdl_seq_SET(aliases, i / 2, import_alias); | |
| 2393 | + } | |
| 2394 | + } | |
| 2395 | + if (mod != NULL) | |
| 2396 | + modname = mod->name; | |
| 2397 | + else | |
| 2398 | + modname = new_identifier("", c->c_arena); | |
| 2399 | + return ImportFrom(modname, aliases, ndots, lineno, col_offset, | |
| 2400 | + c->c_arena); | |
| 2401 | + } | |
| 2402 | + PyErr_Format(PyExc_SystemError, | |
| 2403 | + "unknown import statement: starts with command '%s'", | |
| 2404 | + STR(CHILD(n, 0))); | |
| 2405 | + return NULL; | |
| 2406 | +} | |
| 2407 | + | |
| 2408 | +static stmt_ty | |
| 2409 | +ast_for_global_stmt(struct compiling *c, const node *n) | |
| 2410 | +{ | |
| 2411 | + /* global_stmt: 'global' NAME (',' NAME)* */ | |
| 2412 | + identifier name; | |
| 2413 | + asdl_seq *s; | |
| 2414 | + int i; | |
| 2415 | + | |
| 2416 | + REQ(n, global_stmt); | |
| 2417 | + s = asdl_seq_new(NCH(n) / 2, c->c_arena); | |
| 2418 | + if (!s) | |
| 2419 | + return NULL; | |
| 2420 | + for (i = 1; i < NCH(n); i += 2) { | |
| 2421 | + name = NEW_IDENTIFIER(CHILD(n, i)); | |
| 2422 | + if (!name) | |
| 2423 | + return NULL; | |
| 2424 | + asdl_seq_SET(s, i / 2, name); | |
| 2425 | + } | |
| 2426 | + return Global(s, LINENO(n), n->n_col_offset, c->c_arena); | |
| 2427 | +} | |
| 2428 | + | |
| 2429 | +static stmt_ty | |
| 2430 | +ast_for_exec_stmt(struct compiling *c, const node *n) | |
| 2431 | +{ | |
| 2432 | + expr_ty expr1, globals = NULL, locals = NULL; | |
| 2433 | + int n_children = NCH(n); | |
| 2434 | + if (n_children != 2 && n_children != 4 && n_children != 6) { | |
| 2435 | + PyErr_Format(PyExc_SystemError, | |
| 2436 | + "poorly formed 'exec' statement: %d parts to statement", | |
| 2437 | + n_children); | |
| 2438 | + return NULL; | |
| 2439 | + } | |
| 2440 | + | |
| 2441 | + /* exec_stmt: 'exec' expr ['in' test [',' test]] */ | |
| 2442 | + REQ(n, exec_stmt); | |
| 2443 | + expr1 = ast_for_expr(c, CHILD(n, 1)); | |
| 2444 | + if (!expr1) | |
| 2445 | + return NULL; | |
| 2446 | + if (n_children >= 4) { | |
| 2447 | + globals = ast_for_expr(c, CHILD(n, 3)); | |
| 2448 | + if (!globals) | |
| 2449 | + return NULL; | |
| 2450 | + } | |
| 2451 | + if (n_children == 6) { | |
| 2452 | + locals = ast_for_expr(c, CHILD(n, 5)); | |
| 2453 | + if (!locals) | |
| 2454 | + return NULL; | |
| 2455 | + } | |
| 2456 | + | |
| 2457 | + return Exec(expr1, globals, locals, LINENO(n), n->n_col_offset, c->c_arena); | |
| 2458 | +} | |
| 2459 | + | |
| 2460 | +static stmt_ty | |
| 2461 | +ast_for_assert_stmt(struct compiling *c, const node *n) | |
| 2462 | +{ | |
| 2463 | + /* assert_stmt: 'assert' test [',' test] */ | |
| 2464 | + REQ(n, assert_stmt); | |
| 2465 | + if (NCH(n) == 2) { | |
| 2466 | + expr_ty expression = ast_for_expr(c, CHILD(n, 1)); | |
| 2467 | + if (!expression) | |
| 2468 | + return NULL; | |
| 2469 | + return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena); | |
| 2470 | + } | |
| 2471 | + else if (NCH(n) == 4) { | |
| 2472 | + expr_ty expr1, expr2; | |
| 2473 | + | |
| 2474 | + expr1 = ast_for_expr(c, CHILD(n, 1)); | |
| 2475 | + if (!expr1) | |
| 2476 | + return NULL; | |
| 2477 | + expr2 = ast_for_expr(c, CHILD(n, 3)); | |
| 2478 | + if (!expr2) | |
| 2479 | + return NULL; | |
| 2480 | + | |
| 2481 | + return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena); | |
| 2482 | + } | |
| 2483 | + PyErr_Format(PyExc_SystemError, | |
| 2484 | + "improper number of parts to 'assert' statement: %d", | |
| 2485 | + NCH(n)); | |
| 2486 | + return NULL; | |
| 2487 | +} | |
| 2488 | + | |
| 2489 | +static asdl_seq * | |
| 2490 | +ast_for_suite(struct compiling *c, const node *n) | |
| 2491 | +{ | |
| 2492 | + /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */ | |
| 2493 | + asdl_seq *seq; | |
| 2494 | + stmt_ty s; | |
| 2495 | + int i, total, num, end, pos = 0; | |
| 2496 | + node *ch; | |
| 2497 | + | |
| 2498 | + REQ(n, suite); | |
| 2499 | + | |
| 2500 | + total = num_stmts(n); | |
| 2501 | + seq = asdl_seq_new(total, c->c_arena); | |
| 2502 | + if (!seq) | |
| 2503 | + return NULL; | |
| 2504 | + if (TYPE(CHILD(n, 0)) == simple_stmt) { | |
| 2505 | + n = CHILD(n, 0); | |
| 2506 | + /* simple_stmt always ends with a NEWLINE, | |
| 2507 | + and may have a trailing SEMI | |
| 2508 | + */ | |
| 2509 | + end = NCH(n) - 1; | |
| 2510 | + if (TYPE(CHILD(n, end - 1)) == SEMI) | |
| 2511 | + end--; | |
| 2512 | + /* loop by 2 to skip semi-colons */ | |
| 2513 | + for (i = 0; i < end; i += 2) { | |
| 2514 | + ch = CHILD(n, i); | |
| 2515 | + s = ast_for_stmt(c, ch); | |
| 2516 | + if (!s) | |
| 2517 | + return NULL; | |
| 2518 | + asdl_seq_SET(seq, pos++, s); | |
| 2519 | + } | |
| 2520 | + } | |
| 2521 | + else { | |
| 2522 | + for (i = 2; i < (NCH(n) - 1); i++) { | |
| 2523 | + ch = CHILD(n, i); | |
| 2524 | + REQ(ch, stmt); | |
| 2525 | + num = num_stmts(ch); | |
| 2526 | + if (num == 1) { | |
| 2527 | + /* small_stmt or compound_stmt with only one child */ | |
| 2528 | + s = ast_for_stmt(c, ch); | |
| 2529 | + if (!s) | |
| 2530 | + return NULL; | |
| 2531 | + asdl_seq_SET(seq, pos++, s); | |
| 2532 | + } | |
| 2533 | + else { | |
| 2534 | + int j; | |
| 2535 | + ch = CHILD(ch, 0); | |
| 2536 | + REQ(ch, simple_stmt); | |
| 2537 | + for (j = 0; j < NCH(ch); j += 2) { | |
| 2538 | + /* statement terminates with a semi-colon ';' */ | |
| 2539 | + if (NCH(CHILD(ch, j)) == 0) { | |
| 2540 | + assert((j + 1) == NCH(ch)); | |
| 2541 | + break; | |
| 2542 | + } | |
| 2543 | + s = ast_for_stmt(c, CHILD(ch, j)); | |
| 2544 | + if (!s) | |
| 2545 | + return NULL; | |
| 2546 | + asdl_seq_SET(seq, pos++, s); | |
| 2547 | + } | |
| 2548 | + } | |
| 2549 | + } | |
| 2550 | + } | |
| 2551 | + assert(pos == seq->size); | |
| 2552 | + return seq; | |
| 2553 | +} | |
| 2554 | + | |
| 2555 | +static stmt_ty | |
| 2556 | +ast_for_if_stmt(struct compiling *c, const node *n) | |
| 2557 | +{ | |
| 2558 | + /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)* | |
| 2559 | + ['else' ':' suite] | |
| 2560 | + */ | |
| 2561 | + char *s; | |
| 2562 | + | |
| 2563 | + REQ(n, if_stmt); | |
| 2564 | + | |
| 2565 | + if (NCH(n) == 4) { | |
| 2566 | + expr_ty expression; | |
| 2567 | + asdl_seq *suite_seq; | |
| 2568 | + | |
| 2569 | + expression = ast_for_expr(c, CHILD(n, 1)); | |
| 2570 | + if (!expression) | |
| 2571 | + return NULL; | |
| 2572 | + suite_seq = ast_for_suite(c, CHILD(n, 3)); | |
| 2573 | + if (!suite_seq) | |
| 2574 | + return NULL; | |
| 2575 | + | |
| 2576 | + return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena); | |
| 2577 | + } | |
| 2578 | + | |
| 2579 | + s = STR(CHILD(n, 4)); | |
| 2580 | + /* s[2], the third character in the string, will be | |
| 2581 | + 's' for el_s_e, or | |
| 2582 | + 'i' for el_i_f | |
| 2583 | + */ | |
| 2584 | + if (s[2] == 's') { | |
| 2585 | + expr_ty expression; | |
| 2586 | + asdl_seq *seq1, *seq2; | |
| 2587 | + | |
| 2588 | + expression = ast_for_expr(c, CHILD(n, 1)); | |
| 2589 | + if (!expression) | |
| 2590 | + return NULL; | |
| 2591 | + seq1 = ast_for_suite(c, CHILD(n, 3)); | |
| 2592 | + if (!seq1) | |
| 2593 | + return NULL; | |
| 2594 | + seq2 = ast_for_suite(c, CHILD(n, 6)); | |
| 2595 | + if (!seq2) | |
| 2596 | + return NULL; | |
| 2597 | + | |
| 2598 | + return If(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena); | |
| 2599 | + } | |
| 2600 | + else if (s[2] == 'i') { | |
| 2601 | + int i, n_elif, has_else = 0; | |
| 2602 | + expr_ty expression; | |
| 2603 | + asdl_seq *suite_seq; | |
| 2604 | + asdl_seq *orelse = NULL; | |
| 2605 | + n_elif = NCH(n) - 4; | |
| 2606 | + /* must reference the child n_elif+1 since 'else' token is third, | |
| 2607 | + not fourth, child from the end. */ | |
| 2608 | + if (TYPE(CHILD(n, (n_elif + 1))) == NAME | |
| 2609 | + && STR(CHILD(n, (n_elif + 1)))[2] == 's') { | |
| 2610 | + has_else = 1; | |
| 2611 | + n_elif -= 3; | |
| 2612 | + } | |
| 2613 | + n_elif /= 4; | |
| 2614 | + | |
| 2615 | + if (has_else) { | |
| 2616 | + asdl_seq *suite_seq2; | |
| 2617 | + | |
| 2618 | + orelse = asdl_seq_new(1, c->c_arena); | |
| 2619 | + if (!orelse) | |
| 2620 | + return NULL; | |
| 2621 | + expression = ast_for_expr(c, CHILD(n, NCH(n) - 6)); | |
| 2622 | + if (!expression) | |
| 2623 | + return NULL; | |
| 2624 | + suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4)); | |
| 2625 | + if (!suite_seq) | |
| 2626 | + return NULL; | |
| 2627 | + suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1)); | |
| 2628 | + if (!suite_seq2) | |
| 2629 | + return NULL; | |
| 2630 | + | |
| 2631 | + asdl_seq_SET(orelse, 0, If(expression, suite_seq, suite_seq2, | |
| 2632 | + LINENO(CHILD(n, NCH(n) - 6)), CHILD(n, NCH(n) - 6)->n_col_offset, | |
| 2633 | + c->c_arena)); | |
| 2634 | + /* the just-created orelse handled the last elif */ | |
| 2635 | + n_elif--; | |
| 2636 | + } | |
| 2637 | + | |
| 2638 | + for (i = 0; i < n_elif; i++) { | |
| 2639 | + int off = 5 + (n_elif - i - 1) * 4; | |
| 2640 | + asdl_seq *newobj = asdl_seq_new(1, c->c_arena); | |
| 2641 | + if (!newobj) | |
| 2642 | + return NULL; | |
| 2643 | + expression = ast_for_expr(c, CHILD(n, off)); | |
| 2644 | + if (!expression) | |
| 2645 | + return NULL; | |
| 2646 | + suite_seq = ast_for_suite(c, CHILD(n, off + 2)); | |
| 2647 | + if (!suite_seq) | |
| 2648 | + return NULL; | |
| 2649 | + | |
| 2650 | + asdl_seq_SET(newobj, 0, | |
| 2651 | + If(expression, suite_seq, orelse, | |
| 2652 | + LINENO(CHILD(n, off)), CHILD(n, off)->n_col_offset, c->c_arena)); | |
| 2653 | + orelse = newobj; | |
| 2654 | + } | |
| 2655 | + expression = ast_for_expr(c, CHILD(n, 1)); | |
| 2656 | + if (!expression) | |
| 2657 | + return NULL; | |
| 2658 | + suite_seq = ast_for_suite(c, CHILD(n, 3)); | |
| 2659 | + if (!suite_seq) | |
| 2660 | + return NULL; | |
| 2661 | + return If(expression, suite_seq, orelse, | |
| 2662 | + LINENO(n), n->n_col_offset, c->c_arena); | |
| 2663 | + } | |
| 2664 | + | |
| 2665 | + PyErr_Format(PyExc_SystemError, | |
| 2666 | + "unexpected token in 'if' statement: %s", s); | |
| 2667 | + return NULL; | |
| 2668 | +} | |
| 2669 | + | |
| 2670 | +static stmt_ty | |
| 2671 | +ast_for_while_stmt(struct compiling *c, const node *n) | |
| 2672 | +{ | |
| 2673 | + /* while_stmt: 'while' test ':' suite ['else' ':' suite] */ | |
| 2674 | + REQ(n, while_stmt); | |
| 2675 | + | |
| 2676 | + if (NCH(n) == 4) { | |
| 2677 | + expr_ty expression; | |
| 2678 | + asdl_seq *suite_seq; | |
| 2679 | + | |
| 2680 | + expression = ast_for_expr(c, CHILD(n, 1)); | |
| 2681 | + if (!expression) | |
| 2682 | + return NULL; | |
| 2683 | + suite_seq = ast_for_suite(c, CHILD(n, 3)); | |
| 2684 | + if (!suite_seq) | |
| 2685 | + return NULL; | |
| 2686 | + return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena); | |
| 2687 | + } | |
| 2688 | + else if (NCH(n) == 7) { | |
| 2689 | + expr_ty expression; | |
| 2690 | + asdl_seq *seq1, *seq2; | |
| 2691 | + | |
| 2692 | + expression = ast_for_expr(c, CHILD(n, 1)); | |
| 2693 | + if (!expression) | |
| 2694 | + return NULL; | |
| 2695 | + seq1 = ast_for_suite(c, CHILD(n, 3)); | |
| 2696 | + if (!seq1) | |
| 2697 | + return NULL; | |
| 2698 | + seq2 = ast_for_suite(c, CHILD(n, 6)); | |
| 2699 | + if (!seq2) | |
| 2700 | + return NULL; | |
| 2701 | + | |
| 2702 | + return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena); | |
| 2703 | + } | |
| 2704 | + | |
| 2705 | + PyErr_Format(PyExc_SystemError, | |
| 2706 | + "wrong number of tokens for 'while' statement: %d", | |
| 2707 | + NCH(n)); | |
| 2708 | + return NULL; | |
| 2709 | +} | |
| 2710 | + | |
| 2711 | +static stmt_ty | |
| 2712 | +ast_for_for_stmt(struct compiling *c, const node *n) | |
| 2713 | +{ | |
| 2714 | + asdl_seq *_target, *seq = NULL, *suite_seq; | |
| 2715 | + expr_ty expression; | |
| 2716 | + expr_ty target; | |
| 2717 | + const node *node_target; | |
| 2718 | + /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */ | |
| 2719 | + REQ(n, for_stmt); | |
| 2720 | + | |
| 2721 | + if (NCH(n) == 9) { | |
| 2722 | + seq = ast_for_suite(c, CHILD(n, 8)); | |
| 2723 | + if (!seq) | |
| 2724 | + return NULL; | |
| 2725 | + } | |
| 2726 | + | |
| 2727 | + node_target = CHILD(n, 1); | |
| 2728 | + _target = ast_for_exprlist(c, node_target, Store); | |
| 2729 | + if (!_target) | |
| 2730 | + return NULL; | |
| 2731 | + /* Check the # of children rather than the length of _target, since | |
| 2732 | + for x, in ... has 1 element in _target, but still requires a Tuple. */ | |
| 2733 | + if (NCH(node_target) == 1) | |
| 2734 | + target = (expr_ty)asdl_seq_GET(_target, 0); | |
| 2735 | + else | |
| 2736 | + target = Tuple(_target, Store, LINENO(n), n->n_col_offset, c->c_arena); | |
| 2737 | + | |
| 2738 | + expression = ast_for_testlist(c, CHILD(n, 3)); | |
| 2739 | + if (!expression) | |
| 2740 | + return NULL; | |
| 2741 | + suite_seq = ast_for_suite(c, CHILD(n, 5)); | |
| 2742 | + if (!suite_seq) | |
| 2743 | + return NULL; | |
| 2744 | + | |
| 2745 | + return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset, | |
| 2746 | + c->c_arena); | |
| 2747 | +} | |
| 2748 | + | |
| 2749 | +static excepthandler_ty | |
| 2750 | +ast_for_except_clause(struct compiling *c, const node *exc, node *body) | |
| 2751 | +{ | |
| 2752 | + /* except_clause: 'except' [test [',' test]] */ | |
| 2753 | + REQ(exc, except_clause); | |
| 2754 | + REQ(body, suite); | |
| 2755 | + | |
| 2756 | + if (NCH(exc) == 1) { | |
| 2757 | + asdl_seq *suite_seq = ast_for_suite(c, body); | |
| 2758 | + if (!suite_seq) | |
| 2759 | + return NULL; | |
| 2760 | + | |
| 2761 | + return excepthandler(NULL, NULL, suite_seq, LINENO(exc), | |
| 2762 | + exc->n_col_offset, c->c_arena); | |
| 2763 | + } | |
| 2764 | + else if (NCH(exc) == 2) { | |
| 2765 | + expr_ty expression; | |
| 2766 | + asdl_seq *suite_seq; | |
| 2767 | + | |
| 2768 | + expression = ast_for_expr(c, CHILD(exc, 1)); | |
| 2769 | + if (!expression) | |
| 2770 | + return NULL; | |
| 2771 | + suite_seq = ast_for_suite(c, body); | |
| 2772 | + if (!suite_seq) | |
| 2773 | + return NULL; | |
| 2774 | + | |
| 2775 | + return excepthandler(expression, NULL, suite_seq, LINENO(exc), | |
| 2776 | + exc->n_col_offset, c->c_arena); | |
| 2777 | + } | |
| 2778 | + else if (NCH(exc) == 4) { | |
| 2779 | + asdl_seq *suite_seq; | |
| 2780 | + expr_ty expression; | |
| 2781 | + expr_ty e = ast_for_expr(c, CHILD(exc, 3)); | |
| 2782 | + if (!e) | |
| 2783 | + return NULL; | |
| 2784 | + if (!set_context(e, Store, CHILD(exc, 3))) | |
| 2785 | + return NULL; | |
| 2786 | + expression = ast_for_expr(c, CHILD(exc, 1)); | |
| 2787 | + if (!expression) | |
| 2788 | + return NULL; | |
| 2789 | + suite_seq = ast_for_suite(c, body); | |
| 2790 | + if (!suite_seq) | |
| 2791 | + return NULL; | |
| 2792 | + | |
| 2793 | + return excepthandler(expression, e, suite_seq, LINENO(exc), | |
| 2794 | + exc->n_col_offset, c->c_arena); | |
| 2795 | + } | |
| 2796 | + | |
| 2797 | + PyErr_Format(PyExc_SystemError, | |
| 2798 | + "wrong number of children for 'except' clause: %d", | |
| 2799 | + NCH(exc)); | |
| 2800 | + return NULL; | |
| 2801 | +} | |
| 2802 | + | |
| 2803 | +static stmt_ty | |
| 2804 | +ast_for_try_stmt(struct compiling *c, const node *n) | |
| 2805 | +{ | |
| 2806 | + const int nch = NCH(n); | |
| 2807 | + int n_except = (nch - 3)/3; | |
| 2808 | + asdl_seq *body, *orelse = NULL, *finally = NULL; | |
| 2809 | + | |
| 2810 | + REQ(n, try_stmt); | |
| 2811 | + | |
| 2812 | + body = ast_for_suite(c, CHILD(n, 2)); | |
| 2813 | + if (body == NULL) | |
| 2814 | + return NULL; | |
| 2815 | + | |
| 2816 | + if (TYPE(CHILD(n, nch - 3)) == NAME) { | |
| 2817 | + if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) { | |
| 2818 | + if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) { | |
| 2819 | + /* we can assume it's an "else", | |
| 2820 | + because nch >= 9 for try-else-finally and | |
| 2821 | + it would otherwise have a type of except_clause */ | |
| 2822 | + orelse = ast_for_suite(c, CHILD(n, nch - 4)); | |
| 2823 | + if (orelse == NULL) | |
| 2824 | + return NULL; | |
| 2825 | + n_except--; | |
| 2826 | + } | |
| 2827 | + | |
| 2828 | + finally = ast_for_suite(c, CHILD(n, nch - 1)); | |
| 2829 | + if (finally == NULL) | |
| 2830 | + return NULL; | |
| 2831 | + n_except--; | |
| 2832 | + } | |
| 2833 | + else { | |
| 2834 | + /* we can assume it's an "else", | |
| 2835 | + otherwise it would have a type of except_clause */ | |
| 2836 | + orelse = ast_for_suite(c, CHILD(n, nch - 1)); | |
| 2837 | + if (orelse == NULL) | |
| 2838 | + return NULL; | |
| 2839 | + n_except--; | |
| 2840 | + } | |
| 2841 | + } | |
| 2842 | + else if (TYPE(CHILD(n, nch - 3)) != except_clause) { | |
| 2843 | + ast_error(n, "malformed 'try' statement"); | |
| 2844 | + return NULL; | |
| 2845 | + } | |
| 2846 | + | |
| 2847 | + if (n_except > 0) { | |
| 2848 | + int i; | |
| 2849 | + stmt_ty except_st; | |
| 2850 | + /* process except statements to create a try ... except */ | |
| 2851 | + asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena); | |
| 2852 | + if (handlers == NULL) | |
| 2853 | + return NULL; | |
| 2854 | + | |
| 2855 | + for (i = 0; i < n_except; i++) { | |
| 2856 | + excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3), | |
| 2857 | + CHILD(n, 5 + i * 3)); | |
| 2858 | + if (!e) | |
| 2859 | + return NULL; | |
| 2860 | + asdl_seq_SET(handlers, i, e); | |
| 2861 | + } | |
| 2862 | + | |
| 2863 | + except_st = TryExcept(body, handlers, orelse, LINENO(n), | |
| 2864 | + n->n_col_offset, c->c_arena); | |
| 2865 | + if (!finally) | |
| 2866 | + return except_st; | |
| 2867 | + | |
| 2868 | + /* if a 'finally' is present too, we nest the TryExcept within a | |
| 2869 | + TryFinally to emulate try ... except ... finally */ | |
| 2870 | + body = asdl_seq_new(1, c->c_arena); | |
| 2871 | + if (body == NULL) | |
| 2872 | + return NULL; | |
| 2873 | + asdl_seq_SET(body, 0, except_st); | |
| 2874 | + } | |
| 2875 | + | |
| 2876 | + /* must be a try ... finally (except clauses are in body, if any exist) */ | |
| 2877 | + assert(finally != NULL); | |
| 2878 | + return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena); | |
| 2879 | +} | |
| 2880 | + | |
| 2881 | +static expr_ty | |
| 2882 | +ast_for_with_var(struct compiling *c, const node *n) | |
| 2883 | +{ | |
| 2884 | + REQ(n, with_var); | |
| 2885 | + if (strcmp(STR(CHILD(n, 0)), "as") != 0) { | |
| 2886 | + ast_error(n, "expected \"with [expr] as [var]\""); | |
| 2887 | + return NULL; | |
| 2888 | + } | |
| 2889 | + return ast_for_expr(c, CHILD(n, 1)); | |
| 2890 | +} | |
| 2891 | + | |
| 2892 | +/* with_stmt: 'with' test [ with_var ] ':' suite */ | |
| 2893 | +static stmt_ty | |
| 2894 | +ast_for_with_stmt(struct compiling *c, const node *n) | |
| 2895 | +{ | |
| 2896 | + expr_ty context_expr, optional_vars = NULL; | |
| 2897 | + int suite_index = 3; /* skip 'with', test, and ':' */ | |
| 2898 | + asdl_seq *suite_seq; | |
| 2899 | + | |
| 2900 | + assert(TYPE(n) == with_stmt); | |
| 2901 | + context_expr = ast_for_expr(c, CHILD(n, 1)); | |
| 2902 | + if (!context_expr) | |
| 2903 | + return NULL; | |
| 2904 | + if (TYPE(CHILD(n, 2)) == with_var) { | |
| 2905 | + optional_vars = ast_for_with_var(c, CHILD(n, 2)); | |
| 2906 | + | |
| 2907 | + if (!optional_vars) { | |
| 2908 | + return NULL; | |
| 2909 | + } | |
| 2910 | + if (!set_context(optional_vars, Store, n)) { | |
| 2911 | + return NULL; | |
| 2912 | + } | |
| 2913 | + suite_index = 4; | |
| 2914 | + } | |
| 2915 | + | |
| 2916 | + suite_seq = ast_for_suite(c, CHILD(n, suite_index)); | |
| 2917 | + if (!suite_seq) { | |
| 2918 | + return NULL; | |
| 2919 | + } | |
| 2920 | + return With(context_expr, optional_vars, suite_seq, LINENO(n), | |
| 2921 | + n->n_col_offset, c->c_arena); | |
| 2922 | +} | |
| 2923 | + | |
| 2924 | +static stmt_ty | |
| 2925 | +ast_for_classdef(struct compiling *c, const node *n) | |
| 2926 | +{ | |
| 2927 | + /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */ | |
| 2928 | + asdl_seq *bases, *s; | |
| 2929 | + | |
| 2930 | + REQ(n, classdef); | |
| 2931 | + | |
| 2932 | + if (!strcmp(STR(CHILD(n, 1)), "None")) { | |
| 2933 | + ast_error(n, "assignment to None"); | |
| 2934 | + return NULL; | |
| 2935 | + } | |
| 2936 | + | |
| 2937 | + if (NCH(n) == 4) { | |
| 2938 | + s = ast_for_suite(c, CHILD(n, 3)); | |
| 2939 | + if (!s) | |
| 2940 | + return NULL; | |
| 2941 | + return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n), | |
| 2942 | + n->n_col_offset, c->c_arena); | |
| 2943 | + } | |
| 2944 | + /* check for empty base list */ | |
| 2945 | + if (TYPE(CHILD(n,3)) == RPAR) { | |
| 2946 | + s = ast_for_suite(c, CHILD(n,5)); | |
| 2947 | + if (!s) | |
| 2948 | + return NULL; | |
| 2949 | + return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n), | |
| 2950 | + n->n_col_offset, c->c_arena); | |
| 2951 | + } | |
| 2952 | + | |
| 2953 | + /* else handle the base class list */ | |
| 2954 | + bases = ast_for_class_bases(c, CHILD(n, 3)); | |
| 2955 | + if (!bases) | |
| 2956 | + return NULL; | |
| 2957 | + | |
| 2958 | + s = ast_for_suite(c, CHILD(n, 6)); | |
| 2959 | + if (!s) | |
| 2960 | + return NULL; | |
| 2961 | + return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, LINENO(n), | |
| 2962 | + n->n_col_offset, c->c_arena); | |
| 2963 | +} | |
| 2964 | + | |
| 2965 | +static stmt_ty | |
| 2966 | +ast_for_stmt(struct compiling *c, const node *n) | |
| 2967 | +{ | |
| 2968 | + if (TYPE(n) == stmt) { | |
| 2969 | + assert(NCH(n) == 1); | |
| 2970 | + n = CHILD(n, 0); | |
| 2971 | + } | |
| 2972 | + if (TYPE(n) == simple_stmt) { | |
| 2973 | + assert(num_stmts(n) == 1); | |
| 2974 | + n = CHILD(n, 0); | |
| 2975 | + } | |
| 2976 | + if (TYPE(n) == small_stmt) { | |
| 2977 | + REQ(n, small_stmt); | |
| 2978 | + n = CHILD(n, 0); | |
| 2979 | + /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt | |
| 2980 | + | flow_stmt | import_stmt | global_stmt | exec_stmt | |
| 2981 | + | assert_stmt | |
| 2982 | + */ | |
| 2983 | + switch (TYPE(n)) { | |
| 2984 | + case expr_stmt: | |
| 2985 | + return ast_for_expr_stmt(c, n); | |
| 2986 | + case print_stmt: | |
| 2987 | + return ast_for_print_stmt(c, n); | |
| 2988 | + case del_stmt: | |
| 2989 | + return ast_for_del_stmt(c, n); | |
| 2990 | + case pass_stmt: | |
| 2991 | + return Pass(LINENO(n), n->n_col_offset, c->c_arena); | |
| 2992 | + case flow_stmt: | |
| 2993 | + return ast_for_flow_stmt(c, n); | |
| 2994 | + case import_stmt: | |
| 2995 | + return ast_for_import_stmt(c, n); | |
| 2996 | + case global_stmt: | |
| 2997 | + return ast_for_global_stmt(c, n); | |
| 2998 | + case exec_stmt: | |
| 2999 | + return ast_for_exec_stmt(c, n); | |
| 3000 | + case assert_stmt: | |
| 3001 | + return ast_for_assert_stmt(c, n); | |
| 3002 | + default: | |
| 3003 | + PyErr_Format(PyExc_SystemError, | |
| 3004 | + "unhandled small_stmt: TYPE=%d NCH=%d\n", | |
| 3005 | + TYPE(n), NCH(n)); | |
| 3006 | + return NULL; | |
| 3007 | + } | |
| 3008 | + } | |
| 3009 | + else { | |
| 3010 | + /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | |
| 3011 | + | funcdef | classdef | |
| 3012 | + */ | |
| 3013 | + node *ch = CHILD(n, 0); | |
| 3014 | + REQ(n, compound_stmt); | |
| 3015 | + switch (TYPE(ch)) { | |
| 3016 | + case if_stmt: | |
| 3017 | + return ast_for_if_stmt(c, ch); | |
| 3018 | + case while_stmt: | |
| 3019 | + return ast_for_while_stmt(c, ch); | |
| 3020 | + case for_stmt: | |
| 3021 | + return ast_for_for_stmt(c, ch); | |
| 3022 | + case try_stmt: | |
| 3023 | + return ast_for_try_stmt(c, ch); | |
| 3024 | + case with_stmt: | |
| 3025 | + return ast_for_with_stmt(c, ch); | |
| 3026 | + case funcdef: | |
| 3027 | + return ast_for_funcdef(c, ch); | |
| 3028 | + case classdef: | |
| 3029 | + return ast_for_classdef(c, ch); | |
| 3030 | + default: | |
| 3031 | + PyErr_Format(PyExc_SystemError, | |
| 3032 | + "unhandled small_stmt: TYPE=%d NCH=%d\n", | |
| 3033 | + TYPE(n), NCH(n)); | |
| 3034 | + return NULL; | |
| 3035 | + } | |
| 3036 | + } | |
| 3037 | +} | |
| 3038 | + | |
| 3039 | +static PyObject * | |
| 3040 | +parsenumber(const char *s) | |
| 3041 | +{ | |
| 3042 | + const char *end; | |
| 3043 | + long x; | |
| 3044 | + double dx; | |
| 3045 | +#ifndef WITHOUT_COMPLEX | |
| 3046 | + Py_complex c; | |
| 3047 | + int imflag; | |
| 3048 | +#endif | |
| 3049 | + | |
| 3050 | + errno = 0; | |
| 3051 | + end = s + strlen(s) - 1; | |
| 3052 | +#ifndef WITHOUT_COMPLEX | |
| 3053 | + imflag = *end == 'j' || *end == 'J'; | |
| 3054 | +#endif | |
| 3055 | + if (*end == 'l' || *end == 'L') | |
| 3056 | + return PyLong_FromString((char *)s, (char **)0, 0); | |
| 3057 | + if (s[0] == '0') { | |
| 3058 | + x = (long) PyOS_strtoul((char *)s, (char **)&end, 0); | |
| 3059 | + if (x < 0 && errno == 0) { | |
| 3060 | + return PyLong_FromString((char *)s, | |
| 3061 | + (char **)0, | |
| 3062 | + 0); | |
| 3063 | + } | |
| 3064 | + } | |
| 3065 | + else | |
| 3066 | + x = PyOS_strtol((char *)s, (char **)&end, 0); | |
| 3067 | + if (*end == '\0') { | |
| 3068 | + if (errno != 0) | |
| 3069 | + return PyLong_FromString((char *)s, (char **)0, 0); | |
| 3070 | + return PyInt_FromLong(x); | |
| 3071 | + } | |
| 3072 | + /* XXX Huge floats may silently fail */ | |
| 3073 | +#ifndef WITHOUT_COMPLEX | |
| 3074 | + if (imflag) { | |
| 3075 | + c.real = 0.; | |
| 3076 | + PyFPE_START_PROTECT("atof", return 0) | |
| 3077 | + c.imag = PyOS_ascii_atof(s); | |
| 3078 | + PyFPE_END_PROTECT(c) | |
| 3079 | + return PyComplex_FromCComplex(c); | |
| 3080 | + } | |
| 3081 | + else | |
| 3082 | +#endif | |
| 3083 | + { | |
| 3084 | + PyFPE_START_PROTECT("atof", return 0) | |
| 3085 | + dx = PyOS_ascii_atof(s); | |
| 3086 | + PyFPE_END_PROTECT(dx) | |
| 3087 | + return PyFloat_FromDouble(dx); | |
| 3088 | + } | |
| 3089 | +} | |
| 3090 | + | |
| 3091 | +static PyObject * | |
| 3092 | +decode_utf8(const char **sPtr, const char *end, char* encoding) | |
| 3093 | +{ | |
| 3094 | +#ifndef Py_USING_UNICODE | |
| 3095 | + Py_FatalError("decode_utf8 should not be called in this build."); | |
| 3096 | + return NULL; | |
| 3097 | +#else | |
| 3098 | + PyObject *u, *v; | |
| 3099 | + char *s, *t; | |
| 3100 | + t = s = (char *)*sPtr; | |
| 3101 | + /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */ | |
| 3102 | + while (s < end && (*s & 0x80)) s++; | |
| 3103 | + *sPtr = s; | |
| 3104 | + u = PyUnicode_DecodeUTF8(t, s - t, NULL); | |
| 3105 | + if (u == NULL) | |
| 3106 | + return NULL; | |
| 3107 | + v = PyUnicode_AsEncodedString(u, encoding, NULL); | |
| 3108 | + Py_DECREF(u); | |
| 3109 | + return v; | |
| 3110 | +#endif | |
| 3111 | +} | |
| 3112 | + | |
| 3113 | +static PyObject * | |
| 3114 | +decode_unicode(const char *s, size_t len, int rawmode, const char *encoding) | |
| 3115 | +{ | |
| 3116 | + PyObject *v, *u; | |
| 3117 | + char *buf; | |
| 3118 | + char *p; | |
| 3119 | + const char *end; | |
| 3120 | + if (encoding == NULL) { | |
| 3121 | + buf = (char *)s; | |
| 3122 | + u = NULL; | |
| 3123 | + } else if (strcmp(encoding, "iso-8859-1") == 0) { | |
| 3124 | + buf = (char *)s; | |
| 3125 | + u = NULL; | |
| 3126 | + } else { | |
| 3127 | + /* "\XX" may become "\u005c\uHHLL" (12 bytes) */ | |
| 3128 | + u = PyString_FromStringAndSize((char *)NULL, len * 4); | |
| 3129 | + if (u == NULL) | |
| 3130 | + return NULL; | |
| 3131 | + p = buf = PyString_AsString(u); | |
| 3132 | + end = s + len; | |
| 3133 | + while (s < end) { | |
| 3134 | + if (*s == '\\') { | |
| 3135 | + *p++ = *s++; | |
| 3136 | + if (*s & 0x80) { | |
| 3137 | + strcpy(p, "u005c"); | |
| 3138 | + p += 5; | |
| 3139 | + } | |
| 3140 | + } | |
| 3141 | + if (*s & 0x80) { /* XXX inefficient */ | |
| 3142 | + PyObject *w; | |
| 3143 | + char *r; | |
| 3144 | + Py_ssize_t rn, i; | |
| 3145 | + w = decode_utf8(&s, end, "utf-16-be"); | |
| 3146 | + if (w == NULL) { | |
| 3147 | + Py_DECREF(u); | |
| 3148 | + return NULL; | |
| 3149 | + } | |
| 3150 | + r = PyString_AsString(w); | |
| 3151 | + rn = PyString_Size(w); | |
| 3152 | + assert(rn % 2 == 0); | |
| 3153 | + for (i = 0; i < rn; i += 2) { | |
| 3154 | + sprintf(p, "\\u%02x%02x", | |
| 3155 | + r[i + 0] & 0xFF, | |
| 3156 | + r[i + 1] & 0xFF); | |
| 3157 | + p += 6; | |
| 3158 | + } | |
| 3159 | + Py_DECREF(w); | |
| 3160 | + } else { | |
| 3161 | + *p++ = *s++; | |
| 3162 | + } | |
| 3163 | + } | |
| 3164 | + len = p - buf; | |
| 3165 | + s = buf; | |
| 3166 | + } | |
| 3167 | + if (rawmode) | |
| 3168 | + v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL); | |
| 3169 | + else | |
| 3170 | + v = PyUnicode_DecodeUnicodeEscape(s, len, NULL); | |
| 3171 | + Py_XDECREF(u); | |
| 3172 | + return v; | |
| 3173 | +} | |
| 3174 | + | |
| 3175 | +/* s is a Python string literal, including the bracketing quote characters, | |
| 3176 | + * and r &/or u prefixes (if any), and embedded escape sequences (if any). | |
| 3177 | + * parsestr parses it, and returns the decoded Python string object. | |
| 3178 | + */ | |
| 3179 | +static PyObject * | |
| 3180 | +parsestr(const char *s, const char *encoding) | |
| 3181 | +{ | |
| 3182 | + size_t len; | |
| 3183 | + int quote = Py_CHARMASK(*s); | |
| 3184 | + int rawmode = 0; | |
| 3185 | + int need_encoding; | |
| 3186 | + int unicode = 0; | |
| 3187 | + | |
| 3188 | + if (isalpha(quote) || quote == '_') { | |
| 3189 | + if (quote == 'u' || quote == 'U') { | |
| 3190 | + quote = *++s; | |
| 3191 | + unicode = 1; | |
| 3192 | + } | |
| 3193 | + if (quote == 'r' || quote == 'R') { | |
| 3194 | + quote = *++s; | |
| 3195 | + rawmode = 1; | |
| 3196 | + } | |
| 3197 | + } | |
| 3198 | + if (quote != '\'' && quote != '\"') { | |
| 3199 | + PyErr_BadInternalCall(); | |
| 3200 | + return NULL; | |
| 3201 | + } | |
| 3202 | + s++; | |
| 3203 | + len = strlen(s); | |
| 3204 | + if (len > INT_MAX) { | |
| 3205 | + PyErr_SetString(PyExc_OverflowError, | |
| 3206 | + "string to parse is too long"); | |
| 3207 | + return NULL; | |
| 3208 | + } | |
| 3209 | + if (s[--len] != quote) { | |
| 3210 | + PyErr_BadInternalCall(); | |
| 3211 | + return NULL; | |
| 3212 | + } | |
| 3213 | + if (len >= 4 && s[0] == quote && s[1] == quote) { | |
| 3214 | + s += 2; | |
| 3215 | + len -= 2; | |
| 3216 | + if (s[--len] != quote || s[--len] != quote) { | |
| 3217 | + PyErr_BadInternalCall(); | |
| 3218 | + return NULL; | |
| 3219 | + } | |
| 3220 | + } | |
| 3221 | +#ifdef Py_USING_UNICODE | |
| 3222 | + if (unicode || Py_UnicodeFlag) { | |
| 3223 | + return decode_unicode(s, len, rawmode, encoding); | |
| 3224 | + } | |
| 3225 | +#endif | |
| 3226 | + need_encoding = (encoding != NULL && | |
| 3227 | + strcmp(encoding, "utf-8") != 0 && | |
| 3228 | + strcmp(encoding, "iso-8859-1") != 0); | |
| 3229 | + if (rawmode || strchr(s, '\\') == NULL) { | |
| 3230 | + if (need_encoding) { | |
| 3231 | +#ifndef Py_USING_UNICODE | |
| 3232 | + /* This should not happen - we never see any other | |
| 3233 | + encoding. */ | |
| 3234 | + Py_FatalError( | |
| 3235 | + "cannot deal with encodings in this build."); | |
| 3236 | +#else | |
| 3237 | + PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL); | |
| 3238 | + if (u == NULL) | |
| 3239 | + return NULL; | |
| 3240 | + v = PyUnicode_AsEncodedString(u, encoding, NULL); | |
| 3241 | + Py_DECREF(u); | |
| 3242 | + return v; | |
| 3243 | +#endif | |
| 3244 | + } else { | |
| 3245 | + return PyString_FromStringAndSize(s, len); | |
| 3246 | + } | |
| 3247 | + } | |
| 3248 | + | |
| 3249 | + return PyString_DecodeEscape(s, len, NULL, unicode, | |
| 3250 | + need_encoding ? encoding : NULL); | |
| 3251 | +} | |
| 3252 | + | |
| 3253 | +/* Build a Python string object out of a STRING atom. This takes care of | |
| 3254 | + * compile-time literal catenation, calling parsestr() on each piece, and | |
| 3255 | + * pasting the intermediate results together. | |
| 3256 | + */ | |
| 3257 | +static PyObject * | |
| 3258 | +parsestrplus(struct compiling *c, const node *n) | |
| 3259 | +{ | |
| 3260 | + PyObject *v; | |
| 3261 | + int i; | |
| 3262 | + REQ(CHILD(n, 0), STRING); | |
| 3263 | + if ((v = parsestr(STR(CHILD(n, 0)), c->c_encoding)) != NULL) { | |
| 3264 | + /* String literal concatenation */ | |
| 3265 | + for (i = 1; i < NCH(n); i++) { | |
| 3266 | + PyObject *s; | |
| 3267 | + s = parsestr(STR(CHILD(n, i)), c->c_encoding); | |
| 3268 | + if (s == NULL) | |
| 3269 | + goto onError; | |
| 3270 | + if (PyString_Check(v) && PyString_Check(s)) { | |
| 3271 | + PyString_ConcatAndDel(&v, s); | |
| 3272 | + if (v == NULL) | |
| 3273 | + goto onError; | |
| 3274 | + } | |
| 3275 | +#ifdef Py_USING_UNICODE | |
| 3276 | + else { | |
| 3277 | + PyObject *temp = PyUnicode_Concat(v, s); | |
| 3278 | + Py_DECREF(s); | |
| 3279 | + Py_DECREF(v); | |
| 3280 | + v = temp; | |
| 3281 | + if (v == NULL) | |
| 3282 | + goto onError; | |
| 3283 | + } | |
| 3284 | +#endif | |
| 3285 | + } | |
| 3286 | + } | |
| 3287 | + return v; | |
| 3288 | + | |
| 3289 | + onError: | |
| 3290 | + Py_XDECREF(v); | |
| 3291 | + return NULL; | |
| 3292 | +} | |
| @@ -0,0 +1,50 @@ | ||
| 1 | + | |
| 2 | +/* Just in case you haven't got an atof() around... | |
| 3 | + This one doesn't check for bad syntax or overflow, | |
| 4 | + and is slow and inaccurate. | |
| 5 | + But it's good enough for the occasional string literal... */ | |
| 6 | + | |
| 7 | +#include "pyconfig.h" | |
| 8 | + | |
| 9 | +#include <ctype.h> | |
| 10 | + | |
| 11 | +double atof(char *s) | |
| 12 | +{ | |
| 13 | + double a = 0.0; | |
| 14 | + int e = 0; | |
| 15 | + int c; | |
| 16 | + while ((c = *s++) != '\0' && isdigit(c)) { | |
| 17 | + a = a*10.0 + (c - '0'); | |
| 18 | + } | |
| 19 | + if (c == '.') { | |
| 20 | + while ((c = *s++) != '\0' && isdigit(c)) { | |
| 21 | + a = a*10.0 + (c - '0'); | |
| 22 | + e = e-1; | |
| 23 | + } | |
| 24 | + } | |
| 25 | + if (c == 'e' || c == 'E') { | |
| 26 | + int sign = 1; | |
| 27 | + int i = 0; | |
| 28 | + c = *s++; | |
| 29 | + if (c == '+') | |
| 30 | + c = *s++; | |
| 31 | + else if (c == '-') { | |
| 32 | + c = *s++; | |
| 33 | + sign = -1; | |
| 34 | + } | |
| 35 | + while (isdigit(c)) { | |
| 36 | + i = i*10 + (c - '0'); | |
| 37 | + c = *s++; | |
| 38 | + } | |
| 39 | + e += i*sign; | |
| 40 | + } | |
| 41 | + while (e > 0) { | |
| 42 | + a *= 10.0; | |
| 43 | + e--; | |
| 44 | + } | |
| 45 | + while (e < 0) { | |
| 46 | + a *= 0.1; | |
| 47 | + e++; | |
| 48 | + } | |
| 49 | + return a; | |
| 50 | +} | |
| @@ -0,0 +1,2620 @@ | ||
| 1 | +/* Built-in functions */ | |
| 2 | + | |
| 3 | +#include "Python.h" | |
| 4 | + | |
| 5 | +#include "node.h" | |
| 6 | +#include "code.h" | |
| 7 | +#include "eval.h" | |
| 8 | + | |
| 9 | +#include <ctype.h> | |
| 10 | + | |
| 11 | +#ifdef RISCOS | |
| 12 | +#include "unixstuff.h" | |
| 13 | +#endif | |
| 14 | + | |
| 15 | +/* The default encoding used by the platform file system APIs | |
| 16 | + Can remain NULL for all platforms that don't have such a concept | |
| 17 | +*/ | |
| 18 | +#if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) | |
| 19 | +const char *Py_FileSystemDefaultEncoding = "mbcs"; | |
| 20 | +#elif defined(__APPLE__) | |
| 21 | +const char *Py_FileSystemDefaultEncoding = "utf-8"; | |
| 22 | +#else | |
| 23 | +const char *Py_FileSystemDefaultEncoding = NULL; /* use default */ | |
| 24 | +#endif | |
| 25 | + | |
| 26 | +/* Forward */ | |
| 27 | +static PyObject *filterstring(PyObject *, PyObject *); | |
| 28 | +#ifdef Py_USING_UNICODE | |
| 29 | +static PyObject *filterunicode(PyObject *, PyObject *); | |
| 30 | +#endif | |
| 31 | +static PyObject *filtertuple (PyObject *, PyObject *); | |
| 32 | + | |
| 33 | +static PyObject * | |
| 34 | +builtin___import__(PyObject *self, PyObject *args, PyObject *kwds) | |
| 35 | +{ | |
| 36 | + static char *kwlist[] = {"name", "globals", "locals", "fromlist", | |
| 37 | + "level", 0}; | |
| 38 | + char *name; | |
| 39 | + PyObject *globals = NULL; | |
| 40 | + PyObject *locals = NULL; | |
| 41 | + PyObject *fromlist = NULL; | |
| 42 | + int level = -1; | |
| 43 | + | |
| 44 | + if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|OOOi:__import__", | |
| 45 | + kwlist, &name, &globals, &locals, &fromlist, &level)) | |
| 46 | + return NULL; | |
| 47 | + return PyImport_ImportModuleLevel(name, globals, locals, | |
| 48 | + fromlist, level); | |
| 49 | +} | |
| 50 | + | |
| 51 | +PyDoc_STRVAR(import_doc, | |
| 52 | +"__import__(name, globals={}, locals={}, fromlist=[], level=-1) -> module\n\ | |
| 53 | +\n\ | |
| 54 | +Import a module. The globals are only used to determine the context;\n\ | |
| 55 | +they are not modified. The locals are currently unused. The fromlist\n\ | |
| 56 | +should be a list of names to emulate ``from name import ...'', or an\n\ | |
| 57 | +empty list to emulate ``import name''.\n\ | |
| 58 | +When importing a module from a package, note that __import__('A.B', ...)\n\ | |
| 59 | +returns package A when fromlist is empty, but its submodule B when\n\ | |
| 60 | +fromlist is not empty. Level is used to determine whether to perform \n\ | |
| 61 | +absolute or relative imports. -1 is the original strategy of attempting\n\ | |
| 62 | +both absolute and relative imports, 0 is absolute, a positive number\n\ | |
| 63 | +is the number of parent directories to search relative to the current module."); | |
| 64 | + | |
| 65 | + | |
| 66 | +static PyObject * | |
| 67 | +builtin_abs(PyObject *self, PyObject *v) | |
| 68 | +{ | |
| 69 | + return PyNumber_Absolute(v); | |
| 70 | +} | |
| 71 | + | |
| 72 | +PyDoc_STRVAR(abs_doc, | |
| 73 | +"abs(number) -> number\n\ | |
| 74 | +\n\ | |
| 75 | +Return the absolute value of the argument."); | |
| 76 | + | |
| 77 | +static PyObject * | |
| 78 | +builtin_all(PyObject *self, PyObject *v) | |
| 79 | +{ | |
| 80 | + PyObject *it, *item; | |
| 81 | + | |
| 82 | + it = PyObject_GetIter(v); | |
| 83 | + if (it == NULL) | |
| 84 | + return NULL; | |
| 85 | + | |
| 86 | + while ((item = PyIter_Next(it)) != NULL) { | |
| 87 | + int cmp = PyObject_IsTrue(item); | |
| 88 | + Py_DECREF(item); | |
| 89 | + if (cmp < 0) { | |
| 90 | + Py_DECREF(it); | |
| 91 | + return NULL; | |
| 92 | + } | |
| 93 | + if (cmp == 0) { | |
| 94 | + Py_DECREF(it); | |
| 95 | + Py_RETURN_FALSE; | |
| 96 | + } | |
| 97 | + } | |
| 98 | + Py_DECREF(it); | |
| 99 | + if (PyErr_Occurred()) | |
| 100 | + return NULL; | |
| 101 | + Py_RETURN_TRUE; | |
| 102 | +} | |
| 103 | + | |
| 104 | +PyDoc_STRVAR(all_doc, | |
| 105 | +"all(iterable) -> bool\n\ | |
| 106 | +\n\ | |
| 107 | +Return True if bool(x) is True for all values x in the iterable."); | |
| 108 | + | |
| 109 | +static PyObject * | |
| 110 | +builtin_any(PyObject *self, PyObject *v) | |
| 111 | +{ | |
| 112 | + PyObject *it, *item; | |
| 113 | + | |
| 114 | + it = PyObject_GetIter(v); | |
| 115 | + if (it == NULL) | |
| 116 | + return NULL; | |
| 117 | + | |
| 118 | + while ((item = PyIter_Next(it)) != NULL) { | |
| 119 | + int cmp = PyObject_IsTrue(item); | |
| 120 | + Py_DECREF(item); | |
| 121 | + if (cmp < 0) { | |
| 122 | + Py_DECREF(it); | |
| 123 | + return NULL; | |
| 124 | + } | |
| 125 | + if (cmp == 1) { | |
| 126 | + Py_DECREF(it); | |
| 127 | + Py_RETURN_TRUE; | |
| 128 | + } | |
| 129 | + } | |
| 130 | + Py_DECREF(it); | |
| 131 | + if (PyErr_Occurred()) | |
| 132 | + return NULL; | |
| 133 | + Py_RETURN_FALSE; | |
| 134 | +} | |
| 135 | + | |
| 136 | +PyDoc_STRVAR(any_doc, | |
| 137 | +"any(iterable) -> bool\n\ | |
| 138 | +\n\ | |
| 139 | +Return True if bool(x) is True for any x in the iterable."); | |
| 140 | + | |
| 141 | +static PyObject * | |
| 142 | +builtin_apply(PyObject *self, PyObject *args) | |
| 143 | +{ | |
| 144 | + PyObject *func, *alist = NULL, *kwdict = NULL; | |
| 145 | + PyObject *t = NULL, *retval = NULL; | |
| 146 | + | |
| 147 | + if (!PyArg_UnpackTuple(args, "apply", 1, 3, &func, &alist, &kwdict)) | |
| 148 | + return NULL; | |
| 149 | + if (alist != NULL) { | |
| 150 | + if (!PyTuple_Check(alist)) { | |
| 151 | + if (!PySequence_Check(alist)) { | |
| 152 | + PyErr_Format(PyExc_TypeError, | |
| 153 | + "apply() arg 2 expected sequence, found %s", | |
| 154 | + alist->ob_type->tp_name); | |
| 155 | + return NULL; | |
| 156 | + } | |
| 157 | + t = PySequence_Tuple(alist); | |
| 158 | + if (t == NULL) | |
| 159 | + return NULL; | |
| 160 | + alist = t; | |
| 161 | + } | |
| 162 | + } | |
| 163 | + if (kwdict != NULL && !PyDict_Check(kwdict)) { | |
| 164 | + PyErr_Format(PyExc_TypeError, | |
| 165 | + "apply() arg 3 expected dictionary, found %s", | |
| 166 | + kwdict->ob_type->tp_name); | |
| 167 | + goto finally; | |
| 168 | + } | |
| 169 | + retval = PyEval_CallObjectWithKeywords(func, alist, kwdict); | |
| 170 | + finally: | |
| 171 | + Py_XDECREF(t); | |
| 172 | + return retval; | |
| 173 | +} | |
| 174 | + | |
| 175 | +PyDoc_STRVAR(apply_doc, | |
| 176 | +"apply(object[, args[, kwargs]]) -> value\n\ | |
| 177 | +\n\ | |
| 178 | +Call a callable object with positional arguments taken from the tuple args,\n\ | |
| 179 | +and keyword arguments taken from the optional dictionary kwargs.\n\ | |
| 180 | +Note that classes are callable, as are instances with a __call__() method.\n\ | |
| 181 | +\n\ | |
| 182 | +Deprecated since release 2.3. Instead, use the extended call syntax:\n\ | |
| 183 | + function(*args, **keywords)."); | |
| 184 | + | |
| 185 | + | |
| 186 | +static PyObject * | |
| 187 | +builtin_callable(PyObject *self, PyObject *v) | |
| 188 | +{ | |
| 189 | + return PyBool_FromLong((long)PyCallable_Check(v)); | |
| 190 | +} | |
| 191 | + | |
| 192 | +PyDoc_STRVAR(callable_doc, | |
| 193 | +"callable(object) -> bool\n\ | |
| 194 | +\n\ | |
| 195 | +Return whether the object is callable (i.e., some kind of function).\n\ | |
| 196 | +Note that classes are callable, as are instances with a __call__() method."); | |
| 197 | + | |
| 198 | + | |
| 199 | +static PyObject * | |
| 200 | +builtin_filter(PyObject *self, PyObject *args) | |
| 201 | +{ | |
| 202 | + PyObject *func, *seq, *result, *it, *arg; | |
| 203 | + Py_ssize_t len; /* guess for result list size */ | |
| 204 | + register Py_ssize_t j; | |
| 205 | + | |
| 206 | + if (!PyArg_UnpackTuple(args, "filter", 2, 2, &func, &seq)) | |
| 207 | + return NULL; | |
| 208 | + | |
| 209 | + /* Strings and tuples return a result of the same type. */ | |
| 210 | + if (PyString_Check(seq)) | |
| 211 | + return filterstring(func, seq); | |
| 212 | +#ifdef Py_USING_UNICODE | |
| 213 | + if (PyUnicode_Check(seq)) | |
| 214 | + return filterunicode(func, seq); | |
| 215 | +#endif | |
| 216 | + if (PyTuple_Check(seq)) | |
| 217 | + return filtertuple(func, seq); | |
| 218 | + | |
| 219 | + /* Pre-allocate argument list tuple. */ | |
| 220 | + arg = PyTuple_New(1); | |
| 221 | + if (arg == NULL) | |
| 222 | + return NULL; | |
| 223 | + | |
| 224 | + /* Get iterator. */ | |
| 225 | + it = PyObject_GetIter(seq); | |
| 226 | + if (it == NULL) | |
| 227 | + goto Fail_arg; | |
| 228 | + | |
| 229 | + /* Guess a result list size. */ | |
| 230 | + len = _PyObject_LengthHint(seq); | |
| 231 | + if (len < 0) { | |
| 232 | + if (!PyErr_ExceptionMatches(PyExc_TypeError) && | |
| 233 | + !PyErr_ExceptionMatches(PyExc_AttributeError)) { | |
| 234 | + goto Fail_it; | |
| 235 | + } | |
| 236 | + PyErr_Clear(); | |
| 237 | + len = 8; /* arbitrary */ | |
| 238 | + } | |
| 239 | + | |
| 240 | + /* Get a result list. */ | |
| 241 | + if (PyList_Check(seq) && seq->ob_refcnt == 1) { | |
| 242 | + /* Eww - can modify the list in-place. */ | |
| 243 | + Py_INCREF(seq); | |
| 244 | + result = seq; | |
| 245 | + } | |
| 246 | + else { | |
| 247 | + result = PyList_New(len); | |
| 248 | + if (result == NULL) | |
| 249 | + goto Fail_it; | |
| 250 | + } | |
| 251 | + | |
| 252 | + /* Build the result list. */ | |
| 253 | + j = 0; | |
| 254 | + for (;;) { | |
| 255 | + PyObject *item; | |
| 256 | + int ok; | |
| 257 | + | |
| 258 | + item = PyIter_Next(it); | |
| 259 | + if (item == NULL) { | |
| 260 | + if (PyErr_Occurred()) | |
| 261 | + goto Fail_result_it; | |
| 262 | + break; | |
| 263 | + } | |
| 264 | + | |
| 265 | + if (func == (PyObject *)&PyBool_Type || func == Py_None) { | |
| 266 | + ok = PyObject_IsTrue(item); | |
| 267 | + } | |
| 268 | + else { | |
| 269 | + PyObject *good; | |
| 270 | + PyTuple_SET_ITEM(arg, 0, item); | |
| 271 | + good = PyObject_Call(func, arg, NULL); | |
| 272 | + PyTuple_SET_ITEM(arg, 0, NULL); | |
| 273 | + if (good == NULL) { | |
| 274 | + Py_DECREF(item); | |
| 275 | + goto Fail_result_it; | |
| 276 | + } | |
| 277 | + ok = PyObject_IsTrue(good); | |
| 278 | + Py_DECREF(good); | |
| 279 | + } | |
| 280 | + if (ok) { | |
| 281 | + if (j < len) | |
| 282 | + PyList_SET_ITEM(result, j, item); | |
| 283 | + else { | |
| 284 | + int status = PyList_Append(result, item); | |
| 285 | + Py_DECREF(item); | |
| 286 | + if (status < 0) | |
| 287 | + goto Fail_result_it; | |
| 288 | + } | |
| 289 | + ++j; | |
| 290 | + } | |
| 291 | + else | |
| 292 | + Py_DECREF(item); | |
| 293 | + } | |
| 294 | + | |
| 295 | + | |
| 296 | + /* Cut back result list if len is too big. */ | |
| 297 | + if (j < len && PyList_SetSlice(result, j, len, NULL) < 0) | |
| 298 | + goto Fail_result_it; | |
| 299 | + | |
| 300 | + Py_DECREF(it); | |
| 301 | + Py_DECREF(arg); | |
| 302 | + return result; | |
| 303 | + | |
| 304 | +Fail_result_it: | |
| 305 | + Py_DECREF(result); | |
| 306 | +Fail_it: | |
| 307 | + Py_DECREF(it); | |
| 308 | +Fail_arg: | |
| 309 | + Py_DECREF(arg); | |
| 310 | + return NULL; | |
| 311 | +} | |
| 312 | + | |
| 313 | +PyDoc_STRVAR(filter_doc, | |
| 314 | +"filter(function or None, sequence) -> list, tuple, or string\n" | |
| 315 | +"\n" | |
| 316 | +"Return those items of sequence for which function(item) is true. If\n" | |
| 317 | +"function is None, return the items that are true. If sequence is a tuple\n" | |
| 318 | +"or string, return the same type, else return a list."); | |
| 319 | + | |
| 320 | +static PyObject * | |
| 321 | +builtin_chr(PyObject *self, PyObject *args) | |
| 322 | +{ | |
| 323 | + long x; | |
| 324 | + char s[1]; | |
| 325 | + | |
| 326 | + if (!PyArg_ParseTuple(args, "l:chr", &x)) | |
| 327 | + return NULL; | |
| 328 | + if (x < 0 || x >= 256) { | |
| 329 | + PyErr_SetString(PyExc_ValueError, | |
| 330 | + "chr() arg not in range(256)"); | |
| 331 | + return NULL; | |
| 332 | + } | |
| 333 | + s[0] = (char)x; | |
| 334 | + return PyString_FromStringAndSize(s, 1); | |
| 335 | +} | |
| 336 | + | |
| 337 | +PyDoc_STRVAR(chr_doc, | |
| 338 | +"chr(i) -> character\n\ | |
| 339 | +\n\ | |
| 340 | +Return a string of one character with ordinal i; 0 <= i < 256."); | |
| 341 | + | |
| 342 | + | |
| 343 | +#ifdef Py_USING_UNICODE | |
| 344 | +static PyObject * | |
| 345 | +builtin_unichr(PyObject *self, PyObject *args) | |
| 346 | +{ | |
| 347 | + long x; | |
| 348 | + | |
| 349 | + if (!PyArg_ParseTuple(args, "l:unichr", &x)) | |
| 350 | + return NULL; | |
| 351 | + | |
| 352 | + return PyUnicode_FromOrdinal(x); | |
| 353 | +} | |
| 354 | + | |
| 355 | +PyDoc_STRVAR(unichr_doc, | |
| 356 | +"unichr(i) -> Unicode character\n\ | |
| 357 | +\n\ | |
| 358 | +Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff."); | |
| 359 | +#endif | |
| 360 | + | |
| 361 | + | |
| 362 | +static PyObject * | |
| 363 | +builtin_cmp(PyObject *self, PyObject *args) | |
| 364 | +{ | |
| 365 | + PyObject *a, *b; | |
| 366 | + int c; | |
| 367 | + | |
| 368 | + if (!PyArg_UnpackTuple(args, "cmp", 2, 2, &a, &b)) | |
| 369 | + return NULL; | |
| 370 | + if (PyObject_Cmp(a, b, &c) < 0) | |
| 371 | + return NULL; | |
| 372 | + return PyInt_FromLong((long)c); | |
| 373 | +} | |
| 374 | + | |
| 375 | +PyDoc_STRVAR(cmp_doc, | |
| 376 | +"cmp(x, y) -> integer\n\ | |
| 377 | +\n\ | |
| 378 | +Return negative if x<y, zero if x==y, positive if x>y."); | |
| 379 | + | |
| 380 | + | |
| 381 | +static PyObject * | |
| 382 | +builtin_coerce(PyObject *self, PyObject *args) | |
| 383 | +{ | |
| 384 | + PyObject *v, *w; | |
| 385 | + PyObject *res; | |
| 386 | + | |
| 387 | + if (!PyArg_UnpackTuple(args, "coerce", 2, 2, &v, &w)) | |
| 388 | + return NULL; | |
| 389 | + if (PyNumber_Coerce(&v, &w) < 0) | |
| 390 | + return NULL; | |
| 391 | + res = PyTuple_Pack(2, v, w); | |
| 392 | + Py_DECREF(v); | |
| 393 | + Py_DECREF(w); | |
| 394 | + return res; | |
| 395 | +} | |
| 396 | + | |
| 397 | +PyDoc_STRVAR(coerce_doc, | |
| 398 | +"coerce(x, y) -> (x1, y1)\n\ | |
| 399 | +\n\ | |
| 400 | +Return a tuple consisting of the two numeric arguments converted to\n\ | |
| 401 | +a common type, using the same rules as used by arithmetic operations.\n\ | |
| 402 | +If coercion is not possible, raise TypeError."); | |
| 403 | + | |
| 404 | +static PyObject * | |
| 405 | +builtin_compile(PyObject *self, PyObject *args) | |
| 406 | +{ | |
| 407 | + char *str; | |
| 408 | + char *filename; | |
| 409 | + char *startstr; | |
| 410 | + int start; | |
| 411 | + int dont_inherit = 0; | |
| 412 | + int supplied_flags = 0; | |
| 413 | + PyCompilerFlags cf; | |
| 414 | + PyObject *result = NULL, *cmd, *tmp = NULL; | |
| 415 | + Py_ssize_t length; | |
| 416 | + | |
| 417 | + if (!PyArg_ParseTuple(args, "Oss|ii:compile", &cmd, &filename, | |
| 418 | + &startstr, &supplied_flags, &dont_inherit)) | |
| 419 | + return NULL; | |
| 420 | + | |
| 421 | + cf.cf_flags = supplied_flags; | |
| 422 | + | |
| 423 | +#ifdef Py_USING_UNICODE | |
| 424 | + if (PyUnicode_Check(cmd)) { | |
| 425 | + tmp = PyUnicode_AsUTF8String(cmd); | |
| 426 | + if (tmp == NULL) | |
| 427 | + return NULL; | |
| 428 | + cmd = tmp; | |
| 429 | + cf.cf_flags |= PyCF_SOURCE_IS_UTF8; | |
| 430 | + } | |
| 431 | +#endif | |
| 432 | + if (PyObject_AsReadBuffer(cmd, (const void **)&str, &length)) | |
| 433 | + return NULL; | |
| 434 | + if ((size_t)length != strlen(str)) { | |
| 435 | + PyErr_SetString(PyExc_TypeError, | |
| 436 | + "compile() expected string without null bytes"); | |
| 437 | + goto cleanup; | |
| 438 | + } | |
| 439 | + | |
| 440 | + if (strcmp(startstr, "exec") == 0) | |
| 441 | + start = Py_file_input; | |
| 442 | + else if (strcmp(startstr, "eval") == 0) | |
| 443 | + start = Py_eval_input; | |
| 444 | + else if (strcmp(startstr, "single") == 0) | |
| 445 | + start = Py_single_input; | |
| 446 | + else { | |
| 447 | + PyErr_SetString(PyExc_ValueError, | |
| 448 | + "compile() arg 3 must be 'exec' or 'eval' or 'single'"); | |
| 449 | + goto cleanup; | |
| 450 | + } | |
| 451 | + | |
| 452 | + if (supplied_flags & | |
| 453 | + ~(PyCF_MASK | PyCF_MASK_OBSOLETE | PyCF_DONT_IMPLY_DEDENT | PyCF_ONLY_AST)) | |
| 454 | + { | |
| 455 | + PyErr_SetString(PyExc_ValueError, | |
| 456 | + "compile(): unrecognised flags"); | |
| 457 | + goto cleanup; | |
| 458 | + } | |
| 459 | + /* XXX Warn if (supplied_flags & PyCF_MASK_OBSOLETE) != 0? */ | |
| 460 | + | |
| 461 | + if (!dont_inherit) { | |
| 462 | + PyEval_MergeCompilerFlags(&cf); | |
| 463 | + } | |
| 464 | + result = Py_CompileStringFlags(str, filename, start, &cf); | |
| 465 | +cleanup: | |
| 466 | + Py_XDECREF(tmp); | |
| 467 | + return result; | |
| 468 | +} | |
| 469 | + | |
| 470 | +PyDoc_STRVAR(compile_doc, | |
| 471 | +"compile(source, filename, mode[, flags[, dont_inherit]]) -> code object\n\ | |
| 472 | +\n\ | |
| 473 | +Compile the source string (a Python module, statement or expression)\n\ | |
| 474 | +into a code object that can be executed by the exec statement or eval().\n\ | |
| 475 | +The filename will be used for run-time error messages.\n\ | |
| 476 | +The mode must be 'exec' to compile a module, 'single' to compile a\n\ | |
| 477 | +single (interactive) statement, or 'eval' to compile an expression.\n\ | |
| 478 | +The flags argument, if present, controls which future statements influence\n\ | |
| 479 | +the compilation of the code.\n\ | |
| 480 | +The dont_inherit argument, if non-zero, stops the compilation inheriting\n\ | |
| 481 | +the effects of any future statements in effect in the code calling\n\ | |
| 482 | +compile; if absent or zero these statements do influence the compilation,\n\ | |
| 483 | +in addition to any features explicitly specified."); | |
| 484 | + | |
| 485 | +static PyObject * | |
| 486 | +builtin_dir(PyObject *self, PyObject *args) | |
| 487 | +{ | |
| 488 | + PyObject *arg = NULL; | |
| 489 | + | |
| 490 | + if (!PyArg_UnpackTuple(args, "dir", 0, 1, &arg)) | |
| 491 | + return NULL; | |
| 492 | + return PyObject_Dir(arg); | |
| 493 | +} | |
| 494 | + | |
| 495 | +PyDoc_STRVAR(dir_doc, | |
| 496 | +"dir([object]) -> list of strings\n" | |
| 497 | +"\n" | |
| 498 | +"Return an alphabetized list of names comprising (some of) the attributes\n" | |
| 499 | +"of the given object, and of attributes reachable from it:\n" | |
| 500 | +"\n" | |
| 501 | +"No argument: the names in the current scope.\n" | |
| 502 | +"Module object: the module attributes.\n" | |
| 503 | +"Type or class object: its attributes, and recursively the attributes of\n" | |
| 504 | +" its bases.\n" | |
| 505 | +"Otherwise: its attributes, its class's attributes, and recursively the\n" | |
| 506 | +" attributes of its class's base classes."); | |
| 507 | + | |
| 508 | +static PyObject * | |
| 509 | +builtin_divmod(PyObject *self, PyObject *args) | |
| 510 | +{ | |
| 511 | + PyObject *v, *w; | |
| 512 | + | |
| 513 | + if (!PyArg_UnpackTuple(args, "divmod", 2, 2, &v, &w)) | |
| 514 | + return NULL; | |
| 515 | + return PyNumber_Divmod(v, w); | |
| 516 | +} | |
| 517 | + | |
| 518 | +PyDoc_STRVAR(divmod_doc, | |
| 519 | +"divmod(x, y) -> (div, mod)\n\ | |
| 520 | +\n\ | |
| 521 | +Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x."); | |
| 522 | + | |
| 523 | + | |
| 524 | +static PyObject * | |
| 525 | +builtin_eval(PyObject *self, PyObject *args) | |
| 526 | +{ | |
| 527 | + PyObject *cmd, *result, *tmp = NULL; | |
| 528 | + PyObject *globals = Py_None, *locals = Py_None; | |
| 529 | + char *str; | |
| 530 | + PyCompilerFlags cf; | |
| 531 | + | |
| 532 | + if (!PyArg_UnpackTuple(args, "eval", 1, 3, &cmd, &globals, &locals)) | |
| 533 | + return NULL; | |
| 534 | + if (locals != Py_None && !PyMapping_Check(locals)) { | |
| 535 | + PyErr_SetString(PyExc_TypeError, "locals must be a mapping"); | |
| 536 | + return NULL; | |
| 537 | + } | |
| 538 | + if (globals != Py_None && !PyDict_Check(globals)) { | |
| 539 | + PyErr_SetString(PyExc_TypeError, PyMapping_Check(globals) ? | |
| 540 | + "globals must be a real dict; try eval(expr, {}, mapping)" | |
| 541 | + : "globals must be a dict"); | |
| 542 | + return NULL; | |
| 543 | + } | |
| 544 | + if (globals == Py_None) { | |
| 545 | + globals = PyEval_GetGlobals(); | |
| 546 | + if (locals == Py_None) | |
| 547 | + locals = PyEval_GetLocals(); | |
| 548 | + } | |
| 549 | + else if (locals == Py_None) | |
| 550 | + locals = globals; | |
| 551 | + | |
| 552 | + if (globals == NULL || locals == NULL) { | |
| 553 | + PyErr_SetString(PyExc_TypeError, | |
| 554 | + "eval must be given globals and locals " | |
| 555 | + "when called without a frame"); | |
| 556 | + return NULL; | |
| 557 | + } | |
| 558 | + | |
| 559 | + if (PyDict_GetItemString(globals, "__builtins__") == NULL) { | |
| 560 | + if (PyDict_SetItemString(globals, "__builtins__", | |
| 561 | + PyEval_GetBuiltins()) != 0) | |
| 562 | + return NULL; | |
| 563 | + } | |
| 564 | + | |
| 565 | + if (PyCode_Check(cmd)) { | |
| 566 | + if (PyCode_GetNumFree((PyCodeObject *)cmd) > 0) { | |
| 567 | + PyErr_SetString(PyExc_TypeError, | |
| 568 | + "code object passed to eval() may not contain free variables"); | |
| 569 | + return NULL; | |
| 570 | + } | |
| 571 | + return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals); | |
| 572 | + } | |
| 573 | + | |
| 574 | + if (!PyString_Check(cmd) && | |
| 575 | + !PyUnicode_Check(cmd)) { | |
| 576 | + PyErr_SetString(PyExc_TypeError, | |
| 577 | + "eval() arg 1 must be a string or code object"); | |
| 578 | + return NULL; | |
| 579 | + } | |
| 580 | + cf.cf_flags = 0; | |
| 581 | + | |
| 582 | +#ifdef Py_USING_UNICODE | |
| 583 | + if (PyUnicode_Check(cmd)) { | |
| 584 | + tmp = PyUnicode_AsUTF8String(cmd); | |
| 585 | + if (tmp == NULL) | |
| 586 | + return NULL; | |
| 587 | + cmd = tmp; | |
| 588 | + cf.cf_flags |= PyCF_SOURCE_IS_UTF8; | |
| 589 | + } | |
| 590 | +#endif | |
| 591 | + if (PyString_AsStringAndSize(cmd, &str, NULL)) { | |
| 592 | + Py_XDECREF(tmp); | |
| 593 | + return NULL; | |
| 594 | + } | |
| 595 | + while (*str == ' ' || *str == '\t') | |
| 596 | + str++; | |
| 597 | + | |
| 598 | + (void)PyEval_MergeCompilerFlags(&cf); | |
| 599 | + result = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf); | |
| 600 | + Py_XDECREF(tmp); | |
| 601 | + return result; | |
| 602 | +} | |
| 603 | + | |
| 604 | +PyDoc_STRVAR(eval_doc, | |
| 605 | +"eval(source[, globals[, locals]]) -> value\n\ | |
| 606 | +\n\ | |
| 607 | +Evaluate the source in the context of globals and locals.\n\ | |
| 608 | +The source may be a string representing a Python expression\n\ | |
| 609 | +or a code object as returned by compile().\n\ | |
| 610 | +The globals must be a dictionary and locals can be any mapping,\n\ | |
| 611 | +defaulting to the current globals and locals.\n\ | |
| 612 | +If only globals is given, locals defaults to it.\n"); | |
| 613 | + | |
| 614 | + | |
| 615 | +static PyObject * | |
| 616 | +builtin_execfile(PyObject *self, PyObject *args) | |
| 617 | +{ | |
| 618 | + char *filename; | |
| 619 | + PyObject *globals = Py_None, *locals = Py_None; | |
| 620 | + PyObject *res; | |
| 621 | + FILE* fp = NULL; | |
| 622 | + PyCompilerFlags cf; | |
| 623 | + int exists; | |
| 624 | + | |
| 625 | + if (!PyArg_ParseTuple(args, "s|O!O:execfile", | |
| 626 | + &filename, | |
| 627 | + &PyDict_Type, &globals, | |
| 628 | + &locals)) | |
| 629 | + return NULL; | |
| 630 | + if (locals != Py_None && !PyMapping_Check(locals)) { | |
| 631 | + PyErr_SetString(PyExc_TypeError, "locals must be a mapping"); | |
| 632 | + return NULL; | |
| 633 | + } | |
| 634 | + if (globals == Py_None) { | |
| 635 | + globals = PyEval_GetGlobals(); | |
| 636 | + if (locals == Py_None) | |
| 637 | + locals = PyEval_GetLocals(); | |
| 638 | + } | |
| 639 | + else if (locals == Py_None) | |
| 640 | + locals = globals; | |
| 641 | + if (PyDict_GetItemString(globals, "__builtins__") == NULL) { | |
| 642 | + if (PyDict_SetItemString(globals, "__builtins__", | |
| 643 | + PyEval_GetBuiltins()) != 0) | |
| 644 | + return NULL; | |
| 645 | + } | |
| 646 | + | |
| 647 | + exists = 0; | |
| 648 | + /* Test for existence or directory. */ | |
| 649 | +#if defined(PLAN9) | |
| 650 | + { | |
| 651 | + Dir *d; | |
| 652 | + | |
| 653 | + if ((d = dirstat(filename))!=nil) { | |
| 654 | + if(d->mode & DMDIR) | |
| 655 | + werrstr("is a directory"); | |
| 656 | + else | |
| 657 | + exists = 1; | |
| 658 | + free(d); | |
| 659 | + } | |
| 660 | + } | |
| 661 | +#elif defined(RISCOS) | |
| 662 | + if (object_exists(filename)) { | |
| 663 | + if (isdir(filename)) | |
| 664 | + errno = EISDIR; | |
| 665 | + else | |
| 666 | + exists = 1; | |
| 667 | + } | |
| 668 | +#else /* standard Posix */ | |
| 669 | + { | |
| 670 | + struct stat s; | |
| 671 | + if (stat(filename, &s) == 0) { | |
| 672 | + if (S_ISDIR(s.st_mode)) | |
| 673 | +# if defined(PYOS_OS2) && defined(PYCC_VACPP) | |
| 674 | + errno = EOS2ERR; | |
| 675 | +# else | |
| 676 | + errno = EISDIR; | |
| 677 | +# endif | |
| 678 | + else | |
| 679 | + exists = 1; | |
| 680 | + } | |
| 681 | + } | |
| 682 | +#endif | |
| 683 | + | |
| 684 | + if (exists) { | |
| 685 | + Py_BEGIN_ALLOW_THREADS | |
| 686 | + fp = fopen(filename, "r" PY_STDIOTEXTMODE); | |
| 687 | + Py_END_ALLOW_THREADS | |
| 688 | + | |
| 689 | + if (fp == NULL) { | |
| 690 | + exists = 0; | |
| 691 | + } | |
| 692 | + } | |
| 693 | + | |
| 694 | + if (!exists) { | |
| 695 | + PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename); | |
| 696 | + return NULL; | |
| 697 | + } | |
| 698 | + cf.cf_flags = 0; | |
| 699 | + if (PyEval_MergeCompilerFlags(&cf)) | |
| 700 | + res = PyRun_FileExFlags(fp, filename, Py_file_input, globals, | |
| 701 | + locals, 1, &cf); | |
| 702 | + else | |
| 703 | + res = PyRun_FileEx(fp, filename, Py_file_input, globals, | |
| 704 | + locals, 1); | |
| 705 | + return res; | |
| 706 | +} | |
| 707 | + | |
| 708 | +PyDoc_STRVAR(execfile_doc, | |
| 709 | +"execfile(filename[, globals[, locals]])\n\ | |
| 710 | +\n\ | |
| 711 | +Read and execute a Python script from a file.\n\ | |
| 712 | +The globals and locals are dictionaries, defaulting to the current\n\ | |
| 713 | +globals and locals. If only globals is given, locals defaults to it."); | |
| 714 | + | |
| 715 | + | |
| 716 | +static PyObject * | |
| 717 | +builtin_getattr(PyObject *self, PyObject *args) | |
| 718 | +{ | |
| 719 | + PyObject *v, *result, *dflt = NULL; | |
| 720 | + PyObject *name; | |
| 721 | + | |
| 722 | + if (!PyArg_UnpackTuple(args, "getattr", 2, 3, &v, &name, &dflt)) | |
| 723 | + return NULL; | |
| 724 | +#ifdef Py_USING_UNICODE | |
| 725 | + if (PyUnicode_Check(name)) { | |
| 726 | + name = _PyUnicode_AsDefaultEncodedString(name, NULL); | |
| 727 | + if (name == NULL) | |
| 728 | + return NULL; | |
| 729 | + } | |
| 730 | +#endif | |
| 731 | + | |
| 732 | + if (!PyString_Check(name)) { | |
| 733 | + PyErr_SetString(PyExc_TypeError, | |
| 734 | + "getattr(): attribute name must be string"); | |
| 735 | + return NULL; | |
| 736 | + } | |
| 737 | + result = PyObject_GetAttr(v, name); | |
| 738 | + if (result == NULL && dflt != NULL && | |
| 739 | + PyErr_ExceptionMatches(PyExc_AttributeError)) | |
| 740 | + { | |
| 741 | + PyErr_Clear(); | |
| 742 | + Py_INCREF(dflt); | |
| 743 | + result = dflt; | |
| 744 | + } | |
| 745 | + return result; | |
| 746 | +} | |
| 747 | + | |
| 748 | +PyDoc_STRVAR(getattr_doc, | |
| 749 | +"getattr(object, name[, default]) -> value\n\ | |
| 750 | +\n\ | |
| 751 | +Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\ | |
| 752 | +When a default argument is given, it is returned when the attribute doesn't\n\ | |
| 753 | +exist; without it, an exception is raised in that case."); | |
| 754 | + | |
| 755 | + | |
| 756 | +static PyObject * | |
| 757 | +builtin_globals(PyObject *self) | |
| 758 | +{ | |
| 759 | + PyObject *d; | |
| 760 | + | |
| 761 | + d = PyEval_GetGlobals(); | |
| 762 | + Py_XINCREF(d); | |
| 763 | + return d; | |
| 764 | +} | |
| 765 | + | |
| 766 | +PyDoc_STRVAR(globals_doc, | |
| 767 | +"globals() -> dictionary\n\ | |
| 768 | +\n\ | |
| 769 | +Return the dictionary containing the current scope's global variables."); | |
| 770 | + | |
| 771 | + | |
| 772 | +static PyObject * | |
| 773 | +builtin_hasattr(PyObject *self, PyObject *args) | |
| 774 | +{ | |
| 775 | + PyObject *v; | |
| 776 | + PyObject *name; | |
| 777 | + | |
| 778 | + if (!PyArg_UnpackTuple(args, "hasattr", 2, 2, &v, &name)) | |
| 779 | + return NULL; | |
| 780 | +#ifdef Py_USING_UNICODE | |
| 781 | + if (PyUnicode_Check(name)) { | |
| 782 | + name = _PyUnicode_AsDefaultEncodedString(name, NULL); | |
| 783 | + if (name == NULL) | |
| 784 | + return NULL; | |
| 785 | + } | |
| 786 | +#endif | |
| 787 | + | |
| 788 | + if (!PyString_Check(name)) { | |
| 789 | + PyErr_SetString(PyExc_TypeError, | |
| 790 | + "hasattr(): attribute name must be string"); | |
| 791 | + return NULL; | |
| 792 | + } | |
| 793 | + v = PyObject_GetAttr(v, name); | |
| 794 | + if (v == NULL) { | |
| 795 | + PyErr_Clear(); | |
| 796 | + Py_INCREF(Py_False); | |
| 797 | + return Py_False; | |
| 798 | + } | |
| 799 | + Py_DECREF(v); | |
| 800 | + Py_INCREF(Py_True); | |
| 801 | + return Py_True; | |
| 802 | +} | |
| 803 | + | |
| 804 | +PyDoc_STRVAR(hasattr_doc, | |
| 805 | +"hasattr(object, name) -> bool\n\ | |
| 806 | +\n\ | |
| 807 | +Return whether the object has an attribute with the given name.\n\ | |
| 808 | +(This is done by calling getattr(object, name) and catching exceptions.)"); | |
| 809 | + | |
| 810 | + | |
| 811 | +static PyObject * | |
| 812 | +builtin_id(PyObject *self, PyObject *v) | |
| 813 | +{ | |
| 814 | + return PyLong_FromVoidPtr(v); | |
| 815 | +} | |
| 816 | + | |
| 817 | +PyDoc_STRVAR(id_doc, | |
| 818 | +"id(object) -> integer\n\ | |
| 819 | +\n\ | |
| 820 | +Return the identity of an object. This is guaranteed to be unique among\n\ | |
| 821 | +simultaneously existing objects. (Hint: it's the object's memory address.)"); | |
| 822 | + | |
| 823 | + | |
| 824 | +static PyObject * | |
| 825 | +builtin_map(PyObject *self, PyObject *args) | |
| 826 | +{ | |
| 827 | + typedef struct { | |
| 828 | + PyObject *it; /* the iterator object */ | |
| 829 | + int saw_StopIteration; /* bool: did the iterator end? */ | |
| 830 | + } sequence; | |
| 831 | + | |
| 832 | + PyObject *func, *result; | |
| 833 | + sequence *seqs = NULL, *sqp; | |
| 834 | + Py_ssize_t n, len; | |
| 835 | + register int i, j; | |
| 836 | + | |
| 837 | + n = PyTuple_Size(args); | |
| 838 | + if (n < 2) { | |
| 839 | + PyErr_SetString(PyExc_TypeError, | |
| 840 | + "map() requires at least two args"); | |
| 841 | + return NULL; | |
| 842 | + } | |
| 843 | + | |
| 844 | + func = PyTuple_GetItem(args, 0); | |
| 845 | + n--; | |
| 846 | + | |
| 847 | + if (func == Py_None && n == 1) { | |
| 848 | + /* map(None, S) is the same as list(S). */ | |
| 849 | + return PySequence_List(PyTuple_GetItem(args, 1)); | |
| 850 | + } | |
| 851 | + | |
| 852 | + /* Get space for sequence descriptors. Must NULL out the iterator | |
| 853 | + * pointers so that jumping to Fail_2 later doesn't see trash. | |
| 854 | + */ | |
| 855 | + if ((seqs = PyMem_NEW(sequence, n)) == NULL) { | |
| 856 | + PyErr_NoMemory(); | |
| 857 | + return NULL; | |
| 858 | + } | |
| 859 | + for (i = 0; i < n; ++i) { | |
| 860 | + seqs[i].it = (PyObject*)NULL; | |
| 861 | + seqs[i].saw_StopIteration = 0; | |
| 862 | + } | |
| 863 | + | |
| 864 | + /* Do a first pass to obtain iterators for the arguments, and set len | |
| 865 | + * to the largest of their lengths. | |
| 866 | + */ | |
| 867 | + len = 0; | |
| 868 | + for (i = 0, sqp = seqs; i < n; ++i, ++sqp) { | |
| 869 | + PyObject *curseq; | |
| 870 | + Py_ssize_t curlen; | |
| 871 | + | |
| 872 | + /* Get iterator. */ | |
| 873 | + curseq = PyTuple_GetItem(args, i+1); | |
| 874 | + sqp->it = PyObject_GetIter(curseq); | |
| 875 | + if (sqp->it == NULL) { | |
| 876 | + static char errmsg[] = | |
| 877 | + "argument %d to map() must support iteration"; | |
| 878 | + char errbuf[sizeof(errmsg) + 25]; | |
| 879 | + PyOS_snprintf(errbuf, sizeof(errbuf), errmsg, i+2); | |
| 880 | + PyErr_SetString(PyExc_TypeError, errbuf); | |
| 881 | + goto Fail_2; | |
| 882 | + } | |
| 883 | + | |
| 884 | + /* Update len. */ | |
| 885 | + curlen = _PyObject_LengthHint(curseq); | |
| 886 | + if (curlen < 0) { | |
| 887 | + if (!PyErr_ExceptionMatches(PyExc_TypeError) && | |
| 888 | + !PyErr_ExceptionMatches(PyExc_AttributeError)) { | |
| 889 | + goto Fail_2; | |
| 890 | + } | |
| 891 | + PyErr_Clear(); | |
| 892 | + curlen = 8; /* arbitrary */ | |
| 893 | + } | |
| 894 | + if (curlen > len) | |
| 895 | + len = curlen; | |
| 896 | + } | |
| 897 | + | |
| 898 | + /* Get space for the result list. */ | |
| 899 | + if ((result = (PyObject *) PyList_New(len)) == NULL) | |
| 900 | + goto Fail_2; | |
| 901 | + | |
| 902 | + /* Iterate over the sequences until all have stopped. */ | |
| 903 | + for (i = 0; ; ++i) { | |
| 904 | + PyObject *alist, *item=NULL, *value; | |
| 905 | + int numactive = 0; | |
| 906 | + | |
| 907 | + if (func == Py_None && n == 1) | |
| 908 | + alist = NULL; | |
| 909 | + else if ((alist = PyTuple_New(n)) == NULL) | |
| 910 | + goto Fail_1; | |
| 911 | + | |
| 912 | + for (j = 0, sqp = seqs; j < n; ++j, ++sqp) { | |
| 913 | + if (sqp->saw_StopIteration) { | |
| 914 | + Py_INCREF(Py_None); | |
| 915 | + item = Py_None; | |
| 916 | + } | |
| 917 | + else { | |
| 918 | + item = PyIter_Next(sqp->it); | |
| 919 | + if (item) | |
| 920 | + ++numactive; | |
| 921 | + else { | |
| 922 | + if (PyErr_Occurred()) { | |
| 923 | + Py_XDECREF(alist); | |
| 924 | + goto Fail_1; | |
| 925 | + } | |
| 926 | + Py_INCREF(Py_None); | |
| 927 | + item = Py_None; | |
| 928 | + sqp->saw_StopIteration = 1; | |
| 929 | + } | |
| 930 | + } | |
| 931 | + if (alist) | |
| 932 | + PyTuple_SET_ITEM(alist, j, item); | |
| 933 | + else | |
| 934 | + break; | |
| 935 | + } | |
| 936 | + | |
| 937 | + if (!alist) | |
| 938 | + alist = item; | |
| 939 | + | |
| 940 | + if (numactive == 0) { | |
| 941 | + Py_DECREF(alist); | |
| 942 | + break; | |
| 943 | + } | |
| 944 | + | |
| 945 | + if (func == Py_None) | |
| 946 | + value = alist; | |
| 947 | + else { | |
| 948 | + value = PyEval_CallObject(func, alist); | |
| 949 | + Py_DECREF(alist); | |
| 950 | + if (value == NULL) | |
| 951 | + goto Fail_1; | |
| 952 | + } | |
| 953 | + if (i >= len) { | |
| 954 | + int status = PyList_Append(result, value); | |
| 955 | + Py_DECREF(value); | |
| 956 | + if (status < 0) | |
| 957 | + goto Fail_1; | |
| 958 | + } | |
| 959 | + else if (PyList_SetItem(result, i, value) < 0) | |
| 960 | + goto Fail_1; | |
| 961 | + } | |
| 962 | + | |
| 963 | + if (i < len && PyList_SetSlice(result, i, len, NULL) < 0) | |
| 964 | + goto Fail_1; | |
| 965 | + | |
| 966 | + goto Succeed; | |
| 967 | + | |
| 968 | +Fail_1: | |
| 969 | + Py_DECREF(result); | |
| 970 | +Fail_2: | |
| 971 | + result = NULL; | |
| 972 | +Succeed: | |
| 973 | + assert(seqs); | |
| 974 | + for (i = 0; i < n; ++i) | |
| 975 | + Py_XDECREF(seqs[i].it); | |
| 976 | + PyMem_DEL(seqs); | |
| 977 | + return result; | |
| 978 | +} | |
| 979 | + | |
| 980 | +PyDoc_STRVAR(map_doc, | |
| 981 | +"map(function, sequence[, sequence, ...]) -> list\n\ | |
| 982 | +\n\ | |
| 983 | +Return a list of the results of applying the function to the items of\n\ | |
| 984 | +the argument sequence(s). If more than one sequence is given, the\n\ | |
| 985 | +function is called with an argument list consisting of the corresponding\n\ | |
| 986 | +item of each sequence, substituting None for missing values when not all\n\ | |
| 987 | +sequences have the same length. If the function is None, return a list of\n\ | |
| 988 | +the items of the sequence (or a list of tuples if more than one sequence)."); | |
| 989 | + | |
| 990 | + | |
| 991 | +static PyObject * | |
| 992 | +builtin_setattr(PyObject *self, PyObject *args) | |
| 993 | +{ | |
| 994 | + PyObject *v; | |
| 995 | + PyObject *name; | |
| 996 | + PyObject *value; | |
| 997 | + | |
| 998 | + if (!PyArg_UnpackTuple(args, "setattr", 3, 3, &v, &name, &value)) | |
| 999 | + return NULL; | |
| 1000 | + if (PyObject_SetAttr(v, name, value) != 0) | |
| 1001 | + return NULL; | |
| 1002 | + Py_INCREF(Py_None); | |
| 1003 | + return Py_None; | |
| 1004 | +} | |
| 1005 | + | |
| 1006 | +PyDoc_STRVAR(setattr_doc, | |
| 1007 | +"setattr(object, name, value)\n\ | |
| 1008 | +\n\ | |
| 1009 | +Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\ | |
| 1010 | +``x.y = v''."); | |
| 1011 | + | |
| 1012 | + | |
| 1013 | +static PyObject * | |
| 1014 | +builtin_delattr(PyObject *self, PyObject *args) | |
| 1015 | +{ | |
| 1016 | + PyObject *v; | |
| 1017 | + PyObject *name; | |
| 1018 | + | |
| 1019 | + if (!PyArg_UnpackTuple(args, "delattr", 2, 2, &v, &name)) | |
| 1020 | + return NULL; | |
| 1021 | + if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0) | |
| 1022 | + return NULL; | |
| 1023 | + Py_INCREF(Py_None); | |
| 1024 | + return Py_None; | |
| 1025 | +} | |
| 1026 | + | |
| 1027 | +PyDoc_STRVAR(delattr_doc, | |
| 1028 | +"delattr(object, name)\n\ | |
| 1029 | +\n\ | |
| 1030 | +Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\ | |
| 1031 | +``del x.y''."); | |
| 1032 | + | |
| 1033 | + | |
| 1034 | +static PyObject * | |
| 1035 | +builtin_hash(PyObject *self, PyObject *v) | |
| 1036 | +{ | |
| 1037 | + long x; | |
| 1038 | + | |
| 1039 | + x = PyObject_Hash(v); | |
| 1040 | + if (x == -1) | |
| 1041 | + return NULL; | |
| 1042 | + return PyInt_FromLong(x); | |
| 1043 | +} | |
| 1044 | + | |
| 1045 | +PyDoc_STRVAR(hash_doc, | |
| 1046 | +"hash(object) -> integer\n\ | |
| 1047 | +\n\ | |
| 1048 | +Return a hash value for the object. Two objects with the same value have\n\ | |
| 1049 | +the same hash value. The reverse is not necessarily true, but likely."); | |
| 1050 | + | |
| 1051 | + | |
| 1052 | +static PyObject * | |
| 1053 | +builtin_hex(PyObject *self, PyObject *v) | |
| 1054 | +{ | |
| 1055 | + PyNumberMethods *nb; | |
| 1056 | + PyObject *res; | |
| 1057 | + | |
| 1058 | + if ((nb = v->ob_type->tp_as_number) == NULL || | |
| 1059 | + nb->nb_hex == NULL) { | |
| 1060 | + PyErr_SetString(PyExc_TypeError, | |
| 1061 | + "hex() argument can't be converted to hex"); | |
| 1062 | + return NULL; | |
| 1063 | + } | |
| 1064 | + res = (*nb->nb_hex)(v); | |
| 1065 | + if (res && !PyString_Check(res)) { | |
| 1066 | + PyErr_Format(PyExc_TypeError, | |
| 1067 | + "__hex__ returned non-string (type %.200s)", | |
| 1068 | + res->ob_type->tp_name); | |
| 1069 | + Py_DECREF(res); | |
| 1070 | + return NULL; | |
| 1071 | + } | |
| 1072 | + return res; | |
| 1073 | +} | |
| 1074 | + | |
| 1075 | +PyDoc_STRVAR(hex_doc, | |
| 1076 | +"hex(number) -> string\n\ | |
| 1077 | +\n\ | |
| 1078 | +Return the hexadecimal representation of an integer or long integer."); | |
| 1079 | + | |
| 1080 | + | |
| 1081 | +static PyObject *builtin_raw_input(PyObject *, PyObject *); | |
| 1082 | + | |
| 1083 | +static PyObject * | |
| 1084 | +builtin_input(PyObject *self, PyObject *args) | |
| 1085 | +{ | |
| 1086 | + PyObject *line; | |
| 1087 | + char *str; | |
| 1088 | + PyObject *res; | |
| 1089 | + PyObject *globals, *locals; | |
| 1090 | + PyCompilerFlags cf; | |
| 1091 | + | |
| 1092 | + line = builtin_raw_input(self, args); | |
| 1093 | + if (line == NULL) | |
| 1094 | + return line; | |
| 1095 | + if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str)) | |
| 1096 | + return NULL; | |
| 1097 | + while (*str == ' ' || *str == '\t') | |
| 1098 | + str++; | |
| 1099 | + globals = PyEval_GetGlobals(); | |
| 1100 | + locals = PyEval_GetLocals(); | |
| 1101 | + if (PyDict_GetItemString(globals, "__builtins__") == NULL) { | |
| 1102 | + if (PyDict_SetItemString(globals, "__builtins__", | |
| 1103 | + PyEval_GetBuiltins()) != 0) | |
| 1104 | + return NULL; | |
| 1105 | + } | |
| 1106 | + cf.cf_flags = 0; | |
| 1107 | + PyEval_MergeCompilerFlags(&cf); | |
| 1108 | + res = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf); | |
| 1109 | + Py_DECREF(line); | |
| 1110 | + return res; | |
| 1111 | +} | |
| 1112 | + | |
| 1113 | +PyDoc_STRVAR(input_doc, | |
| 1114 | +"input([prompt]) -> value\n\ | |
| 1115 | +\n\ | |
| 1116 | +Equivalent to eval(raw_input(prompt))."); | |
| 1117 | + | |
| 1118 | + | |
| 1119 | +static PyObject * | |
| 1120 | +builtin_intern(PyObject *self, PyObject *args) | |
| 1121 | +{ | |
| 1122 | + PyObject *s; | |
| 1123 | + if (!PyArg_ParseTuple(args, "S:intern", &s)) | |
| 1124 | + return NULL; | |
| 1125 | + if (!PyString_CheckExact(s)) { | |
| 1126 | + PyErr_SetString(PyExc_TypeError, | |
| 1127 | + "can't intern subclass of string"); | |
| 1128 | + return NULL; | |
| 1129 | + } | |
| 1130 | + Py_INCREF(s); | |
| 1131 | + PyString_InternInPlace(&s); | |
| 1132 | + return s; | |
| 1133 | +} | |
| 1134 | + | |
| 1135 | +PyDoc_STRVAR(intern_doc, | |
| 1136 | +"intern(string) -> string\n\ | |
| 1137 | +\n\ | |
| 1138 | +``Intern'' the given string. This enters the string in the (global)\n\ | |
| 1139 | +table of interned strings whose purpose is to speed up dictionary lookups.\n\ | |
| 1140 | +Return the string itself or the previously interned string object with the\n\ | |
| 1141 | +same value."); | |
| 1142 | + | |
| 1143 | + | |
| 1144 | +static PyObject * | |
| 1145 | +builtin_iter(PyObject *self, PyObject *args) | |
| 1146 | +{ | |
| 1147 | + PyObject *v, *w = NULL; | |
| 1148 | + | |
| 1149 | + if (!PyArg_UnpackTuple(args, "iter", 1, 2, &v, &w)) | |
| 1150 | + return NULL; | |
| 1151 | + if (w == NULL) | |
| 1152 | + return PyObject_GetIter(v); | |
| 1153 | + if (!PyCallable_Check(v)) { | |
| 1154 | + PyErr_SetString(PyExc_TypeError, | |
| 1155 | + "iter(v, w): v must be callable"); | |
| 1156 | + return NULL; | |
| 1157 | + } | |
| 1158 | + return PyCallIter_New(v, w); | |
| 1159 | +} | |
| 1160 | + | |
| 1161 | +PyDoc_STRVAR(iter_doc, | |
| 1162 | +"iter(collection) -> iterator\n\ | |
| 1163 | +iter(callable, sentinel) -> iterator\n\ | |
| 1164 | +\n\ | |
| 1165 | +Get an iterator from an object. In the first form, the argument must\n\ | |
| 1166 | +supply its own iterator, or be a sequence.\n\ | |
| 1167 | +In the second form, the callable is called until it returns the sentinel."); | |
| 1168 | + | |
| 1169 | + | |
| 1170 | +static PyObject * | |
| 1171 | +builtin_len(PyObject *self, PyObject *v) | |
| 1172 | +{ | |
| 1173 | + Py_ssize_t res; | |
| 1174 | + | |
| 1175 | + res = PyObject_Size(v); | |
| 1176 | + if (res < 0 && PyErr_Occurred()) | |
| 1177 | + return NULL; | |
| 1178 | + return PyInt_FromSsize_t(res); | |
| 1179 | +} | |
| 1180 | + | |
| 1181 | +PyDoc_STRVAR(len_doc, | |
| 1182 | +"len(object) -> integer\n\ | |
| 1183 | +\n\ | |
| 1184 | +Return the number of items of a sequence or mapping."); | |
| 1185 | + | |
| 1186 | + | |
| 1187 | +static PyObject * | |
| 1188 | +builtin_locals(PyObject *self) | |
| 1189 | +{ | |
| 1190 | + PyObject *d; | |
| 1191 | + | |
| 1192 | + d = PyEval_GetLocals(); | |
| 1193 | + Py_XINCREF(d); | |
| 1194 | + return d; | |
| 1195 | +} | |
| 1196 | + | |
| 1197 | +PyDoc_STRVAR(locals_doc, | |
| 1198 | +"locals() -> dictionary\n\ | |
| 1199 | +\n\ | |
| 1200 | +Update and return a dictionary containing the current scope's local variables."); | |
| 1201 | + | |
| 1202 | + | |
| 1203 | +static PyObject * | |
| 1204 | +min_max(PyObject *args, PyObject *kwds, int op) | |
| 1205 | +{ | |
| 1206 | + PyObject *v, *it, *item, *val, *maxitem, *maxval, *keyfunc=NULL; | |
| 1207 | + const char *name = op == Py_LT ? "min" : "max"; | |
| 1208 | + | |
| 1209 | + if (PyTuple_Size(args) > 1) | |
| 1210 | + v = args; | |
| 1211 | + else if (!PyArg_UnpackTuple(args, (char *)name, 1, 1, &v)) | |
| 1212 | + return NULL; | |
| 1213 | + | |
| 1214 | + if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds)) { | |
| 1215 | + keyfunc = PyDict_GetItemString(kwds, "key"); | |
| 1216 | + if (PyDict_Size(kwds)!=1 || keyfunc == NULL) { | |
| 1217 | + PyErr_Format(PyExc_TypeError, | |
| 1218 | + "%s() got an unexpected keyword argument", name); | |
| 1219 | + return NULL; | |
| 1220 | + } | |
| 1221 | + } | |
| 1222 | + | |
| 1223 | + it = PyObject_GetIter(v); | |
| 1224 | + if (it == NULL) | |
| 1225 | + return NULL; | |
| 1226 | + | |
| 1227 | + maxitem = NULL; /* the result */ | |
| 1228 | + maxval = NULL; /* the value associated with the result */ | |
| 1229 | + while (( item = PyIter_Next(it) )) { | |
| 1230 | + /* get the value from the key function */ | |
| 1231 | + if (keyfunc != NULL) { | |
| 1232 | + val = PyObject_CallFunctionObjArgs(keyfunc, item, NULL); | |
| 1233 | + if (val == NULL) | |
| 1234 | + goto Fail_it_item; | |
| 1235 | + } | |
| 1236 | + /* no key function; the value is the item */ | |
| 1237 | + else { | |
| 1238 | + val = item; | |
| 1239 | + Py_INCREF(val); | |
| 1240 | + } | |
| 1241 | + | |
| 1242 | + /* maximum value and item are unset; set them */ | |
| 1243 | + if (maxval == NULL) { | |
| 1244 | + maxitem = item; | |
| 1245 | + maxval = val; | |
| 1246 | + } | |
| 1247 | + /* maximum value and item are set; update them as necessary */ | |
| 1248 | + else { | |
| 1249 | + int cmp = PyObject_RichCompareBool(val, maxval, op); | |
| 1250 | + if (cmp < 0) | |
| 1251 | + goto Fail_it_item_and_val; | |
| 1252 | + else if (cmp > 0) { | |
| 1253 | + Py_DECREF(maxval); | |
| 1254 | + Py_DECREF(maxitem); | |
| 1255 | + maxval = val; | |
| 1256 | + maxitem = item; | |
| 1257 | + } | |
| 1258 | + else { | |
| 1259 | + Py_DECREF(item); | |
| 1260 | + Py_DECREF(val); | |
| 1261 | + } | |
| 1262 | + } | |
| 1263 | + } | |
| 1264 | + if (PyErr_Occurred()) | |
| 1265 | + goto Fail_it; | |
| 1266 | + if (maxval == NULL) { | |
| 1267 | + PyErr_Format(PyExc_ValueError, | |
| 1268 | + "%s() arg is an empty sequence", name); | |
| 1269 | + assert(maxitem == NULL); | |
| 1270 | + } | |
| 1271 | + else | |
| 1272 | + Py_DECREF(maxval); | |
| 1273 | + Py_DECREF(it); | |
| 1274 | + return maxitem; | |
| 1275 | + | |
| 1276 | +Fail_it_item_and_val: | |
| 1277 | + Py_DECREF(val); | |
| 1278 | +Fail_it_item: | |
| 1279 | + Py_DECREF(item); | |
| 1280 | +Fail_it: | |
| 1281 | + Py_XDECREF(maxval); | |
| 1282 | + Py_XDECREF(maxitem); | |
| 1283 | + Py_DECREF(it); | |
| 1284 | + return NULL; | |
| 1285 | +} | |
| 1286 | + | |
| 1287 | +static PyObject * | |
| 1288 | +builtin_min(PyObject *self, PyObject *args, PyObject *kwds) | |
| 1289 | +{ | |
| 1290 | + return min_max(args, kwds, Py_LT); | |
| 1291 | +} | |
| 1292 | + | |
| 1293 | +PyDoc_STRVAR(min_doc, | |
| 1294 | +"min(iterable[, key=func]) -> value\n\ | |
| 1295 | +min(a, b, c, ...[, key=func]) -> value\n\ | |
| 1296 | +\n\ | |
| 1297 | +With a single iterable argument, return its smallest item.\n\ | |
| 1298 | +With two or more arguments, return the smallest argument."); | |
| 1299 | + | |
| 1300 | + | |
| 1301 | +static PyObject * | |
| 1302 | +builtin_max(PyObject *self, PyObject *args, PyObject *kwds) | |
| 1303 | +{ | |
| 1304 | + return min_max(args, kwds, Py_GT); | |
| 1305 | +} | |
| 1306 | + | |
| 1307 | +PyDoc_STRVAR(max_doc, | |
| 1308 | +"max(iterable[, key=func]) -> value\n\ | |
| 1309 | +max(a, b, c, ...[, key=func]) -> value\n\ | |
| 1310 | +\n\ | |
| 1311 | +With a single iterable argument, return its largest item.\n\ | |
| 1312 | +With two or more arguments, return the largest argument."); | |
| 1313 | + | |
| 1314 | + | |
| 1315 | +static PyObject * | |
| 1316 | +builtin_oct(PyObject *self, PyObject *v) | |
| 1317 | +{ | |
| 1318 | + PyNumberMethods *nb; | |
| 1319 | + PyObject *res; | |
| 1320 | + | |
| 1321 | + if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL || | |
| 1322 | + nb->nb_oct == NULL) { | |
| 1323 | + PyErr_SetString(PyExc_TypeError, | |
| 1324 | + "oct() argument can't be converted to oct"); | |
| 1325 | + return NULL; | |
| 1326 | + } | |
| 1327 | + res = (*nb->nb_oct)(v); | |
| 1328 | + if (res && !PyString_Check(res)) { | |
| 1329 | + PyErr_Format(PyExc_TypeError, | |
| 1330 | + "__oct__ returned non-string (type %.200s)", | |
| 1331 | + res->ob_type->tp_name); | |
| 1332 | + Py_DECREF(res); | |
| 1333 | + return NULL; | |
| 1334 | + } | |
| 1335 | + return res; | |
| 1336 | +} | |
| 1337 | + | |
| 1338 | +PyDoc_STRVAR(oct_doc, | |
| 1339 | +"oct(number) -> string\n\ | |
| 1340 | +\n\ | |
| 1341 | +Return the octal representation of an integer or long integer."); | |
| 1342 | + | |
| 1343 | + | |
| 1344 | +static PyObject * | |
| 1345 | +builtin_open(PyObject *self, PyObject *args, PyObject *kwds) | |
| 1346 | +{ | |
| 1347 | + return PyObject_Call((PyObject*)&PyFile_Type, args, kwds); | |
| 1348 | +} | |
| 1349 | + | |
| 1350 | +PyDoc_STRVAR(open_doc, | |
| 1351 | +"open(name[, mode[, buffering]]) -> file object\n\ | |
| 1352 | +\n\ | |
| 1353 | +Open a file using the file() type, returns a file object."); | |
| 1354 | + | |
| 1355 | + | |
| 1356 | +static PyObject * | |
| 1357 | +builtin_ord(PyObject *self, PyObject* obj) | |
| 1358 | +{ | |
| 1359 | + long ord; | |
| 1360 | + Py_ssize_t size; | |
| 1361 | + | |
| 1362 | + if (PyString_Check(obj)) { | |
| 1363 | + size = PyString_GET_SIZE(obj); | |
| 1364 | + if (size == 1) { | |
| 1365 | + ord = (long)((unsigned char)*PyString_AS_STRING(obj)); | |
| 1366 | + return PyInt_FromLong(ord); | |
| 1367 | + } | |
| 1368 | +#ifdef Py_USING_UNICODE | |
| 1369 | + } else if (PyUnicode_Check(obj)) { | |
| 1370 | + size = PyUnicode_GET_SIZE(obj); | |
| 1371 | + if (size == 1) { | |
| 1372 | + ord = (long)*PyUnicode_AS_UNICODE(obj); | |
| 1373 | + return PyInt_FromLong(ord); | |
| 1374 | + } | |
| 1375 | +#endif | |
| 1376 | + } else { | |
| 1377 | + PyErr_Format(PyExc_TypeError, | |
| 1378 | + "ord() expected string of length 1, but " \ | |
| 1379 | + "%.200s found", obj->ob_type->tp_name); | |
| 1380 | + return NULL; | |
| 1381 | + } | |
| 1382 | + | |
| 1383 | + PyErr_Format(PyExc_TypeError, | |
| 1384 | + "ord() expected a character, " | |
| 1385 | + "but string of length %zd found", | |
| 1386 | + size); | |
| 1387 | + return NULL; | |
| 1388 | +} | |
| 1389 | + | |
| 1390 | +PyDoc_STRVAR(ord_doc, | |
| 1391 | +"ord(c) -> integer\n\ | |
| 1392 | +\n\ | |
| 1393 | +Return the integer ordinal of a one-character string."); | |
| 1394 | + | |
| 1395 | + | |
| 1396 | +static PyObject * | |
| 1397 | +builtin_pow(PyObject *self, PyObject *args) | |
| 1398 | +{ | |
| 1399 | + PyObject *v, *w, *z = Py_None; | |
| 1400 | + | |
| 1401 | + if (!PyArg_UnpackTuple(args, "pow", 2, 3, &v, &w, &z)) | |
| 1402 | + return NULL; | |
| 1403 | + return PyNumber_Power(v, w, z); | |
| 1404 | +} | |
| 1405 | + | |
| 1406 | +PyDoc_STRVAR(pow_doc, | |
| 1407 | +"pow(x, y[, z]) -> number\n\ | |
| 1408 | +\n\ | |
| 1409 | +With two arguments, equivalent to x**y. With three arguments,\n\ | |
| 1410 | +equivalent to (x**y) % z, but may be more efficient (e.g. for longs)."); | |
| 1411 | + | |
| 1412 | + | |
| 1413 | + | |
| 1414 | +/* Return number of items in range (lo, hi, step), when arguments are | |
| 1415 | + * PyInt or PyLong objects. step > 0 required. Return a value < 0 if | |
| 1416 | + * & only if the true value is too large to fit in a signed long. | |
| 1417 | + * Arguments MUST return 1 with either PyInt_Check() or | |
| 1418 | + * PyLong_Check(). Return -1 when there is an error. | |
| 1419 | + */ | |
| 1420 | +static long | |
| 1421 | +get_len_of_range_longs(PyObject *lo, PyObject *hi, PyObject *step) | |
| 1422 | +{ | |
| 1423 | + /* ------------------------------------------------------------- | |
| 1424 | + Algorithm is equal to that of get_len_of_range(), but it operates | |
| 1425 | + on PyObjects (which are assumed to be PyLong or PyInt objects). | |
| 1426 | + ---------------------------------------------------------------*/ | |
| 1427 | + long n; | |
| 1428 | + PyObject *diff = NULL; | |
| 1429 | + PyObject *one = NULL; | |
| 1430 | + PyObject *tmp1 = NULL, *tmp2 = NULL, *tmp3 = NULL; | |
| 1431 | + /* holds sub-expression evaluations */ | |
| 1432 | + | |
| 1433 | + /* if (lo >= hi), return length of 0. */ | |
| 1434 | + if (PyObject_Compare(lo, hi) >= 0) | |
| 1435 | + return 0; | |
| 1436 | + | |
| 1437 | + if ((one = PyLong_FromLong(1L)) == NULL) | |
| 1438 | + goto Fail; | |
| 1439 | + | |
| 1440 | + if ((tmp1 = PyNumber_Subtract(hi, lo)) == NULL) | |
| 1441 | + goto Fail; | |
| 1442 | + | |
| 1443 | + if ((diff = PyNumber_Subtract(tmp1, one)) == NULL) | |
| 1444 | + goto Fail; | |
| 1445 | + | |
| 1446 | + if ((tmp2 = PyNumber_FloorDivide(diff, step)) == NULL) | |
| 1447 | + goto Fail; | |
| 1448 | + | |
| 1449 | + if ((tmp3 = PyNumber_Add(tmp2, one)) == NULL) | |
| 1450 | + goto Fail; | |
| 1451 | + | |
| 1452 | + n = PyLong_AsLong(tmp3); | |
| 1453 | + if (PyErr_Occurred()) { /* Check for Overflow */ | |
| 1454 | + PyErr_Clear(); | |
| 1455 | + goto Fail; | |
| 1456 | + } | |
| 1457 | + | |
| 1458 | + Py_DECREF(tmp3); | |
| 1459 | + Py_DECREF(tmp2); | |
| 1460 | + Py_DECREF(diff); | |
| 1461 | + Py_DECREF(tmp1); | |
| 1462 | + Py_DECREF(one); | |
| 1463 | + return n; | |
| 1464 | + | |
| 1465 | + Fail: | |
| 1466 | + Py_XDECREF(tmp3); | |
| 1467 | + Py_XDECREF(tmp2); | |
| 1468 | + Py_XDECREF(diff); | |
| 1469 | + Py_XDECREF(tmp1); | |
| 1470 | + Py_XDECREF(one); | |
| 1471 | + return -1; | |
| 1472 | +} | |
| 1473 | + | |
| 1474 | +/* An extension of builtin_range() that handles the case when PyLong | |
| 1475 | + * arguments are given. */ | |
| 1476 | +static PyObject * | |
| 1477 | +handle_range_longs(PyObject *self, PyObject *args) | |
| 1478 | +{ | |
| 1479 | + PyObject *ilow; | |
| 1480 | + PyObject *ihigh = NULL; | |
| 1481 | + PyObject *istep = NULL; | |
| 1482 | + | |
| 1483 | + PyObject *curnum = NULL; | |
| 1484 | + PyObject *v = NULL; | |
| 1485 | + long bign; | |
| 1486 | + int i, n; | |
| 1487 | + int cmp_result; | |
| 1488 | + | |
| 1489 | + PyObject *zero = PyLong_FromLong(0); | |
| 1490 | + | |
| 1491 | + if (zero == NULL) | |
| 1492 | + return NULL; | |
| 1493 | + | |
| 1494 | + if (!PyArg_UnpackTuple(args, "range", 1, 3, &ilow, &ihigh, &istep)) { | |
| 1495 | + Py_DECREF(zero); | |
| 1496 | + return NULL; | |
| 1497 | + } | |
| 1498 | + | |
| 1499 | + /* Figure out which way we were called, supply defaults, and be | |
| 1500 | + * sure to incref everything so that the decrefs at the end | |
| 1501 | + * are correct. | |
| 1502 | + */ | |
| 1503 | + assert(ilow != NULL); | |
| 1504 | + if (ihigh == NULL) { | |
| 1505 | + /* only 1 arg -- it's the upper limit */ | |
| 1506 | + ihigh = ilow; | |
| 1507 | + ilow = NULL; | |
| 1508 | + } | |
| 1509 | + assert(ihigh != NULL); | |
| 1510 | + Py_INCREF(ihigh); | |
| 1511 | + | |
| 1512 | + /* ihigh correct now; do ilow */ | |
| 1513 | + if (ilow == NULL) | |
| 1514 | + ilow = zero; | |
| 1515 | + Py_INCREF(ilow); | |
| 1516 | + | |
| 1517 | + /* ilow and ihigh correct now; do istep */ | |
| 1518 | + if (istep == NULL) { | |
| 1519 | + istep = PyLong_FromLong(1L); | |
| 1520 | + if (istep == NULL) | |
| 1521 | + goto Fail; | |
| 1522 | + } | |
| 1523 | + else { | |
| 1524 | + Py_INCREF(istep); | |
| 1525 | + } | |
| 1526 | + | |
| 1527 | + if (!PyInt_Check(ilow) && !PyLong_Check(ilow)) { | |
| 1528 | + PyErr_Format(PyExc_TypeError, | |
| 1529 | + "range() integer start argument expected, got %s.", | |
| 1530 | + ilow->ob_type->tp_name); | |
| 1531 | + goto Fail; | |
| 1532 | + } | |
| 1533 | + | |
| 1534 | + if (!PyInt_Check(ihigh) && !PyLong_Check(ihigh)) { | |
| 1535 | + PyErr_Format(PyExc_TypeError, | |
| 1536 | + "range() integer end argument expected, got %s.", | |
| 1537 | + ihigh->ob_type->tp_name); | |
| 1538 | + goto Fail; | |
| 1539 | + } | |
| 1540 | + | |
| 1541 | + if (!PyInt_Check(istep) && !PyLong_Check(istep)) { | |
| 1542 | + PyErr_Format(PyExc_TypeError, | |
| 1543 | + "range() integer step argument expected, got %s.", | |
| 1544 | + istep->ob_type->tp_name); | |
| 1545 | + goto Fail; | |
| 1546 | + } | |
| 1547 | + | |
| 1548 | + if (PyObject_Cmp(istep, zero, &cmp_result) == -1) | |
| 1549 | + goto Fail; | |
| 1550 | + if (cmp_result == 0) { | |
| 1551 | + PyErr_SetString(PyExc_ValueError, | |
| 1552 | + "range() step argument must not be zero"); | |
| 1553 | + goto Fail; | |
| 1554 | + } | |
| 1555 | + | |
| 1556 | + if (cmp_result > 0) | |
| 1557 | + bign = get_len_of_range_longs(ilow, ihigh, istep); | |
| 1558 | + else { | |
| 1559 | + PyObject *neg_istep = PyNumber_Negative(istep); | |
| 1560 | + if (neg_istep == NULL) | |
| 1561 | + goto Fail; | |
| 1562 | + bign = get_len_of_range_longs(ihigh, ilow, neg_istep); | |
| 1563 | + Py_DECREF(neg_istep); | |
| 1564 | + } | |
| 1565 | + | |
| 1566 | + n = (int)bign; | |
| 1567 | + if (bign < 0 || (long)n != bign) { | |
| 1568 | + PyErr_SetString(PyExc_OverflowError, | |
| 1569 | + "range() result has too many items"); | |
| 1570 | + goto Fail; | |
| 1571 | + } | |
| 1572 | + | |
| 1573 | + v = PyList_New(n); | |
| 1574 | + if (v == NULL) | |
| 1575 | + goto Fail; | |
| 1576 | + | |
| 1577 | + curnum = ilow; | |
| 1578 | + Py_INCREF(curnum); | |
| 1579 | + | |
| 1580 | + for (i = 0; i < n; i++) { | |
| 1581 | + PyObject *w = PyNumber_Long(curnum); | |
| 1582 | + PyObject *tmp_num; | |
| 1583 | + if (w == NULL) | |
| 1584 | + goto Fail; | |
| 1585 | + | |
| 1586 | + PyList_SET_ITEM(v, i, w); | |
| 1587 | + | |
| 1588 | + tmp_num = PyNumber_Add(curnum, istep); | |
| 1589 | + if (tmp_num == NULL) | |
| 1590 | + goto Fail; | |
| 1591 | + | |
| 1592 | + Py_DECREF(curnum); | |
| 1593 | + curnum = tmp_num; | |
| 1594 | + } | |
| 1595 | + Py_DECREF(ilow); | |
| 1596 | + Py_DECREF(ihigh); | |
| 1597 | + Py_DECREF(istep); | |
| 1598 | + Py_DECREF(zero); | |
| 1599 | + Py_DECREF(curnum); | |
| 1600 | + return v; | |
| 1601 | + | |
| 1602 | + Fail: | |
| 1603 | + Py_DECREF(ilow); | |
| 1604 | + Py_DECREF(ihigh); | |
| 1605 | + Py_XDECREF(istep); | |
| 1606 | + Py_DECREF(zero); | |
| 1607 | + Py_XDECREF(curnum); | |
| 1608 | + Py_XDECREF(v); | |
| 1609 | + return NULL; | |
| 1610 | +} | |
| 1611 | + | |
| 1612 | +/* Return number of items in range/xrange (lo, hi, step). step > 0 | |
| 1613 | + * required. Return a value < 0 if & only if the true value is too | |
| 1614 | + * large to fit in a signed long. | |
| 1615 | + */ | |
| 1616 | +static long | |
| 1617 | +get_len_of_range(long lo, long hi, long step) | |
| 1618 | +{ | |
| 1619 | + /* ------------------------------------------------------------- | |
| 1620 | + If lo >= hi, the range is empty. | |
| 1621 | + Else if n values are in the range, the last one is | |
| 1622 | + lo + (n-1)*step, which must be <= hi-1. Rearranging, | |
| 1623 | + n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives | |
| 1624 | + the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so | |
| 1625 | + the RHS is non-negative and so truncation is the same as the | |
| 1626 | + floor. Letting M be the largest positive long, the worst case | |
| 1627 | + for the RHS numerator is hi=M, lo=-M-1, and then | |
| 1628 | + hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough | |
| 1629 | + precision to compute the RHS exactly. | |
| 1630 | + ---------------------------------------------------------------*/ | |
| 1631 | + long n = 0; | |
| 1632 | + if (lo < hi) { | |
| 1633 | + unsigned long uhi = (unsigned long)hi; | |
| 1634 | + unsigned long ulo = (unsigned long)lo; | |
| 1635 | + unsigned long diff = uhi - ulo - 1; | |
| 1636 | + n = (long)(diff / (unsigned long)step + 1); | |
| 1637 | + } | |
| 1638 | + return n; | |
| 1639 | +} | |
| 1640 | + | |
| 1641 | +static PyObject * | |
| 1642 | +builtin_range(PyObject *self, PyObject *args) | |
| 1643 | +{ | |
| 1644 | + long ilow = 0, ihigh = 0, istep = 1; | |
| 1645 | + long bign; | |
| 1646 | + int i, n; | |
| 1647 | + | |
| 1648 | + PyObject *v; | |
| 1649 | + | |
| 1650 | + if (PyTuple_Size(args) <= 1) { | |
| 1651 | + if (!PyArg_ParseTuple(args, | |
| 1652 | + "l;range() requires 1-3 int arguments", | |
| 1653 | + &ihigh)) { | |
| 1654 | + PyErr_Clear(); | |
| 1655 | + return handle_range_longs(self, args); | |
| 1656 | + } | |
| 1657 | + } | |
| 1658 | + else { | |
| 1659 | + if (!PyArg_ParseTuple(args, | |
| 1660 | + "ll|l;range() requires 1-3 int arguments", | |
| 1661 | + &ilow, &ihigh, &istep)) { | |
| 1662 | + PyErr_Clear(); | |
| 1663 | + return handle_range_longs(self, args); | |
| 1664 | + } | |
| 1665 | + } | |
| 1666 | + if (istep == 0) { | |
| 1667 | + PyErr_SetString(PyExc_ValueError, | |
| 1668 | + "range() step argument must not be zero"); | |
| 1669 | + return NULL; | |
| 1670 | + } | |
| 1671 | + if (istep > 0) | |
| 1672 | + bign = get_len_of_range(ilow, ihigh, istep); | |
| 1673 | + else | |
| 1674 | + bign = get_len_of_range(ihigh, ilow, -istep); | |
| 1675 | + n = (int)bign; | |
| 1676 | + if (bign < 0 || (long)n != bign) { | |
| 1677 | + PyErr_SetString(PyExc_OverflowError, | |
| 1678 | + "range() result has too many items"); | |
| 1679 | + return NULL; | |
| 1680 | + } | |
| 1681 | + v = PyList_New(n); | |
| 1682 | + if (v == NULL) | |
| 1683 | + return NULL; | |
| 1684 | + for (i = 0; i < n; i++) { | |
| 1685 | + PyObject *w = PyInt_FromLong(ilow); | |
| 1686 | + if (w == NULL) { | |
| 1687 | + Py_DECREF(v); | |
| 1688 | + return NULL; | |
| 1689 | + } | |
| 1690 | + PyList_SET_ITEM(v, i, w); | |
| 1691 | + ilow += istep; | |
| 1692 | + } | |
| 1693 | + return v; | |
| 1694 | +} | |
| 1695 | + | |
| 1696 | +PyDoc_STRVAR(range_doc, | |
| 1697 | +"range([start,] stop[, step]) -> list of integers\n\ | |
| 1698 | +\n\ | |
| 1699 | +Return a list containing an arithmetic progression of integers.\n\ | |
| 1700 | +range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\ | |
| 1701 | +When step is given, it specifies the increment (or decrement).\n\ | |
| 1702 | +For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\ | |
| 1703 | +These are exactly the valid indices for a list of 4 elements."); | |
| 1704 | + | |
| 1705 | + | |
| 1706 | +static PyObject * | |
| 1707 | +builtin_raw_input(PyObject *self, PyObject *args) | |
| 1708 | +{ | |
| 1709 | + PyObject *v = NULL; | |
| 1710 | + PyObject *fin = PySys_GetObject("stdin"); | |
| 1711 | + PyObject *fout = PySys_GetObject("stdout"); | |
| 1712 | + | |
| 1713 | + if (!PyArg_UnpackTuple(args, "[raw_]input", 0, 1, &v)) | |
| 1714 | + return NULL; | |
| 1715 | + | |
| 1716 | + if (fin == NULL) { | |
| 1717 | + PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdin"); | |
| 1718 | + return NULL; | |
| 1719 | + } | |
| 1720 | + if (fout == NULL) { | |
| 1721 | + PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdout"); | |
| 1722 | + return NULL; | |
| 1723 | + } | |
| 1724 | + if (PyFile_SoftSpace(fout, 0)) { | |
| 1725 | + if (PyFile_WriteString(" ", fout) != 0) | |
| 1726 | + return NULL; | |
| 1727 | + } | |
| 1728 | + if (PyFile_AsFile(fin) && PyFile_AsFile(fout) | |
| 1729 | + && isatty(fileno(PyFile_AsFile(fin))) | |
| 1730 | + && isatty(fileno(PyFile_AsFile(fout)))) { | |
| 1731 | + PyObject *po; | |
| 1732 | + char *prompt; | |
| 1733 | + char *s; | |
| 1734 | + PyObject *result; | |
| 1735 | + if (v != NULL) { | |
| 1736 | + po = PyObject_Str(v); | |
| 1737 | + if (po == NULL) | |
| 1738 | + return NULL; | |
| 1739 | + prompt = PyString_AsString(po); | |
| 1740 | + if (prompt == NULL) | |
| 1741 | + return NULL; | |
| 1742 | + } | |
| 1743 | + else { | |
| 1744 | + po = NULL; | |
| 1745 | + prompt = ""; | |
| 1746 | + } | |
| 1747 | + s = PyOS_Readline(PyFile_AsFile(fin), PyFile_AsFile(fout), | |
| 1748 | + prompt); | |
| 1749 | + Py_XDECREF(po); | |
| 1750 | + if (s == NULL) { | |
| 1751 | + if (!PyErr_Occurred()) | |
| 1752 | + PyErr_SetNone(PyExc_KeyboardInterrupt); | |
| 1753 | + return NULL; | |
| 1754 | + } | |
| 1755 | + if (*s == '\0') { | |
| 1756 | + PyErr_SetNone(PyExc_EOFError); | |
| 1757 | + result = NULL; | |
| 1758 | + } | |
| 1759 | + else { /* strip trailing '\n' */ | |
| 1760 | + size_t len = strlen(s); | |
| 1761 | + if (len > PY_SSIZE_T_MAX) { | |
| 1762 | + PyErr_SetString(PyExc_OverflowError, | |
| 1763 | + "[raw_]input: input too long"); | |
| 1764 | + result = NULL; | |
| 1765 | + } | |
| 1766 | + else { | |
| 1767 | + result = PyString_FromStringAndSize(s, len-1); | |
| 1768 | + } | |
| 1769 | + } | |
| 1770 | + PyMem_FREE(s); | |
| 1771 | + return result; | |
| 1772 | + } | |
| 1773 | + if (v != NULL) { | |
| 1774 | + if (PyFile_WriteObject(v, fout, Py_PRINT_RAW) != 0) | |
| 1775 | + return NULL; | |
| 1776 | + } | |
| 1777 | + return PyFile_GetLine(fin, -1); | |
| 1778 | +} | |
| 1779 | + | |
| 1780 | +PyDoc_STRVAR(raw_input_doc, | |
| 1781 | +"raw_input([prompt]) -> string\n\ | |
| 1782 | +\n\ | |
| 1783 | +Read a string from standard input. The trailing newline is stripped.\n\ | |
| 1784 | +If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\ | |
| 1785 | +On Unix, GNU readline is used if enabled. The prompt string, if given,\n\ | |
| 1786 | +is printed without a trailing newline before reading."); | |
| 1787 | + | |
| 1788 | + | |
| 1789 | +static PyObject * | |
| 1790 | +builtin_reduce(PyObject *self, PyObject *args) | |
| 1791 | +{ | |
| 1792 | + PyObject *seq, *func, *result = NULL, *it; | |
| 1793 | + | |
| 1794 | + if (!PyArg_UnpackTuple(args, "reduce", 2, 3, &func, &seq, &result)) | |
| 1795 | + return NULL; | |
| 1796 | + if (result != NULL) | |
| 1797 | + Py_INCREF(result); | |
| 1798 | + | |
| 1799 | + it = PyObject_GetIter(seq); | |
| 1800 | + if (it == NULL) { | |
| 1801 | + PyErr_SetString(PyExc_TypeError, | |
| 1802 | + "reduce() arg 2 must support iteration"); | |
| 1803 | + Py_XDECREF(result); | |
| 1804 | + return NULL; | |
| 1805 | + } | |
| 1806 | + | |
| 1807 | + if ((args = PyTuple_New(2)) == NULL) | |
| 1808 | + goto Fail; | |
| 1809 | + | |
| 1810 | + for (;;) { | |
| 1811 | + PyObject *op2; | |
| 1812 | + | |
| 1813 | + if (args->ob_refcnt > 1) { | |
| 1814 | + Py_DECREF(args); | |
| 1815 | + if ((args = PyTuple_New(2)) == NULL) | |
| 1816 | + goto Fail; | |
| 1817 | + } | |
| 1818 | + | |
| 1819 | + op2 = PyIter_Next(it); | |
| 1820 | + if (op2 == NULL) { | |
| 1821 | + if (PyErr_Occurred()) | |
| 1822 | + goto Fail; | |
| 1823 | + break; | |
| 1824 | + } | |
| 1825 | + | |
| 1826 | + if (result == NULL) | |
| 1827 | + result = op2; | |
| 1828 | + else { | |
| 1829 | + PyTuple_SetItem(args, 0, result); | |
| 1830 | + PyTuple_SetItem(args, 1, op2); | |
| 1831 | + if ((result = PyEval_CallObject(func, args)) == NULL) | |
| 1832 | + goto Fail; | |
| 1833 | + } | |
| 1834 | + } | |
| 1835 | + | |
| 1836 | + Py_DECREF(args); | |
| 1837 | + | |
| 1838 | + if (result == NULL) | |
| 1839 | + PyErr_SetString(PyExc_TypeError, | |
| 1840 | + "reduce() of empty sequence with no initial value"); | |
| 1841 | + | |
| 1842 | + Py_DECREF(it); | |
| 1843 | + return result; | |
| 1844 | + | |
| 1845 | +Fail: | |
| 1846 | + Py_XDECREF(args); | |
| 1847 | + Py_XDECREF(result); | |
| 1848 | + Py_DECREF(it); | |
| 1849 | + return NULL; | |
| 1850 | +} | |
| 1851 | + | |
| 1852 | +PyDoc_STRVAR(reduce_doc, | |
| 1853 | +"reduce(function, sequence[, initial]) -> value\n\ | |
| 1854 | +\n\ | |
| 1855 | +Apply a function of two arguments cumulatively to the items of a sequence,\n\ | |
| 1856 | +from left to right, so as to reduce the sequence to a single value.\n\ | |
| 1857 | +For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\ | |
| 1858 | +((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\ | |
| 1859 | +of the sequence in the calculation, and serves as a default when the\n\ | |
| 1860 | +sequence is empty."); | |
| 1861 | + | |
| 1862 | + | |
| 1863 | +static PyObject * | |
| 1864 | +builtin_reload(PyObject *self, PyObject *v) | |
| 1865 | +{ | |
| 1866 | + return PyImport_ReloadModule(v); | |
| 1867 | +} | |
| 1868 | + | |
| 1869 | +PyDoc_STRVAR(reload_doc, | |
| 1870 | +"reload(module) -> module\n\ | |
| 1871 | +\n\ | |
| 1872 | +Reload the module. The module must have been successfully imported before."); | |
| 1873 | + | |
| 1874 | + | |
| 1875 | +static PyObject * | |
| 1876 | +builtin_repr(PyObject *self, PyObject *v) | |
| 1877 | +{ | |
| 1878 | + return PyObject_Repr(v); | |
| 1879 | +} | |
| 1880 | + | |
| 1881 | +PyDoc_STRVAR(repr_doc, | |
| 1882 | +"repr(object) -> string\n\ | |
| 1883 | +\n\ | |
| 1884 | +Return the canonical string representation of the object.\n\ | |
| 1885 | +For most object types, eval(repr(object)) == object."); | |
| 1886 | + | |
| 1887 | + | |
| 1888 | +static PyObject * | |
| 1889 | +builtin_round(PyObject *self, PyObject *args, PyObject *kwds) | |
| 1890 | +{ | |
| 1891 | + double number; | |
| 1892 | + double f; | |
| 1893 | + int ndigits = 0; | |
| 1894 | + int i; | |
| 1895 | + static char *kwlist[] = {"number", "ndigits", 0}; | |
| 1896 | + | |
| 1897 | + if (!PyArg_ParseTupleAndKeywords(args, kwds, "d|i:round", | |
| 1898 | + kwlist, &number, &ndigits)) | |
| 1899 | + return NULL; | |
| 1900 | + f = 1.0; | |
| 1901 | + i = abs(ndigits); | |
| 1902 | + while (--i >= 0) | |
| 1903 | + f = f*10.0; | |
| 1904 | + if (ndigits < 0) | |
| 1905 | + number /= f; | |
| 1906 | + else | |
| 1907 | + number *= f; | |
| 1908 | + if (number >= 0.0) | |
| 1909 | + number = floor(number + 0.5); | |
| 1910 | + else | |
| 1911 | + number = ceil(number - 0.5); | |
| 1912 | + if (ndigits < 0) | |
| 1913 | + number *= f; | |
| 1914 | + else | |
| 1915 | + number /= f; | |
| 1916 | + return PyFloat_FromDouble(number); | |
| 1917 | +} | |
| 1918 | + | |
| 1919 | +PyDoc_STRVAR(round_doc, | |
| 1920 | +"round(number[, ndigits]) -> floating point number\n\ | |
| 1921 | +\n\ | |
| 1922 | +Round a number to a given precision in decimal digits (default 0 digits).\n\ | |
| 1923 | +This always returns a floating point number. Precision may be negative."); | |
| 1924 | + | |
| 1925 | +static PyObject * | |
| 1926 | +builtin_sorted(PyObject *self, PyObject *args, PyObject *kwds) | |
| 1927 | +{ | |
| 1928 | + PyObject *newlist, *v, *seq, *compare=NULL, *keyfunc=NULL, *newargs; | |
| 1929 | + PyObject *callable; | |
| 1930 | + static char *kwlist[] = {"iterable", "cmp", "key", "reverse", 0}; | |
| 1931 | + int reverse; | |
| 1932 | + | |
| 1933 | + /* args 1-4 should match listsort in Objects/listobject.c */ | |
| 1934 | + if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OOi:sorted", | |
| 1935 | + kwlist, &seq, &compare, &keyfunc, &reverse)) | |
| 1936 | + return NULL; | |
| 1937 | + | |
| 1938 | + newlist = PySequence_List(seq); | |
| 1939 | + if (newlist == NULL) | |
| 1940 | + return NULL; | |
| 1941 | + | |
| 1942 | + callable = PyObject_GetAttrString(newlist, "sort"); | |
| 1943 | + if (callable == NULL) { | |
| 1944 | + Py_DECREF(newlist); | |
| 1945 | + return NULL; | |
| 1946 | + } | |
| 1947 | + | |
| 1948 | + newargs = PyTuple_GetSlice(args, 1, 4); | |
| 1949 | + if (newargs == NULL) { | |
| 1950 | + Py_DECREF(newlist); | |
| 1951 | + Py_DECREF(callable); | |
| 1952 | + return NULL; | |
| 1953 | + } | |
| 1954 | + | |
| 1955 | + v = PyObject_Call(callable, newargs, kwds); | |
| 1956 | + Py_DECREF(newargs); | |
| 1957 | + Py_DECREF(callable); | |
| 1958 | + if (v == NULL) { | |
| 1959 | + Py_DECREF(newlist); | |
| 1960 | + return NULL; | |
| 1961 | + } | |
| 1962 | + Py_DECREF(v); | |
| 1963 | + return newlist; | |
| 1964 | +} | |
| 1965 | + | |
| 1966 | +PyDoc_STRVAR(sorted_doc, | |
| 1967 | +"sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list"); | |
| 1968 | + | |
| 1969 | +static PyObject * | |
| 1970 | +builtin_vars(PyObject *self, PyObject *args) | |
| 1971 | +{ | |
| 1972 | + PyObject *v = NULL; | |
| 1973 | + PyObject *d; | |
| 1974 | + | |
| 1975 | + if (!PyArg_UnpackTuple(args, "vars", 0, 1, &v)) | |
| 1976 | + return NULL; | |
| 1977 | + if (v == NULL) { | |
| 1978 | + d = PyEval_GetLocals(); | |
| 1979 | + if (d == NULL) { | |
| 1980 | + if (!PyErr_Occurred()) | |
| 1981 | + PyErr_SetString(PyExc_SystemError, | |
| 1982 | + "vars(): no locals!?"); | |
| 1983 | + } | |
| 1984 | + else | |
| 1985 | + Py_INCREF(d); | |
| 1986 | + } | |
| 1987 | + else { | |
| 1988 | + d = PyObject_GetAttrString(v, "__dict__"); | |
| 1989 | + if (d == NULL) { | |
| 1990 | + PyErr_SetString(PyExc_TypeError, | |
| 1991 | + "vars() argument must have __dict__ attribute"); | |
| 1992 | + return NULL; | |
| 1993 | + } | |
| 1994 | + } | |
| 1995 | + return d; | |
| 1996 | +} | |
| 1997 | + | |
| 1998 | +PyDoc_STRVAR(vars_doc, | |
| 1999 | +"vars([object]) -> dictionary\n\ | |
| 2000 | +\n\ | |
| 2001 | +Without arguments, equivalent to locals().\n\ | |
| 2002 | +With an argument, equivalent to object.__dict__."); | |
| 2003 | + | |
| 2004 | + | |
| 2005 | +static PyObject* | |
| 2006 | +builtin_sum(PyObject *self, PyObject *args) | |
| 2007 | +{ | |
| 2008 | + PyObject *seq; | |
| 2009 | + PyObject *result = NULL; | |
| 2010 | + PyObject *temp, *item, *iter; | |
| 2011 | + | |
| 2012 | + if (!PyArg_UnpackTuple(args, "sum", 1, 2, &seq, &result)) | |
| 2013 | + return NULL; | |
| 2014 | + | |
| 2015 | + iter = PyObject_GetIter(seq); | |
| 2016 | + if (iter == NULL) | |
| 2017 | + return NULL; | |
| 2018 | + | |
| 2019 | + if (result == NULL) { | |
| 2020 | + result = PyInt_FromLong(0); | |
| 2021 | + if (result == NULL) { | |
| 2022 | + Py_DECREF(iter); | |
| 2023 | + return NULL; | |
| 2024 | + } | |
| 2025 | + } else { | |
| 2026 | + /* reject string values for 'start' parameter */ | |
| 2027 | + if (PyObject_TypeCheck(result, &PyBaseString_Type)) { | |
| 2028 | + PyErr_SetString(PyExc_TypeError, | |
| 2029 | + "sum() can't sum strings [use ''.join(seq) instead]"); | |
| 2030 | + Py_DECREF(iter); | |
| 2031 | + return NULL; | |
| 2032 | + } | |
| 2033 | + Py_INCREF(result); | |
| 2034 | + } | |
| 2035 | + | |
| 2036 | + for(;;) { | |
| 2037 | + item = PyIter_Next(iter); | |
| 2038 | + if (item == NULL) { | |
| 2039 | + /* error, or end-of-sequence */ | |
| 2040 | + if (PyErr_Occurred()) { | |
| 2041 | + Py_DECREF(result); | |
| 2042 | + result = NULL; | |
| 2043 | + } | |
| 2044 | + break; | |
| 2045 | + } | |
| 2046 | + temp = PyNumber_Add(result, item); | |
| 2047 | + Py_DECREF(result); | |
| 2048 | + Py_DECREF(item); | |
| 2049 | + result = temp; | |
| 2050 | + if (result == NULL) | |
| 2051 | + break; | |
| 2052 | + } | |
| 2053 | + Py_DECREF(iter); | |
| 2054 | + return result; | |
| 2055 | +} | |
| 2056 | + | |
| 2057 | +PyDoc_STRVAR(sum_doc, | |
| 2058 | +"sum(sequence, start=0) -> value\n\ | |
| 2059 | +\n\ | |
| 2060 | +Returns the sum of a sequence of numbers (NOT strings) plus the value\n\ | |
| 2061 | +of parameter 'start'. When the sequence is empty, returns start."); | |
| 2062 | + | |
| 2063 | + | |
| 2064 | +static PyObject * | |
| 2065 | +builtin_isinstance(PyObject *self, PyObject *args) | |
| 2066 | +{ | |
| 2067 | + PyObject *inst; | |
| 2068 | + PyObject *cls; | |
| 2069 | + int retval; | |
| 2070 | + | |
| 2071 | + if (!PyArg_UnpackTuple(args, "isinstance", 2, 2, &inst, &cls)) | |
| 2072 | + return NULL; | |
| 2073 | + | |
| 2074 | + retval = PyObject_IsInstance(inst, cls); | |
| 2075 | + if (retval < 0) | |
| 2076 | + return NULL; | |
| 2077 | + return PyBool_FromLong(retval); | |
| 2078 | +} | |
| 2079 | + | |
| 2080 | +PyDoc_STRVAR(isinstance_doc, | |
| 2081 | +"isinstance(object, class-or-type-or-tuple) -> bool\n\ | |
| 2082 | +\n\ | |
| 2083 | +Return whether an object is an instance of a class or of a subclass thereof.\n\ | |
| 2084 | +With a type as second argument, return whether that is the object's type.\n\ | |
| 2085 | +The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for\n\ | |
| 2086 | +isinstance(x, A) or isinstance(x, B) or ... (etc.)."); | |
| 2087 | + | |
| 2088 | + | |
| 2089 | +static PyObject * | |
| 2090 | +builtin_issubclass(PyObject *self, PyObject *args) | |
| 2091 | +{ | |
| 2092 | + PyObject *derived; | |
| 2093 | + PyObject *cls; | |
| 2094 | + int retval; | |
| 2095 | + | |
| 2096 | + if (!PyArg_UnpackTuple(args, "issubclass", 2, 2, &derived, &cls)) | |
| 2097 | + return NULL; | |
| 2098 | + | |
| 2099 | + retval = PyObject_IsSubclass(derived, cls); | |
| 2100 | + if (retval < 0) | |
| 2101 | + return NULL; | |
| 2102 | + return PyBool_FromLong(retval); | |
| 2103 | +} | |
| 2104 | + | |
| 2105 | +PyDoc_STRVAR(issubclass_doc, | |
| 2106 | +"issubclass(C, B) -> bool\n\ | |
| 2107 | +\n\ | |
| 2108 | +Return whether class C is a subclass (i.e., a derived class) of class B.\n\ | |
| 2109 | +When using a tuple as the second argument issubclass(X, (A, B, ...)),\n\ | |
| 2110 | +is a shortcut for issubclass(X, A) or issubclass(X, B) or ... (etc.)."); | |
| 2111 | + | |
| 2112 | + | |
| 2113 | +static PyObject* | |
| 2114 | +builtin_zip(PyObject *self, PyObject *args) | |
| 2115 | +{ | |
| 2116 | + PyObject *ret; | |
| 2117 | + const Py_ssize_t itemsize = PySequence_Length(args); | |
| 2118 | + Py_ssize_t i; | |
| 2119 | + PyObject *itlist; /* tuple of iterators */ | |
| 2120 | + Py_ssize_t len; /* guess at result length */ | |
| 2121 | + | |
| 2122 | + if (itemsize == 0) | |
| 2123 | + return PyList_New(0); | |
| 2124 | + | |
| 2125 | + /* args must be a tuple */ | |
| 2126 | + assert(PyTuple_Check(args)); | |
| 2127 | + | |
| 2128 | + /* Guess at result length: the shortest of the input lengths. | |
| 2129 | + If some argument refuses to say, we refuse to guess too, lest | |
| 2130 | + an argument like xrange(sys.maxint) lead us astray.*/ | |
| 2131 | + len = -1; /* unknown */ | |
| 2132 | + for (i = 0; i < itemsize; ++i) { | |
| 2133 | + PyObject *item = PyTuple_GET_ITEM(args, i); | |
| 2134 | + Py_ssize_t thislen = _PyObject_LengthHint(item); | |
| 2135 | + if (thislen < 0) { | |
| 2136 | + if (!PyErr_ExceptionMatches(PyExc_TypeError) && | |
| 2137 | + !PyErr_ExceptionMatches(PyExc_AttributeError)) { | |
| 2138 | + return NULL; | |
| 2139 | + } | |
| 2140 | + PyErr_Clear(); | |
| 2141 | + len = -1; | |
| 2142 | + break; | |
| 2143 | + } | |
| 2144 | + else if (len < 0 || thislen < len) | |
| 2145 | + len = thislen; | |
| 2146 | + } | |
| 2147 | + | |
| 2148 | + /* allocate result list */ | |
| 2149 | + if (len < 0) | |
| 2150 | + len = 10; /* arbitrary */ | |
| 2151 | + if ((ret = PyList_New(len)) == NULL) | |
| 2152 | + return NULL; | |
| 2153 | + | |
| 2154 | + /* obtain iterators */ | |
| 2155 | + itlist = PyTuple_New(itemsize); | |
| 2156 | + if (itlist == NULL) | |
| 2157 | + goto Fail_ret; | |
| 2158 | + for (i = 0; i < itemsize; ++i) { | |
| 2159 | + PyObject *item = PyTuple_GET_ITEM(args, i); | |
| 2160 | + PyObject *it = PyObject_GetIter(item); | |
| 2161 | + if (it == NULL) { | |
| 2162 | + if (PyErr_ExceptionMatches(PyExc_TypeError)) | |
| 2163 | + PyErr_Format(PyExc_TypeError, | |
| 2164 | + "zip argument #%zd must support iteration", | |
| 2165 | + i+1); | |
| 2166 | + goto Fail_ret_itlist; | |
| 2167 | + } | |
| 2168 | + PyTuple_SET_ITEM(itlist, i, it); | |
| 2169 | + } | |
| 2170 | + | |
| 2171 | + /* build result into ret list */ | |
| 2172 | + for (i = 0; ; ++i) { | |
| 2173 | + int j; | |
| 2174 | + PyObject *next = PyTuple_New(itemsize); | |
| 2175 | + if (!next) | |
| 2176 | + goto Fail_ret_itlist; | |
| 2177 | + | |
| 2178 | + for (j = 0; j < itemsize; j++) { | |
| 2179 | + PyObject *it = PyTuple_GET_ITEM(itlist, j); | |
| 2180 | + PyObject *item = PyIter_Next(it); | |
| 2181 | + if (!item) { | |
| 2182 | + if (PyErr_Occurred()) { | |
| 2183 | + Py_DECREF(ret); | |
| 2184 | + ret = NULL; | |
| 2185 | + } | |
| 2186 | + Py_DECREF(next); | |
| 2187 | + Py_DECREF(itlist); | |
| 2188 | + goto Done; | |
| 2189 | + } | |
| 2190 | + PyTuple_SET_ITEM(next, j, item); | |
| 2191 | + } | |
| 2192 | + | |
| 2193 | + if (i < len) | |
| 2194 | + PyList_SET_ITEM(ret, i, next); | |
| 2195 | + else { | |
| 2196 | + int status = PyList_Append(ret, next); | |
| 2197 | + Py_DECREF(next); | |
| 2198 | + ++len; | |
| 2199 | + if (status < 0) | |
| 2200 | + goto Fail_ret_itlist; | |
| 2201 | + } | |
| 2202 | + } | |
| 2203 | + | |
| 2204 | +Done: | |
| 2205 | + if (ret != NULL && i < len) { | |
| 2206 | + /* The list is too big. */ | |
| 2207 | + if (PyList_SetSlice(ret, i, len, NULL) < 0) | |
| 2208 | + return NULL; | |
| 2209 | + } | |
| 2210 | + return ret; | |
| 2211 | + | |
| 2212 | +Fail_ret_itlist: | |
| 2213 | + Py_DECREF(itlist); | |
| 2214 | +Fail_ret: | |
| 2215 | + Py_DECREF(ret); | |
| 2216 | + return NULL; | |
| 2217 | +} | |
| 2218 | + | |
| 2219 | + | |
| 2220 | +PyDoc_STRVAR(zip_doc, | |
| 2221 | +"zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]\n\ | |
| 2222 | +\n\ | |
| 2223 | +Return a list of tuples, where each tuple contains the i-th element\n\ | |
| 2224 | +from each of the argument sequences. The returned list is truncated\n\ | |
| 2225 | +in length to the length of the shortest argument sequence."); | |
| 2226 | + | |
| 2227 | + | |
| 2228 | +static PyMethodDef builtin_methods[] = { | |
| 2229 | + {"__import__", (PyCFunction)builtin___import__, METH_VARARGS | METH_KEYWORDS, import_doc}, | |
| 2230 | + {"abs", builtin_abs, METH_O, abs_doc}, | |
| 2231 | + {"all", builtin_all, METH_O, all_doc}, | |
| 2232 | + {"any", builtin_any, METH_O, any_doc}, | |
| 2233 | + {"apply", builtin_apply, METH_VARARGS, apply_doc}, | |
| 2234 | + {"callable", builtin_callable, METH_O, callable_doc}, | |
| 2235 | + {"chr", builtin_chr, METH_VARARGS, chr_doc}, | |
| 2236 | + {"cmp", builtin_cmp, METH_VARARGS, cmp_doc}, | |
| 2237 | + {"coerce", builtin_coerce, METH_VARARGS, coerce_doc}, | |
| 2238 | + {"compile", builtin_compile, METH_VARARGS, compile_doc}, | |
| 2239 | + {"delattr", builtin_delattr, METH_VARARGS, delattr_doc}, | |
| 2240 | + {"dir", builtin_dir, METH_VARARGS, dir_doc}, | |
| 2241 | + {"divmod", builtin_divmod, METH_VARARGS, divmod_doc}, | |
| 2242 | + {"eval", builtin_eval, METH_VARARGS, eval_doc}, | |
| 2243 | + {"execfile", builtin_execfile, METH_VARARGS, execfile_doc}, | |
| 2244 | + {"filter", builtin_filter, METH_VARARGS, filter_doc}, | |
| 2245 | + {"getattr", builtin_getattr, METH_VARARGS, getattr_doc}, | |
| 2246 | + {"globals", (PyCFunction)builtin_globals, METH_NOARGS, globals_doc}, | |
| 2247 | + {"hasattr", builtin_hasattr, METH_VARARGS, hasattr_doc}, | |
| 2248 | + {"hash", builtin_hash, METH_O, hash_doc}, | |
| 2249 | + {"hex", builtin_hex, METH_O, hex_doc}, | |
| 2250 | + {"id", builtin_id, METH_O, id_doc}, | |
| 2251 | + {"input", builtin_input, METH_VARARGS, input_doc}, | |
| 2252 | + {"intern", builtin_intern, METH_VARARGS, intern_doc}, | |
| 2253 | + {"isinstance", builtin_isinstance, METH_VARARGS, isinstance_doc}, | |
| 2254 | + {"issubclass", builtin_issubclass, METH_VARARGS, issubclass_doc}, | |
| 2255 | + {"iter", builtin_iter, METH_VARARGS, iter_doc}, | |
| 2256 | + {"len", builtin_len, METH_O, len_doc}, | |
| 2257 | + {"locals", (PyCFunction)builtin_locals, METH_NOARGS, locals_doc}, | |
| 2258 | + {"map", builtin_map, METH_VARARGS, map_doc}, | |
| 2259 | + {"max", (PyCFunction)builtin_max, METH_VARARGS | METH_KEYWORDS, max_doc}, | |
| 2260 | + {"min", (PyCFunction)builtin_min, METH_VARARGS | METH_KEYWORDS, min_doc}, | |
| 2261 | + {"oct", builtin_oct, METH_O, oct_doc}, | |
| 2262 | + {"open", (PyCFunction)builtin_open, METH_VARARGS | METH_KEYWORDS, open_doc}, | |
| 2263 | + {"ord", builtin_ord, METH_O, ord_doc}, | |
| 2264 | + {"pow", builtin_pow, METH_VARARGS, pow_doc}, | |
| 2265 | + {"range", builtin_range, METH_VARARGS, range_doc}, | |
| 2266 | + {"raw_input", builtin_raw_input, METH_VARARGS, raw_input_doc}, | |
| 2267 | + {"reduce", builtin_reduce, METH_VARARGS, reduce_doc}, | |
| 2268 | + {"reload", builtin_reload, METH_O, reload_doc}, | |
| 2269 | + {"repr", builtin_repr, METH_O, repr_doc}, | |
| 2270 | + {"round", (PyCFunction)builtin_round, METH_VARARGS | METH_KEYWORDS, round_doc}, | |
| 2271 | + {"setattr", builtin_setattr, METH_VARARGS, setattr_doc}, | |
| 2272 | + {"sorted", (PyCFunction)builtin_sorted, METH_VARARGS | METH_KEYWORDS, sorted_doc}, | |
| 2273 | + {"sum", builtin_sum, METH_VARARGS, sum_doc}, | |
| 2274 | +#ifdef Py_USING_UNICODE | |
| 2275 | + {"unichr", builtin_unichr, METH_VARARGS, unichr_doc}, | |
| 2276 | +#endif | |
| 2277 | + {"vars", builtin_vars, METH_VARARGS, vars_doc}, | |
| 2278 | + {"zip", builtin_zip, METH_VARARGS, zip_doc}, | |
| 2279 | + {NULL, NULL}, | |
| 2280 | +}; | |
| 2281 | + | |
| 2282 | +PyDoc_STRVAR(builtin_doc, | |
| 2283 | +"Built-in functions, exceptions, and other objects.\n\ | |
| 2284 | +\n\ | |
| 2285 | +Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices."); | |
| 2286 | + | |
| 2287 | +PyObject * | |
| 2288 | +_PyBuiltin_Init(void) | |
| 2289 | +{ | |
| 2290 | + PyObject *mod, *dict, *debug; | |
| 2291 | + mod = Py_InitModule4("__builtin__", builtin_methods, | |
| 2292 | + builtin_doc, (PyObject *)NULL, | |
| 2293 | + PYTHON_API_VERSION); | |
| 2294 | + if (mod == NULL) | |
| 2295 | + return NULL; | |
| 2296 | + dict = PyModule_GetDict(mod); | |
| 2297 | + | |
| 2298 | +#ifdef Py_TRACE_REFS | |
| 2299 | + /* __builtin__ exposes a number of statically allocated objects | |
| 2300 | + * that, before this code was added in 2.3, never showed up in | |
| 2301 | + * the list of "all objects" maintained by Py_TRACE_REFS. As a | |
| 2302 | + * result, programs leaking references to None and False (etc) | |
| 2303 | + * couldn't be diagnosed by examining sys.getobjects(0). | |
| 2304 | + */ | |
| 2305 | +#define ADD_TO_ALL(OBJECT) _Py_AddToAllObjects((PyObject *)(OBJECT), 0) | |
| 2306 | +#else | |
| 2307 | +#define ADD_TO_ALL(OBJECT) (void)0 | |
| 2308 | +#endif | |
| 2309 | + | |
| 2310 | +#define SETBUILTIN(NAME, OBJECT) \ | |
| 2311 | + if (PyDict_SetItemString(dict, NAME, (PyObject *)OBJECT) < 0) \ | |
| 2312 | + return NULL; \ | |
| 2313 | + ADD_TO_ALL(OBJECT) | |
| 2314 | + | |
| 2315 | + SETBUILTIN("None", Py_None); | |
| 2316 | + SETBUILTIN("Ellipsis", Py_Ellipsis); | |
| 2317 | + SETBUILTIN("NotImplemented", Py_NotImplemented); | |
| 2318 | + SETBUILTIN("False", Py_False); | |
| 2319 | + SETBUILTIN("True", Py_True); | |
| 2320 | + SETBUILTIN("basestring", &PyBaseString_Type); | |
| 2321 | + SETBUILTIN("bool", &PyBool_Type); | |
| 2322 | + SETBUILTIN("buffer", &PyBuffer_Type); | |
| 2323 | + SETBUILTIN("classmethod", &PyClassMethod_Type); | |
| 2324 | +#ifndef WITHOUT_COMPLEX | |
| 2325 | + SETBUILTIN("complex", &PyComplex_Type); | |
| 2326 | +#endif | |
| 2327 | + SETBUILTIN("dict", &PyDict_Type); | |
| 2328 | + SETBUILTIN("enumerate", &PyEnum_Type); | |
| 2329 | + SETBUILTIN("file", &PyFile_Type); | |
| 2330 | + SETBUILTIN("float", &PyFloat_Type); | |
| 2331 | + SETBUILTIN("frozenset", &PyFrozenSet_Type); | |
| 2332 | + SETBUILTIN("property", &PyProperty_Type); | |
| 2333 | + SETBUILTIN("int", &PyInt_Type); | |
| 2334 | + SETBUILTIN("list", &PyList_Type); | |
| 2335 | + SETBUILTIN("long", &PyLong_Type); | |
| 2336 | + SETBUILTIN("object", &PyBaseObject_Type); | |
| 2337 | + SETBUILTIN("reversed", &PyReversed_Type); | |
| 2338 | + SETBUILTIN("set", &PySet_Type); | |
| 2339 | + SETBUILTIN("slice", &PySlice_Type); | |
| 2340 | + SETBUILTIN("staticmethod", &PyStaticMethod_Type); | |
| 2341 | + SETBUILTIN("str", &PyString_Type); | |
| 2342 | + SETBUILTIN("super", &PySuper_Type); | |
| 2343 | + SETBUILTIN("tuple", &PyTuple_Type); | |
| 2344 | + SETBUILTIN("type", &PyType_Type); | |
| 2345 | + SETBUILTIN("xrange", &PyRange_Type); | |
| 2346 | +#ifdef Py_USING_UNICODE | |
| 2347 | + SETBUILTIN("unicode", &PyUnicode_Type); | |
| 2348 | +#endif | |
| 2349 | + debug = PyBool_FromLong(Py_OptimizeFlag == 0); | |
| 2350 | + if (PyDict_SetItemString(dict, "__debug__", debug) < 0) { | |
| 2351 | + Py_XDECREF(debug); | |
| 2352 | + return NULL; | |
| 2353 | + } | |
| 2354 | + Py_XDECREF(debug); | |
| 2355 | + | |
| 2356 | + return mod; | |
| 2357 | +#undef ADD_TO_ALL | |
| 2358 | +#undef SETBUILTIN | |
| 2359 | +} | |
| 2360 | + | |
| 2361 | +/* Helper for filter(): filter a tuple through a function */ | |
| 2362 | + | |
| 2363 | +static PyObject * | |
| 2364 | +filtertuple(PyObject *func, PyObject *tuple) | |
| 2365 | +{ | |
| 2366 | + PyObject *result; | |
| 2367 | + Py_ssize_t i, j; | |
| 2368 | + Py_ssize_t len = PyTuple_Size(tuple); | |
| 2369 | + | |
| 2370 | + if (len == 0) { | |
| 2371 | + if (PyTuple_CheckExact(tuple)) | |
| 2372 | + Py_INCREF(tuple); | |
| 2373 | + else | |
| 2374 | + tuple = PyTuple_New(0); | |
| 2375 | + return tuple; | |
| 2376 | + } | |
| 2377 | + | |
| 2378 | + if ((result = PyTuple_New(len)) == NULL) | |
| 2379 | + return NULL; | |
| 2380 | + | |
| 2381 | + for (i = j = 0; i < len; ++i) { | |
| 2382 | + PyObject *item, *good; | |
| 2383 | + int ok; | |
| 2384 | + | |
| 2385 | + if (tuple->ob_type->tp_as_sequence && | |
| 2386 | + tuple->ob_type->tp_as_sequence->sq_item) { | |
| 2387 | + item = tuple->ob_type->tp_as_sequence->sq_item(tuple, i); | |
| 2388 | + if (item == NULL) | |
| 2389 | + goto Fail_1; | |
| 2390 | + } else { | |
| 2391 | + PyErr_SetString(PyExc_TypeError, "filter(): unsubscriptable tuple"); | |
| 2392 | + goto Fail_1; | |
| 2393 | + } | |
| 2394 | + if (func == Py_None) { | |
| 2395 | + Py_INCREF(item); | |
| 2396 | + good = item; | |
| 2397 | + } | |
| 2398 | + else { | |
| 2399 | + PyObject *arg = PyTuple_Pack(1, item); | |
| 2400 | + if (arg == NULL) { | |
| 2401 | + Py_DECREF(item); | |
| 2402 | + goto Fail_1; | |
| 2403 | + } | |
| 2404 | + good = PyEval_CallObject(func, arg); | |
| 2405 | + Py_DECREF(arg); | |
| 2406 | + if (good == NULL) { | |
| 2407 | + Py_DECREF(item); | |
| 2408 | + goto Fail_1; | |
| 2409 | + } | |
| 2410 | + } | |
| 2411 | + ok = PyObject_IsTrue(good); | |
| 2412 | + Py_DECREF(good); | |
| 2413 | + if (ok) { | |
| 2414 | + if (PyTuple_SetItem(result, j++, item) < 0) | |
| 2415 | + goto Fail_1; | |
| 2416 | + } | |
| 2417 | + else | |
| 2418 | + Py_DECREF(item); | |
| 2419 | + } | |
| 2420 | + | |
| 2421 | + if (_PyTuple_Resize(&result, j) < 0) | |
| 2422 | + return NULL; | |
| 2423 | + | |
| 2424 | + return result; | |
| 2425 | + | |
| 2426 | +Fail_1: | |
| 2427 | + Py_DECREF(result); | |
| 2428 | + return NULL; | |
| 2429 | +} | |
| 2430 | + | |
| 2431 | + | |
| 2432 | +/* Helper for filter(): filter a string through a function */ | |
| 2433 | + | |
| 2434 | +static PyObject * | |
| 2435 | +filterstring(PyObject *func, PyObject *strobj) | |
| 2436 | +{ | |
| 2437 | + PyObject *result; | |
| 2438 | + Py_ssize_t i, j; | |
| 2439 | + Py_ssize_t len = PyString_Size(strobj); | |
| 2440 | + Py_ssize_t outlen = len; | |
| 2441 | + | |
| 2442 | + if (func == Py_None) { | |
| 2443 | + /* If it's a real string we can return the original, | |
| 2444 | + * as no character is ever false and __getitem__ | |
| 2445 | + * does return this character. If it's a subclass | |
| 2446 | + * we must go through the __getitem__ loop */ | |
| 2447 | + if (PyString_CheckExact(strobj)) { | |
| 2448 | + Py_INCREF(strobj); | |
| 2449 | + return strobj; | |
| 2450 | + } | |
| 2451 | + } | |
| 2452 | + if ((result = PyString_FromStringAndSize(NULL, len)) == NULL) | |
| 2453 | + return NULL; | |
| 2454 | + | |
| 2455 | + for (i = j = 0; i < len; ++i) { | |
| 2456 | + PyObject *item; | |
| 2457 | + int ok; | |
| 2458 | + | |
| 2459 | + item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i); | |
| 2460 | + if (item == NULL) | |
| 2461 | + goto Fail_1; | |
| 2462 | + if (func==Py_None) { | |
| 2463 | + ok = 1; | |
| 2464 | + } else { | |
| 2465 | + PyObject *arg, *good; | |
| 2466 | + arg = PyTuple_Pack(1, item); | |
| 2467 | + if (arg == NULL) { | |
| 2468 | + Py_DECREF(item); | |
| 2469 | + goto Fail_1; | |
| 2470 | + } | |
| 2471 | + good = PyEval_CallObject(func, arg); | |
| 2472 | + Py_DECREF(arg); | |
| 2473 | + if (good == NULL) { | |
| 2474 | + Py_DECREF(item); | |
| 2475 | + goto Fail_1; | |
| 2476 | + } | |
| 2477 | + ok = PyObject_IsTrue(good); | |
| 2478 | + Py_DECREF(good); | |
| 2479 | + } | |
| 2480 | + if (ok) { | |
| 2481 | + Py_ssize_t reslen; | |
| 2482 | + if (!PyString_Check(item)) { | |
| 2483 | + PyErr_SetString(PyExc_TypeError, "can't filter str to str:" | |
| 2484 | + " __getitem__ returned different type"); | |
| 2485 | + Py_DECREF(item); | |
| 2486 | + goto Fail_1; | |
| 2487 | + } | |
| 2488 | + reslen = PyString_GET_SIZE(item); | |
| 2489 | + if (reslen == 1) { | |
| 2490 | + PyString_AS_STRING(result)[j++] = | |
| 2491 | + PyString_AS_STRING(item)[0]; | |
| 2492 | + } else { | |
| 2493 | + /* do we need more space? */ | |
| 2494 | + Py_ssize_t need = j + reslen + len-i-1; | |
| 2495 | + if (need > outlen) { | |
| 2496 | + /* overallocate, to avoid reallocations */ | |
| 2497 | + if (need<2*outlen) | |
| 2498 | + need = 2*outlen; | |
| 2499 | + if (_PyString_Resize(&result, need)) { | |
| 2500 | + Py_DECREF(item); | |
| 2501 | + return NULL; | |
| 2502 | + } | |
| 2503 | + outlen = need; | |
| 2504 | + } | |
| 2505 | + memcpy( | |
| 2506 | + PyString_AS_STRING(result) + j, | |
| 2507 | + PyString_AS_STRING(item), | |
| 2508 | + reslen | |
| 2509 | + ); | |
| 2510 | + j += reslen; | |
| 2511 | + } | |
| 2512 | + } | |
| 2513 | + Py_DECREF(item); | |
| 2514 | + } | |
| 2515 | + | |
| 2516 | + if (j < outlen) | |
| 2517 | + _PyString_Resize(&result, j); | |
| 2518 | + | |
| 2519 | + return result; | |
| 2520 | + | |
| 2521 | +Fail_1: | |
| 2522 | + Py_DECREF(result); | |
| 2523 | + return NULL; | |
| 2524 | +} | |
| 2525 | + | |
| 2526 | +#ifdef Py_USING_UNICODE | |
| 2527 | +/* Helper for filter(): filter a Unicode object through a function */ | |
| 2528 | + | |
| 2529 | +static PyObject * | |
| 2530 | +filterunicode(PyObject *func, PyObject *strobj) | |
| 2531 | +{ | |
| 2532 | + PyObject *result; | |
| 2533 | + register Py_ssize_t i, j; | |
| 2534 | + Py_ssize_t len = PyUnicode_GetSize(strobj); | |
| 2535 | + Py_ssize_t outlen = len; | |
| 2536 | + | |
| 2537 | + if (func == Py_None) { | |
| 2538 | + /* If it's a real string we can return the original, | |
| 2539 | + * as no character is ever false and __getitem__ | |
| 2540 | + * does return this character. If it's a subclass | |
| 2541 | + * we must go through the __getitem__ loop */ | |
| 2542 | + if (PyUnicode_CheckExact(strobj)) { | |
| 2543 | + Py_INCREF(strobj); | |
| 2544 | + return strobj; | |
| 2545 | + } | |
| 2546 | + } | |
| 2547 | + if ((result = PyUnicode_FromUnicode(NULL, len)) == NULL) | |
| 2548 | + return NULL; | |
| 2549 | + | |
| 2550 | + for (i = j = 0; i < len; ++i) { | |
| 2551 | + PyObject *item, *arg, *good; | |
| 2552 | + int ok; | |
| 2553 | + | |
| 2554 | + item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i); | |
| 2555 | + if (item == NULL) | |
| 2556 | + goto Fail_1; | |
| 2557 | + if (func == Py_None) { | |
| 2558 | + ok = 1; | |
| 2559 | + } else { | |
| 2560 | + arg = PyTuple_Pack(1, item); | |
| 2561 | + if (arg == NULL) { | |
| 2562 | + Py_DECREF(item); | |
| 2563 | + goto Fail_1; | |
| 2564 | + } | |
| 2565 | + good = PyEval_CallObject(func, arg); | |
| 2566 | + Py_DECREF(arg); | |
| 2567 | + if (good == NULL) { | |
| 2568 | + Py_DECREF(item); | |
| 2569 | + goto Fail_1; | |
| 2570 | + } | |
| 2571 | + ok = PyObject_IsTrue(good); | |
| 2572 | + Py_DECREF(good); | |
| 2573 | + } | |
| 2574 | + if (ok) { | |
| 2575 | + Py_ssize_t reslen; | |
| 2576 | + if (!PyUnicode_Check(item)) { | |
| 2577 | + PyErr_SetString(PyExc_TypeError, | |
| 2578 | + "can't filter unicode to unicode:" | |
| 2579 | + " __getitem__ returned different type"); | |
| 2580 | + Py_DECREF(item); | |
| 2581 | + goto Fail_1; | |
| 2582 | + } | |
| 2583 | + reslen = PyUnicode_GET_SIZE(item); | |
| 2584 | + if (reslen == 1) | |
| 2585 | + PyUnicode_AS_UNICODE(result)[j++] = | |
| 2586 | + PyUnicode_AS_UNICODE(item)[0]; | |
| 2587 | + else { | |
| 2588 | + /* do we need more space? */ | |
| 2589 | + Py_ssize_t need = j + reslen + len - i - 1; | |
| 2590 | + if (need > outlen) { | |
| 2591 | + /* overallocate, | |
| 2592 | + to avoid reallocations */ | |
| 2593 | + if (need < 2 * outlen) | |
| 2594 | + need = 2 * outlen; | |
| 2595 | + if (PyUnicode_Resize( | |
| 2596 | + &result, need) < 0) { | |
| 2597 | + Py_DECREF(item); | |
| 2598 | + goto Fail_1; | |
| 2599 | + } | |
| 2600 | + outlen = need; | |
| 2601 | + } | |
| 2602 | + memcpy(PyUnicode_AS_UNICODE(result) + j, | |
| 2603 | + PyUnicode_AS_UNICODE(item), | |
| 2604 | + reslen*sizeof(Py_UNICODE)); | |
| 2605 | + j += reslen; | |
| 2606 | + } | |
| 2607 | + } | |
| 2608 | + Py_DECREF(item); | |
| 2609 | + } | |
| 2610 | + | |
| 2611 | + if (j < outlen) | |
| 2612 | + PyUnicode_Resize(&result, j); | |
| 2613 | + | |
| 2614 | + return result; | |
| 2615 | + | |
| 2616 | +Fail_1: | |
| 2617 | + Py_DECREF(result); | |
| 2618 | + return NULL; | |
| 2619 | +} | |
| 2620 | +#endif | |
| @@ -0,0 +1,4351 @@ | ||
| 1 | + | |
| 2 | +/* Execute compiled code */ | |
| 3 | + | |
| 4 | +/* XXX TO DO: | |
| 5 | + XXX speed up searching for keywords by using a dictionary | |
| 6 | + XXX document it! | |
| 7 | + */ | |
| 8 | + | |
| 9 | +/* enable more aggressive intra-module optimizations, where available */ | |
| 10 | +#define PY_LOCAL_AGGRESSIVE | |
| 11 | + | |
| 12 | +#include "Python.h" | |
| 13 | + | |
| 14 | +#include "code.h" | |
| 15 | +#include "frameobject.h" | |
| 16 | +#include "eval.h" | |
| 17 | +#include "opcode.h" | |
| 18 | +#include "structmember.h" | |
| 19 | + | |
| 20 | +#include <ctype.h> | |
| 21 | + | |
| 22 | +#ifndef WITH_TSC | |
| 23 | + | |
| 24 | +#define READ_TIMESTAMP(var) | |
| 25 | + | |
| 26 | +#else | |
| 27 | + | |
| 28 | +typedef unsigned long long uint64; | |
| 29 | + | |
| 30 | +#if defined(__ppc__) /* <- Don't know if this is the correct symbol; this | |
| 31 | + section should work for GCC on any PowerPC platform, | |
| 32 | + irrespective of OS. POWER? Who knows :-) */ | |
| 33 | + | |
| 34 | +#define READ_TIMESTAMP(var) ppc_getcounter(&var) | |
| 35 | + | |
| 36 | +static void | |
| 37 | +ppc_getcounter(uint64 *v) | |
| 38 | +{ | |
| 39 | + register unsigned long tbu, tb, tbu2; | |
| 40 | + | |
| 41 | + loop: | |
| 42 | + asm volatile ("mftbu %0" : "=r" (tbu) ); | |
| 43 | + asm volatile ("mftb %0" : "=r" (tb) ); | |
| 44 | + asm volatile ("mftbu %0" : "=r" (tbu2)); | |
| 45 | + if (__builtin_expect(tbu != tbu2, 0)) goto loop; | |
| 46 | + | |
| 47 | + /* The slightly peculiar way of writing the next lines is | |
| 48 | + compiled better by GCC than any other way I tried. */ | |
| 49 | + ((long*)(v))[0] = tbu; | |
| 50 | + ((long*)(v))[1] = tb; | |
| 51 | +} | |
| 52 | + | |
| 53 | +#else /* this is for linux/x86 (and probably any other GCC/x86 combo) */ | |
| 54 | + | |
| 55 | +#define READ_TIMESTAMP(val) \ | |
| 56 | + __asm__ __volatile__("rdtsc" : "=A" (val)) | |
| 57 | + | |
| 58 | +#endif | |
| 59 | + | |
| 60 | +void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1, | |
| 61 | + uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1) | |
| 62 | +{ | |
| 63 | + uint64 intr, inst, loop; | |
| 64 | + PyThreadState *tstate = PyThreadState_Get(); | |
| 65 | + if (!tstate->interp->tscdump) | |
| 66 | + return; | |
| 67 | + intr = intr1 - intr0; | |
| 68 | + inst = inst1 - inst0 - intr; | |
| 69 | + loop = loop1 - loop0 - intr; | |
| 70 | + fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n", | |
| 71 | + opcode, ticked, inst, loop); | |
| 72 | +} | |
| 73 | + | |
| 74 | +#endif | |
| 75 | + | |
| 76 | +/* Turn this on if your compiler chokes on the big switch: */ | |
| 77 | +/* #define CASE_TOO_BIG 1 */ | |
| 78 | + | |
| 79 | +#ifdef Py_DEBUG | |
| 80 | +/* For debugging the interpreter: */ | |
| 81 | +#define LLTRACE 1 /* Low-level trace feature */ | |
| 82 | +#define CHECKEXC 1 /* Double-check exception checking */ | |
| 83 | +#endif | |
| 84 | + | |
| 85 | +typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *); | |
| 86 | + | |
| 87 | +/* Forward declarations */ | |
| 88 | +#ifdef WITH_TSC | |
| 89 | +static PyObject * call_function(PyObject ***, int, uint64*, uint64*); | |
| 90 | +#else | |
| 91 | +static PyObject * call_function(PyObject ***, int); | |
| 92 | +#endif | |
| 93 | +static PyObject * fast_function(PyObject *, PyObject ***, int, int, int); | |
| 94 | +static PyObject * do_call(PyObject *, PyObject ***, int, int); | |
| 95 | +static PyObject * ext_do_call(PyObject *, PyObject ***, int, int, int); | |
| 96 | +static PyObject * update_keyword_args(PyObject *, int, PyObject ***,PyObject *); | |
| 97 | +static PyObject * update_star_args(int, int, PyObject *, PyObject ***); | |
| 98 | +static PyObject * load_args(PyObject ***, int); | |
| 99 | +#define CALL_FLAG_VAR 1 | |
| 100 | +#define CALL_FLAG_KW 2 | |
| 101 | + | |
| 102 | +#ifdef LLTRACE | |
| 103 | +static int lltrace; | |
| 104 | +static int prtrace(PyObject *, char *); | |
| 105 | +#endif | |
| 106 | +static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *, | |
| 107 | + int, PyObject *); | |
| 108 | +static void call_trace_protected(Py_tracefunc, PyObject *, | |
| 109 | + PyFrameObject *, int, PyObject *); | |
| 110 | +static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *); | |
| 111 | +static int maybe_call_line_trace(Py_tracefunc, PyObject *, | |
| 112 | + PyFrameObject *, int *, int *, int *); | |
| 113 | + | |
| 114 | +static PyObject * apply_slice(PyObject *, PyObject *, PyObject *); | |
| 115 | +static int assign_slice(PyObject *, PyObject *, | |
| 116 | + PyObject *, PyObject *); | |
| 117 | +static PyObject * cmp_outcome(int, PyObject *, PyObject *); | |
| 118 | +static PyObject * import_from(PyObject *, PyObject *); | |
| 119 | +static int import_all_from(PyObject *, PyObject *); | |
| 120 | +static PyObject * build_class(PyObject *, PyObject *, PyObject *); | |
| 121 | +static int exec_statement(PyFrameObject *, | |
| 122 | + PyObject *, PyObject *, PyObject *); | |
| 123 | +static void set_exc_info(PyThreadState *, PyObject *, PyObject *, PyObject *); | |
| 124 | +static void reset_exc_info(PyThreadState *); | |
| 125 | +static void format_exc_check_arg(PyObject *, char *, PyObject *); | |
| 126 | +static PyObject * string_concatenate(PyObject *, PyObject *, | |
| 127 | + PyFrameObject *, unsigned char *); | |
| 128 | + | |
| 129 | +#define NAME_ERROR_MSG \ | |
| 130 | + "name '%.200s' is not defined" | |
| 131 | +#define GLOBAL_NAME_ERROR_MSG \ | |
| 132 | + "global name '%.200s' is not defined" | |
| 133 | +#define UNBOUNDLOCAL_ERROR_MSG \ | |
| 134 | + "local variable '%.200s' referenced before assignment" | |
| 135 | +#define UNBOUNDFREE_ERROR_MSG \ | |
| 136 | + "free variable '%.200s' referenced before assignment" \ | |
| 137 | + " in enclosing scope" | |
| 138 | + | |
| 139 | +/* Dynamic execution profile */ | |
| 140 | +#ifdef DYNAMIC_EXECUTION_PROFILE | |
| 141 | +#ifdef DXPAIRS | |
| 142 | +static long dxpairs[257][256]; | |
| 143 | +#define dxp dxpairs[256] | |
| 144 | +#else | |
| 145 | +static long dxp[256]; | |
| 146 | +#endif | |
| 147 | +#endif | |
| 148 | + | |
| 149 | +/* Function call profile */ | |
| 150 | +#ifdef CALL_PROFILE | |
| 151 | +#define PCALL_NUM 11 | |
| 152 | +static int pcall[PCALL_NUM]; | |
| 153 | + | |
| 154 | +#define PCALL_ALL 0 | |
| 155 | +#define PCALL_FUNCTION 1 | |
| 156 | +#define PCALL_FAST_FUNCTION 2 | |
| 157 | +#define PCALL_FASTER_FUNCTION 3 | |
| 158 | +#define PCALL_METHOD 4 | |
| 159 | +#define PCALL_BOUND_METHOD 5 | |
| 160 | +#define PCALL_CFUNCTION 6 | |
| 161 | +#define PCALL_TYPE 7 | |
| 162 | +#define PCALL_GENERATOR 8 | |
| 163 | +#define PCALL_OTHER 9 | |
| 164 | +#define PCALL_POP 10 | |
| 165 | + | |
| 166 | +/* Notes about the statistics | |
| 167 | + | |
| 168 | + PCALL_FAST stats | |
| 169 | + | |
| 170 | + FAST_FUNCTION means no argument tuple needs to be created. | |
| 171 | + FASTER_FUNCTION means that the fast-path frame setup code is used. | |
| 172 | + | |
| 173 | + If there is a method call where the call can be optimized by changing | |
| 174 | + the argument tuple and calling the function directly, it gets recorded | |
| 175 | + twice. | |
| 176 | + | |
| 177 | + As a result, the relationship among the statistics appears to be | |
| 178 | + PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD + | |
| 179 | + PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER | |
| 180 | + PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION | |
| 181 | + PCALL_METHOD > PCALL_BOUND_METHOD | |
| 182 | +*/ | |
| 183 | + | |
| 184 | +#define PCALL(POS) pcall[POS]++ | |
| 185 | + | |
| 186 | +PyObject * | |
| 187 | +PyEval_GetCallStats(PyObject *self) | |
| 188 | +{ | |
| 189 | + return Py_BuildValue("iiiiiiiiiii", | |
| 190 | + pcall[0], pcall[1], pcall[2], pcall[3], | |
| 191 | + pcall[4], pcall[5], pcall[6], pcall[7], | |
| 192 | + pcall[8], pcall[9], pcall[10]); | |
| 193 | +} | |
| 194 | +#else | |
| 195 | +#define PCALL(O) | |
| 196 | + | |
| 197 | +PyObject * | |
| 198 | +PyEval_GetCallStats(PyObject *self) | |
| 199 | +{ | |
| 200 | + Py_INCREF(Py_None); | |
| 201 | + return Py_None; | |
| 202 | +} | |
| 203 | +#endif | |
| 204 | + | |
| 205 | + | |
| 206 | +#ifdef WITH_THREAD | |
| 207 | + | |
| 208 | +#ifdef HAVE_ERRNO_H | |
| 209 | +#include <errno.h> | |
| 210 | +#endif | |
| 211 | +#include "pythread.h" | |
| 212 | + | |
| 213 | +static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */ | |
| 214 | +static long main_thread = 0; | |
| 215 | + | |
| 216 | +int | |
| 217 | +PyEval_ThreadsInitialized(void) | |
| 218 | +{ | |
| 219 | + return interpreter_lock != 0; | |
| 220 | +} | |
| 221 | + | |
| 222 | +void | |
| 223 | +PyEval_InitThreads(void) | |
| 224 | +{ | |
| 225 | + if (interpreter_lock) | |
| 226 | + return; | |
| 227 | + interpreter_lock = PyThread_allocate_lock(); | |
| 228 | + PyThread_acquire_lock(interpreter_lock, 1); | |
| 229 | + main_thread = PyThread_get_thread_ident(); | |
| 230 | +} | |
| 231 | + | |
| 232 | +void | |
| 233 | +PyEval_AcquireLock(void) | |
| 234 | +{ | |
| 235 | + PyThread_acquire_lock(interpreter_lock, 1); | |
| 236 | +} | |
| 237 | + | |
| 238 | +void | |
| 239 | +PyEval_ReleaseLock(void) | |
| 240 | +{ | |
| 241 | + PyThread_release_lock(interpreter_lock); | |
| 242 | +} | |
| 243 | + | |
| 244 | +void | |
| 245 | +PyEval_AcquireThread(PyThreadState *tstate) | |
| 246 | +{ | |
| 247 | + if (tstate == NULL) | |
| 248 | + Py_FatalError("PyEval_AcquireThread: NULL new thread state"); | |
| 249 | + /* Check someone has called PyEval_InitThreads() to create the lock */ | |
| 250 | + assert(interpreter_lock); | |
| 251 | + PyThread_acquire_lock(interpreter_lock, 1); | |
| 252 | + if (PyThreadState_Swap(tstate) != NULL) | |
| 253 | + Py_FatalError( | |
| 254 | + "PyEval_AcquireThread: non-NULL old thread state"); | |
| 255 | +} | |
| 256 | + | |
| 257 | +void | |
| 258 | +PyEval_ReleaseThread(PyThreadState *tstate) | |
| 259 | +{ | |
| 260 | + if (tstate == NULL) | |
| 261 | + Py_FatalError("PyEval_ReleaseThread: NULL thread state"); | |
| 262 | + if (PyThreadState_Swap(NULL) != tstate) | |
| 263 | + Py_FatalError("PyEval_ReleaseThread: wrong thread state"); | |
| 264 | + PyThread_release_lock(interpreter_lock); | |
| 265 | +} | |
| 266 | + | |
| 267 | +/* This function is called from PyOS_AfterFork to ensure that newly | |
| 268 | + created child processes don't hold locks referring to threads which | |
| 269 | + are not running in the child process. (This could also be done using | |
| 270 | + pthread_atfork mechanism, at least for the pthreads implementation.) */ | |
| 271 | + | |
| 272 | +void | |
| 273 | +PyEval_ReInitThreads(void) | |
| 274 | +{ | |
| 275 | + if (!interpreter_lock) | |
| 276 | + return; | |
| 277 | + /*XXX Can't use PyThread_free_lock here because it does too | |
| 278 | + much error-checking. Doing this cleanly would require | |
| 279 | + adding a new function to each thread_*.h. Instead, just | |
| 280 | + create a new lock and waste a little bit of memory */ | |
| 281 | + interpreter_lock = PyThread_allocate_lock(); | |
| 282 | + PyThread_acquire_lock(interpreter_lock, 1); | |
| 283 | + main_thread = PyThread_get_thread_ident(); | |
| 284 | +} | |
| 285 | +#endif | |
| 286 | + | |
| 287 | +/* Functions save_thread and restore_thread are always defined so | |
| 288 | + dynamically loaded modules needn't be compiled separately for use | |
| 289 | + with and without threads: */ | |
| 290 | + | |
| 291 | +PyThreadState * | |
| 292 | +PyEval_SaveThread(void) | |
| 293 | +{ | |
| 294 | + PyThreadState *tstate = PyThreadState_Swap(NULL); | |
| 295 | + if (tstate == NULL) | |
| 296 | + Py_FatalError("PyEval_SaveThread: NULL tstate"); | |
| 297 | +#ifdef WITH_THREAD | |
| 298 | + if (interpreter_lock) | |
| 299 | + PyThread_release_lock(interpreter_lock); | |
| 300 | +#endif | |
| 301 | + return tstate; | |
| 302 | +} | |
| 303 | + | |
| 304 | +void | |
| 305 | +PyEval_RestoreThread(PyThreadState *tstate) | |
| 306 | +{ | |
| 307 | + if (tstate == NULL) | |
| 308 | + Py_FatalError("PyEval_RestoreThread: NULL tstate"); | |
| 309 | +#ifdef WITH_THREAD | |
| 310 | + if (interpreter_lock) { | |
| 311 | + int err = errno; | |
| 312 | + PyThread_acquire_lock(interpreter_lock, 1); | |
| 313 | + errno = err; | |
| 314 | + } | |
| 315 | +#endif | |
| 316 | + PyThreadState_Swap(tstate); | |
| 317 | +} | |
| 318 | + | |
| 319 | + | |
| 320 | +/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX | |
| 321 | + signal handlers or Mac I/O completion routines) can schedule calls | |
| 322 | + to a function to be called synchronously. | |
| 323 | + The synchronous function is called with one void* argument. | |
| 324 | + It should return 0 for success or -1 for failure -- failure should | |
| 325 | + be accompanied by an exception. | |
| 326 | + | |
| 327 | + If registry succeeds, the registry function returns 0; if it fails | |
| 328 | + (e.g. due to too many pending calls) it returns -1 (without setting | |
| 329 | + an exception condition). | |
| 330 | + | |
| 331 | + Note that because registry may occur from within signal handlers, | |
| 332 | + or other asynchronous events, calling malloc() is unsafe! | |
| 333 | + | |
| 334 | +#ifdef WITH_THREAD | |
| 335 | + Any thread can schedule pending calls, but only the main thread | |
| 336 | + will execute them. | |
| 337 | +#endif | |
| 338 | + | |
| 339 | + XXX WARNING! ASYNCHRONOUSLY EXECUTING CODE! | |
| 340 | + There are two possible race conditions: | |
| 341 | + (1) nested asynchronous registry calls; | |
| 342 | + (2) registry calls made while pending calls are being processed. | |
| 343 | + While (1) is very unlikely, (2) is a real possibility. | |
| 344 | + The current code is safe against (2), but not against (1). | |
| 345 | + The safety against (2) is derived from the fact that only one | |
| 346 | + thread (the main thread) ever takes things out of the queue. | |
| 347 | + | |
| 348 | + XXX Darn! With the advent of thread state, we should have an array | |
| 349 | + of pending calls per thread in the thread state! Later... | |
| 350 | +*/ | |
| 351 | + | |
| 352 | +#define NPENDINGCALLS 32 | |
| 353 | +static struct { | |
| 354 | + int (*func)(void *); | |
| 355 | + void *arg; | |
| 356 | +} pendingcalls[NPENDINGCALLS]; | |
| 357 | +static volatile int pendingfirst = 0; | |
| 358 | +static volatile int pendinglast = 0; | |
| 359 | +static volatile int things_to_do = 0; | |
| 360 | + | |
| 361 | +int | |
| 362 | +Py_AddPendingCall(int (*func)(void *), void *arg) | |
| 363 | +{ | |
| 364 | + static int busy = 0; | |
| 365 | + int i, j; | |
| 366 | + /* XXX Begin critical section */ | |
| 367 | + /* XXX If you want this to be safe against nested | |
| 368 | + XXX asynchronous calls, you'll have to work harder! */ | |
| 369 | + if (busy) | |
| 370 | + return -1; | |
| 371 | + busy = 1; | |
| 372 | + i = pendinglast; | |
| 373 | + j = (i + 1) % NPENDINGCALLS; | |
| 374 | + if (j == pendingfirst) { | |
| 375 | + busy = 0; | |
| 376 | + return -1; /* Queue full */ | |
| 377 | + } | |
| 378 | + pendingcalls[i].func = func; | |
| 379 | + pendingcalls[i].arg = arg; | |
| 380 | + pendinglast = j; | |
| 381 | + | |
| 382 | + _Py_Ticker = 0; | |
| 383 | + things_to_do = 1; /* Signal main loop */ | |
| 384 | + busy = 0; | |
| 385 | + /* XXX End critical section */ | |
| 386 | + return 0; | |
| 387 | +} | |
| 388 | + | |
| 389 | +int | |
| 390 | +Py_MakePendingCalls(void) | |
| 391 | +{ | |
| 392 | + static int busy = 0; | |
| 393 | +#ifdef WITH_THREAD | |
| 394 | + if (main_thread && PyThread_get_thread_ident() != main_thread) | |
| 395 | + return 0; | |
| 396 | +#endif | |
| 397 | + if (busy) | |
| 398 | + return 0; | |
| 399 | + busy = 1; | |
| 400 | + things_to_do = 0; | |
| 401 | + for (;;) { | |
| 402 | + int i; | |
| 403 | + int (*func)(void *); | |
| 404 | + void *arg; | |
| 405 | + i = pendingfirst; | |
| 406 | + if (i == pendinglast) | |
| 407 | + break; /* Queue empty */ | |
| 408 | + func = pendingcalls[i].func; | |
| 409 | + arg = pendingcalls[i].arg; | |
| 410 | + pendingfirst = (i + 1) % NPENDINGCALLS; | |
| 411 | + if (func(arg) < 0) { | |
| 412 | + busy = 0; | |
| 413 | + things_to_do = 1; /* We're not done yet */ | |
| 414 | + return -1; | |
| 415 | + } | |
| 416 | + } | |
| 417 | + busy = 0; | |
| 418 | + return 0; | |
| 419 | +} | |
| 420 | + | |
| 421 | + | |
| 422 | +/* The interpreter's recursion limit */ | |
| 423 | + | |
| 424 | +#ifndef Py_DEFAULT_RECURSION_LIMIT | |
| 425 | +#define Py_DEFAULT_RECURSION_LIMIT 1000 | |
| 426 | +#endif | |
| 427 | +static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT; | |
| 428 | +int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT; | |
| 429 | + | |
| 430 | +int | |
| 431 | +Py_GetRecursionLimit(void) | |
| 432 | +{ | |
| 433 | + return recursion_limit; | |
| 434 | +} | |
| 435 | + | |
| 436 | +void | |
| 437 | +Py_SetRecursionLimit(int new_limit) | |
| 438 | +{ | |
| 439 | + recursion_limit = new_limit; | |
| 440 | + _Py_CheckRecursionLimit = recursion_limit; | |
| 441 | +} | |
| 442 | + | |
| 443 | +/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall() | |
| 444 | + if the recursion_depth reaches _Py_CheckRecursionLimit. | |
| 445 | + If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit | |
| 446 | + to guarantee that _Py_CheckRecursiveCall() is regularly called. | |
| 447 | + Without USE_STACKCHECK, there is no need for this. */ | |
| 448 | +int | |
| 449 | +_Py_CheckRecursiveCall(char *where) | |
| 450 | +{ | |
| 451 | + PyThreadState *tstate = PyThreadState_GET(); | |
| 452 | + | |
| 453 | +#ifdef USE_STACKCHECK | |
| 454 | + if (PyOS_CheckStack()) { | |
| 455 | + --tstate->recursion_depth; | |
| 456 | + PyErr_SetString(PyExc_MemoryError, "Stack overflow"); | |
| 457 | + return -1; | |
| 458 | + } | |
| 459 | +#endif | |
| 460 | + if (tstate->recursion_depth > recursion_limit) { | |
| 461 | + --tstate->recursion_depth; | |
| 462 | + PyErr_Format(PyExc_RuntimeError, | |
| 463 | + "maximum recursion depth exceeded%s", | |
| 464 | + where); | |
| 465 | + return -1; | |
| 466 | + } | |
| 467 | + _Py_CheckRecursionLimit = recursion_limit; | |
| 468 | + return 0; | |
| 469 | +} | |
| 470 | + | |
| 471 | +/* Status code for main loop (reason for stack unwind) */ | |
| 472 | +enum why_code { | |
| 473 | + WHY_NOT = 0x0001, /* No error */ | |
| 474 | + WHY_EXCEPTION = 0x0002, /* Exception occurred */ | |
| 475 | + WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */ | |
| 476 | + WHY_RETURN = 0x0008, /* 'return' statement */ | |
| 477 | + WHY_BREAK = 0x0010, /* 'break' statement */ | |
| 478 | + WHY_CONTINUE = 0x0020, /* 'continue' statement */ | |
| 479 | + WHY_YIELD = 0x0040 /* 'yield' operator */ | |
| 480 | +}; | |
| 481 | + | |
| 482 | +static enum why_code do_raise(PyObject *, PyObject *, PyObject *); | |
| 483 | +static int unpack_iterable(PyObject *, int, PyObject **); | |
| 484 | + | |
| 485 | +/* for manipulating the thread switch and periodic "stuff" - used to be | |
| 486 | + per thread, now just a pair o' globals */ | |
| 487 | +int _Py_CheckInterval = 100; | |
| 488 | +volatile int _Py_Ticker = 100; | |
| 489 | + | |
| 490 | +PyObject * | |
| 491 | +PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals) | |
| 492 | +{ | |
| 493 | + /* XXX raise SystemError if globals is NULL */ | |
| 494 | + return PyEval_EvalCodeEx(co, | |
| 495 | + globals, locals, | |
| 496 | + (PyObject **)NULL, 0, | |
| 497 | + (PyObject **)NULL, 0, | |
| 498 | + (PyObject **)NULL, 0, | |
| 499 | + NULL); | |
| 500 | +} | |
| 501 | + | |
| 502 | + | |
| 503 | +/* Interpreter main loop */ | |
| 504 | + | |
| 505 | +PyObject * | |
| 506 | +PyEval_EvalFrame(PyFrameObject *f) { | |
| 507 | + /* This is for backward compatibility with extension modules that | |
| 508 | + used this API; core interpreter code should call PyEval_EvalFrameEx() */ | |
| 509 | + return PyEval_EvalFrameEx(f, 0); | |
| 510 | +} | |
| 511 | + | |
| 512 | +PyObject * | |
| 513 | +PyEval_EvalFrameEx(PyFrameObject *f, int throwflag) | |
| 514 | +{ | |
| 515 | +#ifdef DXPAIRS | |
| 516 | + int lastopcode = 0; | |
| 517 | +#endif | |
| 518 | + register PyObject **stack_pointer; /* Next free slot in value stack */ | |
| 519 | + register unsigned char *next_instr; | |
| 520 | + register int opcode; /* Current opcode */ | |
| 521 | + register int oparg; /* Current opcode argument, if any */ | |
| 522 | + register enum why_code why; /* Reason for block stack unwind */ | |
| 523 | + register int err; /* Error status -- nonzero if error */ | |
| 524 | + register PyObject *x; /* Result object -- NULL if error */ | |
| 525 | + register PyObject *v; /* Temporary objects popped off stack */ | |
| 526 | + register PyObject *w; | |
| 527 | + register PyObject *u; | |
| 528 | + register PyObject *t; | |
| 529 | + register PyObject *stream = NULL; /* for PRINT opcodes */ | |
| 530 | + register PyObject **fastlocals, **freevars; | |
| 531 | + PyObject *retval = NULL; /* Return value */ | |
| 532 | + PyThreadState *tstate = PyThreadState_GET(); | |
| 533 | + PyCodeObject *co; | |
| 534 | + | |
| 535 | + /* when tracing we set things up so that | |
| 536 | + | |
| 537 | + not (instr_lb <= current_bytecode_offset < instr_ub) | |
| 538 | + | |
| 539 | + is true when the line being executed has changed. The | |
| 540 | + initial values are such as to make this false the first | |
| 541 | + time it is tested. */ | |
| 542 | + int instr_ub = -1, instr_lb = 0, instr_prev = -1; | |
| 543 | + | |
| 544 | + unsigned char *first_instr; | |
| 545 | + PyObject *names; | |
| 546 | + PyObject *consts; | |
| 547 | +#if defined(Py_DEBUG) || defined(LLTRACE) | |
| 548 | + /* Make it easier to find out where we are with a debugger */ | |
| 549 | + char *filename; | |
| 550 | +#endif | |
| 551 | + | |
| 552 | +/* Tuple access macros */ | |
| 553 | + | |
| 554 | +#ifndef Py_DEBUG | |
| 555 | +#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i)) | |
| 556 | +#else | |
| 557 | +#define GETITEM(v, i) PyTuple_GetItem((v), (i)) | |
| 558 | +#endif | |
| 559 | + | |
| 560 | +#ifdef WITH_TSC | |
| 561 | +/* Use Pentium timestamp counter to mark certain events: | |
| 562 | + inst0 -- beginning of switch statement for opcode dispatch | |
| 563 | + inst1 -- end of switch statement (may be skipped) | |
| 564 | + loop0 -- the top of the mainloop | |
| 565 | + loop1 -- place where control returns again to top of mainloop | |
| 566 | + (may be skipped) | |
| 567 | + intr1 -- beginning of long interruption | |
| 568 | + intr2 -- end of long interruption | |
| 569 | + | |
| 570 | + Many opcodes call out to helper C functions. In some cases, the | |
| 571 | + time in those functions should be counted towards the time for the | |
| 572 | + opcode, but not in all cases. For example, a CALL_FUNCTION opcode | |
| 573 | + calls another Python function; there's no point in charge all the | |
| 574 | + bytecode executed by the called function to the caller. | |
| 575 | + | |
| 576 | + It's hard to make a useful judgement statically. In the presence | |
| 577 | + of operator overloading, it's impossible to tell if a call will | |
| 578 | + execute new Python code or not. | |
| 579 | + | |
| 580 | + It's a case-by-case judgement. I'll use intr1 for the following | |
| 581 | + cases: | |
| 582 | + | |
| 583 | + EXEC_STMT | |
| 584 | + IMPORT_STAR | |
| 585 | + IMPORT_FROM | |
| 586 | + CALL_FUNCTION (and friends) | |
| 587 | + | |
| 588 | + */ | |
| 589 | + uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0; | |
| 590 | + int ticked = 0; | |
| 591 | + | |
| 592 | + READ_TIMESTAMP(inst0); | |
| 593 | + READ_TIMESTAMP(inst1); | |
| 594 | + READ_TIMESTAMP(loop0); | |
| 595 | + READ_TIMESTAMP(loop1); | |
| 596 | + | |
| 597 | + /* shut up the compiler */ | |
| 598 | + opcode = 0; | |
| 599 | +#endif | |
| 600 | + | |
| 601 | +/* Code access macros */ | |
| 602 | + | |
| 603 | +#define INSTR_OFFSET() ((int)(next_instr - first_instr)) | |
| 604 | +#define NEXTOP() (*next_instr++) | |
| 605 | +#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2]) | |
| 606 | +#define PEEKARG() ((next_instr[2]<<8) + next_instr[1]) | |
| 607 | +#define JUMPTO(x) (next_instr = first_instr + (x)) | |
| 608 | +#define JUMPBY(x) (next_instr += (x)) | |
| 609 | + | |
| 610 | +/* OpCode prediction macros | |
| 611 | + Some opcodes tend to come in pairs thus making it possible to predict | |
| 612 | + the second code when the first is run. For example, COMPARE_OP is often | |
| 613 | + followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And, those opcodes are often | |
| 614 | + followed by a POP_TOP. | |
| 615 | + | |
| 616 | + Verifying the prediction costs a single high-speed test of register | |
| 617 | + variable against a constant. If the pairing was good, then the | |
| 618 | + processor has a high likelihood of making its own successful branch | |
| 619 | + prediction which results in a nearly zero overhead transition to the | |
| 620 | + next opcode. | |
| 621 | + | |
| 622 | + A successful prediction saves a trip through the eval-loop including | |
| 623 | + its two unpredictable branches, the HASARG test and the switch-case. | |
| 624 | + | |
| 625 | + If collecting opcode statistics, turn off prediction so that | |
| 626 | + statistics are accurately maintained (the predictions bypass | |
| 627 | + the opcode frequency counter updates). | |
| 628 | +*/ | |
| 629 | + | |
| 630 | +#ifdef DYNAMIC_EXECUTION_PROFILE | |
| 631 | +#define PREDICT(op) if (0) goto PRED_##op | |
| 632 | +#else | |
| 633 | +#define PREDICT(op) if (*next_instr == op) goto PRED_##op | |
| 634 | +#endif | |
| 635 | + | |
| 636 | +#define PREDICTED(op) PRED_##op: next_instr++ | |
| 637 | +#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3 | |
| 638 | + | |
| 639 | +/* Stack manipulation macros */ | |
| 640 | + | |
| 641 | +/* The stack can grow at most MAXINT deep, as co_nlocals and | |
| 642 | + co_stacksize are ints. */ | |
| 643 | +#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack)) | |
| 644 | +#define EMPTY() (STACK_LEVEL() == 0) | |
| 645 | +#define TOP() (stack_pointer[-1]) | |
| 646 | +#define SECOND() (stack_pointer[-2]) | |
| 647 | +#define THIRD() (stack_pointer[-3]) | |
| 648 | +#define FOURTH() (stack_pointer[-4]) | |
| 649 | +#define SET_TOP(v) (stack_pointer[-1] = (v)) | |
| 650 | +#define SET_SECOND(v) (stack_pointer[-2] = (v)) | |
| 651 | +#define SET_THIRD(v) (stack_pointer[-3] = (v)) | |
| 652 | +#define SET_FOURTH(v) (stack_pointer[-4] = (v)) | |
| 653 | +#define BASIC_STACKADJ(n) (stack_pointer += n) | |
| 654 | +#define BASIC_PUSH(v) (*stack_pointer++ = (v)) | |
| 655 | +#define BASIC_POP() (*--stack_pointer) | |
| 656 | + | |
| 657 | +#ifdef LLTRACE | |
| 658 | +#define PUSH(v) { (void)(BASIC_PUSH(v), \ | |
| 659 | + lltrace && prtrace(TOP(), "push")); \ | |
| 660 | + assert(STACK_LEVEL() <= co->co_stacksize); } | |
| 661 | +#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), BASIC_POP()) | |
| 662 | +#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \ | |
| 663 | + lltrace && prtrace(TOP(), "stackadj")); \ | |
| 664 | + assert(STACK_LEVEL() <= co->co_stacksize); } | |
| 665 | +#define EXT_POP(STACK_POINTER) (lltrace && prtrace(*(STACK_POINTER), "ext_pop"), *--(STACK_POINTER)) | |
| 666 | +#else | |
| 667 | +#define PUSH(v) BASIC_PUSH(v) | |
| 668 | +#define POP() BASIC_POP() | |
| 669 | +#define STACKADJ(n) BASIC_STACKADJ(n) | |
| 670 | +#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER)) | |
| 671 | +#endif | |
| 672 | + | |
| 673 | +/* Local variable macros */ | |
| 674 | + | |
| 675 | +#define GETLOCAL(i) (fastlocals[i]) | |
| 676 | + | |
| 677 | +/* The SETLOCAL() macro must not DECREF the local variable in-place and | |
| 678 | + then store the new value; it must copy the old value to a temporary | |
| 679 | + value, then store the new value, and then DECREF the temporary value. | |
| 680 | + This is because it is possible that during the DECREF the frame is | |
| 681 | + accessed by other code (e.g. a __del__ method or gc.collect()) and the | |
| 682 | + variable would be pointing to already-freed memory. */ | |
| 683 | +#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \ | |
| 684 | + GETLOCAL(i) = value; \ | |
| 685 | + Py_XDECREF(tmp); } while (0) | |
| 686 | + | |
| 687 | +/* Start of code */ | |
| 688 | + | |
| 689 | + if (f == NULL) | |
| 690 | + return NULL; | |
| 691 | + | |
| 692 | + /* push frame */ | |
| 693 | + if (Py_EnterRecursiveCall("")) | |
| 694 | + return NULL; | |
| 695 | + | |
| 696 | + tstate->frame = f; | |
| 697 | + | |
| 698 | + if (tstate->use_tracing) { | |
| 699 | + if (tstate->c_tracefunc != NULL) { | |
| 700 | + /* tstate->c_tracefunc, if defined, is a | |
| 701 | + function that will be called on *every* entry | |
| 702 | + to a code block. Its return value, if not | |
| 703 | + None, is a function that will be called at | |
| 704 | + the start of each executed line of code. | |
| 705 | + (Actually, the function must return itself | |
| 706 | + in order to continue tracing.) The trace | |
| 707 | + functions are called with three arguments: | |
| 708 | + a pointer to the current frame, a string | |
| 709 | + indicating why the function is called, and | |
| 710 | + an argument which depends on the situation. | |
| 711 | + The global trace function is also called | |
| 712 | + whenever an exception is detected. */ | |
| 713 | + if (call_trace(tstate->c_tracefunc, tstate->c_traceobj, | |
| 714 | + f, PyTrace_CALL, Py_None)) { | |
| 715 | + /* Trace function raised an error */ | |
| 716 | + goto exit_eval_frame; | |
| 717 | + } | |
| 718 | + } | |
| 719 | + if (tstate->c_profilefunc != NULL) { | |
| 720 | + /* Similar for c_profilefunc, except it needn't | |
| 721 | + return itself and isn't called for "line" events */ | |
| 722 | + if (call_trace(tstate->c_profilefunc, | |
| 723 | + tstate->c_profileobj, | |
| 724 | + f, PyTrace_CALL, Py_None)) { | |
| 725 | + /* Profile function raised an error */ | |
| 726 | + goto exit_eval_frame; | |
| 727 | + } | |
| 728 | + } | |
| 729 | + } | |
| 730 | + | |
| 731 | + co = f->f_code; | |
| 732 | + names = co->co_names; | |
| 733 | + consts = co->co_consts; | |
| 734 | + fastlocals = f->f_localsplus; | |
| 735 | + freevars = f->f_localsplus + co->co_nlocals; | |
| 736 | + first_instr = (unsigned char*) PyString_AS_STRING(co->co_code); | |
| 737 | + /* An explanation is in order for the next line. | |
| 738 | + | |
| 739 | + f->f_lasti now refers to the index of the last instruction | |
| 740 | + executed. You might think this was obvious from the name, but | |
| 741 | + this wasn't always true before 2.3! PyFrame_New now sets | |
| 742 | + f->f_lasti to -1 (i.e. the index *before* the first instruction) | |
| 743 | + and YIELD_VALUE doesn't fiddle with f_lasti any more. So this | |
| 744 | + does work. Promise. */ | |
| 745 | + next_instr = first_instr + f->f_lasti + 1; | |
| 746 | + stack_pointer = f->f_stacktop; | |
| 747 | + assert(stack_pointer != NULL); | |
| 748 | + f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */ | |
| 749 | + | |
| 750 | +#ifdef LLTRACE | |
| 751 | + lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL; | |
| 752 | +#endif | |
| 753 | +#if defined(Py_DEBUG) || defined(LLTRACE) | |
| 754 | + filename = PyString_AsString(co->co_filename); | |
| 755 | +#endif | |
| 756 | + | |
| 757 | + why = WHY_NOT; | |
| 758 | + err = 0; | |
| 759 | + x = Py_None; /* Not a reference, just anything non-NULL */ | |
| 760 | + w = NULL; | |
| 761 | + | |
| 762 | + if (throwflag) { /* support for generator.throw() */ | |
| 763 | + why = WHY_EXCEPTION; | |
| 764 | + goto on_error; | |
| 765 | + } | |
| 766 | + | |
| 767 | + for (;;) { | |
| 768 | +#ifdef WITH_TSC | |
| 769 | + if (inst1 == 0) { | |
| 770 | + /* Almost surely, the opcode executed a break | |
| 771 | + or a continue, preventing inst1 from being set | |
| 772 | + on the way out of the loop. | |
| 773 | + */ | |
| 774 | + READ_TIMESTAMP(inst1); | |
| 775 | + loop1 = inst1; | |
| 776 | + } | |
| 777 | + dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1, | |
| 778 | + intr0, intr1); | |
| 779 | + ticked = 0; | |
| 780 | + inst1 = 0; | |
| 781 | + intr0 = 0; | |
| 782 | + intr1 = 0; | |
| 783 | + READ_TIMESTAMP(loop0); | |
| 784 | +#endif | |
| 785 | + assert(stack_pointer >= f->f_valuestack); /* else underflow */ | |
| 786 | + assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */ | |
| 787 | + | |
| 788 | + /* Do periodic things. Doing this every time through | |
| 789 | + the loop would add too much overhead, so we do it | |
| 790 | + only every Nth instruction. We also do it if | |
| 791 | + ``things_to_do'' is set, i.e. when an asynchronous | |
| 792 | + event needs attention (e.g. a signal handler or | |
| 793 | + async I/O handler); see Py_AddPendingCall() and | |
| 794 | + Py_MakePendingCalls() above. */ | |
| 795 | + | |
| 796 | + if (--_Py_Ticker < 0) { | |
| 797 | + if (*next_instr == SETUP_FINALLY) { | |
| 798 | + /* Make the last opcode before | |
| 799 | + a try: finally: block uninterruptable. */ | |
| 800 | + goto fast_next_opcode; | |
| 801 | + } | |
| 802 | + _Py_Ticker = _Py_CheckInterval; | |
| 803 | + tstate->tick_counter++; | |
| 804 | +#ifdef WITH_TSC | |
| 805 | + ticked = 1; | |
| 806 | +#endif | |
| 807 | + if (things_to_do) { | |
| 808 | + if (Py_MakePendingCalls() < 0) { | |
| 809 | + why = WHY_EXCEPTION; | |
| 810 | + goto on_error; | |
| 811 | + } | |
| 812 | + if (things_to_do) | |
| 813 | + /* MakePendingCalls() didn't succeed. | |
| 814 | + Force early re-execution of this | |
| 815 | + "periodic" code, possibly after | |
| 816 | + a thread switch */ | |
| 817 | + _Py_Ticker = 0; | |
| 818 | + } | |
| 819 | +#ifdef WITH_THREAD | |
| 820 | + if (interpreter_lock) { | |
| 821 | + /* Give another thread a chance */ | |
| 822 | + | |
| 823 | + if (PyThreadState_Swap(NULL) != tstate) | |
| 824 | + Py_FatalError("ceval: tstate mix-up"); | |
| 825 | + PyThread_release_lock(interpreter_lock); | |
| 826 | + | |
| 827 | + /* Other threads may run now */ | |
| 828 | + | |
| 829 | + PyThread_acquire_lock(interpreter_lock, 1); | |
| 830 | + if (PyThreadState_Swap(tstate) != NULL) | |
| 831 | + Py_FatalError("ceval: orphan tstate"); | |
| 832 | + | |
| 833 | + /* Check for thread interrupts */ | |
| 834 | + | |
| 835 | + if (tstate->async_exc != NULL) { | |
| 836 | + x = tstate->async_exc; | |
| 837 | + tstate->async_exc = NULL; | |
| 838 | + PyErr_SetNone(x); | |
| 839 | + Py_DECREF(x); | |
| 840 | + why = WHY_EXCEPTION; | |
| 841 | + goto on_error; | |
| 842 | + } | |
| 843 | + } | |
| 844 | +#endif | |
| 845 | + } | |
| 846 | + | |
| 847 | + fast_next_opcode: | |
| 848 | + f->f_lasti = INSTR_OFFSET(); | |
| 849 | + | |
| 850 | + /* line-by-line tracing support */ | |
| 851 | + | |
| 852 | + if (tstate->c_tracefunc != NULL && !tstate->tracing) { | |
| 853 | + /* see maybe_call_line_trace | |
| 854 | + for expository comments */ | |
| 855 | + f->f_stacktop = stack_pointer; | |
| 856 | + | |
| 857 | + err = maybe_call_line_trace(tstate->c_tracefunc, | |
| 858 | + tstate->c_traceobj, | |
| 859 | + f, &instr_lb, &instr_ub, | |
| 860 | + &instr_prev); | |
| 861 | + /* Reload possibly changed frame fields */ | |
| 862 | + JUMPTO(f->f_lasti); | |
| 863 | + if (f->f_stacktop != NULL) { | |
| 864 | + stack_pointer = f->f_stacktop; | |
| 865 | + f->f_stacktop = NULL; | |
| 866 | + } | |
| 867 | + if (err) { | |
| 868 | + /* trace function raised an exception */ | |
| 869 | + goto on_error; | |
| 870 | + } | |
| 871 | + } | |
| 872 | + | |
| 873 | + /* Extract opcode and argument */ | |
| 874 | + | |
| 875 | + opcode = NEXTOP(); | |
| 876 | + oparg = 0; /* allows oparg to be stored in a register because | |
| 877 | + it doesn't have to be remembered across a full loop */ | |
| 878 | + if (HAS_ARG(opcode)) | |
| 879 | + oparg = NEXTARG(); | |
| 880 | + dispatch_opcode: | |
| 881 | +#ifdef DYNAMIC_EXECUTION_PROFILE | |
| 882 | +#ifdef DXPAIRS | |
| 883 | + dxpairs[lastopcode][opcode]++; | |
| 884 | + lastopcode = opcode; | |
| 885 | +#endif | |
| 886 | + dxp[opcode]++; | |
| 887 | +#endif | |
| 888 | + | |
| 889 | +#ifdef LLTRACE | |
| 890 | + /* Instruction tracing */ | |
| 891 | + | |
| 892 | + if (lltrace) { | |
| 893 | + if (HAS_ARG(opcode)) { | |
| 894 | + printf("%d: %d, %d\n", | |
| 895 | + f->f_lasti, opcode, oparg); | |
| 896 | + } | |
| 897 | + else { | |
| 898 | + printf("%d: %d\n", | |
| 899 | + f->f_lasti, opcode); | |
| 900 | + } | |
| 901 | + } | |
| 902 | +#endif | |
| 903 | + | |
| 904 | + /* Main switch on opcode */ | |
| 905 | + READ_TIMESTAMP(inst0); | |
| 906 | + | |
| 907 | + switch (opcode) { | |
| 908 | + | |
| 909 | + /* BEWARE! | |
| 910 | + It is essential that any operation that fails sets either | |
| 911 | + x to NULL, err to nonzero, or why to anything but WHY_NOT, | |
| 912 | + and that no operation that succeeds does this! */ | |
| 913 | + | |
| 914 | + /* case STOP_CODE: this is an error! */ | |
| 915 | + | |
| 916 | + case NOP: | |
| 917 | + goto fast_next_opcode; | |
| 918 | + | |
| 919 | + case LOAD_FAST: | |
| 920 | + x = GETLOCAL(oparg); | |
| 921 | + if (x != NULL) { | |
| 922 | + Py_INCREF(x); | |
| 923 | + PUSH(x); | |
| 924 | + goto fast_next_opcode; | |
| 925 | + } | |
| 926 | + format_exc_check_arg(PyExc_UnboundLocalError, | |
| 927 | + UNBOUNDLOCAL_ERROR_MSG, | |
| 928 | + PyTuple_GetItem(co->co_varnames, oparg)); | |
| 929 | + break; | |
| 930 | + | |
| 931 | + case LOAD_CONST: | |
| 932 | + x = GETITEM(consts, oparg); | |
| 933 | + Py_INCREF(x); | |
| 934 | + PUSH(x); | |
| 935 | + goto fast_next_opcode; | |
| 936 | + | |
| 937 | + PREDICTED_WITH_ARG(STORE_FAST); | |
| 938 | + case STORE_FAST: | |
| 939 | + v = POP(); | |
| 940 | + SETLOCAL(oparg, v); | |
| 941 | + goto fast_next_opcode; | |
| 942 | + | |
| 943 | + PREDICTED(POP_TOP); | |
| 944 | + case POP_TOP: | |
| 945 | + v = POP(); | |
| 946 | + Py_DECREF(v); | |
| 947 | + goto fast_next_opcode; | |
| 948 | + | |
| 949 | + case ROT_TWO: | |
| 950 | + v = TOP(); | |
| 951 | + w = SECOND(); | |
| 952 | + SET_TOP(w); | |
| 953 | + SET_SECOND(v); | |
| 954 | + goto fast_next_opcode; | |
| 955 | + | |
| 956 | + case ROT_THREE: | |
| 957 | + v = TOP(); | |
| 958 | + w = SECOND(); | |
| 959 | + x = THIRD(); | |
| 960 | + SET_TOP(w); | |
| 961 | + SET_SECOND(x); | |
| 962 | + SET_THIRD(v); | |
| 963 | + goto fast_next_opcode; | |
| 964 | + | |
| 965 | + case ROT_FOUR: | |
| 966 | + u = TOP(); | |
| 967 | + v = SECOND(); | |
| 968 | + w = THIRD(); | |
| 969 | + x = FOURTH(); | |
| 970 | + SET_TOP(v); | |
| 971 | + SET_SECOND(w); | |
| 972 | + SET_THIRD(x); | |
| 973 | + SET_FOURTH(u); | |
| 974 | + goto fast_next_opcode; | |
| 975 | + | |
| 976 | + case DUP_TOP: | |
| 977 | + v = TOP(); | |
| 978 | + Py_INCREF(v); | |
| 979 | + PUSH(v); | |
| 980 | + goto fast_next_opcode; | |
| 981 | + | |
| 982 | + case DUP_TOPX: | |
| 983 | + if (oparg == 2) { | |
| 984 | + x = TOP(); | |
| 985 | + Py_INCREF(x); | |
| 986 | + w = SECOND(); | |
| 987 | + Py_INCREF(w); | |
| 988 | + STACKADJ(2); | |
| 989 | + SET_TOP(x); | |
| 990 | + SET_SECOND(w); | |
| 991 | + goto fast_next_opcode; | |
| 992 | + } else if (oparg == 3) { | |
| 993 | + x = TOP(); | |
| 994 | + Py_INCREF(x); | |
| 995 | + w = SECOND(); | |
| 996 | + Py_INCREF(w); | |
| 997 | + v = THIRD(); | |
| 998 | + Py_INCREF(v); | |
| 999 | + STACKADJ(3); | |
| 1000 | + SET_TOP(x); | |
| 1001 | + SET_SECOND(w); | |
| 1002 | + SET_THIRD(v); | |
| 1003 | + goto fast_next_opcode; | |
| 1004 | + } | |
| 1005 | + Py_FatalError("invalid argument to DUP_TOPX" | |
| 1006 | + " (bytecode corruption?)"); | |
| 1007 | + break; | |
| 1008 | + | |
| 1009 | + case UNARY_POSITIVE: | |
| 1010 | + v = TOP(); | |
| 1011 | + x = PyNumber_Positive(v); | |
| 1012 | + Py_DECREF(v); | |
| 1013 | + SET_TOP(x); | |
| 1014 | + if (x != NULL) continue; | |
| 1015 | + break; | |
| 1016 | + | |
| 1017 | + case UNARY_NEGATIVE: | |
| 1018 | + v = TOP(); | |
| 1019 | + x = PyNumber_Negative(v); | |
| 1020 | + Py_DECREF(v); | |
| 1021 | + SET_TOP(x); | |
| 1022 | + if (x != NULL) continue; | |
| 1023 | + break; | |
| 1024 | + | |
| 1025 | + case UNARY_NOT: | |
| 1026 | + v = TOP(); | |
| 1027 | + err = PyObject_IsTrue(v); | |
| 1028 | + Py_DECREF(v); | |
| 1029 | + if (err == 0) { | |
| 1030 | + Py_INCREF(Py_True); | |
| 1031 | + SET_TOP(Py_True); | |
| 1032 | + continue; | |
| 1033 | + } | |
| 1034 | + else if (err > 0) { | |
| 1035 | + Py_INCREF(Py_False); | |
| 1036 | + SET_TOP(Py_False); | |
| 1037 | + err = 0; | |
| 1038 | + continue; | |
| 1039 | + } | |
| 1040 | + STACKADJ(-1); | |
| 1041 | + break; | |
| 1042 | + | |
| 1043 | + case UNARY_CONVERT: | |
| 1044 | + v = TOP(); | |
| 1045 | + x = PyObject_Repr(v); | |
| 1046 | + Py_DECREF(v); | |
| 1047 | + SET_TOP(x); | |
| 1048 | + if (x != NULL) continue; | |
| 1049 | + break; | |
| 1050 | + | |
| 1051 | + case UNARY_INVERT: | |
| 1052 | + v = TOP(); | |
| 1053 | + x = PyNumber_Invert(v); | |
| 1054 | + Py_DECREF(v); | |
| 1055 | + SET_TOP(x); | |
| 1056 | + if (x != NULL) continue; | |
| 1057 | + break; | |
| 1058 | + | |
| 1059 | + case BINARY_POWER: | |
| 1060 | + w = POP(); | |
| 1061 | + v = TOP(); | |
| 1062 | + x = PyNumber_Power(v, w, Py_None); | |
| 1063 | + Py_DECREF(v); | |
| 1064 | + Py_DECREF(w); | |
| 1065 | + SET_TOP(x); | |
| 1066 | + if (x != NULL) continue; | |
| 1067 | + break; | |
| 1068 | + | |
| 1069 | + case BINARY_MULTIPLY: | |
| 1070 | + w = POP(); | |
| 1071 | + v = TOP(); | |
| 1072 | + x = PyNumber_Multiply(v, w); | |
| 1073 | + Py_DECREF(v); | |
| 1074 | + Py_DECREF(w); | |
| 1075 | + SET_TOP(x); | |
| 1076 | + if (x != NULL) continue; | |
| 1077 | + break; | |
| 1078 | + | |
| 1079 | + case BINARY_DIVIDE: | |
| 1080 | + if (!_Py_QnewFlag) { | |
| 1081 | + w = POP(); | |
| 1082 | + v = TOP(); | |
| 1083 | + x = PyNumber_Divide(v, w); | |
| 1084 | + Py_DECREF(v); | |
| 1085 | + Py_DECREF(w); | |
| 1086 | + SET_TOP(x); | |
| 1087 | + if (x != NULL) continue; | |
| 1088 | + break; | |
| 1089 | + } | |
| 1090 | + /* -Qnew is in effect: fall through to | |
| 1091 | + BINARY_TRUE_DIVIDE */ | |
| 1092 | + case BINARY_TRUE_DIVIDE: | |
| 1093 | + w = POP(); | |
| 1094 | + v = TOP(); | |
| 1095 | + x = PyNumber_TrueDivide(v, w); | |
| 1096 | + Py_DECREF(v); | |
| 1097 | + Py_DECREF(w); | |
| 1098 | + SET_TOP(x); | |
| 1099 | + if (x != NULL) continue; | |
| 1100 | + break; | |
| 1101 | + | |
| 1102 | + case BINARY_FLOOR_DIVIDE: | |
| 1103 | + w = POP(); | |
| 1104 | + v = TOP(); | |
| 1105 | + x = PyNumber_FloorDivide(v, w); | |
| 1106 | + Py_DECREF(v); | |
| 1107 | + Py_DECREF(w); | |
| 1108 | + SET_TOP(x); | |
| 1109 | + if (x != NULL) continue; | |
| 1110 | + break; | |
| 1111 | + | |
| 1112 | + case BINARY_MODULO: | |
| 1113 | + w = POP(); | |
| 1114 | + v = TOP(); | |
| 1115 | + x = PyNumber_Remainder(v, w); | |
| 1116 | + Py_DECREF(v); | |
| 1117 | + Py_DECREF(w); | |
| 1118 | + SET_TOP(x); | |
| 1119 | + if (x != NULL) continue; | |
| 1120 | + break; | |
| 1121 | + | |
| 1122 | + case BINARY_ADD: | |
| 1123 | + w = POP(); | |
| 1124 | + v = TOP(); | |
| 1125 | + if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) { | |
| 1126 | + /* INLINE: int + int */ | |
| 1127 | + register long a, b, i; | |
| 1128 | + a = PyInt_AS_LONG(v); | |
| 1129 | + b = PyInt_AS_LONG(w); | |
| 1130 | + i = a + b; | |
| 1131 | + if ((i^a) < 0 && (i^b) < 0) | |
| 1132 | + goto slow_add; | |
| 1133 | + x = PyInt_FromLong(i); | |
| 1134 | + } | |
| 1135 | + else if (PyString_CheckExact(v) && | |
| 1136 | + PyString_CheckExact(w)) { | |
| 1137 | + x = string_concatenate(v, w, f, next_instr); | |
| 1138 | + /* string_concatenate consumed the ref to v */ | |
| 1139 | + goto skip_decref_vx; | |
| 1140 | + } | |
| 1141 | + else { | |
| 1142 | + slow_add: | |
| 1143 | + x = PyNumber_Add(v, w); | |
| 1144 | + } | |
| 1145 | + Py_DECREF(v); | |
| 1146 | + skip_decref_vx: | |
| 1147 | + Py_DECREF(w); | |
| 1148 | + SET_TOP(x); | |
| 1149 | + if (x != NULL) continue; | |
| 1150 | + break; | |
| 1151 | + | |
| 1152 | + case BINARY_SUBTRACT: | |
| 1153 | + w = POP(); | |
| 1154 | + v = TOP(); | |
| 1155 | + if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) { | |
| 1156 | + /* INLINE: int - int */ | |
| 1157 | + register long a, b, i; | |
| 1158 | + a = PyInt_AS_LONG(v); | |
| 1159 | + b = PyInt_AS_LONG(w); | |
| 1160 | + i = a - b; | |
| 1161 | + if ((i^a) < 0 && (i^~b) < 0) | |
| 1162 | + goto slow_sub; | |
| 1163 | + x = PyInt_FromLong(i); | |
| 1164 | + } | |
| 1165 | + else { | |
| 1166 | + slow_sub: | |
| 1167 | + x = PyNumber_Subtract(v, w); | |
| 1168 | + } | |
| 1169 | + Py_DECREF(v); | |
| 1170 | + Py_DECREF(w); | |
| 1171 | + SET_TOP(x); | |
| 1172 | + if (x != NULL) continue; | |
| 1173 | + break; | |
| 1174 | + | |
| 1175 | + case BINARY_SUBSCR: | |
| 1176 | + w = POP(); | |
| 1177 | + v = TOP(); | |
| 1178 | + if (PyList_CheckExact(v) && PyInt_CheckExact(w)) { | |
| 1179 | + /* INLINE: list[int] */ | |
| 1180 | + Py_ssize_t i = PyInt_AsSsize_t(w); | |
| 1181 | + if (i < 0) | |
| 1182 | + i += PyList_GET_SIZE(v); | |
| 1183 | + if (i >= 0 && i < PyList_GET_SIZE(v)) { | |
| 1184 | + x = PyList_GET_ITEM(v, i); | |
| 1185 | + Py_INCREF(x); | |
| 1186 | + } | |
| 1187 | + else | |
| 1188 | + goto slow_get; | |
| 1189 | + } | |
| 1190 | + else | |
| 1191 | + slow_get: | |
| 1192 | + x = PyObject_GetItem(v, w); | |
| 1193 | + Py_DECREF(v); | |
| 1194 | + Py_DECREF(w); | |
| 1195 | + SET_TOP(x); | |
| 1196 | + if (x != NULL) continue; | |
| 1197 | + break; | |
| 1198 | + | |
| 1199 | + case BINARY_LSHIFT: | |
| 1200 | + w = POP(); | |
| 1201 | + v = TOP(); | |
| 1202 | + x = PyNumber_Lshift(v, w); | |
| 1203 | + Py_DECREF(v); | |
| 1204 | + Py_DECREF(w); | |
| 1205 | + SET_TOP(x); | |
| 1206 | + if (x != NULL) continue; | |
| 1207 | + break; | |
| 1208 | + | |
| 1209 | + case BINARY_RSHIFT: | |
| 1210 | + w = POP(); | |
| 1211 | + v = TOP(); | |
| 1212 | + x = PyNumber_Rshift(v, w); | |
| 1213 | + Py_DECREF(v); | |
| 1214 | + Py_DECREF(w); | |
| 1215 | + SET_TOP(x); | |
| 1216 | + if (x != NULL) continue; | |
| 1217 | + break; | |
| 1218 | + | |
| 1219 | + case BINARY_AND: | |
| 1220 | + w = POP(); | |
| 1221 | + v = TOP(); | |
| 1222 | + x = PyNumber_And(v, w); | |
| 1223 | + Py_DECREF(v); | |
| 1224 | + Py_DECREF(w); | |
| 1225 | + SET_TOP(x); | |
| 1226 | + if (x != NULL) continue; | |
| 1227 | + break; | |
| 1228 | + | |
| 1229 | + case BINARY_XOR: | |
| 1230 | + w = POP(); | |
| 1231 | + v = TOP(); | |
| 1232 | + x = PyNumber_Xor(v, w); | |
| 1233 | + Py_DECREF(v); | |
| 1234 | + Py_DECREF(w); | |
| 1235 | + SET_TOP(x); | |
| 1236 | + if (x != NULL) continue; | |
| 1237 | + break; | |
| 1238 | + | |
| 1239 | + case BINARY_OR: | |
| 1240 | + w = POP(); | |
| 1241 | + v = TOP(); | |
| 1242 | + x = PyNumber_Or(v, w); | |
| 1243 | + Py_DECREF(v); | |
| 1244 | + Py_DECREF(w); | |
| 1245 | + SET_TOP(x); | |
| 1246 | + if (x != NULL) continue; | |
| 1247 | + break; | |
| 1248 | + | |
| 1249 | + case LIST_APPEND: | |
| 1250 | + w = POP(); | |
| 1251 | + v = POP(); | |
| 1252 | + err = PyList_Append(v, w); | |
| 1253 | + Py_DECREF(v); | |
| 1254 | + Py_DECREF(w); | |
| 1255 | + if (err == 0) { | |
| 1256 | + PREDICT(JUMP_ABSOLUTE); | |
| 1257 | + continue; | |
| 1258 | + } | |
| 1259 | + break; | |
| 1260 | + | |
| 1261 | + case INPLACE_POWER: | |
| 1262 | + w = POP(); | |
| 1263 | + v = TOP(); | |
| 1264 | + x = PyNumber_InPlacePower(v, w, Py_None); | |
| 1265 | + Py_DECREF(v); | |
| 1266 | + Py_DECREF(w); | |
| 1267 | + SET_TOP(x); | |
| 1268 | + if (x != NULL) continue; | |
| 1269 | + break; | |
| 1270 | + | |
| 1271 | + case INPLACE_MULTIPLY: | |
| 1272 | + w = POP(); | |
| 1273 | + v = TOP(); | |
| 1274 | + x = PyNumber_InPlaceMultiply(v, w); | |
| 1275 | + Py_DECREF(v); | |
| 1276 | + Py_DECREF(w); | |
| 1277 | + SET_TOP(x); | |
| 1278 | + if (x != NULL) continue; | |
| 1279 | + break; | |
| 1280 | + | |
| 1281 | + case INPLACE_DIVIDE: | |
| 1282 | + if (!_Py_QnewFlag) { | |
| 1283 | + w = POP(); | |
| 1284 | + v = TOP(); | |
| 1285 | + x = PyNumber_InPlaceDivide(v, w); | |
| 1286 | + Py_DECREF(v); | |
| 1287 | + Py_DECREF(w); | |
| 1288 | + SET_TOP(x); | |
| 1289 | + if (x != NULL) continue; | |
| 1290 | + break; | |
| 1291 | + } | |
| 1292 | + /* -Qnew is in effect: fall through to | |
| 1293 | + INPLACE_TRUE_DIVIDE */ | |
| 1294 | + case INPLACE_TRUE_DIVIDE: | |
| 1295 | + w = POP(); | |
| 1296 | + v = TOP(); | |
| 1297 | + x = PyNumber_InPlaceTrueDivide(v, w); | |
| 1298 | + Py_DECREF(v); | |
| 1299 | + Py_DECREF(w); | |
| 1300 | + SET_TOP(x); | |
| 1301 | + if (x != NULL) continue; | |
| 1302 | + break; | |
| 1303 | + | |
| 1304 | + case INPLACE_FLOOR_DIVIDE: | |
| 1305 | + w = POP(); | |
| 1306 | + v = TOP(); | |
| 1307 | + x = PyNumber_InPlaceFloorDivide(v, w); | |
| 1308 | + Py_DECREF(v); | |
| 1309 | + Py_DECREF(w); | |
| 1310 | + SET_TOP(x); | |
| 1311 | + if (x != NULL) continue; | |
| 1312 | + break; | |
| 1313 | + | |
| 1314 | + case INPLACE_MODULO: | |
| 1315 | + w = POP(); | |
| 1316 | + v = TOP(); | |
| 1317 | + x = PyNumber_InPlaceRemainder(v, w); | |
| 1318 | + Py_DECREF(v); | |
| 1319 | + Py_DECREF(w); | |
| 1320 | + SET_TOP(x); | |
| 1321 | + if (x != NULL) continue; | |
| 1322 | + break; | |
| 1323 | + | |
| 1324 | + case INPLACE_ADD: | |
| 1325 | + w = POP(); | |
| 1326 | + v = TOP(); | |
| 1327 | + if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) { | |
| 1328 | + /* INLINE: int + int */ | |
| 1329 | + register long a, b, i; | |
| 1330 | + a = PyInt_AS_LONG(v); | |
| 1331 | + b = PyInt_AS_LONG(w); | |
| 1332 | + i = a + b; | |
| 1333 | + if ((i^a) < 0 && (i^b) < 0) | |
| 1334 | + goto slow_iadd; | |
| 1335 | + x = PyInt_FromLong(i); | |
| 1336 | + } | |
| 1337 | + else if (PyString_CheckExact(v) && | |
| 1338 | + PyString_CheckExact(w)) { | |
| 1339 | + x = string_concatenate(v, w, f, next_instr); | |
| 1340 | + /* string_concatenate consumed the ref to v */ | |
| 1341 | + goto skip_decref_v; | |
| 1342 | + } | |
| 1343 | + else { | |
| 1344 | + slow_iadd: | |
| 1345 | + x = PyNumber_InPlaceAdd(v, w); | |
| 1346 | + } | |
| 1347 | + Py_DECREF(v); | |
| 1348 | + skip_decref_v: | |
| 1349 | + Py_DECREF(w); | |
| 1350 | + SET_TOP(x); | |
| 1351 | + if (x != NULL) continue; | |
| 1352 | + break; | |
| 1353 | + | |
| 1354 | + case INPLACE_SUBTRACT: | |
| 1355 | + w = POP(); | |
| 1356 | + v = TOP(); | |
| 1357 | + if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) { | |
| 1358 | + /* INLINE: int - int */ | |
| 1359 | + register long a, b, i; | |
| 1360 | + a = PyInt_AS_LONG(v); | |
| 1361 | + b = PyInt_AS_LONG(w); | |
| 1362 | + i = a - b; | |
| 1363 | + if ((i^a) < 0 && (i^~b) < 0) | |
| 1364 | + goto slow_isub; | |
| 1365 | + x = PyInt_FromLong(i); | |
| 1366 | + } | |
| 1367 | + else { | |
| 1368 | + slow_isub: | |
| 1369 | + x = PyNumber_InPlaceSubtract(v, w); | |
| 1370 | + } | |
| 1371 | + Py_DECREF(v); | |
| 1372 | + Py_DECREF(w); | |
| 1373 | + SET_TOP(x); | |
| 1374 | + if (x != NULL) continue; | |
| 1375 | + break; | |
| 1376 | + | |
| 1377 | + case INPLACE_LSHIFT: | |
| 1378 | + w = POP(); | |
| 1379 | + v = TOP(); | |
| 1380 | + x = PyNumber_InPlaceLshift(v, w); | |
| 1381 | + Py_DECREF(v); | |
| 1382 | + Py_DECREF(w); | |
| 1383 | + SET_TOP(x); | |
| 1384 | + if (x != NULL) continue; | |
| 1385 | + break; | |
| 1386 | + | |
| 1387 | + case INPLACE_RSHIFT: | |
| 1388 | + w = POP(); | |
| 1389 | + v = TOP(); | |
| 1390 | + x = PyNumber_InPlaceRshift(v, w); | |
| 1391 | + Py_DECREF(v); | |
| 1392 | + Py_DECREF(w); | |
| 1393 | + SET_TOP(x); | |
| 1394 | + if (x != NULL) continue; | |
| 1395 | + break; | |
| 1396 | + | |
| 1397 | + case INPLACE_AND: | |
| 1398 | + w = POP(); | |
| 1399 | + v = TOP(); | |
| 1400 | + x = PyNumber_InPlaceAnd(v, w); | |
| 1401 | + Py_DECREF(v); | |
| 1402 | + Py_DECREF(w); | |
| 1403 | + SET_TOP(x); | |
| 1404 | + if (x != NULL) continue; | |
| 1405 | + break; | |
| 1406 | + | |
| 1407 | + case INPLACE_XOR: | |
| 1408 | + w = POP(); | |
| 1409 | + v = TOP(); | |
| 1410 | + x = PyNumber_InPlaceXor(v, w); | |
| 1411 | + Py_DECREF(v); | |
| 1412 | + Py_DECREF(w); | |
| 1413 | + SET_TOP(x); | |
| 1414 | + if (x != NULL) continue; | |
| 1415 | + break; | |
| 1416 | + | |
| 1417 | + case INPLACE_OR: | |
| 1418 | + w = POP(); | |
| 1419 | + v = TOP(); | |
| 1420 | + x = PyNumber_InPlaceOr(v, w); | |
| 1421 | + Py_DECREF(v); | |
| 1422 | + Py_DECREF(w); | |
| 1423 | + SET_TOP(x); | |
| 1424 | + if (x != NULL) continue; | |
| 1425 | + break; | |
| 1426 | + | |
| 1427 | + case SLICE+0: | |
| 1428 | + case SLICE+1: | |
| 1429 | + case SLICE+2: | |
| 1430 | + case SLICE+3: | |
| 1431 | + if ((opcode-SLICE) & 2) | |
| 1432 | + w = POP(); | |
| 1433 | + else | |
| 1434 | + w = NULL; | |
| 1435 | + if ((opcode-SLICE) & 1) | |
| 1436 | + v = POP(); | |
| 1437 | + else | |
| 1438 | + v = NULL; | |
| 1439 | + u = TOP(); | |
| 1440 | + x = apply_slice(u, v, w); | |
| 1441 | + Py_DECREF(u); | |
| 1442 | + Py_XDECREF(v); | |
| 1443 | + Py_XDECREF(w); | |
| 1444 | + SET_TOP(x); | |
| 1445 | + if (x != NULL) continue; | |
| 1446 | + break; | |
| 1447 | + | |
| 1448 | + case STORE_SLICE+0: | |
| 1449 | + case STORE_SLICE+1: | |
| 1450 | + case STORE_SLICE+2: | |
| 1451 | + case STORE_SLICE+3: | |
| 1452 | + if ((opcode-STORE_SLICE) & 2) | |
| 1453 | + w = POP(); | |
| 1454 | + else | |
| 1455 | + w = NULL; | |
| 1456 | + if ((opcode-STORE_SLICE) & 1) | |
| 1457 | + v = POP(); | |
| 1458 | + else | |
| 1459 | + v = NULL; | |
| 1460 | + u = POP(); | |
| 1461 | + t = POP(); | |
| 1462 | + err = assign_slice(u, v, w, t); /* u[v:w] = t */ | |
| 1463 | + Py_DECREF(t); | |
| 1464 | + Py_DECREF(u); | |
| 1465 | + Py_XDECREF(v); | |
| 1466 | + Py_XDECREF(w); | |
| 1467 | + if (err == 0) continue; | |
| 1468 | + break; | |
| 1469 | + | |
| 1470 | + case DELETE_SLICE+0: | |
| 1471 | + case DELETE_SLICE+1: | |
| 1472 | + case DELETE_SLICE+2: | |
| 1473 | + case DELETE_SLICE+3: | |
| 1474 | + if ((opcode-DELETE_SLICE) & 2) | |
| 1475 | + w = POP(); | |
| 1476 | + else | |
| 1477 | + w = NULL; | |
| 1478 | + if ((opcode-DELETE_SLICE) & 1) | |
| 1479 | + v = POP(); | |
| 1480 | + else | |
| 1481 | + v = NULL; | |
| 1482 | + u = POP(); | |
| 1483 | + err = assign_slice(u, v, w, (PyObject *)NULL); | |
| 1484 | + /* del u[v:w] */ | |
| 1485 | + Py_DECREF(u); | |
| 1486 | + Py_XDECREF(v); | |
| 1487 | + Py_XDECREF(w); | |
| 1488 | + if (err == 0) continue; | |
| 1489 | + break; | |
| 1490 | + | |
| 1491 | + case STORE_SUBSCR: | |
| 1492 | + w = TOP(); | |
| 1493 | + v = SECOND(); | |
| 1494 | + u = THIRD(); | |
| 1495 | + STACKADJ(-3); | |
| 1496 | + /* v[w] = u */ | |
| 1497 | + err = PyObject_SetItem(v, w, u); | |
| 1498 | + Py_DECREF(u); | |
| 1499 | + Py_DECREF(v); | |
| 1500 | + Py_DECREF(w); | |
| 1501 | + if (err == 0) continue; | |
| 1502 | + break; | |
| 1503 | + | |
| 1504 | + case DELETE_SUBSCR: | |
| 1505 | + w = TOP(); | |
| 1506 | + v = SECOND(); | |
| 1507 | + STACKADJ(-2); | |
| 1508 | + /* del v[w] */ | |
| 1509 | + err = PyObject_DelItem(v, w); | |
| 1510 | + Py_DECREF(v); | |
| 1511 | + Py_DECREF(w); | |
| 1512 | + if (err == 0) continue; | |
| 1513 | + break; | |
| 1514 | + | |
| 1515 | + case PRINT_EXPR: | |
| 1516 | + v = POP(); | |
| 1517 | + w = PySys_GetObject("displayhook"); | |
| 1518 | + if (w == NULL) { | |
| 1519 | + PyErr_SetString(PyExc_RuntimeError, | |
| 1520 | + "lost sys.displayhook"); | |
| 1521 | + err = -1; | |
| 1522 | + x = NULL; | |
| 1523 | + } | |
| 1524 | + if (err == 0) { | |
| 1525 | + x = PyTuple_Pack(1, v); | |
| 1526 | + if (x == NULL) | |
| 1527 | + err = -1; | |
| 1528 | + } | |
| 1529 | + if (err == 0) { | |
| 1530 | + w = PyEval_CallObject(w, x); | |
| 1531 | + Py_XDECREF(w); | |
| 1532 | + if (w == NULL) | |
| 1533 | + err = -1; | |
| 1534 | + } | |
| 1535 | + Py_DECREF(v); | |
| 1536 | + Py_XDECREF(x); | |
| 1537 | + break; | |
| 1538 | + | |
| 1539 | + case PRINT_ITEM_TO: | |
| 1540 | + w = stream = POP(); | |
| 1541 | + /* fall through to PRINT_ITEM */ | |
| 1542 | + | |
| 1543 | + case PRINT_ITEM: | |
| 1544 | + v = POP(); | |
| 1545 | + if (stream == NULL || stream == Py_None) { | |
| 1546 | + w = PySys_GetObject("stdout"); | |
| 1547 | + if (w == NULL) { | |
| 1548 | + PyErr_SetString(PyExc_RuntimeError, | |
| 1549 | + "lost sys.stdout"); | |
| 1550 | + err = -1; | |
| 1551 | + } | |
| 1552 | + } | |
| 1553 | + /* PyFile_SoftSpace() can exececute arbitrary code | |
| 1554 | + if sys.stdout is an instance with a __getattr__. | |
| 1555 | + If __getattr__ raises an exception, w will | |
| 1556 | + be freed, so we need to prevent that temporarily. */ | |
| 1557 | + Py_XINCREF(w); | |
| 1558 | + if (w != NULL && PyFile_SoftSpace(w, 0)) | |
| 1559 | + err = PyFile_WriteString(" ", w); | |
| 1560 | + if (err == 0) | |
| 1561 | + err = PyFile_WriteObject(v, w, Py_PRINT_RAW); | |
| 1562 | + if (err == 0) { | |
| 1563 | + /* XXX move into writeobject() ? */ | |
| 1564 | + if (PyString_Check(v)) { | |
| 1565 | + char *s = PyString_AS_STRING(v); | |
| 1566 | + Py_ssize_t len = PyString_GET_SIZE(v); | |
| 1567 | + if (len == 0 || | |
| 1568 | + !isspace(Py_CHARMASK(s[len-1])) || | |
| 1569 | + s[len-1] == ' ') | |
| 1570 | + PyFile_SoftSpace(w, 1); | |
| 1571 | + } | |
| 1572 | +#ifdef Py_USING_UNICODE | |
| 1573 | + else if (PyUnicode_Check(v)) { | |
| 1574 | + Py_UNICODE *s = PyUnicode_AS_UNICODE(v); | |
| 1575 | + Py_ssize_t len = PyUnicode_GET_SIZE(v); | |
| 1576 | + if (len == 0 || | |
| 1577 | + !Py_UNICODE_ISSPACE(s[len-1]) || | |
| 1578 | + s[len-1] == ' ') | |
| 1579 | + PyFile_SoftSpace(w, 1); | |
| 1580 | + } | |
| 1581 | +#endif | |
| 1582 | + else | |
| 1583 | + PyFile_SoftSpace(w, 1); | |
| 1584 | + } | |
| 1585 | + Py_XDECREF(w); | |
| 1586 | + Py_DECREF(v); | |
| 1587 | + Py_XDECREF(stream); | |
| 1588 | + stream = NULL; | |
| 1589 | + if (err == 0) | |
| 1590 | + continue; | |
| 1591 | + break; | |
| 1592 | + | |
| 1593 | + case PRINT_NEWLINE_TO: | |
| 1594 | + w = stream = POP(); | |
| 1595 | + /* fall through to PRINT_NEWLINE */ | |
| 1596 | + | |
| 1597 | + case PRINT_NEWLINE: | |
| 1598 | + if (stream == NULL || stream == Py_None) { | |
| 1599 | + w = PySys_GetObject("stdout"); | |
| 1600 | + if (w == NULL) | |
| 1601 | + PyErr_SetString(PyExc_RuntimeError, | |
| 1602 | + "lost sys.stdout"); | |
| 1603 | + } | |
| 1604 | + if (w != NULL) { | |
| 1605 | + err = PyFile_WriteString("\n", w); | |
| 1606 | + if (err == 0) | |
| 1607 | + PyFile_SoftSpace(w, 0); | |
| 1608 | + } | |
| 1609 | + Py_XDECREF(stream); | |
| 1610 | + stream = NULL; | |
| 1611 | + break; | |
| 1612 | + | |
| 1613 | + | |
| 1614 | +#ifdef CASE_TOO_BIG | |
| 1615 | + default: switch (opcode) { | |
| 1616 | +#endif | |
| 1617 | + case RAISE_VARARGS: | |
| 1618 | + u = v = w = NULL; | |
| 1619 | + switch (oparg) { | |
| 1620 | + case 3: | |
| 1621 | + u = POP(); /* traceback */ | |
| 1622 | + /* Fallthrough */ | |
| 1623 | + case 2: | |
| 1624 | + v = POP(); /* value */ | |
| 1625 | + /* Fallthrough */ | |
| 1626 | + case 1: | |
| 1627 | + w = POP(); /* exc */ | |
| 1628 | + case 0: /* Fallthrough */ | |
| 1629 | + why = do_raise(w, v, u); | |
| 1630 | + break; | |
| 1631 | + default: | |
| 1632 | + PyErr_SetString(PyExc_SystemError, | |
| 1633 | + "bad RAISE_VARARGS oparg"); | |
| 1634 | + why = WHY_EXCEPTION; | |
| 1635 | + break; | |
| 1636 | + } | |
| 1637 | + break; | |
| 1638 | + | |
| 1639 | + case LOAD_LOCALS: | |
| 1640 | + if ((x = f->f_locals) != NULL) { | |
| 1641 | + Py_INCREF(x); | |
| 1642 | + PUSH(x); | |
| 1643 | + continue; | |
| 1644 | + } | |
| 1645 | + PyErr_SetString(PyExc_SystemError, "no locals"); | |
| 1646 | + break; | |
| 1647 | + | |
| 1648 | + case RETURN_VALUE: | |
| 1649 | + retval = POP(); | |
| 1650 | + why = WHY_RETURN; | |
| 1651 | + goto fast_block_end; | |
| 1652 | + | |
| 1653 | + case YIELD_VALUE: | |
| 1654 | + retval = POP(); | |
| 1655 | + f->f_stacktop = stack_pointer; | |
| 1656 | + why = WHY_YIELD; | |
| 1657 | + goto fast_yield; | |
| 1658 | + | |
| 1659 | + case EXEC_STMT: | |
| 1660 | + w = TOP(); | |
| 1661 | + v = SECOND(); | |
| 1662 | + u = THIRD(); | |
| 1663 | + STACKADJ(-3); | |
| 1664 | + READ_TIMESTAMP(intr0); | |
| 1665 | + err = exec_statement(f, u, v, w); | |
| 1666 | + READ_TIMESTAMP(intr1); | |
| 1667 | + Py_DECREF(u); | |
| 1668 | + Py_DECREF(v); | |
| 1669 | + Py_DECREF(w); | |
| 1670 | + break; | |
| 1671 | + | |
| 1672 | + case POP_BLOCK: | |
| 1673 | + { | |
| 1674 | + PyTryBlock *b = PyFrame_BlockPop(f); | |
| 1675 | + while (STACK_LEVEL() > b->b_level) { | |
| 1676 | + v = POP(); | |
| 1677 | + Py_DECREF(v); | |
| 1678 | + } | |
| 1679 | + } | |
| 1680 | + continue; | |
| 1681 | + | |
| 1682 | + case END_FINALLY: | |
| 1683 | + v = POP(); | |
| 1684 | + if (PyInt_Check(v)) { | |
| 1685 | + why = (enum why_code) PyInt_AS_LONG(v); | |
| 1686 | + assert(why != WHY_YIELD); | |
| 1687 | + if (why == WHY_RETURN || | |
| 1688 | + why == WHY_CONTINUE) | |
| 1689 | + retval = POP(); | |
| 1690 | + } | |
| 1691 | + else if (PyExceptionClass_Check(v) || PyString_Check(v)) { | |
| 1692 | + w = POP(); | |
| 1693 | + u = POP(); | |
| 1694 | + PyErr_Restore(v, w, u); | |
| 1695 | + why = WHY_RERAISE; | |
| 1696 | + break; | |
| 1697 | + } | |
| 1698 | + else if (v != Py_None) { | |
| 1699 | + PyErr_SetString(PyExc_SystemError, | |
| 1700 | + "'finally' pops bad exception"); | |
| 1701 | + why = WHY_EXCEPTION; | |
| 1702 | + } | |
| 1703 | + Py_DECREF(v); | |
| 1704 | + break; | |
| 1705 | + | |
| 1706 | + case BUILD_CLASS: | |
| 1707 | + u = TOP(); | |
| 1708 | + v = SECOND(); | |
| 1709 | + w = THIRD(); | |
| 1710 | + STACKADJ(-2); | |
| 1711 | + x = build_class(u, v, w); | |
| 1712 | + SET_TOP(x); | |
| 1713 | + Py_DECREF(u); | |
| 1714 | + Py_DECREF(v); | |
| 1715 | + Py_DECREF(w); | |
| 1716 | + break; | |
| 1717 | + | |
| 1718 | + case STORE_NAME: | |
| 1719 | + w = GETITEM(names, oparg); | |
| 1720 | + v = POP(); | |
| 1721 | + if ((x = f->f_locals) != NULL) { | |
| 1722 | + if (PyDict_CheckExact(x)) | |
| 1723 | + err = PyDict_SetItem(x, w, v); | |
| 1724 | + else | |
| 1725 | + err = PyObject_SetItem(x, w, v); | |
| 1726 | + Py_DECREF(v); | |
| 1727 | + if (err == 0) continue; | |
| 1728 | + break; | |
| 1729 | + } | |
| 1730 | + PyErr_Format(PyExc_SystemError, | |
| 1731 | + "no locals found when storing %s", | |
| 1732 | + PyObject_REPR(w)); | |
| 1733 | + break; | |
| 1734 | + | |
| 1735 | + case DELETE_NAME: | |
| 1736 | + w = GETITEM(names, oparg); | |
| 1737 | + if ((x = f->f_locals) != NULL) { | |
| 1738 | + if ((err = PyObject_DelItem(x, w)) != 0) | |
| 1739 | + format_exc_check_arg(PyExc_NameError, | |
| 1740 | + NAME_ERROR_MSG ,w); | |
| 1741 | + break; | |
| 1742 | + } | |
| 1743 | + PyErr_Format(PyExc_SystemError, | |
| 1744 | + "no locals when deleting %s", | |
| 1745 | + PyObject_REPR(w)); | |
| 1746 | + break; | |
| 1747 | + | |
| 1748 | + PREDICTED_WITH_ARG(UNPACK_SEQUENCE); | |
| 1749 | + case UNPACK_SEQUENCE: | |
| 1750 | + v = POP(); | |
| 1751 | + if (PyTuple_CheckExact(v) && PyTuple_GET_SIZE(v) == oparg) { | |
| 1752 | + PyObject **items = ((PyTupleObject *)v)->ob_item; | |
| 1753 | + while (oparg--) { | |
| 1754 | + w = items[oparg]; | |
| 1755 | + Py_INCREF(w); | |
| 1756 | + PUSH(w); | |
| 1757 | + } | |
| 1758 | + Py_DECREF(v); | |
| 1759 | + continue; | |
| 1760 | + } else if (PyList_CheckExact(v) && PyList_GET_SIZE(v) == oparg) { | |
| 1761 | + PyObject **items = ((PyListObject *)v)->ob_item; | |
| 1762 | + while (oparg--) { | |
| 1763 | + w = items[oparg]; | |
| 1764 | + Py_INCREF(w); | |
| 1765 | + PUSH(w); | |
| 1766 | + } | |
| 1767 | + } else if (unpack_iterable(v, oparg, | |
| 1768 | + stack_pointer + oparg)) { | |
| 1769 | + stack_pointer += oparg; | |
| 1770 | + } else { | |
| 1771 | + /* unpack_iterable() raised an exception */ | |
| 1772 | + why = WHY_EXCEPTION; | |
| 1773 | + } | |
| 1774 | + Py_DECREF(v); | |
| 1775 | + break; | |
| 1776 | + | |
| 1777 | + case STORE_ATTR: | |
| 1778 | + w = GETITEM(names, oparg); | |
| 1779 | + v = TOP(); | |
| 1780 | + u = SECOND(); | |
| 1781 | + STACKADJ(-2); | |
| 1782 | + err = PyObject_SetAttr(v, w, u); /* v.w = u */ | |
| 1783 | + Py_DECREF(v); | |
| 1784 | + Py_DECREF(u); | |
| 1785 | + if (err == 0) continue; | |
| 1786 | + break; | |
| 1787 | + | |
| 1788 | + case DELETE_ATTR: | |
| 1789 | + w = GETITEM(names, oparg); | |
| 1790 | + v = POP(); | |
| 1791 | + err = PyObject_SetAttr(v, w, (PyObject *)NULL); | |
| 1792 | + /* del v.w */ | |
| 1793 | + Py_DECREF(v); | |
| 1794 | + break; | |
| 1795 | + | |
| 1796 | + case STORE_GLOBAL: | |
| 1797 | + w = GETITEM(names, oparg); | |
| 1798 | + v = POP(); | |
| 1799 | + err = PyDict_SetItem(f->f_globals, w, v); | |
| 1800 | + Py_DECREF(v); | |
| 1801 | + if (err == 0) continue; | |
| 1802 | + break; | |
| 1803 | + | |
| 1804 | + case DELETE_GLOBAL: | |
| 1805 | + w = GETITEM(names, oparg); | |
| 1806 | + if ((err = PyDict_DelItem(f->f_globals, w)) != 0) | |
| 1807 | + format_exc_check_arg( | |
| 1808 | + PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w); | |
| 1809 | + break; | |
| 1810 | + | |
| 1811 | + case LOAD_NAME: | |
| 1812 | + w = GETITEM(names, oparg); | |
| 1813 | + if ((v = f->f_locals) == NULL) { | |
| 1814 | + PyErr_Format(PyExc_SystemError, | |
| 1815 | + "no locals when loading %s", | |
| 1816 | + PyObject_REPR(w)); | |
| 1817 | + break; | |
| 1818 | + } | |
| 1819 | + if (PyDict_CheckExact(v)) { | |
| 1820 | + x = PyDict_GetItem(v, w); | |
| 1821 | + Py_XINCREF(x); | |
| 1822 | + } | |
| 1823 | + else { | |
| 1824 | + x = PyObject_GetItem(v, w); | |
| 1825 | + if (x == NULL && PyErr_Occurred()) { | |
| 1826 | + if (!PyErr_ExceptionMatches(PyExc_KeyError)) | |
| 1827 | + break; | |
| 1828 | + PyErr_Clear(); | |
| 1829 | + } | |
| 1830 | + } | |
| 1831 | + if (x == NULL) { | |
| 1832 | + x = PyDict_GetItem(f->f_globals, w); | |
| 1833 | + if (x == NULL) { | |
| 1834 | + x = PyDict_GetItem(f->f_builtins, w); | |
| 1835 | + if (x == NULL) { | |
| 1836 | + format_exc_check_arg( | |
| 1837 | + PyExc_NameError, | |
| 1838 | + NAME_ERROR_MSG ,w); | |
| 1839 | + break; | |
| 1840 | + } | |
| 1841 | + } | |
| 1842 | + Py_INCREF(x); | |
| 1843 | + } | |
| 1844 | + PUSH(x); | |
| 1845 | + continue; | |
| 1846 | + | |
| 1847 | + case LOAD_GLOBAL: | |
| 1848 | + w = GETITEM(names, oparg); | |
| 1849 | + if (PyString_CheckExact(w)) { | |
| 1850 | + /* Inline the PyDict_GetItem() calls. | |
| 1851 | + WARNING: this is an extreme speed hack. | |
| 1852 | + Do not try this at home. */ | |
| 1853 | + long hash = ((PyStringObject *)w)->ob_shash; | |
| 1854 | + if (hash != -1) { | |
| 1855 | + PyDictObject *d; | |
| 1856 | + PyDictEntry *e; | |
| 1857 | + d = (PyDictObject *)(f->f_globals); | |
| 1858 | + e = d->ma_lookup(d, w, hash); | |
| 1859 | + if (e == NULL) { | |
| 1860 | + x = NULL; | |
| 1861 | + break; | |
| 1862 | + } | |
| 1863 | + x = e->me_value; | |
| 1864 | + if (x != NULL) { | |
| 1865 | + Py_INCREF(x); | |
| 1866 | + PUSH(x); | |
| 1867 | + continue; | |
| 1868 | + } | |
| 1869 | + d = (PyDictObject *)(f->f_builtins); | |
| 1870 | + e = d->ma_lookup(d, w, hash); | |
| 1871 | + if (e == NULL) { | |
| 1872 | + x = NULL; | |
| 1873 | + break; | |
| 1874 | + } | |
| 1875 | + x = e->me_value; | |
| 1876 | + if (x != NULL) { | |
| 1877 | + Py_INCREF(x); | |
| 1878 | + PUSH(x); | |
| 1879 | + continue; | |
| 1880 | + } | |
| 1881 | + goto load_global_error; | |
| 1882 | + } | |
| 1883 | + } | |
| 1884 | + /* This is the un-inlined version of the code above */ | |
| 1885 | + x = PyDict_GetItem(f->f_globals, w); | |
| 1886 | + if (x == NULL) { | |
| 1887 | + x = PyDict_GetItem(f->f_builtins, w); | |
| 1888 | + if (x == NULL) { | |
| 1889 | + load_global_error: | |
| 1890 | + format_exc_check_arg( | |
| 1891 | + PyExc_NameError, | |
| 1892 | + GLOBAL_NAME_ERROR_MSG, w); | |
| 1893 | + break; | |
| 1894 | + } | |
| 1895 | + } | |
| 1896 | + Py_INCREF(x); | |
| 1897 | + PUSH(x); | |
| 1898 | + continue; | |
| 1899 | + | |
| 1900 | + case DELETE_FAST: | |
| 1901 | + x = GETLOCAL(oparg); | |
| 1902 | + if (x != NULL) { | |
| 1903 | + SETLOCAL(oparg, NULL); | |
| 1904 | + continue; | |
| 1905 | + } | |
| 1906 | + format_exc_check_arg( | |
| 1907 | + PyExc_UnboundLocalError, | |
| 1908 | + UNBOUNDLOCAL_ERROR_MSG, | |
| 1909 | + PyTuple_GetItem(co->co_varnames, oparg) | |
| 1910 | + ); | |
| 1911 | + break; | |
| 1912 | + | |
| 1913 | + case LOAD_CLOSURE: | |
| 1914 | + x = freevars[oparg]; | |
| 1915 | + Py_INCREF(x); | |
| 1916 | + PUSH(x); | |
| 1917 | + if (x != NULL) continue; | |
| 1918 | + break; | |
| 1919 | + | |
| 1920 | + case LOAD_DEREF: | |
| 1921 | + x = freevars[oparg]; | |
| 1922 | + w = PyCell_Get(x); | |
| 1923 | + if (w != NULL) { | |
| 1924 | + PUSH(w); | |
| 1925 | + continue; | |
| 1926 | + } | |
| 1927 | + err = -1; | |
| 1928 | + /* Don't stomp existing exception */ | |
| 1929 | + if (PyErr_Occurred()) | |
| 1930 | + break; | |
| 1931 | + if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) { | |
| 1932 | + v = PyTuple_GET_ITEM(co->co_cellvars, | |
| 1933 | + oparg); | |
| 1934 | + format_exc_check_arg( | |
| 1935 | + PyExc_UnboundLocalError, | |
| 1936 | + UNBOUNDLOCAL_ERROR_MSG, | |
| 1937 | + v); | |
| 1938 | + } else { | |
| 1939 | + v = PyTuple_GET_ITEM( | |
| 1940 | + co->co_freevars, | |
| 1941 | + oparg - PyTuple_GET_SIZE(co->co_cellvars)); | |
| 1942 | + format_exc_check_arg( | |
| 1943 | + PyExc_NameError, | |
| 1944 | + UNBOUNDFREE_ERROR_MSG, | |
| 1945 | + v); | |
| 1946 | + } | |
| 1947 | + break; | |
| 1948 | + | |
| 1949 | + case STORE_DEREF: | |
| 1950 | + w = POP(); | |
| 1951 | + x = freevars[oparg]; | |
| 1952 | + PyCell_Set(x, w); | |
| 1953 | + Py_DECREF(w); | |
| 1954 | + continue; | |
| 1955 | + | |
| 1956 | + case BUILD_TUPLE: | |
| 1957 | + x = PyTuple_New(oparg); | |
| 1958 | + if (x != NULL) { | |
| 1959 | + for (; --oparg >= 0;) { | |
| 1960 | + w = POP(); | |
| 1961 | + PyTuple_SET_ITEM(x, oparg, w); | |
| 1962 | + } | |
| 1963 | + PUSH(x); | |
| 1964 | + continue; | |
| 1965 | + } | |
| 1966 | + break; | |
| 1967 | + | |
| 1968 | + case BUILD_LIST: | |
| 1969 | + x = PyList_New(oparg); | |
| 1970 | + if (x != NULL) { | |
| 1971 | + for (; --oparg >= 0;) { | |
| 1972 | + w = POP(); | |
| 1973 | + PyList_SET_ITEM(x, oparg, w); | |
| 1974 | + } | |
| 1975 | + PUSH(x); | |
| 1976 | + continue; | |
| 1977 | + } | |
| 1978 | + break; | |
| 1979 | + | |
| 1980 | + case BUILD_MAP: | |
| 1981 | + x = PyDict_New(); | |
| 1982 | + PUSH(x); | |
| 1983 | + if (x != NULL) continue; | |
| 1984 | + break; | |
| 1985 | + | |
| 1986 | + case LOAD_ATTR: | |
| 1987 | + w = GETITEM(names, oparg); | |
| 1988 | + v = TOP(); | |
| 1989 | + x = PyObject_GetAttr(v, w); | |
| 1990 | + Py_DECREF(v); | |
| 1991 | + SET_TOP(x); | |
| 1992 | + if (x != NULL) continue; | |
| 1993 | + break; | |
| 1994 | + | |
| 1995 | + case COMPARE_OP: | |
| 1996 | + w = POP(); | |
| 1997 | + v = TOP(); | |
| 1998 | + if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) { | |
| 1999 | + /* INLINE: cmp(int, int) */ | |
| 2000 | + register long a, b; | |
| 2001 | + register int res; | |
| 2002 | + a = PyInt_AS_LONG(v); | |
| 2003 | + b = PyInt_AS_LONG(w); | |
| 2004 | + switch (oparg) { | |
| 2005 | + case PyCmp_LT: res = a < b; break; | |
| 2006 | + case PyCmp_LE: res = a <= b; break; | |
| 2007 | + case PyCmp_EQ: res = a == b; break; | |
| 2008 | + case PyCmp_NE: res = a != b; break; | |
| 2009 | + case PyCmp_GT: res = a > b; break; | |
| 2010 | + case PyCmp_GE: res = a >= b; break; | |
| 2011 | + case PyCmp_IS: res = v == w; break; | |
| 2012 | + case PyCmp_IS_NOT: res = v != w; break; | |
| 2013 | + default: goto slow_compare; | |
| 2014 | + } | |
| 2015 | + x = res ? Py_True : Py_False; | |
| 2016 | + Py_INCREF(x); | |
| 2017 | + } | |
| 2018 | + else { | |
| 2019 | + slow_compare: | |
| 2020 | + x = cmp_outcome(oparg, v, w); | |
| 2021 | + } | |
| 2022 | + Py_DECREF(v); | |
| 2023 | + Py_DECREF(w); | |
| 2024 | + SET_TOP(x); | |
| 2025 | + if (x == NULL) break; | |
| 2026 | + PREDICT(JUMP_IF_FALSE); | |
| 2027 | + PREDICT(JUMP_IF_TRUE); | |
| 2028 | + continue; | |
| 2029 | + | |
| 2030 | + case IMPORT_NAME: | |
| 2031 | + w = GETITEM(names, oparg); | |
| 2032 | + x = PyDict_GetItemString(f->f_builtins, "__import__"); | |
| 2033 | + if (x == NULL) { | |
| 2034 | + PyErr_SetString(PyExc_ImportError, | |
| 2035 | + "__import__ not found"); | |
| 2036 | + break; | |
| 2037 | + } | |
| 2038 | + v = POP(); | |
| 2039 | + u = TOP(); | |
| 2040 | + if (PyInt_AsLong(u) != -1 || PyErr_Occurred()) | |
| 2041 | + w = PyTuple_Pack(5, | |
| 2042 | + w, | |
| 2043 | + f->f_globals, | |
| 2044 | + f->f_locals == NULL ? | |
| 2045 | + Py_None : f->f_locals, | |
| 2046 | + v, | |
| 2047 | + u); | |
| 2048 | + else | |
| 2049 | + w = PyTuple_Pack(4, | |
| 2050 | + w, | |
| 2051 | + f->f_globals, | |
| 2052 | + f->f_locals == NULL ? | |
| 2053 | + Py_None : f->f_locals, | |
| 2054 | + v); | |
| 2055 | + Py_DECREF(v); | |
| 2056 | + Py_DECREF(u); | |
| 2057 | + if (w == NULL) { | |
| 2058 | + u = POP(); | |
| 2059 | + x = NULL; | |
| 2060 | + break; | |
| 2061 | + } | |
| 2062 | + READ_TIMESTAMP(intr0); | |
| 2063 | + x = PyEval_CallObject(x, w); | |
| 2064 | + READ_TIMESTAMP(intr1); | |
| 2065 | + Py_DECREF(w); | |
| 2066 | + SET_TOP(x); | |
| 2067 | + if (x != NULL) continue; | |
| 2068 | + break; | |
| 2069 | + | |
| 2070 | + case IMPORT_STAR: | |
| 2071 | + v = POP(); | |
| 2072 | + PyFrame_FastToLocals(f); | |
| 2073 | + if ((x = f->f_locals) == NULL) { | |
| 2074 | + PyErr_SetString(PyExc_SystemError, | |
| 2075 | + "no locals found during 'import *'"); | |
| 2076 | + break; | |
| 2077 | + } | |
| 2078 | + READ_TIMESTAMP(intr0); | |
| 2079 | + err = import_all_from(x, v); | |
| 2080 | + READ_TIMESTAMP(intr1); | |
| 2081 | + PyFrame_LocalsToFast(f, 0); | |
| 2082 | + Py_DECREF(v); | |
| 2083 | + if (err == 0) continue; | |
| 2084 | + break; | |
| 2085 | + | |
| 2086 | + case IMPORT_FROM: | |
| 2087 | + w = GETITEM(names, oparg); | |
| 2088 | + v = TOP(); | |
| 2089 | + READ_TIMESTAMP(intr0); | |
| 2090 | + x = import_from(v, w); | |
| 2091 | + READ_TIMESTAMP(intr1); | |
| 2092 | + PUSH(x); | |
| 2093 | + if (x != NULL) continue; | |
| 2094 | + break; | |
| 2095 | + | |
| 2096 | + case JUMP_FORWARD: | |
| 2097 | + JUMPBY(oparg); | |
| 2098 | + goto fast_next_opcode; | |
| 2099 | + | |
| 2100 | + PREDICTED_WITH_ARG(JUMP_IF_FALSE); | |
| 2101 | + case JUMP_IF_FALSE: | |
| 2102 | + w = TOP(); | |
| 2103 | + if (w == Py_True) { | |
| 2104 | + PREDICT(POP_TOP); | |
| 2105 | + goto fast_next_opcode; | |
| 2106 | + } | |
| 2107 | + if (w == Py_False) { | |
| 2108 | + JUMPBY(oparg); | |
| 2109 | + goto fast_next_opcode; | |
| 2110 | + } | |
| 2111 | + err = PyObject_IsTrue(w); | |
| 2112 | + if (err > 0) | |
| 2113 | + err = 0; | |
| 2114 | + else if (err == 0) | |
| 2115 | + JUMPBY(oparg); | |
| 2116 | + else | |
| 2117 | + break; | |
| 2118 | + continue; | |
| 2119 | + | |
| 2120 | + PREDICTED_WITH_ARG(JUMP_IF_TRUE); | |
| 2121 | + case JUMP_IF_TRUE: | |
| 2122 | + w = TOP(); | |
| 2123 | + if (w == Py_False) { | |
| 2124 | + PREDICT(POP_TOP); | |
| 2125 | + goto fast_next_opcode; | |
| 2126 | + } | |
| 2127 | + if (w == Py_True) { | |
| 2128 | + JUMPBY(oparg); | |
| 2129 | + goto fast_next_opcode; | |
| 2130 | + } | |
| 2131 | + err = PyObject_IsTrue(w); | |
| 2132 | + if (err > 0) { | |
| 2133 | + err = 0; | |
| 2134 | + JUMPBY(oparg); | |
| 2135 | + } | |
| 2136 | + else if (err == 0) | |
| 2137 | + ; | |
| 2138 | + else | |
| 2139 | + break; | |
| 2140 | + continue; | |
| 2141 | + | |
| 2142 | + PREDICTED_WITH_ARG(JUMP_ABSOLUTE); | |
| 2143 | + case JUMP_ABSOLUTE: | |
| 2144 | + JUMPTO(oparg); | |
| 2145 | + continue; | |
| 2146 | + | |
| 2147 | + case GET_ITER: | |
| 2148 | + /* before: [obj]; after [getiter(obj)] */ | |
| 2149 | + v = TOP(); | |
| 2150 | + x = PyObject_GetIter(v); | |
| 2151 | + Py_DECREF(v); | |
| 2152 | + if (x != NULL) { | |
| 2153 | + SET_TOP(x); | |
| 2154 | + PREDICT(FOR_ITER); | |
| 2155 | + continue; | |
| 2156 | + } | |
| 2157 | + STACKADJ(-1); | |
| 2158 | + break; | |
| 2159 | + | |
| 2160 | + PREDICTED_WITH_ARG(FOR_ITER); | |
| 2161 | + case FOR_ITER: | |
| 2162 | + /* before: [iter]; after: [iter, iter()] *or* [] */ | |
| 2163 | + v = TOP(); | |
| 2164 | + x = (*v->ob_type->tp_iternext)(v); | |
| 2165 | + if (x != NULL) { | |
| 2166 | + PUSH(x); | |
| 2167 | + PREDICT(STORE_FAST); | |
| 2168 | + PREDICT(UNPACK_SEQUENCE); | |
| 2169 | + continue; | |
| 2170 | + } | |
| 2171 | + if (PyErr_Occurred()) { | |
| 2172 | + if (!PyErr_ExceptionMatches(PyExc_StopIteration)) | |
| 2173 | + break; | |
| 2174 | + PyErr_Clear(); | |
| 2175 | + } | |
| 2176 | + /* iterator ended normally */ | |
| 2177 | + x = v = POP(); | |
| 2178 | + Py_DECREF(v); | |
| 2179 | + JUMPBY(oparg); | |
| 2180 | + continue; | |
| 2181 | + | |
| 2182 | + case BREAK_LOOP: | |
| 2183 | + why = WHY_BREAK; | |
| 2184 | + goto fast_block_end; | |
| 2185 | + | |
| 2186 | + case CONTINUE_LOOP: | |
| 2187 | + retval = PyInt_FromLong(oparg); | |
| 2188 | + if (!retval) { | |
| 2189 | + x = NULL; | |
| 2190 | + break; | |
| 2191 | + } | |
| 2192 | + why = WHY_CONTINUE; | |
| 2193 | + goto fast_block_end; | |
| 2194 | + | |
| 2195 | + case SETUP_LOOP: | |
| 2196 | + case SETUP_EXCEPT: | |
| 2197 | + case SETUP_FINALLY: | |
| 2198 | + /* NOTE: If you add any new block-setup opcodes that are not try/except/finally | |
| 2199 | + handlers, you may need to update the PyGen_NeedsFinalizing() function. */ | |
| 2200 | + | |
| 2201 | + PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg, | |
| 2202 | + STACK_LEVEL()); | |
| 2203 | + continue; | |
| 2204 | + | |
| 2205 | + case WITH_CLEANUP: | |
| 2206 | + { | |
| 2207 | + /* TOP is the context.__exit__ bound method. | |
| 2208 | + Below that are 1-3 values indicating how/why | |
| 2209 | + we entered the finally clause: | |
| 2210 | + - SECOND = None | |
| 2211 | + - (SECOND, THIRD) = (WHY_{RETURN,CONTINUE}), retval | |
| 2212 | + - SECOND = WHY_*; no retval below it | |
| 2213 | + - (SECOND, THIRD, FOURTH) = exc_info() | |
| 2214 | + In the last case, we must call | |
| 2215 | + TOP(SECOND, THIRD, FOURTH) | |
| 2216 | + otherwise we must call | |
| 2217 | + TOP(None, None, None) | |
| 2218 | + | |
| 2219 | + In addition, if the stack represents an exception, | |
| 2220 | + *and* the function call returns a 'true' value, we | |
| 2221 | + "zap" this information, to prevent END_FINALLY from | |
| 2222 | + re-raising the exception. (But non-local gotos | |
| 2223 | + should still be resumed.) | |
| 2224 | + */ | |
| 2225 | + | |
| 2226 | + x = TOP(); | |
| 2227 | + u = SECOND(); | |
| 2228 | + if (PyInt_Check(u) || u == Py_None) { | |
| 2229 | + u = v = w = Py_None; | |
| 2230 | + } | |
| 2231 | + else { | |
| 2232 | + v = THIRD(); | |
| 2233 | + w = FOURTH(); | |
| 2234 | + } | |
| 2235 | + /* XXX Not the fastest way to call it... */ | |
| 2236 | + x = PyObject_CallFunctionObjArgs(x, u, v, w, NULL); | |
| 2237 | + if (x == NULL) | |
| 2238 | + break; /* Go to error exit */ | |
| 2239 | + if (u != Py_None && PyObject_IsTrue(x)) { | |
| 2240 | + /* There was an exception and a true return */ | |
| 2241 | + Py_DECREF(x); | |
| 2242 | + x = TOP(); /* Again */ | |
| 2243 | + STACKADJ(-3); | |
| 2244 | + Py_INCREF(Py_None); | |
| 2245 | + SET_TOP(Py_None); | |
| 2246 | + Py_DECREF(x); | |
| 2247 | + Py_DECREF(u); | |
| 2248 | + Py_DECREF(v); | |
| 2249 | + Py_DECREF(w); | |
| 2250 | + } else { | |
| 2251 | + /* Let END_FINALLY do its thing */ | |
| 2252 | + Py_DECREF(x); | |
| 2253 | + x = POP(); | |
| 2254 | + Py_DECREF(x); | |
| 2255 | + } | |
| 2256 | + break; | |
| 2257 | + } | |
| 2258 | + | |
| 2259 | + case CALL_FUNCTION: | |
| 2260 | + { | |
| 2261 | + PyObject **sp; | |
| 2262 | + PCALL(PCALL_ALL); | |
| 2263 | + sp = stack_pointer; | |
| 2264 | +#ifdef WITH_TSC | |
| 2265 | + x = call_function(&sp, oparg, &intr0, &intr1); | |
| 2266 | +#else | |
| 2267 | + x = call_function(&sp, oparg); | |
| 2268 | +#endif | |
| 2269 | + stack_pointer = sp; | |
| 2270 | + PUSH(x); | |
| 2271 | + if (x != NULL) | |
| 2272 | + continue; | |
| 2273 | + break; | |
| 2274 | + } | |
| 2275 | + | |
| 2276 | + case CALL_FUNCTION_VAR: | |
| 2277 | + case CALL_FUNCTION_KW: | |
| 2278 | + case CALL_FUNCTION_VAR_KW: | |
| 2279 | + { | |
| 2280 | + int na = oparg & 0xff; | |
| 2281 | + int nk = (oparg>>8) & 0xff; | |
| 2282 | + int flags = (opcode - CALL_FUNCTION) & 3; | |
| 2283 | + int n = na + 2 * nk; | |
| 2284 | + PyObject **pfunc, *func, **sp; | |
| 2285 | + PCALL(PCALL_ALL); | |
| 2286 | + if (flags & CALL_FLAG_VAR) | |
| 2287 | + n++; | |
| 2288 | + if (flags & CALL_FLAG_KW) | |
| 2289 | + n++; | |
| 2290 | + pfunc = stack_pointer - n - 1; | |
| 2291 | + func = *pfunc; | |
| 2292 | + | |
| 2293 | + if (PyMethod_Check(func) | |
| 2294 | + && PyMethod_GET_SELF(func) != NULL) { | |
| 2295 | + PyObject *self = PyMethod_GET_SELF(func); | |
| 2296 | + Py_INCREF(self); | |
| 2297 | + func = PyMethod_GET_FUNCTION(func); | |
| 2298 | + Py_INCREF(func); | |
| 2299 | + Py_DECREF(*pfunc); | |
| 2300 | + *pfunc = self; | |
| 2301 | + na++; | |
| 2302 | + n++; | |
| 2303 | + } else | |
| 2304 | + Py_INCREF(func); | |
| 2305 | + sp = stack_pointer; | |
| 2306 | + READ_TIMESTAMP(intr0); | |
| 2307 | + x = ext_do_call(func, &sp, flags, na, nk); | |
| 2308 | + READ_TIMESTAMP(intr1); | |
| 2309 | + stack_pointer = sp; | |
| 2310 | + Py_DECREF(func); | |
| 2311 | + | |
| 2312 | + while (stack_pointer > pfunc) { | |
| 2313 | + w = POP(); | |
| 2314 | + Py_DECREF(w); | |
| 2315 | + } | |
| 2316 | + PUSH(x); | |
| 2317 | + if (x != NULL) | |
| 2318 | + continue; | |
| 2319 | + break; | |
| 2320 | + } | |
| 2321 | + | |
| 2322 | + case MAKE_FUNCTION: | |
| 2323 | + v = POP(); /* code object */ | |
| 2324 | + x = PyFunction_New(v, f->f_globals); | |
| 2325 | + Py_DECREF(v); | |
| 2326 | + /* XXX Maybe this should be a separate opcode? */ | |
| 2327 | + if (x != NULL && oparg > 0) { | |
| 2328 | + v = PyTuple_New(oparg); | |
| 2329 | + if (v == NULL) { | |
| 2330 | + Py_DECREF(x); | |
| 2331 | + x = NULL; | |
| 2332 | + break; | |
| 2333 | + } | |
| 2334 | + while (--oparg >= 0) { | |
| 2335 | + w = POP(); | |
| 2336 | + PyTuple_SET_ITEM(v, oparg, w); | |
| 2337 | + } | |
| 2338 | + err = PyFunction_SetDefaults(x, v); | |
| 2339 | + Py_DECREF(v); | |
| 2340 | + } | |
| 2341 | + PUSH(x); | |
| 2342 | + break; | |
| 2343 | + | |
| 2344 | + case MAKE_CLOSURE: | |
| 2345 | + { | |
| 2346 | + v = POP(); /* code object */ | |
| 2347 | + x = PyFunction_New(v, f->f_globals); | |
| 2348 | + Py_DECREF(v); | |
| 2349 | + if (x != NULL) { | |
| 2350 | + v = POP(); | |
| 2351 | + err = PyFunction_SetClosure(x, v); | |
| 2352 | + Py_DECREF(v); | |
| 2353 | + } | |
| 2354 | + if (x != NULL && oparg > 0) { | |
| 2355 | + v = PyTuple_New(oparg); | |
| 2356 | + if (v == NULL) { | |
| 2357 | + Py_DECREF(x); | |
| 2358 | + x = NULL; | |
| 2359 | + break; | |
| 2360 | + } | |
| 2361 | + while (--oparg >= 0) { | |
| 2362 | + w = POP(); | |
| 2363 | + PyTuple_SET_ITEM(v, oparg, w); | |
| 2364 | + } | |
| 2365 | + err = PyFunction_SetDefaults(x, v); | |
| 2366 | + Py_DECREF(v); | |
| 2367 | + } | |
| 2368 | + PUSH(x); | |
| 2369 | + break; | |
| 2370 | + } | |
| 2371 | + | |
| 2372 | + case BUILD_SLICE: | |
| 2373 | + if (oparg == 3) | |
| 2374 | + w = POP(); | |
| 2375 | + else | |
| 2376 | + w = NULL; | |
| 2377 | + v = POP(); | |
| 2378 | + u = TOP(); | |
| 2379 | + x = PySlice_New(u, v, w); | |
| 2380 | + Py_DECREF(u); | |
| 2381 | + Py_DECREF(v); | |
| 2382 | + Py_XDECREF(w); | |
| 2383 | + SET_TOP(x); | |
| 2384 | + if (x != NULL) continue; | |
| 2385 | + break; | |
| 2386 | + | |
| 2387 | + case EXTENDED_ARG: | |
| 2388 | + opcode = NEXTOP(); | |
| 2389 | + oparg = oparg<<16 | NEXTARG(); | |
| 2390 | + goto dispatch_opcode; | |
| 2391 | + | |
| 2392 | + default: | |
| 2393 | + fprintf(stderr, | |
| 2394 | + "XXX lineno: %d, opcode: %d\n", | |
| 2395 | + PyCode_Addr2Line(f->f_code, f->f_lasti), | |
| 2396 | + opcode); | |
| 2397 | + PyErr_SetString(PyExc_SystemError, "unknown opcode"); | |
| 2398 | + why = WHY_EXCEPTION; | |
| 2399 | + break; | |
| 2400 | + | |
| 2401 | +#ifdef CASE_TOO_BIG | |
| 2402 | + } | |
| 2403 | +#endif | |
| 2404 | + | |
| 2405 | + } /* switch */ | |
| 2406 | + | |
| 2407 | + on_error: | |
| 2408 | + | |
| 2409 | + READ_TIMESTAMP(inst1); | |
| 2410 | + | |
| 2411 | + /* Quickly continue if no error occurred */ | |
| 2412 | + | |
| 2413 | + if (why == WHY_NOT) { | |
| 2414 | + if (err == 0 && x != NULL) { | |
| 2415 | +#ifdef CHECKEXC | |
| 2416 | + /* This check is expensive! */ | |
| 2417 | + if (PyErr_Occurred()) | |
| 2418 | + fprintf(stderr, | |
| 2419 | + "XXX undetected error\n"); | |
| 2420 | + else { | |
| 2421 | +#endif | |
| 2422 | + READ_TIMESTAMP(loop1); | |
| 2423 | + continue; /* Normal, fast path */ | |
| 2424 | +#ifdef CHECKEXC | |
| 2425 | + } | |
| 2426 | +#endif | |
| 2427 | + } | |
| 2428 | + why = WHY_EXCEPTION; | |
| 2429 | + x = Py_None; | |
| 2430 | + err = 0; | |
| 2431 | + } | |
| 2432 | + | |
| 2433 | + /* Double-check exception status */ | |
| 2434 | + | |
| 2435 | + if (why == WHY_EXCEPTION || why == WHY_RERAISE) { | |
| 2436 | + if (!PyErr_Occurred()) { | |
| 2437 | + PyErr_SetString(PyExc_SystemError, | |
| 2438 | + "error return without exception set"); | |
| 2439 | + why = WHY_EXCEPTION; | |
| 2440 | + } | |
| 2441 | + } | |
| 2442 | +#ifdef CHECKEXC | |
| 2443 | + else { | |
| 2444 | + /* This check is expensive! */ | |
| 2445 | + if (PyErr_Occurred()) { | |
| 2446 | + char buf[1024]; | |
| 2447 | + sprintf(buf, "Stack unwind with exception " | |
| 2448 | + "set and why=%d", why); | |
| 2449 | + Py_FatalError(buf); | |
| 2450 | + } | |
| 2451 | + } | |
| 2452 | +#endif | |
| 2453 | + | |
| 2454 | + /* Log traceback info if this is a real exception */ | |
| 2455 | + | |
| 2456 | + if (why == WHY_EXCEPTION) { | |
| 2457 | + PyTraceBack_Here(f); | |
| 2458 | + | |
| 2459 | + if (tstate->c_tracefunc != NULL) | |
| 2460 | + call_exc_trace(tstate->c_tracefunc, | |
| 2461 | + tstate->c_traceobj, f); | |
| 2462 | + } | |
| 2463 | + | |
| 2464 | + /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */ | |
| 2465 | + | |
| 2466 | + if (why == WHY_RERAISE) | |
| 2467 | + why = WHY_EXCEPTION; | |
| 2468 | + | |
| 2469 | + /* Unwind stacks if a (pseudo) exception occurred */ | |
| 2470 | + | |
| 2471 | +fast_block_end: | |
| 2472 | + while (why != WHY_NOT && f->f_iblock > 0) { | |
| 2473 | + PyTryBlock *b = PyFrame_BlockPop(f); | |
| 2474 | + | |
| 2475 | + assert(why != WHY_YIELD); | |
| 2476 | + if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) { | |
| 2477 | + /* For a continue inside a try block, | |
| 2478 | + don't pop the block for the loop. */ | |
| 2479 | + PyFrame_BlockSetup(f, b->b_type, b->b_handler, | |
| 2480 | + b->b_level); | |
| 2481 | + why = WHY_NOT; | |
| 2482 | + JUMPTO(PyInt_AS_LONG(retval)); | |
| 2483 | + Py_DECREF(retval); | |
| 2484 | + break; | |
| 2485 | + } | |
| 2486 | + | |
| 2487 | + while (STACK_LEVEL() > b->b_level) { | |
| 2488 | + v = POP(); | |
| 2489 | + Py_XDECREF(v); | |
| 2490 | + } | |
| 2491 | + if (b->b_type == SETUP_LOOP && why == WHY_BREAK) { | |
| 2492 | + why = WHY_NOT; | |
| 2493 | + JUMPTO(b->b_handler); | |
| 2494 | + break; | |
| 2495 | + } | |
| 2496 | + if (b->b_type == SETUP_FINALLY || | |
| 2497 | + (b->b_type == SETUP_EXCEPT && | |
| 2498 | + why == WHY_EXCEPTION)) { | |
| 2499 | + if (why == WHY_EXCEPTION) { | |
| 2500 | + PyObject *exc, *val, *tb; | |
| 2501 | + PyErr_Fetch(&exc, &val, &tb); | |
| 2502 | + if (val == NULL) { | |
| 2503 | + val = Py_None; | |
| 2504 | + Py_INCREF(val); | |
| 2505 | + } | |
| 2506 | + /* Make the raw exception data | |
| 2507 | + available to the handler, | |
| 2508 | + so a program can emulate the | |
| 2509 | + Python main loop. Don't do | |
| 2510 | + this for 'finally'. */ | |
| 2511 | + if (b->b_type == SETUP_EXCEPT) { | |
| 2512 | + PyErr_NormalizeException( | |
| 2513 | + &exc, &val, &tb); | |
| 2514 | + set_exc_info(tstate, | |
| 2515 | + exc, val, tb); | |
| 2516 | + } | |
| 2517 | + if (tb == NULL) { | |
| 2518 | + Py_INCREF(Py_None); | |
| 2519 | + PUSH(Py_None); | |
| 2520 | + } else | |
| 2521 | + PUSH(tb); | |
| 2522 | + PUSH(val); | |
| 2523 | + PUSH(exc); | |
| 2524 | + } | |
| 2525 | + else { | |
| 2526 | + if (why & (WHY_RETURN | WHY_CONTINUE)) | |
| 2527 | + PUSH(retval); | |
| 2528 | + v = PyInt_FromLong((long)why); | |
| 2529 | + PUSH(v); | |
| 2530 | + } | |
| 2531 | + why = WHY_NOT; | |
| 2532 | + JUMPTO(b->b_handler); | |
| 2533 | + break; | |
| 2534 | + } | |
| 2535 | + } /* unwind stack */ | |
| 2536 | + | |
| 2537 | + /* End the loop if we still have an error (or return) */ | |
| 2538 | + | |
| 2539 | + if (why != WHY_NOT) | |
| 2540 | + break; | |
| 2541 | + READ_TIMESTAMP(loop1); | |
| 2542 | + | |
| 2543 | + } /* main loop */ | |
| 2544 | + | |
| 2545 | + assert(why != WHY_YIELD); | |
| 2546 | + /* Pop remaining stack entries. */ | |
| 2547 | + while (!EMPTY()) { | |
| 2548 | + v = POP(); | |
| 2549 | + Py_XDECREF(v); | |
| 2550 | + } | |
| 2551 | + | |
| 2552 | + if (why != WHY_RETURN) | |
| 2553 | + retval = NULL; | |
| 2554 | + | |
| 2555 | +fast_yield: | |
| 2556 | + if (tstate->use_tracing) { | |
| 2557 | + if (tstate->c_tracefunc) { | |
| 2558 | + if (why == WHY_RETURN || why == WHY_YIELD) { | |
| 2559 | + if (call_trace(tstate->c_tracefunc, | |
| 2560 | + tstate->c_traceobj, f, | |
| 2561 | + PyTrace_RETURN, retval)) { | |
| 2562 | + Py_XDECREF(retval); | |
| 2563 | + retval = NULL; | |
| 2564 | + why = WHY_EXCEPTION; | |
| 2565 | + } | |
| 2566 | + } | |
| 2567 | + else if (why == WHY_EXCEPTION) { | |
| 2568 | + call_trace_protected(tstate->c_tracefunc, | |
| 2569 | + tstate->c_traceobj, f, | |
| 2570 | + PyTrace_RETURN, NULL); | |
| 2571 | + } | |
| 2572 | + } | |
| 2573 | + if (tstate->c_profilefunc) { | |
| 2574 | + if (why == WHY_EXCEPTION) | |
| 2575 | + call_trace_protected(tstate->c_profilefunc, | |
| 2576 | + tstate->c_profileobj, f, | |
| 2577 | + PyTrace_RETURN, NULL); | |
| 2578 | + else if (call_trace(tstate->c_profilefunc, | |
| 2579 | + tstate->c_profileobj, f, | |
| 2580 | + PyTrace_RETURN, retval)) { | |
| 2581 | + Py_XDECREF(retval); | |
| 2582 | + retval = NULL; | |
| 2583 | + why = WHY_EXCEPTION; | |
| 2584 | + } | |
| 2585 | + } | |
| 2586 | + } | |
| 2587 | + | |
| 2588 | + if (tstate->frame->f_exc_type != NULL) | |
| 2589 | + reset_exc_info(tstate); | |
| 2590 | + else { | |
| 2591 | + assert(tstate->frame->f_exc_value == NULL); | |
| 2592 | + assert(tstate->frame->f_exc_traceback == NULL); | |
| 2593 | + } | |
| 2594 | + | |
| 2595 | + /* pop frame */ | |
| 2596 | + exit_eval_frame: | |
| 2597 | + Py_LeaveRecursiveCall(); | |
| 2598 | + tstate->frame = f->f_back; | |
| 2599 | + | |
| 2600 | + return retval; | |
| 2601 | +} | |
| 2602 | + | |
| 2603 | +/* This is gonna seem *real weird*, but if you put some other code between | |
| 2604 | + PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust | |
| 2605 | + the test in the if statements in Misc/gdbinit (pystack and pystackv). */ | |
| 2606 | + | |
| 2607 | +PyObject * | |
| 2608 | +PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals, | |
| 2609 | + PyObject **args, int argcount, PyObject **kws, int kwcount, | |
| 2610 | + PyObject **defs, int defcount, PyObject *closure) | |
| 2611 | +{ | |
| 2612 | + register PyFrameObject *f; | |
| 2613 | + register PyObject *retval = NULL; | |
| 2614 | + register PyObject **fastlocals, **freevars; | |
| 2615 | + PyThreadState *tstate = PyThreadState_GET(); | |
| 2616 | + PyObject *x, *u; | |
| 2617 | + | |
| 2618 | + if (globals == NULL) { | |
| 2619 | + PyErr_SetString(PyExc_SystemError, | |
| 2620 | + "PyEval_EvalCodeEx: NULL globals"); | |
| 2621 | + return NULL; | |
| 2622 | + } | |
| 2623 | + | |
| 2624 | + assert(tstate != NULL); | |
| 2625 | + assert(globals != NULL); | |
| 2626 | + f = PyFrame_New(tstate, co, globals, locals); | |
| 2627 | + if (f == NULL) | |
| 2628 | + return NULL; | |
| 2629 | + | |
| 2630 | + fastlocals = f->f_localsplus; | |
| 2631 | + freevars = f->f_localsplus + co->co_nlocals; | |
| 2632 | + | |
| 2633 | + if (co->co_argcount > 0 || | |
| 2634 | + co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) { | |
| 2635 | + int i; | |
| 2636 | + int n = argcount; | |
| 2637 | + PyObject *kwdict = NULL; | |
| 2638 | + if (co->co_flags & CO_VARKEYWORDS) { | |
| 2639 | + kwdict = PyDict_New(); | |
| 2640 | + if (kwdict == NULL) | |
| 2641 | + goto fail; | |
| 2642 | + i = co->co_argcount; | |
| 2643 | + if (co->co_flags & CO_VARARGS) | |
| 2644 | + i++; | |
| 2645 | + SETLOCAL(i, kwdict); | |
| 2646 | + } | |
| 2647 | + if (argcount > co->co_argcount) { | |
| 2648 | + if (!(co->co_flags & CO_VARARGS)) { | |
| 2649 | + PyErr_Format(PyExc_TypeError, | |
| 2650 | + "%.200s() takes %s %d " | |
| 2651 | + "%sargument%s (%d given)", | |
| 2652 | + PyString_AsString(co->co_name), | |
| 2653 | + defcount ? "at most" : "exactly", | |
| 2654 | + co->co_argcount, | |
| 2655 | + kwcount ? "non-keyword " : "", | |
| 2656 | + co->co_argcount == 1 ? "" : "s", | |
| 2657 | + argcount); | |
| 2658 | + goto fail; | |
| 2659 | + } | |
| 2660 | + n = co->co_argcount; | |
| 2661 | + } | |
| 2662 | + for (i = 0; i < n; i++) { | |
| 2663 | + x = args[i]; | |
| 2664 | + Py_INCREF(x); | |
| 2665 | + SETLOCAL(i, x); | |
| 2666 | + } | |
| 2667 | + if (co->co_flags & CO_VARARGS) { | |
| 2668 | + u = PyTuple_New(argcount - n); | |
| 2669 | + if (u == NULL) | |
| 2670 | + goto fail; | |
| 2671 | + SETLOCAL(co->co_argcount, u); | |
| 2672 | + for (i = n; i < argcount; i++) { | |
| 2673 | + x = args[i]; | |
| 2674 | + Py_INCREF(x); | |
| 2675 | + PyTuple_SET_ITEM(u, i-n, x); | |
| 2676 | + } | |
| 2677 | + } | |
| 2678 | + for (i = 0; i < kwcount; i++) { | |
| 2679 | + PyObject *keyword = kws[2*i]; | |
| 2680 | + PyObject *value = kws[2*i + 1]; | |
| 2681 | + int j; | |
| 2682 | + if (keyword == NULL || !PyString_Check(keyword)) { | |
| 2683 | + PyErr_Format(PyExc_TypeError, | |
| 2684 | + "%.200s() keywords must be strings", | |
| 2685 | + PyString_AsString(co->co_name)); | |
| 2686 | + goto fail; | |
| 2687 | + } | |
| 2688 | + /* XXX slow -- speed up using dictionary? */ | |
| 2689 | + for (j = 0; j < co->co_argcount; j++) { | |
| 2690 | + PyObject *nm = PyTuple_GET_ITEM( | |
| 2691 | + co->co_varnames, j); | |
| 2692 | + int cmp = PyObject_RichCompareBool( | |
| 2693 | + keyword, nm, Py_EQ); | |
| 2694 | + if (cmp > 0) | |
| 2695 | + break; | |
| 2696 | + else if (cmp < 0) | |
| 2697 | + goto fail; | |
| 2698 | + } | |
| 2699 | + /* Check errors from Compare */ | |
| 2700 | + if (PyErr_Occurred()) | |
| 2701 | + goto fail; | |
| 2702 | + if (j >= co->co_argcount) { | |
| 2703 | + if (kwdict == NULL) { | |
| 2704 | + PyErr_Format(PyExc_TypeError, | |
| 2705 | + "%.200s() got an unexpected " | |
| 2706 | + "keyword argument '%.400s'", | |
| 2707 | + PyString_AsString(co->co_name), | |
| 2708 | + PyString_AsString(keyword)); | |
| 2709 | + goto fail; | |
| 2710 | + } | |
| 2711 | + PyDict_SetItem(kwdict, keyword, value); | |
| 2712 | + } | |
| 2713 | + else { | |
| 2714 | + if (GETLOCAL(j) != NULL) { | |
| 2715 | + PyErr_Format(PyExc_TypeError, | |
| 2716 | + "%.200s() got multiple " | |
| 2717 | + "values for keyword " | |
| 2718 | + "argument '%.400s'", | |
| 2719 | + PyString_AsString(co->co_name), | |
| 2720 | + PyString_AsString(keyword)); | |
| 2721 | + goto fail; | |
| 2722 | + } | |
| 2723 | + Py_INCREF(value); | |
| 2724 | + SETLOCAL(j, value); | |
| 2725 | + } | |
| 2726 | + } | |
| 2727 | + if (argcount < co->co_argcount) { | |
| 2728 | + int m = co->co_argcount - defcount; | |
| 2729 | + for (i = argcount; i < m; i++) { | |
| 2730 | + if (GETLOCAL(i) == NULL) { | |
| 2731 | + PyErr_Format(PyExc_TypeError, | |
| 2732 | + "%.200s() takes %s %d " | |
| 2733 | + "%sargument%s (%d given)", | |
| 2734 | + PyString_AsString(co->co_name), | |
| 2735 | + ((co->co_flags & CO_VARARGS) || | |
| 2736 | + defcount) ? "at least" | |
| 2737 | + : "exactly", | |
| 2738 | + m, kwcount ? "non-keyword " : "", | |
| 2739 | + m == 1 ? "" : "s", i); | |
| 2740 | + goto fail; | |
| 2741 | + } | |
| 2742 | + } | |
| 2743 | + if (n > m) | |
| 2744 | + i = n - m; | |
| 2745 | + else | |
| 2746 | + i = 0; | |
| 2747 | + for (; i < defcount; i++) { | |
| 2748 | + if (GETLOCAL(m+i) == NULL) { | |
| 2749 | + PyObject *def = defs[i]; | |
| 2750 | + Py_INCREF(def); | |
| 2751 | + SETLOCAL(m+i, def); | |
| 2752 | + } | |
| 2753 | + } | |
| 2754 | + } | |
| 2755 | + } | |
| 2756 | + else { | |
| 2757 | + if (argcount > 0 || kwcount > 0) { | |
| 2758 | + PyErr_Format(PyExc_TypeError, | |
| 2759 | + "%.200s() takes no arguments (%d given)", | |
| 2760 | + PyString_AsString(co->co_name), | |
| 2761 | + argcount + kwcount); | |
| 2762 | + goto fail; | |
| 2763 | + } | |
| 2764 | + } | |
| 2765 | + /* Allocate and initialize storage for cell vars, and copy free | |
| 2766 | + vars into frame. This isn't too efficient right now. */ | |
| 2767 | + if (PyTuple_GET_SIZE(co->co_cellvars)) { | |
| 2768 | + int i, j, nargs, found; | |
| 2769 | + char *cellname, *argname; | |
| 2770 | + PyObject *c; | |
| 2771 | + | |
| 2772 | + nargs = co->co_argcount; | |
| 2773 | + if (co->co_flags & CO_VARARGS) | |
| 2774 | + nargs++; | |
| 2775 | + if (co->co_flags & CO_VARKEYWORDS) | |
| 2776 | + nargs++; | |
| 2777 | + | |
| 2778 | + /* Initialize each cell var, taking into account | |
| 2779 | + cell vars that are initialized from arguments. | |
| 2780 | + | |
| 2781 | + Should arrange for the compiler to put cellvars | |
| 2782 | + that are arguments at the beginning of the cellvars | |
| 2783 | + list so that we can march over it more efficiently? | |
| 2784 | + */ | |
| 2785 | + for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) { | |
| 2786 | + cellname = PyString_AS_STRING( | |
| 2787 | + PyTuple_GET_ITEM(co->co_cellvars, i)); | |
| 2788 | + found = 0; | |
| 2789 | + for (j = 0; j < nargs; j++) { | |
| 2790 | + argname = PyString_AS_STRING( | |
| 2791 | + PyTuple_GET_ITEM(co->co_varnames, j)); | |
| 2792 | + if (strcmp(cellname, argname) == 0) { | |
| 2793 | + c = PyCell_New(GETLOCAL(j)); | |
| 2794 | + if (c == NULL) | |
| 2795 | + goto fail; | |
| 2796 | + GETLOCAL(co->co_nlocals + i) = c; | |
| 2797 | + found = 1; | |
| 2798 | + break; | |
| 2799 | + } | |
| 2800 | + } | |
| 2801 | + if (found == 0) { | |
| 2802 | + c = PyCell_New(NULL); | |
| 2803 | + if (c == NULL) | |
| 2804 | + goto fail; | |
| 2805 | + SETLOCAL(co->co_nlocals + i, c); | |
| 2806 | + } | |
| 2807 | + } | |
| 2808 | + } | |
| 2809 | + if (PyTuple_GET_SIZE(co->co_freevars)) { | |
| 2810 | + int i; | |
| 2811 | + for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) { | |
| 2812 | + PyObject *o = PyTuple_GET_ITEM(closure, i); | |
| 2813 | + Py_INCREF(o); | |
| 2814 | + freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o; | |
| 2815 | + } | |
| 2816 | + } | |
| 2817 | + | |
| 2818 | + if (co->co_flags & CO_GENERATOR) { | |
| 2819 | + /* Don't need to keep the reference to f_back, it will be set | |
| 2820 | + * when the generator is resumed. */ | |
| 2821 | + Py_XDECREF(f->f_back); | |
| 2822 | + f->f_back = NULL; | |
| 2823 | + | |
| 2824 | + PCALL(PCALL_GENERATOR); | |
| 2825 | + | |
| 2826 | + /* Create a new generator that owns the ready to run frame | |
| 2827 | + * and return that as the value. */ | |
| 2828 | + return PyGen_New(f); | |
| 2829 | + } | |
| 2830 | + | |
| 2831 | + retval = PyEval_EvalFrameEx(f,0); | |
| 2832 | + | |
| 2833 | + fail: /* Jump here from prelude on failure */ | |
| 2834 | + | |
| 2835 | + /* decref'ing the frame can cause __del__ methods to get invoked, | |
| 2836 | + which can call back into Python. While we're done with the | |
| 2837 | + current Python frame (f), the associated C stack is still in use, | |
| 2838 | + so recursion_depth must be boosted for the duration. | |
| 2839 | + */ | |
| 2840 | + assert(tstate != NULL); | |
| 2841 | + ++tstate->recursion_depth; | |
| 2842 | + Py_DECREF(f); | |
| 2843 | + --tstate->recursion_depth; | |
| 2844 | + return retval; | |
| 2845 | +} | |
| 2846 | + | |
| 2847 | + | |
| 2848 | +/* Implementation notes for set_exc_info() and reset_exc_info(): | |
| 2849 | + | |
| 2850 | +- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and | |
| 2851 | + 'exc_traceback'. These always travel together. | |
| 2852 | + | |
| 2853 | +- tstate->curexc_ZZZ is the "hot" exception that is set by | |
| 2854 | + PyErr_SetString(), cleared by PyErr_Clear(), and so on. | |
| 2855 | + | |
| 2856 | +- Once an exception is caught by an except clause, it is transferred | |
| 2857 | + from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info() | |
| 2858 | + can pick it up. This is the primary task of set_exc_info(). | |
| 2859 | + XXX That can't be right: set_exc_info() doesn't look at tstate->curexc_ZZZ. | |
| 2860 | + | |
| 2861 | +- Now let me explain the complicated dance with frame->f_exc_ZZZ. | |
| 2862 | + | |
| 2863 | + Long ago, when none of this existed, there were just a few globals: | |
| 2864 | + one set corresponding to the "hot" exception, and one set | |
| 2865 | + corresponding to sys.exc_ZZZ. (Actually, the latter weren't C | |
| 2866 | + globals; they were simply stored as sys.exc_ZZZ. For backwards | |
| 2867 | + compatibility, they still are!) The problem was that in code like | |
| 2868 | + this: | |
| 2869 | + | |
| 2870 | + try: | |
| 2871 | + "something that may fail" | |
| 2872 | + except "some exception": | |
| 2873 | + "do something else first" | |
| 2874 | + "print the exception from sys.exc_ZZZ." | |
| 2875 | + | |
| 2876 | + if "do something else first" invoked something that raised and caught | |
| 2877 | + an exception, sys.exc_ZZZ were overwritten. That was a frequent | |
| 2878 | + cause of subtle bugs. I fixed this by changing the semantics as | |
| 2879 | + follows: | |
| 2880 | + | |
| 2881 | + - Within one frame, sys.exc_ZZZ will hold the last exception caught | |
| 2882 | + *in that frame*. | |
| 2883 | + | |
| 2884 | + - But initially, and as long as no exception is caught in a given | |
| 2885 | + frame, sys.exc_ZZZ will hold the last exception caught in the | |
| 2886 | + previous frame (or the frame before that, etc.). | |
| 2887 | + | |
| 2888 | + The first bullet fixed the bug in the above example. The second | |
| 2889 | + bullet was for backwards compatibility: it was (and is) common to | |
| 2890 | + have a function that is called when an exception is caught, and to | |
| 2891 | + have that function access the caught exception via sys.exc_ZZZ. | |
| 2892 | + (Example: traceback.print_exc()). | |
| 2893 | + | |
| 2894 | + At the same time I fixed the problem that sys.exc_ZZZ weren't | |
| 2895 | + thread-safe, by introducing sys.exc_info() which gets it from tstate; | |
| 2896 | + but that's really a separate improvement. | |
| 2897 | + | |
| 2898 | + The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ | |
| 2899 | + variables to what they were before the current frame was called. The | |
| 2900 | + set_exc_info() function saves them on the frame so that | |
| 2901 | + reset_exc_info() can restore them. The invariant is that | |
| 2902 | + frame->f_exc_ZZZ is NULL iff the current frame never caught an | |
| 2903 | + exception (where "catching" an exception applies only to successful | |
| 2904 | + except clauses); and if the current frame ever caught an exception, | |
| 2905 | + frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ | |
| 2906 | + at the start of the current frame. | |
| 2907 | + | |
| 2908 | +*/ | |
| 2909 | + | |
| 2910 | +static void | |
| 2911 | +set_exc_info(PyThreadState *tstate, | |
| 2912 | + PyObject *type, PyObject *value, PyObject *tb) | |
| 2913 | +{ | |
| 2914 | + PyFrameObject *frame = tstate->frame; | |
| 2915 | + PyObject *tmp_type, *tmp_value, *tmp_tb; | |
| 2916 | + | |
| 2917 | + assert(type != NULL); | |
| 2918 | + assert(frame != NULL); | |
| 2919 | + if (frame->f_exc_type == NULL) { | |
| 2920 | + assert(frame->f_exc_value == NULL); | |
| 2921 | + assert(frame->f_exc_traceback == NULL); | |
| 2922 | + /* This frame didn't catch an exception before. */ | |
| 2923 | + /* Save previous exception of this thread in this frame. */ | |
| 2924 | + if (tstate->exc_type == NULL) { | |
| 2925 | + /* XXX Why is this set to Py_None? */ | |
| 2926 | + Py_INCREF(Py_None); | |
| 2927 | + tstate->exc_type = Py_None; | |
| 2928 | + } | |
| 2929 | + Py_INCREF(tstate->exc_type); | |
| 2930 | + Py_XINCREF(tstate->exc_value); | |
| 2931 | + Py_XINCREF(tstate->exc_traceback); | |
| 2932 | + frame->f_exc_type = tstate->exc_type; | |
| 2933 | + frame->f_exc_value = tstate->exc_value; | |
| 2934 | + frame->f_exc_traceback = tstate->exc_traceback; | |
| 2935 | + } | |
| 2936 | + /* Set new exception for this thread. */ | |
| 2937 | + tmp_type = tstate->exc_type; | |
| 2938 | + tmp_value = tstate->exc_value; | |
| 2939 | + tmp_tb = tstate->exc_traceback; | |
| 2940 | + Py_INCREF(type); | |
| 2941 | + Py_XINCREF(value); | |
| 2942 | + Py_XINCREF(tb); | |
| 2943 | + tstate->exc_type = type; | |
| 2944 | + tstate->exc_value = value; | |
| 2945 | + tstate->exc_traceback = tb; | |
| 2946 | + Py_XDECREF(tmp_type); | |
| 2947 | + Py_XDECREF(tmp_value); | |
| 2948 | + Py_XDECREF(tmp_tb); | |
| 2949 | + /* For b/w compatibility */ | |
| 2950 | + PySys_SetObject("exc_type", type); | |
| 2951 | + PySys_SetObject("exc_value", value); | |
| 2952 | + PySys_SetObject("exc_traceback", tb); | |
| 2953 | +} | |
| 2954 | + | |
| 2955 | +static void | |
| 2956 | +reset_exc_info(PyThreadState *tstate) | |
| 2957 | +{ | |
| 2958 | + PyFrameObject *frame; | |
| 2959 | + PyObject *tmp_type, *tmp_value, *tmp_tb; | |
| 2960 | + | |
| 2961 | + /* It's a precondition that the thread state's frame caught an | |
| 2962 | + * exception -- verify in a debug build. | |
| 2963 | + */ | |
| 2964 | + assert(tstate != NULL); | |
| 2965 | + frame = tstate->frame; | |
| 2966 | + assert(frame != NULL); | |
| 2967 | + assert(frame->f_exc_type != NULL); | |
| 2968 | + | |
| 2969 | + /* Copy the frame's exception info back to the thread state. */ | |
| 2970 | + tmp_type = tstate->exc_type; | |
| 2971 | + tmp_value = tstate->exc_value; | |
| 2972 | + tmp_tb = tstate->exc_traceback; | |
| 2973 | + Py_INCREF(frame->f_exc_type); | |
| 2974 | + Py_XINCREF(frame->f_exc_value); | |
| 2975 | + Py_XINCREF(frame->f_exc_traceback); | |
| 2976 | + tstate->exc_type = frame->f_exc_type; | |
| 2977 | + tstate->exc_value = frame->f_exc_value; | |
| 2978 | + tstate->exc_traceback = frame->f_exc_traceback; | |
| 2979 | + Py_XDECREF(tmp_type); | |
| 2980 | + Py_XDECREF(tmp_value); | |
| 2981 | + Py_XDECREF(tmp_tb); | |
| 2982 | + | |
| 2983 | + /* For b/w compatibility */ | |
| 2984 | + PySys_SetObject("exc_type", frame->f_exc_type); | |
| 2985 | + PySys_SetObject("exc_value", frame->f_exc_value); | |
| 2986 | + PySys_SetObject("exc_traceback", frame->f_exc_traceback); | |
| 2987 | + | |
| 2988 | + /* Clear the frame's exception info. */ | |
| 2989 | + tmp_type = frame->f_exc_type; | |
| 2990 | + tmp_value = frame->f_exc_value; | |
| 2991 | + tmp_tb = frame->f_exc_traceback; | |
| 2992 | + frame->f_exc_type = NULL; | |
| 2993 | + frame->f_exc_value = NULL; | |
| 2994 | + frame->f_exc_traceback = NULL; | |
| 2995 | + Py_DECREF(tmp_type); | |
| 2996 | + Py_XDECREF(tmp_value); | |
| 2997 | + Py_XDECREF(tmp_tb); | |
| 2998 | +} | |
| 2999 | + | |
| 3000 | +/* Logic for the raise statement (too complicated for inlining). | |
| 3001 | + This *consumes* a reference count to each of its arguments. */ | |
| 3002 | +static enum why_code | |
| 3003 | +do_raise(PyObject *type, PyObject *value, PyObject *tb) | |
| 3004 | +{ | |
| 3005 | + if (type == NULL) { | |
| 3006 | + /* Reraise */ | |
| 3007 | + PyThreadState *tstate = PyThreadState_GET(); | |
| 3008 | + type = tstate->exc_type == NULL ? Py_None : tstate->exc_type; | |
| 3009 | + value = tstate->exc_value; | |
| 3010 | + tb = tstate->exc_traceback; | |
| 3011 | + Py_XINCREF(type); | |
| 3012 | + Py_XINCREF(value); | |
| 3013 | + Py_XINCREF(tb); | |
| 3014 | + } | |
| 3015 | + | |
| 3016 | + /* We support the following forms of raise: | |
| 3017 | + raise <class>, <classinstance> | |
| 3018 | + raise <class>, <argument tuple> | |
| 3019 | + raise <class>, None | |
| 3020 | + raise <class>, <argument> | |
| 3021 | + raise <classinstance>, None | |
| 3022 | + raise <string>, <object> | |
| 3023 | + raise <string>, None | |
| 3024 | + | |
| 3025 | + An omitted second argument is the same as None. | |
| 3026 | + | |
| 3027 | + In addition, raise <tuple>, <anything> is the same as | |
| 3028 | + raising the tuple's first item (and it better have one!); | |
| 3029 | + this rule is applied recursively. | |
| 3030 | + | |
| 3031 | + Finally, an optional third argument can be supplied, which | |
| 3032 | + gives the traceback to be substituted (useful when | |
| 3033 | + re-raising an exception after examining it). */ | |
| 3034 | + | |
| 3035 | + /* First, check the traceback argument, replacing None with | |
| 3036 | + NULL. */ | |
| 3037 | + if (tb == Py_None) { | |
| 3038 | + Py_DECREF(tb); | |
| 3039 | + tb = NULL; | |
| 3040 | + } | |
| 3041 | + else if (tb != NULL && !PyTraceBack_Check(tb)) { | |
| 3042 | + PyErr_SetString(PyExc_TypeError, | |
| 3043 | + "raise: arg 3 must be a traceback or None"); | |
| 3044 | + goto raise_error; | |
| 3045 | + } | |
| 3046 | + | |
| 3047 | + /* Next, replace a missing value with None */ | |
| 3048 | + if (value == NULL) { | |
| 3049 | + value = Py_None; | |
| 3050 | + Py_INCREF(value); | |
| 3051 | + } | |
| 3052 | + | |
| 3053 | + /* Next, repeatedly, replace a tuple exception with its first item */ | |
| 3054 | + while (PyTuple_Check(type) && PyTuple_Size(type) > 0) { | |
| 3055 | + PyObject *tmp = type; | |
| 3056 | + type = PyTuple_GET_ITEM(type, 0); | |
| 3057 | + Py_INCREF(type); | |
| 3058 | + Py_DECREF(tmp); | |
| 3059 | + } | |
| 3060 | + | |
| 3061 | + if (PyString_CheckExact(type)) { | |
| 3062 | + /* Raising builtin string is deprecated but still allowed -- | |
| 3063 | + * do nothing. Raising an instance of a new-style str | |
| 3064 | + * subclass is right out. */ | |
| 3065 | + if (PyErr_Warn(PyExc_DeprecationWarning, | |
| 3066 | + "raising a string exception is deprecated")) | |
| 3067 | + goto raise_error; | |
| 3068 | + } | |
| 3069 | + else if (PyExceptionClass_Check(type)) | |
| 3070 | + PyErr_NormalizeException(&type, &value, &tb); | |
| 3071 | + | |
| 3072 | + else if (PyExceptionInstance_Check(type)) { | |
| 3073 | + /* Raising an instance. The value should be a dummy. */ | |
| 3074 | + if (value != Py_None) { | |
| 3075 | + PyErr_SetString(PyExc_TypeError, | |
| 3076 | + "instance exception may not have a separate value"); | |
| 3077 | + goto raise_error; | |
| 3078 | + } | |
| 3079 | + else { | |
| 3080 | + /* Normalize to raise <class>, <instance> */ | |
| 3081 | + Py_DECREF(value); | |
| 3082 | + value = type; | |
| 3083 | + type = PyExceptionInstance_Class(type); | |
| 3084 | + Py_INCREF(type); | |
| 3085 | + } | |
| 3086 | + } | |
| 3087 | + else { | |
| 3088 | + /* Not something you can raise. You get an exception | |
| 3089 | + anyway, just not what you specified :-) */ | |
| 3090 | + PyErr_Format(PyExc_TypeError, | |
| 3091 | + "exceptions must be classes, instances, or " | |
| 3092 | + "strings (deprecated), not %s", | |
| 3093 | + type->ob_type->tp_name); | |
| 3094 | + goto raise_error; | |
| 3095 | + } | |
| 3096 | + PyErr_Restore(type, value, tb); | |
| 3097 | + if (tb == NULL) | |
| 3098 | + return WHY_EXCEPTION; | |
| 3099 | + else | |
| 3100 | + return WHY_RERAISE; | |
| 3101 | + raise_error: | |
| 3102 | + Py_XDECREF(value); | |
| 3103 | + Py_XDECREF(type); | |
| 3104 | + Py_XDECREF(tb); | |
| 3105 | + return WHY_EXCEPTION; | |
| 3106 | +} | |
| 3107 | + | |
| 3108 | +/* Iterate v argcnt times and store the results on the stack (via decreasing | |
| 3109 | + sp). Return 1 for success, 0 if error. */ | |
| 3110 | + | |
| 3111 | +static int | |
| 3112 | +unpack_iterable(PyObject *v, int argcnt, PyObject **sp) | |
| 3113 | +{ | |
| 3114 | + int i = 0; | |
| 3115 | + PyObject *it; /* iter(v) */ | |
| 3116 | + PyObject *w; | |
| 3117 | + | |
| 3118 | + assert(v != NULL); | |
| 3119 | + | |
| 3120 | + it = PyObject_GetIter(v); | |
| 3121 | + if (it == NULL) | |
| 3122 | + goto Error; | |
| 3123 | + | |
| 3124 | + for (; i < argcnt; i++) { | |
| 3125 | + w = PyIter_Next(it); | |
| 3126 | + if (w == NULL) { | |
| 3127 | + /* Iterator done, via error or exhaustion. */ | |
| 3128 | + if (!PyErr_Occurred()) { | |
| 3129 | + PyErr_Format(PyExc_ValueError, | |
| 3130 | + "need more than %d value%s to unpack", | |
| 3131 | + i, i == 1 ? "" : "s"); | |
| 3132 | + } | |
| 3133 | + goto Error; | |
| 3134 | + } | |
| 3135 | + *--sp = w; | |
| 3136 | + } | |
| 3137 | + | |
| 3138 | + /* We better have exhausted the iterator now. */ | |
| 3139 | + w = PyIter_Next(it); | |
| 3140 | + if (w == NULL) { | |
| 3141 | + if (PyErr_Occurred()) | |
| 3142 | + goto Error; | |
| 3143 | + Py_DECREF(it); | |
| 3144 | + return 1; | |
| 3145 | + } | |
| 3146 | + Py_DECREF(w); | |
| 3147 | + PyErr_SetString(PyExc_ValueError, "too many values to unpack"); | |
| 3148 | + /* fall through */ | |
| 3149 | +Error: | |
| 3150 | + for (; i > 0; i--, sp++) | |
| 3151 | + Py_DECREF(*sp); | |
| 3152 | + Py_XDECREF(it); | |
| 3153 | + return 0; | |
| 3154 | +} | |
| 3155 | + | |
| 3156 | + | |
| 3157 | +#ifdef LLTRACE | |
| 3158 | +static int | |
| 3159 | +prtrace(PyObject *v, char *str) | |
| 3160 | +{ | |
| 3161 | + printf("%s ", str); | |
| 3162 | + if (PyObject_Print(v, stdout, 0) != 0) | |
| 3163 | + PyErr_Clear(); /* Don't know what else to do */ | |
| 3164 | + printf("\n"); | |
| 3165 | + return 1; | |
| 3166 | +} | |
| 3167 | +#endif | |
| 3168 | + | |
| 3169 | +static void | |
| 3170 | +call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f) | |
| 3171 | +{ | |
| 3172 | + PyObject *type, *value, *traceback, *arg; | |
| 3173 | + int err; | |
| 3174 | + PyErr_Fetch(&type, &value, &traceback); | |
| 3175 | + if (value == NULL) { | |
| 3176 | + value = Py_None; | |
| 3177 | + Py_INCREF(value); | |
| 3178 | + } | |
| 3179 | + arg = PyTuple_Pack(3, type, value, traceback); | |
| 3180 | + if (arg == NULL) { | |
| 3181 | + PyErr_Restore(type, value, traceback); | |
| 3182 | + return; | |
| 3183 | + } | |
| 3184 | + err = call_trace(func, self, f, PyTrace_EXCEPTION, arg); | |
| 3185 | + Py_DECREF(arg); | |
| 3186 | + if (err == 0) | |
| 3187 | + PyErr_Restore(type, value, traceback); | |
| 3188 | + else { | |
| 3189 | + Py_XDECREF(type); | |
| 3190 | + Py_XDECREF(value); | |
| 3191 | + Py_XDECREF(traceback); | |
| 3192 | + } | |
| 3193 | +} | |
| 3194 | + | |
| 3195 | +static void | |
| 3196 | +call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame, | |
| 3197 | + int what, PyObject *arg) | |
| 3198 | +{ | |
| 3199 | + PyObject *type, *value, *traceback; | |
| 3200 | + int err; | |
| 3201 | + PyErr_Fetch(&type, &value, &traceback); | |
| 3202 | + err = call_trace(func, obj, frame, what, arg); | |
| 3203 | + if (err == 0) | |
| 3204 | + PyErr_Restore(type, value, traceback); | |
| 3205 | + else { | |
| 3206 | + Py_XDECREF(type); | |
| 3207 | + Py_XDECREF(value); | |
| 3208 | + Py_XDECREF(traceback); | |
| 3209 | + } | |
| 3210 | +} | |
| 3211 | + | |
| 3212 | +static int | |
| 3213 | +call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame, | |
| 3214 | + int what, PyObject *arg) | |
| 3215 | +{ | |
| 3216 | + register PyThreadState *tstate = frame->f_tstate; | |
| 3217 | + int result; | |
| 3218 | + if (tstate->tracing) | |
| 3219 | + return 0; | |
| 3220 | + tstate->tracing++; | |
| 3221 | + tstate->use_tracing = 0; | |
| 3222 | + result = func(obj, frame, what, arg); | |
| 3223 | + tstate->use_tracing = ((tstate->c_tracefunc != NULL) | |
| 3224 | + || (tstate->c_profilefunc != NULL)); | |
| 3225 | + tstate->tracing--; | |
| 3226 | + return result; | |
| 3227 | +} | |
| 3228 | + | |
| 3229 | +PyObject * | |
| 3230 | +_PyEval_CallTracing(PyObject *func, PyObject *args) | |
| 3231 | +{ | |
| 3232 | + PyFrameObject *frame = PyEval_GetFrame(); | |
| 3233 | + PyThreadState *tstate = frame->f_tstate; | |
| 3234 | + int save_tracing = tstate->tracing; | |
| 3235 | + int save_use_tracing = tstate->use_tracing; | |
| 3236 | + PyObject *result; | |
| 3237 | + | |
| 3238 | + tstate->tracing = 0; | |
| 3239 | + tstate->use_tracing = ((tstate->c_tracefunc != NULL) | |
| 3240 | + || (tstate->c_profilefunc != NULL)); | |
| 3241 | + result = PyObject_Call(func, args, NULL); | |
| 3242 | + tstate->tracing = save_tracing; | |
| 3243 | + tstate->use_tracing = save_use_tracing; | |
| 3244 | + return result; | |
| 3245 | +} | |
| 3246 | + | |
| 3247 | +static int | |
| 3248 | +maybe_call_line_trace(Py_tracefunc func, PyObject *obj, | |
| 3249 | + PyFrameObject *frame, int *instr_lb, int *instr_ub, | |
| 3250 | + int *instr_prev) | |
| 3251 | +{ | |
| 3252 | + int result = 0; | |
| 3253 | + | |
| 3254 | + /* If the last instruction executed isn't in the current | |
| 3255 | + instruction window, reset the window. If the last | |
| 3256 | + instruction happens to fall at the start of a line or if it | |
| 3257 | + represents a jump backwards, call the trace function. | |
| 3258 | + */ | |
| 3259 | + if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) { | |
| 3260 | + int line; | |
| 3261 | + PyAddrPair bounds; | |
| 3262 | + | |
| 3263 | + line = PyCode_CheckLineNumber(frame->f_code, frame->f_lasti, | |
| 3264 | + &bounds); | |
| 3265 | + if (line >= 0) { | |
| 3266 | + frame->f_lineno = line; | |
| 3267 | + result = call_trace(func, obj, frame, | |
| 3268 | + PyTrace_LINE, Py_None); | |
| 3269 | + } | |
| 3270 | + *instr_lb = bounds.ap_lower; | |
| 3271 | + *instr_ub = bounds.ap_upper; | |
| 3272 | + } | |
| 3273 | + else if (frame->f_lasti <= *instr_prev) { | |
| 3274 | + result = call_trace(func, obj, frame, PyTrace_LINE, Py_None); | |
| 3275 | + } | |
| 3276 | + *instr_prev = frame->f_lasti; | |
| 3277 | + return result; | |
| 3278 | +} | |
| 3279 | + | |
| 3280 | +void | |
| 3281 | +PyEval_SetProfile(Py_tracefunc func, PyObject *arg) | |
| 3282 | +{ | |
| 3283 | + PyThreadState *tstate = PyThreadState_GET(); | |
| 3284 | + PyObject *temp = tstate->c_profileobj; | |
| 3285 | + Py_XINCREF(arg); | |
| 3286 | + tstate->c_profilefunc = NULL; | |
| 3287 | + tstate->c_profileobj = NULL; | |
| 3288 | + /* Must make sure that tracing is not ignored if 'temp' is freed */ | |
| 3289 | + tstate->use_tracing = tstate->c_tracefunc != NULL; | |
| 3290 | + Py_XDECREF(temp); | |
| 3291 | + tstate->c_profilefunc = func; | |
| 3292 | + tstate->c_profileobj = arg; | |
| 3293 | + /* Flag that tracing or profiling is turned on */ | |
| 3294 | + tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL); | |
| 3295 | +} | |
| 3296 | + | |
| 3297 | +void | |
| 3298 | +PyEval_SetTrace(Py_tracefunc func, PyObject *arg) | |
| 3299 | +{ | |
| 3300 | + PyThreadState *tstate = PyThreadState_GET(); | |
| 3301 | + PyObject *temp = tstate->c_traceobj; | |
| 3302 | + Py_XINCREF(arg); | |
| 3303 | + tstate->c_tracefunc = NULL; | |
| 3304 | + tstate->c_traceobj = NULL; | |
| 3305 | + /* Must make sure that profiling is not ignored if 'temp' is freed */ | |
| 3306 | + tstate->use_tracing = tstate->c_profilefunc != NULL; | |
| 3307 | + Py_XDECREF(temp); | |
| 3308 | + tstate->c_tracefunc = func; | |
| 3309 | + tstate->c_traceobj = arg; | |
| 3310 | + /* Flag that tracing or profiling is turned on */ | |
| 3311 | + tstate->use_tracing = ((func != NULL) | |
| 3312 | + || (tstate->c_profilefunc != NULL)); | |
| 3313 | +} | |
| 3314 | + | |
| 3315 | +PyObject * | |
| 3316 | +PyEval_GetBuiltins(void) | |
| 3317 | +{ | |
| 3318 | + PyFrameObject *current_frame = PyEval_GetFrame(); | |
| 3319 | + if (current_frame == NULL) | |
| 3320 | + return PyThreadState_GET()->interp->builtins; | |
| 3321 | + else | |
| 3322 | + return current_frame->f_builtins; | |
| 3323 | +} | |
| 3324 | + | |
| 3325 | +PyObject * | |
| 3326 | +PyEval_GetLocals(void) | |
| 3327 | +{ | |
| 3328 | + PyFrameObject *current_frame = PyEval_GetFrame(); | |
| 3329 | + if (current_frame == NULL) | |
| 3330 | + return NULL; | |
| 3331 | + PyFrame_FastToLocals(current_frame); | |
| 3332 | + return current_frame->f_locals; | |
| 3333 | +} | |
| 3334 | + | |
| 3335 | +PyObject * | |
| 3336 | +PyEval_GetGlobals(void) | |
| 3337 | +{ | |
| 3338 | + PyFrameObject *current_frame = PyEval_GetFrame(); | |
| 3339 | + if (current_frame == NULL) | |
| 3340 | + return NULL; | |
| 3341 | + else | |
| 3342 | + return current_frame->f_globals; | |
| 3343 | +} | |
| 3344 | + | |
| 3345 | +PyFrameObject * | |
| 3346 | +PyEval_GetFrame(void) | |
| 3347 | +{ | |
| 3348 | + PyThreadState *tstate = PyThreadState_GET(); | |
| 3349 | + return _PyThreadState_GetFrame(tstate); | |
| 3350 | +} | |
| 3351 | + | |
| 3352 | +int | |
| 3353 | +PyEval_GetRestricted(void) | |
| 3354 | +{ | |
| 3355 | + PyFrameObject *current_frame = PyEval_GetFrame(); | |
| 3356 | + return current_frame == NULL ? 0 : PyFrame_IsRestricted(current_frame); | |
| 3357 | +} | |
| 3358 | + | |
| 3359 | +int | |
| 3360 | +PyEval_MergeCompilerFlags(PyCompilerFlags *cf) | |
| 3361 | +{ | |
| 3362 | + PyFrameObject *current_frame = PyEval_GetFrame(); | |
| 3363 | + int result = cf->cf_flags != 0; | |
| 3364 | + | |
| 3365 | + if (current_frame != NULL) { | |
| 3366 | + const int codeflags = current_frame->f_code->co_flags; | |
| 3367 | + const int compilerflags = codeflags & PyCF_MASK; | |
| 3368 | + if (compilerflags) { | |
| 3369 | + result = 1; | |
| 3370 | + cf->cf_flags |= compilerflags; | |
| 3371 | + } | |
| 3372 | +#if 0 /* future keyword */ | |
| 3373 | + if (codeflags & CO_GENERATOR_ALLOWED) { | |
| 3374 | + result = 1; | |
| 3375 | + cf->cf_flags |= CO_GENERATOR_ALLOWED; | |
| 3376 | + } | |
| 3377 | +#endif | |
| 3378 | + } | |
| 3379 | + return result; | |
| 3380 | +} | |
| 3381 | + | |
| 3382 | +int | |
| 3383 | +Py_FlushLine(void) | |
| 3384 | +{ | |
| 3385 | + PyObject *f = PySys_GetObject("stdout"); | |
| 3386 | + if (f == NULL) | |
| 3387 | + return 0; | |
| 3388 | + if (!PyFile_SoftSpace(f, 0)) | |
| 3389 | + return 0; | |
| 3390 | + return PyFile_WriteString("\n", f); | |
| 3391 | +} | |
| 3392 | + | |
| 3393 | + | |
| 3394 | +/* External interface to call any callable object. | |
| 3395 | + The arg must be a tuple or NULL. */ | |
| 3396 | + | |
| 3397 | +#undef PyEval_CallObject | |
| 3398 | +/* for backward compatibility: export this interface */ | |
| 3399 | + | |
| 3400 | +PyObject * | |
| 3401 | +PyEval_CallObject(PyObject *func, PyObject *arg) | |
| 3402 | +{ | |
| 3403 | + return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL); | |
| 3404 | +} | |
| 3405 | +#define PyEval_CallObject(func,arg) \ | |
| 3406 | + PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL) | |
| 3407 | + | |
| 3408 | +PyObject * | |
| 3409 | +PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw) | |
| 3410 | +{ | |
| 3411 | + PyObject *result; | |
| 3412 | + | |
| 3413 | + if (arg == NULL) { | |
| 3414 | + arg = PyTuple_New(0); | |
| 3415 | + if (arg == NULL) | |
| 3416 | + return NULL; | |
| 3417 | + } | |
| 3418 | + else if (!PyTuple_Check(arg)) { | |
| 3419 | + PyErr_SetString(PyExc_TypeError, | |
| 3420 | + "argument list must be a tuple"); | |
| 3421 | + return NULL; | |
| 3422 | + } | |
| 3423 | + else | |
| 3424 | + Py_INCREF(arg); | |
| 3425 | + | |
| 3426 | + if (kw != NULL && !PyDict_Check(kw)) { | |
| 3427 | + PyErr_SetString(PyExc_TypeError, | |
| 3428 | + "keyword list must be a dictionary"); | |
| 3429 | + Py_DECREF(arg); | |
| 3430 | + return NULL; | |
| 3431 | + } | |
| 3432 | + | |
| 3433 | + result = PyObject_Call(func, arg, kw); | |
| 3434 | + Py_DECREF(arg); | |
| 3435 | + return result; | |
| 3436 | +} | |
| 3437 | + | |
| 3438 | +const char * | |
| 3439 | +PyEval_GetFuncName(PyObject *func) | |
| 3440 | +{ | |
| 3441 | + if (PyMethod_Check(func)) | |
| 3442 | + return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func)); | |
| 3443 | + else if (PyFunction_Check(func)) | |
| 3444 | + return PyString_AsString(((PyFunctionObject*)func)->func_name); | |
| 3445 | + else if (PyCFunction_Check(func)) | |
| 3446 | + return ((PyCFunctionObject*)func)->m_ml->ml_name; | |
| 3447 | + else if (PyClass_Check(func)) | |
| 3448 | + return PyString_AsString(((PyClassObject*)func)->cl_name); | |
| 3449 | + else if (PyInstance_Check(func)) { | |
| 3450 | + return PyString_AsString( | |
| 3451 | + ((PyInstanceObject*)func)->in_class->cl_name); | |
| 3452 | + } else { | |
| 3453 | + return func->ob_type->tp_name; | |
| 3454 | + } | |
| 3455 | +} | |
| 3456 | + | |
| 3457 | +const char * | |
| 3458 | +PyEval_GetFuncDesc(PyObject *func) | |
| 3459 | +{ | |
| 3460 | + if (PyMethod_Check(func)) | |
| 3461 | + return "()"; | |
| 3462 | + else if (PyFunction_Check(func)) | |
| 3463 | + return "()"; | |
| 3464 | + else if (PyCFunction_Check(func)) | |
| 3465 | + return "()"; | |
| 3466 | + else if (PyClass_Check(func)) | |
| 3467 | + return " constructor"; | |
| 3468 | + else if (PyInstance_Check(func)) { | |
| 3469 | + return " instance"; | |
| 3470 | + } else { | |
| 3471 | + return " object"; | |
| 3472 | + } | |
| 3473 | +} | |
| 3474 | + | |
| 3475 | +static void | |
| 3476 | +err_args(PyObject *func, int flags, int nargs) | |
| 3477 | +{ | |
| 3478 | + if (flags & METH_NOARGS) | |
| 3479 | + PyErr_Format(PyExc_TypeError, | |
| 3480 | + "%.200s() takes no arguments (%d given)", | |
| 3481 | + ((PyCFunctionObject *)func)->m_ml->ml_name, | |
| 3482 | + nargs); | |
| 3483 | + else | |
| 3484 | + PyErr_Format(PyExc_TypeError, | |
| 3485 | + "%.200s() takes exactly one argument (%d given)", | |
| 3486 | + ((PyCFunctionObject *)func)->m_ml->ml_name, | |
| 3487 | + nargs); | |
| 3488 | +} | |
| 3489 | + | |
| 3490 | +#define C_TRACE(x, call) \ | |
| 3491 | +if (tstate->use_tracing && tstate->c_profilefunc) { \ | |
| 3492 | + if (call_trace(tstate->c_profilefunc, \ | |
| 3493 | + tstate->c_profileobj, \ | |
| 3494 | + tstate->frame, PyTrace_C_CALL, \ | |
| 3495 | + func)) { \ | |
| 3496 | + x = NULL; \ | |
| 3497 | + } \ | |
| 3498 | + else { \ | |
| 3499 | + x = call; \ | |
| 3500 | + if (tstate->c_profilefunc != NULL) { \ | |
| 3501 | + if (x == NULL) { \ | |
| 3502 | + call_trace_protected(tstate->c_profilefunc, \ | |
| 3503 | + tstate->c_profileobj, \ | |
| 3504 | + tstate->frame, PyTrace_C_EXCEPTION, \ | |
| 3505 | + func); \ | |
| 3506 | + /* XXX should pass (type, value, tb) */ \ | |
| 3507 | + } else { \ | |
| 3508 | + if (call_trace(tstate->c_profilefunc, \ | |
| 3509 | + tstate->c_profileobj, \ | |
| 3510 | + tstate->frame, PyTrace_C_RETURN, \ | |
| 3511 | + func)) { \ | |
| 3512 | + Py_DECREF(x); \ | |
| 3513 | + x = NULL; \ | |
| 3514 | + } \ | |
| 3515 | + } \ | |
| 3516 | + } \ | |
| 3517 | + } \ | |
| 3518 | +} else { \ | |
| 3519 | + x = call; \ | |
| 3520 | + } | |
| 3521 | + | |
| 3522 | +static PyObject * | |
| 3523 | +call_function(PyObject ***pp_stack, int oparg | |
| 3524 | +#ifdef WITH_TSC | |
| 3525 | + , uint64* pintr0, uint64* pintr1 | |
| 3526 | +#endif | |
| 3527 | + ) | |
| 3528 | +{ | |
| 3529 | + int na = oparg & 0xff; | |
| 3530 | + int nk = (oparg>>8) & 0xff; | |
| 3531 | + int n = na + 2 * nk; | |
| 3532 | + PyObject **pfunc = (*pp_stack) - n - 1; | |
| 3533 | + PyObject *func = *pfunc; | |
| 3534 | + PyObject *x, *w; | |
| 3535 | + | |
| 3536 | + /* Always dispatch PyCFunction first, because these are | |
| 3537 | + presumed to be the most frequent callable object. | |
| 3538 | + */ | |
| 3539 | + if (PyCFunction_Check(func) && nk == 0) { | |
| 3540 | + int flags = PyCFunction_GET_FLAGS(func); | |
| 3541 | + PyThreadState *tstate = PyThreadState_GET(); | |
| 3542 | + | |
| 3543 | + PCALL(PCALL_CFUNCTION); | |
| 3544 | + if (flags & (METH_NOARGS | METH_O)) { | |
| 3545 | + PyCFunction meth = PyCFunction_GET_FUNCTION(func); | |
| 3546 | + PyObject *self = PyCFunction_GET_SELF(func); | |
| 3547 | + if (flags & METH_NOARGS && na == 0) { | |
| 3548 | + C_TRACE(x, (*meth)(self,NULL)); | |
| 3549 | + } | |
| 3550 | + else if (flags & METH_O && na == 1) { | |
| 3551 | + PyObject *arg = EXT_POP(*pp_stack); | |
| 3552 | + C_TRACE(x, (*meth)(self,arg)); | |
| 3553 | + Py_DECREF(arg); | |
| 3554 | + } | |
| 3555 | + else { | |
| 3556 | + err_args(func, flags, na); | |
| 3557 | + x = NULL; | |
| 3558 | + } | |
| 3559 | + } | |
| 3560 | + else { | |
| 3561 | + PyObject *callargs; | |
| 3562 | + callargs = load_args(pp_stack, na); | |
| 3563 | + READ_TIMESTAMP(*pintr0); | |
| 3564 | + C_TRACE(x, PyCFunction_Call(func,callargs,NULL)); | |
| 3565 | + READ_TIMESTAMP(*pintr1); | |
| 3566 | + Py_XDECREF(callargs); | |
| 3567 | + } | |
| 3568 | + } else { | |
| 3569 | + if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) { | |
| 3570 | + /* optimize access to bound methods */ | |
| 3571 | + PyObject *self = PyMethod_GET_SELF(func); | |
| 3572 | + PCALL(PCALL_METHOD); | |
| 3573 | + PCALL(PCALL_BOUND_METHOD); | |
| 3574 | + Py_INCREF(self); | |
| 3575 | + func = PyMethod_GET_FUNCTION(func); | |
| 3576 | + Py_INCREF(func); | |
| 3577 | + Py_DECREF(*pfunc); | |
| 3578 | + *pfunc = self; | |
| 3579 | + na++; | |
| 3580 | + n++; | |
| 3581 | + } else | |
| 3582 | + Py_INCREF(func); | |
| 3583 | + READ_TIMESTAMP(*pintr0); | |
| 3584 | + if (PyFunction_Check(func)) | |
| 3585 | + x = fast_function(func, pp_stack, n, na, nk); | |
| 3586 | + else | |
| 3587 | + x = do_call(func, pp_stack, na, nk); | |
| 3588 | + READ_TIMESTAMP(*pintr1); | |
| 3589 | + Py_DECREF(func); | |
| 3590 | + } | |
| 3591 | + | |
| 3592 | + /* Clear the stack of the function object. Also removes | |
| 3593 | + the arguments in case they weren't consumed already | |
| 3594 | + (fast_function() and err_args() leave them on the stack). | |
| 3595 | + */ | |
| 3596 | + while ((*pp_stack) > pfunc) { | |
| 3597 | + w = EXT_POP(*pp_stack); | |
| 3598 | + Py_DECREF(w); | |
| 3599 | + PCALL(PCALL_POP); | |
| 3600 | + } | |
| 3601 | + return x; | |
| 3602 | +} | |
| 3603 | + | |
| 3604 | +/* The fast_function() function optimize calls for which no argument | |
| 3605 | + tuple is necessary; the objects are passed directly from the stack. | |
| 3606 | + For the simplest case -- a function that takes only positional | |
| 3607 | + arguments and is called with only positional arguments -- it | |
| 3608 | + inlines the most primitive frame setup code from | |
| 3609 | + PyEval_EvalCodeEx(), which vastly reduces the checks that must be | |
| 3610 | + done before evaluating the frame. | |
| 3611 | +*/ | |
| 3612 | + | |
| 3613 | +static PyObject * | |
| 3614 | +fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk) | |
| 3615 | +{ | |
| 3616 | + PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func); | |
| 3617 | + PyObject *globals = PyFunction_GET_GLOBALS(func); | |
| 3618 | + PyObject *argdefs = PyFunction_GET_DEFAULTS(func); | |
| 3619 | + PyObject **d = NULL; | |
| 3620 | + int nd = 0; | |
| 3621 | + | |
| 3622 | + PCALL(PCALL_FUNCTION); | |
| 3623 | + PCALL(PCALL_FAST_FUNCTION); | |
| 3624 | + if (argdefs == NULL && co->co_argcount == n && nk==0 && | |
| 3625 | + co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) { | |
| 3626 | + PyFrameObject *f; | |
| 3627 | + PyObject *retval = NULL; | |
| 3628 | + PyThreadState *tstate = PyThreadState_GET(); | |
| 3629 | + PyObject **fastlocals, **stack; | |
| 3630 | + int i; | |
| 3631 | + | |
| 3632 | + PCALL(PCALL_FASTER_FUNCTION); | |
| 3633 | + assert(globals != NULL); | |
| 3634 | + /* XXX Perhaps we should create a specialized | |
| 3635 | + PyFrame_New() that doesn't take locals, but does | |
| 3636 | + take builtins without sanity checking them. | |
| 3637 | + */ | |
| 3638 | + assert(tstate != NULL); | |
| 3639 | + f = PyFrame_New(tstate, co, globals, NULL); | |
| 3640 | + if (f == NULL) | |
| 3641 | + return NULL; | |
| 3642 | + | |
| 3643 | + fastlocals = f->f_localsplus; | |
| 3644 | + stack = (*pp_stack) - n; | |
| 3645 | + | |
| 3646 | + for (i = 0; i < n; i++) { | |
| 3647 | + Py_INCREF(*stack); | |
| 3648 | + fastlocals[i] = *stack++; | |
| 3649 | + } | |
| 3650 | + retval = PyEval_EvalFrameEx(f,0); | |
| 3651 | + ++tstate->recursion_depth; | |
| 3652 | + Py_DECREF(f); | |
| 3653 | + --tstate->recursion_depth; | |
| 3654 | + return retval; | |
| 3655 | + } | |
| 3656 | + if (argdefs != NULL) { | |
| 3657 | + d = &PyTuple_GET_ITEM(argdefs, 0); | |
| 3658 | + nd = ((PyTupleObject *)argdefs)->ob_size; | |
| 3659 | + } | |
| 3660 | + return PyEval_EvalCodeEx(co, globals, | |
| 3661 | + (PyObject *)NULL, (*pp_stack)-n, na, | |
| 3662 | + (*pp_stack)-2*nk, nk, d, nd, | |
| 3663 | + PyFunction_GET_CLOSURE(func)); | |
| 3664 | +} | |
| 3665 | + | |
| 3666 | +static PyObject * | |
| 3667 | +update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack, | |
| 3668 | + PyObject *func) | |
| 3669 | +{ | |
| 3670 | + PyObject *kwdict = NULL; | |
| 3671 | + if (orig_kwdict == NULL) | |
| 3672 | + kwdict = PyDict_New(); | |
| 3673 | + else { | |
| 3674 | + kwdict = PyDict_Copy(orig_kwdict); | |
| 3675 | + Py_DECREF(orig_kwdict); | |
| 3676 | + } | |
| 3677 | + if (kwdict == NULL) | |
| 3678 | + return NULL; | |
| 3679 | + while (--nk >= 0) { | |
| 3680 | + int err; | |
| 3681 | + PyObject *value = EXT_POP(*pp_stack); | |
| 3682 | + PyObject *key = EXT_POP(*pp_stack); | |
| 3683 | + if (PyDict_GetItem(kwdict, key) != NULL) { | |
| 3684 | + PyErr_Format(PyExc_TypeError, | |
| 3685 | + "%.200s%s got multiple values " | |
| 3686 | + "for keyword argument '%.200s'", | |
| 3687 | + PyEval_GetFuncName(func), | |
| 3688 | + PyEval_GetFuncDesc(func), | |
| 3689 | + PyString_AsString(key)); | |
| 3690 | + Py_DECREF(key); | |
| 3691 | + Py_DECREF(value); | |
| 3692 | + Py_DECREF(kwdict); | |
| 3693 | + return NULL; | |
| 3694 | + } | |
| 3695 | + err = PyDict_SetItem(kwdict, key, value); | |
| 3696 | + Py_DECREF(key); | |
| 3697 | + Py_DECREF(value); | |
| 3698 | + if (err) { | |
| 3699 | + Py_DECREF(kwdict); | |
| 3700 | + return NULL; | |
| 3701 | + } | |
| 3702 | + } | |
| 3703 | + return kwdict; | |
| 3704 | +} | |
| 3705 | + | |
| 3706 | +static PyObject * | |
| 3707 | +update_star_args(int nstack, int nstar, PyObject *stararg, | |
| 3708 | + PyObject ***pp_stack) | |
| 3709 | +{ | |
| 3710 | + PyObject *callargs, *w; | |
| 3711 | + | |
| 3712 | + callargs = PyTuple_New(nstack + nstar); | |
| 3713 | + if (callargs == NULL) { | |
| 3714 | + return NULL; | |
| 3715 | + } | |
| 3716 | + if (nstar) { | |
| 3717 | + int i; | |
| 3718 | + for (i = 0; i < nstar; i++) { | |
| 3719 | + PyObject *a = PyTuple_GET_ITEM(stararg, i); | |
| 3720 | + Py_INCREF(a); | |
| 3721 | + PyTuple_SET_ITEM(callargs, nstack + i, a); | |
| 3722 | + } | |
| 3723 | + } | |
| 3724 | + while (--nstack >= 0) { | |
| 3725 | + w = EXT_POP(*pp_stack); | |
| 3726 | + PyTuple_SET_ITEM(callargs, nstack, w); | |
| 3727 | + } | |
| 3728 | + return callargs; | |
| 3729 | +} | |
| 3730 | + | |
| 3731 | +static PyObject * | |
| 3732 | +load_args(PyObject ***pp_stack, int na) | |
| 3733 | +{ | |
| 3734 | + PyObject *args = PyTuple_New(na); | |
| 3735 | + PyObject *w; | |
| 3736 | + | |
| 3737 | + if (args == NULL) | |
| 3738 | + return NULL; | |
| 3739 | + while (--na >= 0) { | |
| 3740 | + w = EXT_POP(*pp_stack); | |
| 3741 | + PyTuple_SET_ITEM(args, na, w); | |
| 3742 | + } | |
| 3743 | + return args; | |
| 3744 | +} | |
| 3745 | + | |
| 3746 | +static PyObject * | |
| 3747 | +do_call(PyObject *func, PyObject ***pp_stack, int na, int nk) | |
| 3748 | +{ | |
| 3749 | + PyObject *callargs = NULL; | |
| 3750 | + PyObject *kwdict = NULL; | |
| 3751 | + PyObject *result = NULL; | |
| 3752 | + | |
| 3753 | + if (nk > 0) { | |
| 3754 | + kwdict = update_keyword_args(NULL, nk, pp_stack, func); | |
| 3755 | + if (kwdict == NULL) | |
| 3756 | + goto call_fail; | |
| 3757 | + } | |
| 3758 | + callargs = load_args(pp_stack, na); | |
| 3759 | + if (callargs == NULL) | |
| 3760 | + goto call_fail; | |
| 3761 | +#ifdef CALL_PROFILE | |
| 3762 | + /* At this point, we have to look at the type of func to | |
| 3763 | + update the call stats properly. Do it here so as to avoid | |
| 3764 | + exposing the call stats machinery outside ceval.c | |
| 3765 | + */ | |
| 3766 | + if (PyFunction_Check(func)) | |
| 3767 | + PCALL(PCALL_FUNCTION); | |
| 3768 | + else if (PyMethod_Check(func)) | |
| 3769 | + PCALL(PCALL_METHOD); | |
| 3770 | + else if (PyType_Check(func)) | |
| 3771 | + PCALL(PCALL_TYPE); | |
| 3772 | + else | |
| 3773 | + PCALL(PCALL_OTHER); | |
| 3774 | +#endif | |
| 3775 | + result = PyObject_Call(func, callargs, kwdict); | |
| 3776 | + call_fail: | |
| 3777 | + Py_XDECREF(callargs); | |
| 3778 | + Py_XDECREF(kwdict); | |
| 3779 | + return result; | |
| 3780 | +} | |
| 3781 | + | |
| 3782 | +static PyObject * | |
| 3783 | +ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk) | |
| 3784 | +{ | |
| 3785 | + int nstar = 0; | |
| 3786 | + PyObject *callargs = NULL; | |
| 3787 | + PyObject *stararg = NULL; | |
| 3788 | + PyObject *kwdict = NULL; | |
| 3789 | + PyObject *result = NULL; | |
| 3790 | + | |
| 3791 | + if (flags & CALL_FLAG_KW) { | |
| 3792 | + kwdict = EXT_POP(*pp_stack); | |
| 3793 | + if (!(kwdict && PyDict_Check(kwdict))) { | |
| 3794 | + PyErr_Format(PyExc_TypeError, | |
| 3795 | + "%s%s argument after ** " | |
| 3796 | + "must be a dictionary", | |
| 3797 | + PyEval_GetFuncName(func), | |
| 3798 | + PyEval_GetFuncDesc(func)); | |
| 3799 | + goto ext_call_fail; | |
| 3800 | + } | |
| 3801 | + } | |
| 3802 | + if (flags & CALL_FLAG_VAR) { | |
| 3803 | + stararg = EXT_POP(*pp_stack); | |
| 3804 | + if (!PyTuple_Check(stararg)) { | |
| 3805 | + PyObject *t = NULL; | |
| 3806 | + t = PySequence_Tuple(stararg); | |
| 3807 | + if (t == NULL) { | |
| 3808 | + if (PyErr_ExceptionMatches(PyExc_TypeError)) { | |
| 3809 | + PyErr_Format(PyExc_TypeError, | |
| 3810 | + "%s%s argument after * " | |
| 3811 | + "must be a sequence", | |
| 3812 | + PyEval_GetFuncName(func), | |
| 3813 | + PyEval_GetFuncDesc(func)); | |
| 3814 | + } | |
| 3815 | + goto ext_call_fail; | |
| 3816 | + } | |
| 3817 | + Py_DECREF(stararg); | |
| 3818 | + stararg = t; | |
| 3819 | + } | |
| 3820 | + nstar = PyTuple_GET_SIZE(stararg); | |
| 3821 | + } | |
| 3822 | + if (nk > 0) { | |
| 3823 | + kwdict = update_keyword_args(kwdict, nk, pp_stack, func); | |
| 3824 | + if (kwdict == NULL) | |
| 3825 | + goto ext_call_fail; | |
| 3826 | + } | |
| 3827 | + callargs = update_star_args(na, nstar, stararg, pp_stack); | |
| 3828 | + if (callargs == NULL) | |
| 3829 | + goto ext_call_fail; | |
| 3830 | +#ifdef CALL_PROFILE | |
| 3831 | + /* At this point, we have to look at the type of func to | |
| 3832 | + update the call stats properly. Do it here so as to avoid | |
| 3833 | + exposing the call stats machinery outside ceval.c | |
| 3834 | + */ | |
| 3835 | + if (PyFunction_Check(func)) | |
| 3836 | + PCALL(PCALL_FUNCTION); | |
| 3837 | + else if (PyMethod_Check(func)) | |
| 3838 | + PCALL(PCALL_METHOD); | |
| 3839 | + else if (PyType_Check(func)) | |
| 3840 | + PCALL(PCALL_TYPE); | |
| 3841 | + else | |
| 3842 | + PCALL(PCALL_OTHER); | |
| 3843 | +#endif | |
| 3844 | + result = PyObject_Call(func, callargs, kwdict); | |
| 3845 | + ext_call_fail: | |
| 3846 | + Py_XDECREF(callargs); | |
| 3847 | + Py_XDECREF(kwdict); | |
| 3848 | + Py_XDECREF(stararg); | |
| 3849 | + return result; | |
| 3850 | +} | |
| 3851 | + | |
| 3852 | +/* Extract a slice index from a PyInt or PyLong or an object with the | |
| 3853 | + nb_index slot defined, and store in *pi. | |
| 3854 | + Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX, | |
| 3855 | + and silently boost values less than -PY_SSIZE_T_MAX-1 to -PY_SSIZE_T_MAX-1. | |
| 3856 | + Return 0 on error, 1 on success. | |
| 3857 | +*/ | |
| 3858 | +/* Note: If v is NULL, return success without storing into *pi. This | |
| 3859 | + is because_PyEval_SliceIndex() is called by apply_slice(), which can be | |
| 3860 | + called by the SLICE opcode with v and/or w equal to NULL. | |
| 3861 | +*/ | |
| 3862 | +int | |
| 3863 | +_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi) | |
| 3864 | +{ | |
| 3865 | + if (v != NULL) { | |
| 3866 | + Py_ssize_t x; | |
| 3867 | + if (PyInt_Check(v)) { | |
| 3868 | + /* XXX(nnorwitz): I think PyInt_AS_LONG is correct, | |
| 3869 | + however, it looks like it should be AsSsize_t. | |
| 3870 | + There should be a comment here explaining why. | |
| 3871 | + */ | |
| 3872 | + x = PyInt_AS_LONG(v); | |
| 3873 | + } | |
| 3874 | + else if (PyIndex_Check(v)) { | |
| 3875 | + x = PyNumber_AsSsize_t(v, NULL); | |
| 3876 | + if (x == -1 && PyErr_Occurred()) | |
| 3877 | + return 0; | |
| 3878 | + } | |
| 3879 | + else { | |
| 3880 | + PyErr_SetString(PyExc_TypeError, | |
| 3881 | + "slice indices must be integers or " | |
| 3882 | + "None or have an __index__ method"); | |
| 3883 | + return 0; | |
| 3884 | + } | |
| 3885 | + *pi = x; | |
| 3886 | + } | |
| 3887 | + return 1; | |
| 3888 | +} | |
| 3889 | + | |
| 3890 | +#undef ISINDEX | |
| 3891 | +#define ISINDEX(x) ((x) == NULL || \ | |
| 3892 | + PyInt_Check(x) || PyLong_Check(x) || PyIndex_Check(x)) | |
| 3893 | + | |
| 3894 | +static PyObject * | |
| 3895 | +apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */ | |
| 3896 | +{ | |
| 3897 | + PyTypeObject *tp = u->ob_type; | |
| 3898 | + PySequenceMethods *sq = tp->tp_as_sequence; | |
| 3899 | + | |
| 3900 | + if (sq && sq->sq_slice && ISINDEX(v) && ISINDEX(w)) { | |
| 3901 | + Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX; | |
| 3902 | + if (!_PyEval_SliceIndex(v, &ilow)) | |
| 3903 | + return NULL; | |
| 3904 | + if (!_PyEval_SliceIndex(w, &ihigh)) | |
| 3905 | + return NULL; | |
| 3906 | + return PySequence_GetSlice(u, ilow, ihigh); | |
| 3907 | + } | |
| 3908 | + else { | |
| 3909 | + PyObject *slice = PySlice_New(v, w, NULL); | |
| 3910 | + if (slice != NULL) { | |
| 3911 | + PyObject *res = PyObject_GetItem(u, slice); | |
| 3912 | + Py_DECREF(slice); | |
| 3913 | + return res; | |
| 3914 | + } | |
| 3915 | + else | |
| 3916 | + return NULL; | |
| 3917 | + } | |
| 3918 | +} | |
| 3919 | + | |
| 3920 | +static int | |
| 3921 | +assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x) | |
| 3922 | + /* u[v:w] = x */ | |
| 3923 | +{ | |
| 3924 | + PyTypeObject *tp = u->ob_type; | |
| 3925 | + PySequenceMethods *sq = tp->tp_as_sequence; | |
| 3926 | + | |
| 3927 | + if (sq && sq->sq_ass_slice && ISINDEX(v) && ISINDEX(w)) { | |
| 3928 | + Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX; | |
| 3929 | + if (!_PyEval_SliceIndex(v, &ilow)) | |
| 3930 | + return -1; | |
| 3931 | + if (!_PyEval_SliceIndex(w, &ihigh)) | |
| 3932 | + return -1; | |
| 3933 | + if (x == NULL) | |
| 3934 | + return PySequence_DelSlice(u, ilow, ihigh); | |
| 3935 | + else | |
| 3936 | + return PySequence_SetSlice(u, ilow, ihigh, x); | |
| 3937 | + } | |
| 3938 | + else { | |
| 3939 | + PyObject *slice = PySlice_New(v, w, NULL); | |
| 3940 | + if (slice != NULL) { | |
| 3941 | + int res; | |
| 3942 | + if (x != NULL) | |
| 3943 | + res = PyObject_SetItem(u, slice, x); | |
| 3944 | + else | |
| 3945 | + res = PyObject_DelItem(u, slice); | |
| 3946 | + Py_DECREF(slice); | |
| 3947 | + return res; | |
| 3948 | + } | |
| 3949 | + else | |
| 3950 | + return -1; | |
| 3951 | + } | |
| 3952 | +} | |
| 3953 | + | |
| 3954 | +static PyObject * | |
| 3955 | +cmp_outcome(int op, register PyObject *v, register PyObject *w) | |
| 3956 | +{ | |
| 3957 | + int res = 0; | |
| 3958 | + switch (op) { | |
| 3959 | + case PyCmp_IS: | |
| 3960 | + res = (v == w); | |
| 3961 | + break; | |
| 3962 | + case PyCmp_IS_NOT: | |
| 3963 | + res = (v != w); | |
| 3964 | + break; | |
| 3965 | + case PyCmp_IN: | |
| 3966 | + res = PySequence_Contains(w, v); | |
| 3967 | + if (res < 0) | |
| 3968 | + return NULL; | |
| 3969 | + break; | |
| 3970 | + case PyCmp_NOT_IN: | |
| 3971 | + res = PySequence_Contains(w, v); | |
| 3972 | + if (res < 0) | |
| 3973 | + return NULL; | |
| 3974 | + res = !res; | |
| 3975 | + break; | |
| 3976 | + case PyCmp_EXC_MATCH: | |
| 3977 | + res = PyErr_GivenExceptionMatches(v, w); | |
| 3978 | + break; | |
| 3979 | + default: | |
| 3980 | + return PyObject_RichCompare(v, w, op); | |
| 3981 | + } | |
| 3982 | + v = res ? Py_True : Py_False; | |
| 3983 | + Py_INCREF(v); | |
| 3984 | + return v; | |
| 3985 | +} | |
| 3986 | + | |
| 3987 | +static PyObject * | |
| 3988 | +import_from(PyObject *v, PyObject *name) | |
| 3989 | +{ | |
| 3990 | + PyObject *x; | |
| 3991 | + | |
| 3992 | + x = PyObject_GetAttr(v, name); | |
| 3993 | + if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) { | |
| 3994 | + PyErr_Format(PyExc_ImportError, | |
| 3995 | + "cannot import name %.230s", | |
| 3996 | + PyString_AsString(name)); | |
| 3997 | + } | |
| 3998 | + return x; | |
| 3999 | +} | |
| 4000 | + | |
| 4001 | +static int | |
| 4002 | +import_all_from(PyObject *locals, PyObject *v) | |
| 4003 | +{ | |
| 4004 | + PyObject *all = PyObject_GetAttrString(v, "__all__"); | |
| 4005 | + PyObject *dict, *name, *value; | |
| 4006 | + int skip_leading_underscores = 0; | |
| 4007 | + int pos, err; | |
| 4008 | + | |
| 4009 | + if (all == NULL) { | |
| 4010 | + if (!PyErr_ExceptionMatches(PyExc_AttributeError)) | |
| 4011 | + return -1; /* Unexpected error */ | |
| 4012 | + PyErr_Clear(); | |
| 4013 | + dict = PyObject_GetAttrString(v, "__dict__"); | |
| 4014 | + if (dict == NULL) { | |
| 4015 | + if (!PyErr_ExceptionMatches(PyExc_AttributeError)) | |
| 4016 | + return -1; | |
| 4017 | + PyErr_SetString(PyExc_ImportError, | |
| 4018 | + "from-import-* object has no __dict__ and no __all__"); | |
| 4019 | + return -1; | |
| 4020 | + } | |
| 4021 | + all = PyMapping_Keys(dict); | |
| 4022 | + Py_DECREF(dict); | |
| 4023 | + if (all == NULL) | |
| 4024 | + return -1; | |
| 4025 | + skip_leading_underscores = 1; | |
| 4026 | + } | |
| 4027 | + | |
| 4028 | + for (pos = 0, err = 0; ; pos++) { | |
| 4029 | + name = PySequence_GetItem(all, pos); | |
| 4030 | + if (name == NULL) { | |
| 4031 | + if (!PyErr_ExceptionMatches(PyExc_IndexError)) | |
| 4032 | + err = -1; | |
| 4033 | + else | |
| 4034 | + PyErr_Clear(); | |
| 4035 | + break; | |
| 4036 | + } | |
| 4037 | + if (skip_leading_underscores && | |
| 4038 | + PyString_Check(name) && | |
| 4039 | + PyString_AS_STRING(name)[0] == '_') | |
| 4040 | + { | |
| 4041 | + Py_DECREF(name); | |
| 4042 | + continue; | |
| 4043 | + } | |
| 4044 | + value = PyObject_GetAttr(v, name); | |
| 4045 | + if (value == NULL) | |
| 4046 | + err = -1; | |
| 4047 | + else if (PyDict_CheckExact(locals)) | |
| 4048 | + err = PyDict_SetItem(locals, name, value); | |
| 4049 | + else | |
| 4050 | + err = PyObject_SetItem(locals, name, value); | |
| 4051 | + Py_DECREF(name); | |
| 4052 | + Py_XDECREF(value); | |
| 4053 | + if (err != 0) | |
| 4054 | + break; | |
| 4055 | + } | |
| 4056 | + Py_DECREF(all); | |
| 4057 | + return err; | |
| 4058 | +} | |
| 4059 | + | |
| 4060 | +static PyObject * | |
| 4061 | +build_class(PyObject *methods, PyObject *bases, PyObject *name) | |
| 4062 | +{ | |
| 4063 | + PyObject *metaclass = NULL, *result, *base; | |
| 4064 | + | |
| 4065 | + if (PyDict_Check(methods)) | |
| 4066 | + metaclass = PyDict_GetItemString(methods, "__metaclass__"); | |
| 4067 | + if (metaclass != NULL) | |
| 4068 | + Py_INCREF(metaclass); | |
| 4069 | + else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) { | |
| 4070 | + base = PyTuple_GET_ITEM(bases, 0); | |
| 4071 | + metaclass = PyObject_GetAttrString(base, "__class__"); | |
| 4072 | + if (metaclass == NULL) { | |
| 4073 | + PyErr_Clear(); | |
| 4074 | + metaclass = (PyObject *)base->ob_type; | |
| 4075 | + Py_INCREF(metaclass); | |
| 4076 | + } | |
| 4077 | + } | |
| 4078 | + else { | |
| 4079 | + PyObject *g = PyEval_GetGlobals(); | |
| 4080 | + if (g != NULL && PyDict_Check(g)) | |
| 4081 | + metaclass = PyDict_GetItemString(g, "__metaclass__"); | |
| 4082 | + if (metaclass == NULL) | |
| 4083 | + metaclass = (PyObject *) &PyClass_Type; | |
| 4084 | + Py_INCREF(metaclass); | |
| 4085 | + } | |
| 4086 | + result = PyObject_CallFunctionObjArgs(metaclass, name, bases, methods, NULL); | |
| 4087 | + Py_DECREF(metaclass); | |
| 4088 | + if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) { | |
| 4089 | + /* A type error here likely means that the user passed | |
| 4090 | + in a base that was not a class (such the random module | |
| 4091 | + instead of the random.random type). Help them out with | |
| 4092 | + by augmenting the error message with more information.*/ | |
| 4093 | + | |
| 4094 | + PyObject *ptype, *pvalue, *ptraceback; | |
| 4095 | + | |
| 4096 | + PyErr_Fetch(&ptype, &pvalue, &ptraceback); | |
| 4097 | + if (PyString_Check(pvalue)) { | |
| 4098 | + PyObject *newmsg; | |
| 4099 | + newmsg = PyString_FromFormat( | |
| 4100 | + "Error when calling the metaclass bases\n %s", | |
| 4101 | + PyString_AS_STRING(pvalue)); | |
| 4102 | + if (newmsg != NULL) { | |
| 4103 | + Py_DECREF(pvalue); | |
| 4104 | + pvalue = newmsg; | |
| 4105 | + } | |
| 4106 | + } | |
| 4107 | + PyErr_Restore(ptype, pvalue, ptraceback); | |
| 4108 | + } | |
| 4109 | + return result; | |
| 4110 | +} | |
| 4111 | + | |
| 4112 | +static int | |
| 4113 | +exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals, | |
| 4114 | + PyObject *locals) | |
| 4115 | +{ | |
| 4116 | + int n; | |
| 4117 | + PyObject *v; | |
| 4118 | + int plain = 0; | |
| 4119 | + | |
| 4120 | + if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None && | |
| 4121 | + ((n = PyTuple_Size(prog)) == 2 || n == 3)) { | |
| 4122 | + /* Backward compatibility hack */ | |
| 4123 | + globals = PyTuple_GetItem(prog, 1); | |
| 4124 | + if (n == 3) | |
| 4125 | + locals = PyTuple_GetItem(prog, 2); | |
| 4126 | + prog = PyTuple_GetItem(prog, 0); | |
| 4127 | + } | |
| 4128 | + if (globals == Py_None) { | |
| 4129 | + globals = PyEval_GetGlobals(); | |
| 4130 | + if (locals == Py_None) { | |
| 4131 | + locals = PyEval_GetLocals(); | |
| 4132 | + plain = 1; | |
| 4133 | + } | |
| 4134 | + if (!globals || !locals) { | |
| 4135 | + PyErr_SetString(PyExc_SystemError, | |
| 4136 | + "globals and locals cannot be NULL"); | |
| 4137 | + return -1; | |
| 4138 | + } | |
| 4139 | + } | |
| 4140 | + else if (locals == Py_None) | |
| 4141 | + locals = globals; | |
| 4142 | + if (!PyString_Check(prog) && | |
| 4143 | + !PyUnicode_Check(prog) && | |
| 4144 | + !PyCode_Check(prog) && | |
| 4145 | + !PyFile_Check(prog)) { | |
| 4146 | + PyErr_SetString(PyExc_TypeError, | |
| 4147 | + "exec: arg 1 must be a string, file, or code object"); | |
| 4148 | + return -1; | |
| 4149 | + } | |
| 4150 | + if (!PyDict_Check(globals)) { | |
| 4151 | + PyErr_SetString(PyExc_TypeError, | |
| 4152 | + "exec: arg 2 must be a dictionary or None"); | |
| 4153 | + return -1; | |
| 4154 | + } | |
| 4155 | + if (!PyMapping_Check(locals)) { | |
| 4156 | + PyErr_SetString(PyExc_TypeError, | |
| 4157 | + "exec: arg 3 must be a mapping or None"); | |
| 4158 | + return -1; | |
| 4159 | + } | |
| 4160 | + if (PyDict_GetItemString(globals, "__builtins__") == NULL) | |
| 4161 | + PyDict_SetItemString(globals, "__builtins__", f->f_builtins); | |
| 4162 | + if (PyCode_Check(prog)) { | |
| 4163 | + if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) { | |
| 4164 | + PyErr_SetString(PyExc_TypeError, | |
| 4165 | + "code object passed to exec may not contain free variables"); | |
| 4166 | + return -1; | |
| 4167 | + } | |
| 4168 | + v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals); | |
| 4169 | + } | |
| 4170 | + else if (PyFile_Check(prog)) { | |
| 4171 | + FILE *fp = PyFile_AsFile(prog); | |
| 4172 | + char *name = PyString_AsString(PyFile_Name(prog)); | |
| 4173 | + PyCompilerFlags cf; | |
| 4174 | + if (name == NULL) | |
| 4175 | + return -1; | |
| 4176 | + cf.cf_flags = 0; | |
| 4177 | + if (PyEval_MergeCompilerFlags(&cf)) | |
| 4178 | + v = PyRun_FileFlags(fp, name, Py_file_input, globals, | |
| 4179 | + locals, &cf); | |
| 4180 | + else | |
| 4181 | + v = PyRun_File(fp, name, Py_file_input, globals, | |
| 4182 | + locals); | |
| 4183 | + } | |
| 4184 | + else { | |
| 4185 | + PyObject *tmp = NULL; | |
| 4186 | + char *str; | |
| 4187 | + PyCompilerFlags cf; | |
| 4188 | + cf.cf_flags = 0; | |
| 4189 | +#ifdef Py_USING_UNICODE | |
| 4190 | + if (PyUnicode_Check(prog)) { | |
| 4191 | + tmp = PyUnicode_AsUTF8String(prog); | |
| 4192 | + if (tmp == NULL) | |
| 4193 | + return -1; | |
| 4194 | + prog = tmp; | |
| 4195 | + cf.cf_flags |= PyCF_SOURCE_IS_UTF8; | |
| 4196 | + } | |
| 4197 | +#endif | |
| 4198 | + if (PyString_AsStringAndSize(prog, &str, NULL)) | |
| 4199 | + return -1; | |
| 4200 | + if (PyEval_MergeCompilerFlags(&cf)) | |
| 4201 | + v = PyRun_StringFlags(str, Py_file_input, globals, | |
| 4202 | + locals, &cf); | |
| 4203 | + else | |
| 4204 | + v = PyRun_String(str, Py_file_input, globals, locals); | |
| 4205 | + Py_XDECREF(tmp); | |
| 4206 | + } | |
| 4207 | + if (plain) | |
| 4208 | + PyFrame_LocalsToFast(f, 0); | |
| 4209 | + if (v == NULL) | |
| 4210 | + return -1; | |
| 4211 | + Py_DECREF(v); | |
| 4212 | + return 0; | |
| 4213 | +} | |
| 4214 | + | |
| 4215 | +static void | |
| 4216 | +format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj) | |
| 4217 | +{ | |
| 4218 | + char *obj_str; | |
| 4219 | + | |
| 4220 | + if (!obj) | |
| 4221 | + return; | |
| 4222 | + | |
| 4223 | + obj_str = PyString_AsString(obj); | |
| 4224 | + if (!obj_str) | |
| 4225 | + return; | |
| 4226 | + | |
| 4227 | + PyErr_Format(exc, format_str, obj_str); | |
| 4228 | +} | |
| 4229 | + | |
| 4230 | +static PyObject * | |
| 4231 | +string_concatenate(PyObject *v, PyObject *w, | |
| 4232 | + PyFrameObject *f, unsigned char *next_instr) | |
| 4233 | +{ | |
| 4234 | + /* This function implements 'variable += expr' when both arguments | |
| 4235 | + are strings. */ | |
| 4236 | + Py_ssize_t v_len = PyString_GET_SIZE(v); | |
| 4237 | + Py_ssize_t w_len = PyString_GET_SIZE(w); | |
| 4238 | + Py_ssize_t new_len = v_len + w_len; | |
| 4239 | + if (new_len < 0) { | |
| 4240 | + PyErr_SetString(PyExc_OverflowError, | |
| 4241 | + "strings are too large to concat"); | |
| 4242 | + return NULL; | |
| 4243 | + } | |
| 4244 | + | |
| 4245 | + if (v->ob_refcnt == 2) { | |
| 4246 | + /* In the common case, there are 2 references to the value | |
| 4247 | + * stored in 'variable' when the += is performed: one on the | |
| 4248 | + * value stack (in 'v') and one still stored in the 'variable'. | |
| 4249 | + * We try to delete the variable now to reduce the refcnt to 1. | |
| 4250 | + */ | |
| 4251 | + switch (*next_instr) { | |
| 4252 | + case STORE_FAST: | |
| 4253 | + { | |
| 4254 | + int oparg = PEEKARG(); | |
| 4255 | + PyObject **fastlocals = f->f_localsplus; | |
| 4256 | + if (GETLOCAL(oparg) == v) | |
| 4257 | + SETLOCAL(oparg, NULL); | |
| 4258 | + break; | |
| 4259 | + } | |
| 4260 | + case STORE_DEREF: | |
| 4261 | + { | |
| 4262 | + PyObject **freevars = f->f_localsplus + f->f_code->co_nlocals; | |
| 4263 | + PyObject *c = freevars[PEEKARG()]; | |
| 4264 | + if (PyCell_GET(c) == v) | |
| 4265 | + PyCell_Set(c, NULL); | |
| 4266 | + break; | |
| 4267 | + } | |
| 4268 | + case STORE_NAME: | |
| 4269 | + { | |
| 4270 | + PyObject *names = f->f_code->co_names; | |
| 4271 | + PyObject *name = GETITEM(names, PEEKARG()); | |
| 4272 | + PyObject *locals = f->f_locals; | |
| 4273 | + if (PyDict_CheckExact(locals) && | |
| 4274 | + PyDict_GetItem(locals, name) == v) { | |
| 4275 | + if (PyDict_DelItem(locals, name) != 0) { | |
| 4276 | + PyErr_Clear(); | |
| 4277 | + } | |
| 4278 | + } | |
| 4279 | + break; | |
| 4280 | + } | |
| 4281 | + } | |
| 4282 | + } | |
| 4283 | + | |
| 4284 | + if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) { | |
| 4285 | + /* Now we own the last reference to 'v', so we can resize it | |
| 4286 | + * in-place. | |
| 4287 | + */ | |
| 4288 | + if (_PyString_Resize(&v, new_len) != 0) { | |
| 4289 | + /* XXX if _PyString_Resize() fails, 'v' has been | |
| 4290 | + * deallocated so it cannot be put back into 'variable'. | |
| 4291 | + * The MemoryError is raised when there is no value in | |
| 4292 | + * 'variable', which might (very remotely) be a cause | |
| 4293 | + * of incompatibilities. | |
| 4294 | + */ | |
| 4295 | + return NULL; | |
| 4296 | + } | |
| 4297 | + /* copy 'w' into the newly allocated area of 'v' */ | |
| 4298 | + memcpy(PyString_AS_STRING(v) + v_len, | |
| 4299 | + PyString_AS_STRING(w), w_len); | |
| 4300 | + return v; | |
| 4301 | + } | |
| 4302 | + else { | |
| 4303 | + /* When in-place resizing is not an option. */ | |
| 4304 | + PyString_Concat(&v, w); | |
| 4305 | + return v; | |
| 4306 | + } | |
| 4307 | +} | |
| 4308 | + | |
| 4309 | +#ifdef DYNAMIC_EXECUTION_PROFILE | |
| 4310 | + | |
| 4311 | +static PyObject * | |
| 4312 | +getarray(long a[256]) | |
| 4313 | +{ | |
| 4314 | + int i; | |
| 4315 | + PyObject *l = PyList_New(256); | |
| 4316 | + if (l == NULL) return NULL; | |
| 4317 | + for (i = 0; i < 256; i++) { | |
| 4318 | + PyObject *x = PyInt_FromLong(a[i]); | |
| 4319 | + if (x == NULL) { | |
| 4320 | + Py_DECREF(l); | |
| 4321 | + return NULL; | |
| 4322 | + } | |
| 4323 | + PyList_SetItem(l, i, x); | |
| 4324 | + } | |
| 4325 | + for (i = 0; i < 256; i++) | |
| 4326 | + a[i] = 0; | |
| 4327 | + return l; | |
| 4328 | +} | |
| 4329 | + | |
| 4330 | +PyObject * | |
| 4331 | +_Py_GetDXProfile(PyObject *self, PyObject *args) | |
| 4332 | +{ | |
| 4333 | +#ifndef DXPAIRS | |
| 4334 | + return getarray(dxp); | |
| 4335 | +#else | |
| 4336 | + int i; | |
| 4337 | + PyObject *l = PyList_New(257); | |
| 4338 | + if (l == NULL) return NULL; | |
| 4339 | + for (i = 0; i < 257; i++) { | |
| 4340 | + PyObject *x = getarray(dxpairs[i]); | |
| 4341 | + if (x == NULL) { | |
| 4342 | + Py_DECREF(l); | |
| 4343 | + return NULL; | |
| 4344 | + } | |
| 4345 | + PyList_SetItem(l, i, x); | |
| 4346 | + } | |
| 4347 | + return l; | |
| 4348 | +#endif | |
| 4349 | +} | |
| 4350 | + | |
| 4351 | +#endif | |
| @@ -0,0 +1,860 @@ | ||
| 1 | +/* ------------------------------------------------------------------------ | |
| 2 | + | |
| 3 | + Python Codec Registry and support functions | |
| 4 | + | |
| 5 | +Written by Marc-Andre Lemburg (mal@lemburg.com). | |
| 6 | + | |
| 7 | +Copyright (c) Corporation for National Research Initiatives. | |
| 8 | + | |
| 9 | + ------------------------------------------------------------------------ */ | |
| 10 | + | |
| 11 | +#include "Python.h" | |
| 12 | +#include <ctype.h> | |
| 13 | + | |
| 14 | +/* --- Codec Registry ----------------------------------------------------- */ | |
| 15 | + | |
| 16 | +/* Import the standard encodings package which will register the first | |
| 17 | + codec search function. | |
| 18 | + | |
| 19 | + This is done in a lazy way so that the Unicode implementation does | |
| 20 | + not downgrade startup time of scripts not needing it. | |
| 21 | + | |
| 22 | + ImportErrors are silently ignored by this function. Only one try is | |
| 23 | + made. | |
| 24 | + | |
| 25 | +*/ | |
| 26 | + | |
| 27 | +static int _PyCodecRegistry_Init(void); /* Forward */ | |
| 28 | + | |
| 29 | +int PyCodec_Register(PyObject *search_function) | |
| 30 | +{ | |
| 31 | + PyInterpreterState *interp = PyThreadState_GET()->interp; | |
| 32 | + if (interp->codec_search_path == NULL && _PyCodecRegistry_Init()) | |
| 33 | + goto onError; | |
| 34 | + if (search_function == NULL) { | |
| 35 | + PyErr_BadArgument(); | |
| 36 | + goto onError; | |
| 37 | + } | |
| 38 | + if (!PyCallable_Check(search_function)) { | |
| 39 | + PyErr_SetString(PyExc_TypeError, "argument must be callable"); | |
| 40 | + goto onError; | |
| 41 | + } | |
| 42 | + return PyList_Append(interp->codec_search_path, search_function); | |
| 43 | + | |
| 44 | + onError: | |
| 45 | + return -1; | |
| 46 | +} | |
| 47 | + | |
| 48 | +/* Convert a string to a normalized Python string: all characters are | |
| 49 | + converted to lower case, spaces are replaced with underscores. */ | |
| 50 | + | |
| 51 | +static | |
| 52 | +PyObject *normalizestring(const char *string) | |
| 53 | +{ | |
| 54 | + register size_t i; | |
| 55 | + size_t len = strlen(string); | |
| 56 | + char *p; | |
| 57 | + PyObject *v; | |
| 58 | + | |
| 59 | + if (len > PY_SSIZE_T_MAX) { | |
| 60 | + PyErr_SetString(PyExc_OverflowError, "string is too large"); | |
| 61 | + return NULL; | |
| 62 | + } | |
| 63 | + | |
| 64 | + v = PyString_FromStringAndSize(NULL, len); | |
| 65 | + if (v == NULL) | |
| 66 | + return NULL; | |
| 67 | + p = PyString_AS_STRING(v); | |
| 68 | + for (i = 0; i < len; i++) { | |
| 69 | + register char ch = string[i]; | |
| 70 | + if (ch == ' ') | |
| 71 | + ch = '-'; | |
| 72 | + else | |
| 73 | + ch = tolower(Py_CHARMASK(ch)); | |
| 74 | + p[i] = ch; | |
| 75 | + } | |
| 76 | + return v; | |
| 77 | +} | |
| 78 | + | |
| 79 | +/* Lookup the given encoding and return a tuple providing the codec | |
| 80 | + facilities. | |
| 81 | + | |
| 82 | + The encoding string is looked up converted to all lower-case | |
| 83 | + characters. This makes encodings looked up through this mechanism | |
| 84 | + effectively case-insensitive. | |
| 85 | + | |
| 86 | + If no codec is found, a LookupError is set and NULL returned. | |
| 87 | + | |
| 88 | + As side effect, this tries to load the encodings package, if not | |
| 89 | + yet done. This is part of the lazy load strategy for the encodings | |
| 90 | + package. | |
| 91 | + | |
| 92 | +*/ | |
| 93 | + | |
| 94 | +PyObject *_PyCodec_Lookup(const char *encoding) | |
| 95 | +{ | |
| 96 | + PyInterpreterState *interp; | |
| 97 | + PyObject *result, *args = NULL, *v; | |
| 98 | + Py_ssize_t i, len; | |
| 99 | + | |
| 100 | + if (encoding == NULL) { | |
| 101 | + PyErr_BadArgument(); | |
| 102 | + goto onError; | |
| 103 | + } | |
| 104 | + | |
| 105 | + interp = PyThreadState_GET()->interp; | |
| 106 | + if (interp->codec_search_path == NULL && _PyCodecRegistry_Init()) | |
| 107 | + goto onError; | |
| 108 | + | |
| 109 | + /* Convert the encoding to a normalized Python string: all | |
| 110 | + characters are converted to lower case, spaces and hyphens are | |
| 111 | + replaced with underscores. */ | |
| 112 | + v = normalizestring(encoding); | |
| 113 | + if (v == NULL) | |
| 114 | + goto onError; | |
| 115 | + PyString_InternInPlace(&v); | |
| 116 | + | |
| 117 | + /* First, try to lookup the name in the registry dictionary */ | |
| 118 | + result = PyDict_GetItem(interp->codec_search_cache, v); | |
| 119 | + if (result != NULL) { | |
| 120 | + Py_INCREF(result); | |
| 121 | + Py_DECREF(v); | |
| 122 | + return result; | |
| 123 | + } | |
| 124 | + | |
| 125 | + /* Next, scan the search functions in order of registration */ | |
| 126 | + args = PyTuple_New(1); | |
| 127 | + if (args == NULL) | |
| 128 | + goto onError; | |
| 129 | + PyTuple_SET_ITEM(args,0,v); | |
| 130 | + | |
| 131 | + len = PyList_Size(interp->codec_search_path); | |
| 132 | + if (len < 0) | |
| 133 | + goto onError; | |
| 134 | + if (len == 0) { | |
| 135 | + PyErr_SetString(PyExc_LookupError, | |
| 136 | + "no codec search functions registered: " | |
| 137 | + "can't find encoding"); | |
| 138 | + goto onError; | |
| 139 | + } | |
| 140 | + | |
| 141 | + for (i = 0; i < len; i++) { | |
| 142 | + PyObject *func; | |
| 143 | + | |
| 144 | + func = PyList_GetItem(interp->codec_search_path, i); | |
| 145 | + if (func == NULL) | |
| 146 | + goto onError; | |
| 147 | + result = PyEval_CallObject(func, args); | |
| 148 | + if (result == NULL) | |
| 149 | + goto onError; | |
| 150 | + if (result == Py_None) { | |
| 151 | + Py_DECREF(result); | |
| 152 | + continue; | |
| 153 | + } | |
| 154 | + if (!PyTuple_Check(result) || PyTuple_GET_SIZE(result) != 4) { | |
| 155 | + PyErr_SetString(PyExc_TypeError, | |
| 156 | + "codec search functions must return 4-tuples"); | |
| 157 | + Py_DECREF(result); | |
| 158 | + goto onError; | |
| 159 | + } | |
| 160 | + break; | |
| 161 | + } | |
| 162 | + if (i == len) { | |
| 163 | + /* XXX Perhaps we should cache misses too ? */ | |
| 164 | + PyErr_Format(PyExc_LookupError, | |
| 165 | + "unknown encoding: %s", encoding); | |
| 166 | + goto onError; | |
| 167 | + } | |
| 168 | + | |
| 169 | + /* Cache and return the result */ | |
| 170 | + PyDict_SetItem(interp->codec_search_cache, v, result); | |
| 171 | + Py_DECREF(args); | |
| 172 | + return result; | |
| 173 | + | |
| 174 | + onError: | |
| 175 | + Py_XDECREF(args); | |
| 176 | + return NULL; | |
| 177 | +} | |
| 178 | + | |
| 179 | +static | |
| 180 | +PyObject *args_tuple(PyObject *object, | |
| 181 | + const char *errors) | |
| 182 | +{ | |
| 183 | + PyObject *args; | |
| 184 | + | |
| 185 | + args = PyTuple_New(1 + (errors != NULL)); | |
| 186 | + if (args == NULL) | |
| 187 | + return NULL; | |
| 188 | + Py_INCREF(object); | |
| 189 | + PyTuple_SET_ITEM(args,0,object); | |
| 190 | + if (errors) { | |
| 191 | + PyObject *v; | |
| 192 | + | |
| 193 | + v = PyString_FromString(errors); | |
| 194 | + if (v == NULL) { | |
| 195 | + Py_DECREF(args); | |
| 196 | + return NULL; | |
| 197 | + } | |
| 198 | + PyTuple_SET_ITEM(args, 1, v); | |
| 199 | + } | |
| 200 | + return args; | |
| 201 | +} | |
| 202 | + | |
| 203 | +/* Helper function to get a codec item */ | |
| 204 | + | |
| 205 | +static | |
| 206 | +PyObject *codec_getitem(const char *encoding, int index) | |
| 207 | +{ | |
| 208 | + PyObject *codecs; | |
| 209 | + PyObject *v; | |
| 210 | + | |
| 211 | + codecs = _PyCodec_Lookup(encoding); | |
| 212 | + if (codecs == NULL) | |
| 213 | + return NULL; | |
| 214 | + v = PyTuple_GET_ITEM(codecs, index); | |
| 215 | + Py_DECREF(codecs); | |
| 216 | + Py_INCREF(v); | |
| 217 | + return v; | |
| 218 | +} | |
| 219 | + | |
| 220 | +/* Helper function to create an incremental codec. */ | |
| 221 | + | |
| 222 | +static | |
| 223 | +PyObject *codec_getincrementalcodec(const char *encoding, | |
| 224 | + const char *errors, | |
| 225 | + const char *attrname) | |
| 226 | +{ | |
| 227 | + PyObject *codecs, *ret, *inccodec; | |
| 228 | + | |
| 229 | + codecs = _PyCodec_Lookup(encoding); | |
| 230 | + if (codecs == NULL) | |
| 231 | + return NULL; | |
| 232 | + inccodec = PyObject_GetAttrString(codecs, attrname); | |
| 233 | + Py_DECREF(codecs); | |
| 234 | + if (inccodec == NULL) | |
| 235 | + return NULL; | |
| 236 | + if (errors) | |
| 237 | + ret = PyObject_CallFunction(inccodec, "s", errors); | |
| 238 | + else | |
| 239 | + ret = PyObject_CallFunction(inccodec, NULL); | |
| 240 | + Py_DECREF(inccodec); | |
| 241 | + return ret; | |
| 242 | +} | |
| 243 | + | |
| 244 | +/* Helper function to create a stream codec. */ | |
| 245 | + | |
| 246 | +static | |
| 247 | +PyObject *codec_getstreamcodec(const char *encoding, | |
| 248 | + PyObject *stream, | |
| 249 | + const char *errors, | |
| 250 | + const int index) | |
| 251 | +{ | |
| 252 | + PyObject *codecs, *streamcodec, *codeccls; | |
| 253 | + | |
| 254 | + codecs = _PyCodec_Lookup(encoding); | |
| 255 | + if (codecs == NULL) | |
| 256 | + return NULL; | |
| 257 | + | |
| 258 | + codeccls = PyTuple_GET_ITEM(codecs, index); | |
| 259 | + if (errors != NULL) | |
| 260 | + streamcodec = PyObject_CallFunction(codeccls, "Os", stream, errors); | |
| 261 | + else | |
| 262 | + streamcodec = PyObject_CallFunction(codeccls, "O", stream); | |
| 263 | + Py_DECREF(codecs); | |
| 264 | + return streamcodec; | |
| 265 | +} | |
| 266 | + | |
| 267 | +/* Convenience APIs to query the Codec registry. | |
| 268 | + | |
| 269 | + All APIs return a codec object with incremented refcount. | |
| 270 | + | |
| 271 | + */ | |
| 272 | + | |
| 273 | +PyObject *PyCodec_Encoder(const char *encoding) | |
| 274 | +{ | |
| 275 | + return codec_getitem(encoding, 0); | |
| 276 | +} | |
| 277 | + | |
| 278 | +PyObject *PyCodec_Decoder(const char *encoding) | |
| 279 | +{ | |
| 280 | + return codec_getitem(encoding, 1); | |
| 281 | +} | |
| 282 | + | |
| 283 | +PyObject *PyCodec_IncrementalEncoder(const char *encoding, | |
| 284 | + const char *errors) | |
| 285 | +{ | |
| 286 | + return codec_getincrementalcodec(encoding, errors, "incrementalencoder"); | |
| 287 | +} | |
| 288 | + | |
| 289 | +PyObject *PyCodec_IncrementalDecoder(const char *encoding, | |
| 290 | + const char *errors) | |
| 291 | +{ | |
| 292 | + return codec_getincrementalcodec(encoding, errors, "incrementaldecoder"); | |
| 293 | +} | |
| 294 | + | |
| 295 | +PyObject *PyCodec_StreamReader(const char *encoding, | |
| 296 | + PyObject *stream, | |
| 297 | + const char *errors) | |
| 298 | +{ | |
| 299 | + return codec_getstreamcodec(encoding, stream, errors, 2); | |
| 300 | +} | |
| 301 | + | |
| 302 | +PyObject *PyCodec_StreamWriter(const char *encoding, | |
| 303 | + PyObject *stream, | |
| 304 | + const char *errors) | |
| 305 | +{ | |
| 306 | + return codec_getstreamcodec(encoding, stream, errors, 3); | |
| 307 | +} | |
| 308 | + | |
| 309 | +/* Encode an object (e.g. an Unicode object) using the given encoding | |
| 310 | + and return the resulting encoded object (usually a Python string). | |
| 311 | + | |
| 312 | + errors is passed to the encoder factory as argument if non-NULL. */ | |
| 313 | + | |
| 314 | +PyObject *PyCodec_Encode(PyObject *object, | |
| 315 | + const char *encoding, | |
| 316 | + const char *errors) | |
| 317 | +{ | |
| 318 | + PyObject *encoder = NULL; | |
| 319 | + PyObject *args = NULL, *result = NULL; | |
| 320 | + PyObject *v; | |
| 321 | + | |
| 322 | + encoder = PyCodec_Encoder(encoding); | |
| 323 | + if (encoder == NULL) | |
| 324 | + goto onError; | |
| 325 | + | |
| 326 | + args = args_tuple(object, errors); | |
| 327 | + if (args == NULL) | |
| 328 | + goto onError; | |
| 329 | + | |
| 330 | + result = PyEval_CallObject(encoder,args); | |
| 331 | + if (result == NULL) | |
| 332 | + goto onError; | |
| 333 | + | |
| 334 | + if (!PyTuple_Check(result) || | |
| 335 | + PyTuple_GET_SIZE(result) != 2) { | |
| 336 | + PyErr_SetString(PyExc_TypeError, | |
| 337 | + "encoder must return a tuple (object,integer)"); | |
| 338 | + goto onError; | |
| 339 | + } | |
| 340 | + v = PyTuple_GET_ITEM(result,0); | |
| 341 | + Py_INCREF(v); | |
| 342 | + /* We don't check or use the second (integer) entry. */ | |
| 343 | + | |
| 344 | + Py_DECREF(args); | |
| 345 | + Py_DECREF(encoder); | |
| 346 | + Py_DECREF(result); | |
| 347 | + return v; | |
| 348 | + | |
| 349 | + onError: | |
| 350 | + Py_XDECREF(result); | |
| 351 | + Py_XDECREF(args); | |
| 352 | + Py_XDECREF(encoder); | |
| 353 | + return NULL; | |
| 354 | +} | |
| 355 | + | |
| 356 | +/* Decode an object (usually a Python string) using the given encoding | |
| 357 | + and return an equivalent object (e.g. an Unicode object). | |
| 358 | + | |
| 359 | + errors is passed to the decoder factory as argument if non-NULL. */ | |
| 360 | + | |
| 361 | +PyObject *PyCodec_Decode(PyObject *object, | |
| 362 | + const char *encoding, | |
| 363 | + const char *errors) | |
| 364 | +{ | |
| 365 | + PyObject *decoder = NULL; | |
| 366 | + PyObject *args = NULL, *result = NULL; | |
| 367 | + PyObject *v; | |
| 368 | + | |
| 369 | + decoder = PyCodec_Decoder(encoding); | |
| 370 | + if (decoder == NULL) | |
| 371 | + goto onError; | |
| 372 | + | |
| 373 | + args = args_tuple(object, errors); | |
| 374 | + if (args == NULL) | |
| 375 | + goto onError; | |
| 376 | + | |
| 377 | + result = PyEval_CallObject(decoder,args); | |
| 378 | + if (result == NULL) | |
| 379 | + goto onError; | |
| 380 | + if (!PyTuple_Check(result) || | |
| 381 | + PyTuple_GET_SIZE(result) != 2) { | |
| 382 | + PyErr_SetString(PyExc_TypeError, | |
| 383 | + "decoder must return a tuple (object,integer)"); | |
| 384 | + goto onError; | |
| 385 | + } | |
| 386 | + v = PyTuple_GET_ITEM(result,0); | |
| 387 | + Py_INCREF(v); | |
| 388 | + /* We don't check or use the second (integer) entry. */ | |
| 389 | + | |
| 390 | + Py_DECREF(args); | |
| 391 | + Py_DECREF(decoder); | |
| 392 | + Py_DECREF(result); | |
| 393 | + return v; | |
| 394 | + | |
| 395 | + onError: | |
| 396 | + Py_XDECREF(args); | |
| 397 | + Py_XDECREF(decoder); | |
| 398 | + Py_XDECREF(result); | |
| 399 | + return NULL; | |
| 400 | +} | |
| 401 | + | |
| 402 | +/* Register the error handling callback function error under the name | |
| 403 | + name. This function will be called by the codec when it encounters | |
| 404 | + an unencodable characters/undecodable bytes and doesn't know the | |
| 405 | + callback name, when name is specified as the error parameter | |
| 406 | + in the call to the encode/decode function. | |
| 407 | + Return 0 on success, -1 on error */ | |
| 408 | +int PyCodec_RegisterError(const char *name, PyObject *error) | |
| 409 | +{ | |
| 410 | + PyInterpreterState *interp = PyThreadState_GET()->interp; | |
| 411 | + if (interp->codec_search_path == NULL && _PyCodecRegistry_Init()) | |
| 412 | + return -1; | |
| 413 | + if (!PyCallable_Check(error)) { | |
| 414 | + PyErr_SetString(PyExc_TypeError, "handler must be callable"); | |
| 415 | + return -1; | |
| 416 | + } | |
| 417 | + return PyDict_SetItemString(interp->codec_error_registry, | |
| 418 | + (char *)name, error); | |
| 419 | +} | |
| 420 | + | |
| 421 | +/* Lookup the error handling callback function registered under the | |
| 422 | + name error. As a special case NULL can be passed, in which case | |
| 423 | + the error handling callback for strict encoding will be returned. */ | |
| 424 | +PyObject *PyCodec_LookupError(const char *name) | |
| 425 | +{ | |
| 426 | + PyObject *handler = NULL; | |
| 427 | + | |
| 428 | + PyInterpreterState *interp = PyThreadState_GET()->interp; | |
| 429 | + if (interp->codec_search_path == NULL && _PyCodecRegistry_Init()) | |
| 430 | + return NULL; | |
| 431 | + | |
| 432 | + if (name==NULL) | |
| 433 | + name = "strict"; | |
| 434 | + handler = PyDict_GetItemString(interp->codec_error_registry, (char *)name); | |
| 435 | + if (!handler) | |
| 436 | + PyErr_Format(PyExc_LookupError, "unknown error handler name '%.400s'", name); | |
| 437 | + else | |
| 438 | + Py_INCREF(handler); | |
| 439 | + return handler; | |
| 440 | +} | |
| 441 | + | |
| 442 | +static void wrong_exception_type(PyObject *exc) | |
| 443 | +{ | |
| 444 | + PyObject *type = PyObject_GetAttrString(exc, "__class__"); | |
| 445 | + if (type != NULL) { | |
| 446 | + PyObject *name = PyObject_GetAttrString(type, "__name__"); | |
| 447 | + Py_DECREF(type); | |
| 448 | + if (name != NULL) { | |
| 449 | + PyObject *string = PyObject_Str(name); | |
| 450 | + Py_DECREF(name); | |
| 451 | + if (string != NULL) { | |
| 452 | + PyErr_Format(PyExc_TypeError, | |
| 453 | + "don't know how to handle %.400s in error callback", | |
| 454 | + PyString_AS_STRING(string)); | |
| 455 | + Py_DECREF(string); | |
| 456 | + } | |
| 457 | + } | |
| 458 | + } | |
| 459 | +} | |
| 460 | + | |
| 461 | +PyObject *PyCodec_StrictErrors(PyObject *exc) | |
| 462 | +{ | |
| 463 | + if (PyExceptionInstance_Check(exc)) | |
| 464 | + PyErr_SetObject(PyExceptionInstance_Class(exc), exc); | |
| 465 | + else | |
| 466 | + PyErr_SetString(PyExc_TypeError, "codec must pass exception instance"); | |
| 467 | + return NULL; | |
| 468 | +} | |
| 469 | + | |
| 470 | + | |
| 471 | +#ifdef Py_USING_UNICODE | |
| 472 | +PyObject *PyCodec_IgnoreErrors(PyObject *exc) | |
| 473 | +{ | |
| 474 | + Py_ssize_t end; | |
| 475 | + if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) { | |
| 476 | + if (PyUnicodeEncodeError_GetEnd(exc, &end)) | |
| 477 | + return NULL; | |
| 478 | + } | |
| 479 | + else if (PyObject_IsInstance(exc, PyExc_UnicodeDecodeError)) { | |
| 480 | + if (PyUnicodeDecodeError_GetEnd(exc, &end)) | |
| 481 | + return NULL; | |
| 482 | + } | |
| 483 | + else if (PyObject_IsInstance(exc, PyExc_UnicodeTranslateError)) { | |
| 484 | + if (PyUnicodeTranslateError_GetEnd(exc, &end)) | |
| 485 | + return NULL; | |
| 486 | + } | |
| 487 | + else { | |
| 488 | + wrong_exception_type(exc); | |
| 489 | + return NULL; | |
| 490 | + } | |
| 491 | + /* ouch: passing NULL, 0, pos gives None instead of u'' */ | |
| 492 | + return Py_BuildValue("(u#n)", &end, 0, end); | |
| 493 | +} | |
| 494 | + | |
| 495 | + | |
| 496 | +PyObject *PyCodec_ReplaceErrors(PyObject *exc) | |
| 497 | +{ | |
| 498 | + PyObject *restuple; | |
| 499 | + Py_ssize_t start; | |
| 500 | + Py_ssize_t end; | |
| 501 | + Py_ssize_t i; | |
| 502 | + | |
| 503 | + if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) { | |
| 504 | + PyObject *res; | |
| 505 | + Py_UNICODE *p; | |
| 506 | + if (PyUnicodeEncodeError_GetStart(exc, &start)) | |
| 507 | + return NULL; | |
| 508 | + if (PyUnicodeEncodeError_GetEnd(exc, &end)) | |
| 509 | + return NULL; | |
| 510 | + res = PyUnicode_FromUnicode(NULL, end-start); | |
| 511 | + if (res == NULL) | |
| 512 | + return NULL; | |
| 513 | + for (p = PyUnicode_AS_UNICODE(res), i = start; | |
| 514 | + i<end; ++p, ++i) | |
| 515 | + *p = '?'; | |
| 516 | + restuple = Py_BuildValue("(On)", res, end); | |
| 517 | + Py_DECREF(res); | |
| 518 | + return restuple; | |
| 519 | + } | |
| 520 | + else if (PyObject_IsInstance(exc, PyExc_UnicodeDecodeError)) { | |
| 521 | + Py_UNICODE res = Py_UNICODE_REPLACEMENT_CHARACTER; | |
| 522 | + if (PyUnicodeDecodeError_GetEnd(exc, &end)) | |
| 523 | + return NULL; | |
| 524 | + return Py_BuildValue("(u#n)", &res, 1, end); | |
| 525 | + } | |
| 526 | + else if (PyObject_IsInstance(exc, PyExc_UnicodeTranslateError)) { | |
| 527 | + PyObject *res; | |
| 528 | + Py_UNICODE *p; | |
| 529 | + if (PyUnicodeTranslateError_GetStart(exc, &start)) | |
| 530 | + return NULL; | |
| 531 | + if (PyUnicodeTranslateError_GetEnd(exc, &end)) | |
| 532 | + return NULL; | |
| 533 | + res = PyUnicode_FromUnicode(NULL, end-start); | |
| 534 | + if (res == NULL) | |
| 535 | + return NULL; | |
| 536 | + for (p = PyUnicode_AS_UNICODE(res), i = start; | |
| 537 | + i<end; ++p, ++i) | |
| 538 | + *p = Py_UNICODE_REPLACEMENT_CHARACTER; | |
| 539 | + restuple = Py_BuildValue("(On)", res, end); | |
| 540 | + Py_DECREF(res); | |
| 541 | + return restuple; | |
| 542 | + } | |
| 543 | + else { | |
| 544 | + wrong_exception_type(exc); | |
| 545 | + return NULL; | |
| 546 | + } | |
| 547 | +} | |
| 548 | + | |
| 549 | +PyObject *PyCodec_XMLCharRefReplaceErrors(PyObject *exc) | |
| 550 | +{ | |
| 551 | + if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) { | |
| 552 | + PyObject *restuple; | |
| 553 | + PyObject *object; | |
| 554 | + Py_ssize_t start; | |
| 555 | + Py_ssize_t end; | |
| 556 | + PyObject *res; | |
| 557 | + Py_UNICODE *p; | |
| 558 | + Py_UNICODE *startp; | |
| 559 | + Py_UNICODE *outp; | |
| 560 | + int ressize; | |
| 561 | + if (PyUnicodeEncodeError_GetStart(exc, &start)) | |
| 562 | + return NULL; | |
| 563 | + if (PyUnicodeEncodeError_GetEnd(exc, &end)) | |
| 564 | + return NULL; | |
| 565 | + if (!(object = PyUnicodeEncodeError_GetObject(exc))) | |
| 566 | + return NULL; | |
| 567 | + startp = PyUnicode_AS_UNICODE(object); | |
| 568 | + for (p = startp+start, ressize = 0; p < startp+end; ++p) { | |
| 569 | + if (*p<10) | |
| 570 | + ressize += 2+1+1; | |
| 571 | + else if (*p<100) | |
| 572 | + ressize += 2+2+1; | |
| 573 | + else if (*p<1000) | |
| 574 | + ressize += 2+3+1; | |
| 575 | + else if (*p<10000) | |
| 576 | + ressize += 2+4+1; | |
| 577 | +#ifndef Py_UNICODE_WIDE | |
| 578 | + else | |
| 579 | + ressize += 2+5+1; | |
| 580 | +#else | |
| 581 | + else if (*p<100000) | |
| 582 | + ressize += 2+5+1; | |
| 583 | + else if (*p<1000000) | |
| 584 | + ressize += 2+6+1; | |
| 585 | + else | |
| 586 | + ressize += 2+7+1; | |
| 587 | +#endif | |
| 588 | + } | |
| 589 | + /* allocate replacement */ | |
| 590 | + res = PyUnicode_FromUnicode(NULL, ressize); | |
| 591 | + if (res == NULL) { | |
| 592 | + Py_DECREF(object); | |
| 593 | + return NULL; | |
| 594 | + } | |
| 595 | + /* generate replacement */ | |
| 596 | + for (p = startp+start, outp = PyUnicode_AS_UNICODE(res); | |
| 597 | + p < startp+end; ++p) { | |
| 598 | + Py_UNICODE c = *p; | |
| 599 | + int digits; | |
| 600 | + int base; | |
| 601 | + *outp++ = '&'; | |
| 602 | + *outp++ = '#'; | |
| 603 | + if (*p<10) { | |
| 604 | + digits = 1; | |
| 605 | + base = 1; | |
| 606 | + } | |
| 607 | + else if (*p<100) { | |
| 608 | + digits = 2; | |
| 609 | + base = 10; | |
| 610 | + } | |
| 611 | + else if (*p<1000) { | |
| 612 | + digits = 3; | |
| 613 | + base = 100; | |
| 614 | + } | |
| 615 | + else if (*p<10000) { | |
| 616 | + digits = 4; | |
| 617 | + base = 1000; | |
| 618 | + } | |
| 619 | +#ifndef Py_UNICODE_WIDE | |
| 620 | + else { | |
| 621 | + digits = 5; | |
| 622 | + base = 10000; | |
| 623 | + } | |
| 624 | +#else | |
| 625 | + else if (*p<100000) { | |
| 626 | + digits = 5; | |
| 627 | + base = 10000; | |
| 628 | + } | |
| 629 | + else if (*p<1000000) { | |
| 630 | + digits = 6; | |
| 631 | + base = 100000; | |
| 632 | + } | |
| 633 | + else { | |
| 634 | + digits = 7; | |
| 635 | + base = 1000000; | |
| 636 | + } | |
| 637 | +#endif | |
| 638 | + while (digits-->0) { | |
| 639 | + *outp++ = '0' + c/base; | |
| 640 | + c %= base; | |
| 641 | + base /= 10; | |
| 642 | + } | |
| 643 | + *outp++ = ';'; | |
| 644 | + } | |
| 645 | + restuple = Py_BuildValue("(On)", res, end); | |
| 646 | + Py_DECREF(res); | |
| 647 | + Py_DECREF(object); | |
| 648 | + return restuple; | |
| 649 | + } | |
| 650 | + else { | |
| 651 | + wrong_exception_type(exc); | |
| 652 | + return NULL; | |
| 653 | + } | |
| 654 | +} | |
| 655 | + | |
| 656 | +static Py_UNICODE hexdigits[] = { | |
| 657 | + '0', '1', '2', '3', '4', '5', '6', '7', | |
| 658 | + '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' | |
| 659 | +}; | |
| 660 | + | |
| 661 | +PyObject *PyCodec_BackslashReplaceErrors(PyObject *exc) | |
| 662 | +{ | |
| 663 | + if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) { | |
| 664 | + PyObject *restuple; | |
| 665 | + PyObject *object; | |
| 666 | + Py_ssize_t start; | |
| 667 | + Py_ssize_t end; | |
| 668 | + PyObject *res; | |
| 669 | + Py_UNICODE *p; | |
| 670 | + Py_UNICODE *startp; | |
| 671 | + Py_UNICODE *outp; | |
| 672 | + int ressize; | |
| 673 | + if (PyUnicodeEncodeError_GetStart(exc, &start)) | |
| 674 | + return NULL; | |
| 675 | + if (PyUnicodeEncodeError_GetEnd(exc, &end)) | |
| 676 | + return NULL; | |
| 677 | + if (!(object = PyUnicodeEncodeError_GetObject(exc))) | |
| 678 | + return NULL; | |
| 679 | + startp = PyUnicode_AS_UNICODE(object); | |
| 680 | + for (p = startp+start, ressize = 0; p < startp+end; ++p) { | |
| 681 | +#ifdef Py_UNICODE_WIDE | |
| 682 | + if (*p >= 0x00010000) | |
| 683 | + ressize += 1+1+8; | |
| 684 | + else | |
| 685 | +#endif | |
| 686 | + if (*p >= 0x100) { | |
| 687 | + ressize += 1+1+4; | |
| 688 | + } | |
| 689 | + else | |
| 690 | + ressize += 1+1+2; | |
| 691 | + } | |
| 692 | + res = PyUnicode_FromUnicode(NULL, ressize); | |
| 693 | + if (res==NULL) | |
| 694 | + return NULL; | |
| 695 | + for (p = startp+start, outp = PyUnicode_AS_UNICODE(res); | |
| 696 | + p < startp+end; ++p) { | |
| 697 | + Py_UNICODE c = *p; | |
| 698 | + *outp++ = '\\'; | |
| 699 | +#ifdef Py_UNICODE_WIDE | |
| 700 | + if (c >= 0x00010000) { | |
| 701 | + *outp++ = 'U'; | |
| 702 | + *outp++ = hexdigits[(c>>28)&0xf]; | |
| 703 | + *outp++ = hexdigits[(c>>24)&0xf]; | |
| 704 | + *outp++ = hexdigits[(c>>20)&0xf]; | |
| 705 | + *outp++ = hexdigits[(c>>16)&0xf]; | |
| 706 | + *outp++ = hexdigits[(c>>12)&0xf]; | |
| 707 | + *outp++ = hexdigits[(c>>8)&0xf]; | |
| 708 | + } | |
| 709 | + else | |
| 710 | +#endif | |
| 711 | + if (c >= 0x100) { | |
| 712 | + *outp++ = 'u'; | |
| 713 | + *outp++ = hexdigits[(c>>12)&0xf]; | |
| 714 | + *outp++ = hexdigits[(c>>8)&0xf]; | |
| 715 | + } | |
| 716 | + else | |
| 717 | + *outp++ = 'x'; | |
| 718 | + *outp++ = hexdigits[(c>>4)&0xf]; | |
| 719 | + *outp++ = hexdigits[c&0xf]; | |
| 720 | + } | |
| 721 | + | |
| 722 | + restuple = Py_BuildValue("(On)", res, end); | |
| 723 | + Py_DECREF(res); | |
| 724 | + Py_DECREF(object); | |
| 725 | + return restuple; | |
| 726 | + } | |
| 727 | + else { | |
| 728 | + wrong_exception_type(exc); | |
| 729 | + return NULL; | |
| 730 | + } | |
| 731 | +} | |
| 732 | +#endif | |
| 733 | + | |
| 734 | +static PyObject *strict_errors(PyObject *self, PyObject *exc) | |
| 735 | +{ | |
| 736 | + return PyCodec_StrictErrors(exc); | |
| 737 | +} | |
| 738 | + | |
| 739 | + | |
| 740 | +#ifdef Py_USING_UNICODE | |
| 741 | +static PyObject *ignore_errors(PyObject *self, PyObject *exc) | |
| 742 | +{ | |
| 743 | + return PyCodec_IgnoreErrors(exc); | |
| 744 | +} | |
| 745 | + | |
| 746 | + | |
| 747 | +static PyObject *replace_errors(PyObject *self, PyObject *exc) | |
| 748 | +{ | |
| 749 | + return PyCodec_ReplaceErrors(exc); | |
| 750 | +} | |
| 751 | + | |
| 752 | + | |
| 753 | +static PyObject *xmlcharrefreplace_errors(PyObject *self, PyObject *exc) | |
| 754 | +{ | |
| 755 | + return PyCodec_XMLCharRefReplaceErrors(exc); | |
| 756 | +} | |
| 757 | + | |
| 758 | + | |
| 759 | +static PyObject *backslashreplace_errors(PyObject *self, PyObject *exc) | |
| 760 | +{ | |
| 761 | + return PyCodec_BackslashReplaceErrors(exc); | |
| 762 | +} | |
| 763 | +#endif | |
| 764 | + | |
| 765 | +static int _PyCodecRegistry_Init(void) | |
| 766 | +{ | |
| 767 | + static struct { | |
| 768 | + char *name; | |
| 769 | + PyMethodDef def; | |
| 770 | + } methods[] = | |
| 771 | + { | |
| 772 | + { | |
| 773 | + "strict", | |
| 774 | + { | |
| 775 | + "strict_errors", | |
| 776 | + strict_errors, | |
| 777 | + METH_O | |
| 778 | + } | |
| 779 | + }, | |
| 780 | +#ifdef Py_USING_UNICODE | |
| 781 | + { | |
| 782 | + "ignore", | |
| 783 | + { | |
| 784 | + "ignore_errors", | |
| 785 | + ignore_errors, | |
| 786 | + METH_O | |
| 787 | + } | |
| 788 | + }, | |
| 789 | + { | |
| 790 | + "replace", | |
| 791 | + { | |
| 792 | + "replace_errors", | |
| 793 | + replace_errors, | |
| 794 | + METH_O | |
| 795 | + } | |
| 796 | + }, | |
| 797 | + { | |
| 798 | + "xmlcharrefreplace", | |
| 799 | + { | |
| 800 | + "xmlcharrefreplace_errors", | |
| 801 | + xmlcharrefreplace_errors, | |
| 802 | + METH_O | |
| 803 | + } | |
| 804 | + }, | |
| 805 | + { | |
| 806 | + "backslashreplace", | |
| 807 | + { | |
| 808 | + "backslashreplace_errors", | |
| 809 | + backslashreplace_errors, | |
| 810 | + METH_O | |
| 811 | + } | |
| 812 | + } | |
| 813 | +#endif | |
| 814 | + }; | |
| 815 | + | |
| 816 | + PyInterpreterState *interp = PyThreadState_GET()->interp; | |
| 817 | + PyObject *mod; | |
| 818 | + unsigned i; | |
| 819 | + | |
| 820 | + if (interp->codec_search_path != NULL) | |
| 821 | + return 0; | |
| 822 | + | |
| 823 | + interp->codec_search_path = PyList_New(0); | |
| 824 | + interp->codec_search_cache = PyDict_New(); | |
| 825 | + interp->codec_error_registry = PyDict_New(); | |
| 826 | + | |
| 827 | + if (interp->codec_error_registry) { | |
| 828 | + for (i = 0; i < sizeof(methods)/sizeof(methods[0]); ++i) { | |
| 829 | + PyObject *func = PyCFunction_New(&methods[i].def, NULL); | |
| 830 | + int res; | |
| 831 | + if (!func) | |
| 832 | + Py_FatalError("can't initialize codec error registry"); | |
| 833 | + res = PyCodec_RegisterError(methods[i].name, func); | |
| 834 | + Py_DECREF(func); | |
| 835 | + if (res) | |
| 836 | + Py_FatalError("can't initialize codec error registry"); | |
| 837 | + } | |
| 838 | + } | |
| 839 | + | |
| 840 | + if (interp->codec_search_path == NULL || | |
| 841 | + interp->codec_search_cache == NULL || | |
| 842 | + interp->codec_error_registry == NULL) | |
| 843 | + Py_FatalError("can't initialize codec registry"); | |
| 844 | + | |
| 845 | + mod = PyImport_ImportModuleLevel("encodings", NULL, NULL, NULL, 0); | |
| 846 | + if (mod == NULL) { | |
| 847 | + if (PyErr_ExceptionMatches(PyExc_ImportError)) { | |
| 848 | + /* Ignore ImportErrors... this is done so that | |
| 849 | + distributions can disable the encodings package. Note | |
| 850 | + that other errors are not masked, e.g. SystemErrors | |
| 851 | + raised to inform the user of an error in the Python | |
| 852 | + configuration are still reported back to the user. */ | |
| 853 | + PyErr_Clear(); | |
| 854 | + return 0; | |
| 855 | + } | |
| 856 | + return -1; | |
| 857 | + } | |
| 858 | + Py_DECREF(mod); | |
| 859 | + return 0; | |
| 860 | +} | |
| @@ -0,0 +1,4570 @@ | ||
| 1 | +/* | |
| 2 | + * This file compiles an abstract syntax tree (AST) into Python bytecode. | |
| 3 | + * | |
| 4 | + * The primary entry point is PyAST_Compile(), which returns a | |
| 5 | + * PyCodeObject. The compiler makes several passes to build the code | |
| 6 | + * object: | |
| 7 | + * 1. Checks for future statements. See future.c | |
| 8 | + * 2. Builds a symbol table. See symtable.c. | |
| 9 | + * 3. Generate code for basic blocks. See compiler_mod() in this file. | |
| 10 | + * 4. Assemble the basic blocks into final code. See assemble() in | |
| 11 | + * this file. | |
| 12 | + * | |
| 13 | + * Note that compiler_mod() suggests module, but the module ast type | |
| 14 | + * (mod_ty) has cases for expressions and interactive statements. | |
| 15 | + * | |
| 16 | + * CAUTION: The VISIT_* macros abort the current function when they | |
| 17 | + * encounter a problem. So don't invoke them when there is memory | |
| 18 | + * which needs to be released. Code blocks are OK, as the compiler | |
| 19 | + * structure takes care of releasing those. | |
| 20 | + */ | |
| 21 | + | |
| 22 | +#include "Python.h" | |
| 23 | + | |
| 24 | +#include "Python-ast.h" | |
| 25 | +#include "node.h" | |
| 26 | +#include "pyarena.h" | |
| 27 | +#include "ast.h" | |
| 28 | +#include "code.h" | |
| 29 | +#include "compile.h" | |
| 30 | +#include "symtable.h" | |
| 31 | +#include "opcode.h" | |
| 32 | + | |
| 33 | +int Py_OptimizeFlag = 0; | |
| 34 | + | |
| 35 | +/* | |
| 36 | + ISSUES: | |
| 37 | + | |
| 38 | + opcode_stack_effect() function should be reviewed since stack depth bugs | |
| 39 | + could be really hard to find later. | |
| 40 | + | |
| 41 | + Dead code is being generated (i.e. after unconditional jumps). | |
| 42 | + XXX(nnorwitz): not sure this is still true | |
| 43 | +*/ | |
| 44 | + | |
| 45 | +#define DEFAULT_BLOCK_SIZE 16 | |
| 46 | +#define DEFAULT_BLOCKS 8 | |
| 47 | +#define DEFAULT_CODE_SIZE 128 | |
| 48 | +#define DEFAULT_LNOTAB_SIZE 16 | |
| 49 | + | |
| 50 | +struct instr { | |
| 51 | + unsigned i_jabs : 1; | |
| 52 | + unsigned i_jrel : 1; | |
| 53 | + unsigned i_hasarg : 1; | |
| 54 | + unsigned char i_opcode; | |
| 55 | + int i_oparg; | |
| 56 | + struct basicblock_ *i_target; /* target block (if jump instruction) */ | |
| 57 | + int i_lineno; | |
| 58 | +}; | |
| 59 | + | |
| 60 | +typedef struct basicblock_ { | |
| 61 | + /* Each basicblock in a compilation unit is linked via b_list in the | |
| 62 | + reverse order that the block are allocated. b_list points to the next | |
| 63 | + block, not to be confused with b_next, which is next by control flow. */ | |
| 64 | + struct basicblock_ *b_list; | |
| 65 | + /* number of instructions used */ | |
| 66 | + int b_iused; | |
| 67 | + /* length of instruction array (b_instr) */ | |
| 68 | + int b_ialloc; | |
| 69 | + /* pointer to an array of instructions, initially NULL */ | |
| 70 | + struct instr *b_instr; | |
| 71 | + /* If b_next is non-NULL, it is a pointer to the next | |
| 72 | + block reached by normal control flow. */ | |
| 73 | + struct basicblock_ *b_next; | |
| 74 | + /* b_seen is used to perform a DFS of basicblocks. */ | |
| 75 | + unsigned b_seen : 1; | |
| 76 | + /* b_return is true if a RETURN_VALUE opcode is inserted. */ | |
| 77 | + unsigned b_return : 1; | |
| 78 | + /* depth of stack upon entry of block, computed by stackdepth() */ | |
| 79 | + int b_startdepth; | |
| 80 | + /* instruction offset for block, computed by assemble_jump_offsets() */ | |
| 81 | + int b_offset; | |
| 82 | +} basicblock; | |
| 83 | + | |
| 84 | +/* fblockinfo tracks the current frame block. | |
| 85 | + | |
| 86 | +A frame block is used to handle loops, try/except, and try/finally. | |
| 87 | +It's called a frame block to distinguish it from a basic block in the | |
| 88 | +compiler IR. | |
| 89 | +*/ | |
| 90 | + | |
| 91 | +enum fblocktype { LOOP, EXCEPT, FINALLY_TRY, FINALLY_END }; | |
| 92 | + | |
| 93 | +struct fblockinfo { | |
| 94 | + enum fblocktype fb_type; | |
| 95 | + basicblock *fb_block; | |
| 96 | +}; | |
| 97 | + | |
| 98 | +/* The following items change on entry and exit of code blocks. | |
| 99 | + They must be saved and restored when returning to a block. | |
| 100 | +*/ | |
| 101 | +struct compiler_unit { | |
| 102 | + PySTEntryObject *u_ste; | |
| 103 | + | |
| 104 | + PyObject *u_name; | |
| 105 | + /* The following fields are dicts that map objects to | |
| 106 | + the index of them in co_XXX. The index is used as | |
| 107 | + the argument for opcodes that refer to those collections. | |
| 108 | + */ | |
| 109 | + PyObject *u_consts; /* all constants */ | |
| 110 | + PyObject *u_names; /* all names */ | |
| 111 | + PyObject *u_varnames; /* local variables */ | |
| 112 | + PyObject *u_cellvars; /* cell variables */ | |
| 113 | + PyObject *u_freevars; /* free variables */ | |
| 114 | + | |
| 115 | + PyObject *u_private; /* for private name mangling */ | |
| 116 | + | |
| 117 | + int u_argcount; /* number of arguments for block */ | |
| 118 | + /* Pointer to the most recently allocated block. By following b_list | |
| 119 | + members, you can reach all early allocated blocks. */ | |
| 120 | + basicblock *u_blocks; | |
| 121 | + basicblock *u_curblock; /* pointer to current block */ | |
| 122 | + int u_tmpname; /* temporary variables for list comps */ | |
| 123 | + | |
| 124 | + int u_nfblocks; | |
| 125 | + struct fblockinfo u_fblock[CO_MAXBLOCKS]; | |
| 126 | + | |
| 127 | + int u_firstlineno; /* the first lineno of the block */ | |
| 128 | + int u_lineno; /* the lineno for the current stmt */ | |
| 129 | + bool u_lineno_set; /* boolean to indicate whether instr | |
| 130 | + has been generated with current lineno */ | |
| 131 | +}; | |
| 132 | + | |
| 133 | +/* This struct captures the global state of a compilation. | |
| 134 | + | |
| 135 | +The u pointer points to the current compilation unit, while units | |
| 136 | +for enclosing blocks are stored in c_stack. The u and c_stack are | |
| 137 | +managed by compiler_enter_scope() and compiler_exit_scope(). | |
| 138 | +*/ | |
| 139 | + | |
| 140 | +struct compiler { | |
| 141 | + const char *c_filename; | |
| 142 | + struct symtable *c_st; | |
| 143 | + PyFutureFeatures *c_future; /* pointer to module's __future__ */ | |
| 144 | + PyCompilerFlags *c_flags; | |
| 145 | + | |
| 146 | + int c_interactive; /* true if in interactive mode */ | |
| 147 | + int c_nestlevel; | |
| 148 | + | |
| 149 | + struct compiler_unit *u; /* compiler state for current block */ | |
| 150 | + PyObject *c_stack; /* Python list holding compiler_unit ptrs */ | |
| 151 | + char *c_encoding; /* source encoding (a borrowed reference) */ | |
| 152 | + PyArena *c_arena; /* pointer to memory allocation arena */ | |
| 153 | +}; | |
| 154 | + | |
| 155 | +struct assembler { | |
| 156 | + PyObject *a_bytecode; /* string containing bytecode */ | |
| 157 | + int a_offset; /* offset into bytecode */ | |
| 158 | + int a_nblocks; /* number of reachable blocks */ | |
| 159 | + basicblock **a_postorder; /* list of blocks in dfs postorder */ | |
| 160 | + PyObject *a_lnotab; /* string containing lnotab */ | |
| 161 | + int a_lnotab_off; /* offset into lnotab */ | |
| 162 | + int a_lineno; /* last lineno of emitted instruction */ | |
| 163 | + int a_lineno_off; /* bytecode offset of last lineno */ | |
| 164 | +}; | |
| 165 | + | |
| 166 | +static int compiler_enter_scope(struct compiler *, identifier, void *, int); | |
| 167 | +static void compiler_free(struct compiler *); | |
| 168 | +static basicblock *compiler_new_block(struct compiler *); | |
| 169 | +static int compiler_next_instr(struct compiler *, basicblock *); | |
| 170 | +static int compiler_addop(struct compiler *, int); | |
| 171 | +static int compiler_addop_o(struct compiler *, int, PyObject *, PyObject *); | |
| 172 | +static int compiler_addop_i(struct compiler *, int, int); | |
| 173 | +static int compiler_addop_j(struct compiler *, int, basicblock *, int); | |
| 174 | +static basicblock *compiler_use_new_block(struct compiler *); | |
| 175 | +static int compiler_error(struct compiler *, const char *); | |
| 176 | +static int compiler_nameop(struct compiler *, identifier, expr_context_ty); | |
| 177 | + | |
| 178 | +static PyCodeObject *compiler_mod(struct compiler *, mod_ty); | |
| 179 | +static int compiler_visit_stmt(struct compiler *, stmt_ty); | |
| 180 | +static int compiler_visit_keyword(struct compiler *, keyword_ty); | |
| 181 | +static int compiler_visit_expr(struct compiler *, expr_ty); | |
| 182 | +static int compiler_augassign(struct compiler *, stmt_ty); | |
| 183 | +static int compiler_visit_slice(struct compiler *, slice_ty, | |
| 184 | + expr_context_ty); | |
| 185 | + | |
| 186 | +static int compiler_push_fblock(struct compiler *, enum fblocktype, | |
| 187 | + basicblock *); | |
| 188 | +static void compiler_pop_fblock(struct compiler *, enum fblocktype, | |
| 189 | + basicblock *); | |
| 190 | +/* Returns true if there is a loop on the fblock stack. */ | |
| 191 | +static int compiler_in_loop(struct compiler *); | |
| 192 | + | |
| 193 | +static int inplace_binop(struct compiler *, operator_ty); | |
| 194 | +static int expr_constant(expr_ty e); | |
| 195 | + | |
| 196 | +static int compiler_with(struct compiler *, stmt_ty); | |
| 197 | + | |
| 198 | +static PyCodeObject *assemble(struct compiler *, int addNone); | |
| 199 | +static PyObject *__doc__; | |
| 200 | + | |
| 201 | +PyObject * | |
| 202 | +_Py_Mangle(PyObject *privateobj, PyObject *ident) | |
| 203 | +{ | |
| 204 | + /* Name mangling: __private becomes _classname__private. | |
| 205 | + This is independent from how the name is used. */ | |
| 206 | + const char *p, *name = PyString_AsString(ident); | |
| 207 | + char *buffer; | |
| 208 | + size_t nlen, plen; | |
| 209 | + if (privateobj == NULL || !PyString_Check(privateobj) || | |
| 210 | + name == NULL || name[0] != '_' || name[1] != '_') { | |
| 211 | + Py_INCREF(ident); | |
| 212 | + return ident; | |
| 213 | + } | |
| 214 | + p = PyString_AsString(privateobj); | |
| 215 | + nlen = strlen(name); | |
| 216 | + if (name[nlen-1] == '_' && name[nlen-2] == '_') { | |
| 217 | + Py_INCREF(ident); | |
| 218 | + return ident; /* Don't mangle __whatever__ */ | |
| 219 | + } | |
| 220 | + /* Strip leading underscores from class name */ | |
| 221 | + while (*p == '_') | |
| 222 | + p++; | |
| 223 | + if (*p == '\0') { | |
| 224 | + Py_INCREF(ident); | |
| 225 | + return ident; /* Don't mangle if class is just underscores */ | |
| 226 | + } | |
| 227 | + plen = strlen(p); | |
| 228 | + ident = PyString_FromStringAndSize(NULL, 1 + nlen + plen); | |
| 229 | + if (!ident) | |
| 230 | + return 0; | |
| 231 | + /* ident = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */ | |
| 232 | + buffer = PyString_AS_STRING(ident); | |
| 233 | + buffer[0] = '_'; | |
| 234 | + strncpy(buffer+1, p, plen); | |
| 235 | + strcpy(buffer+1+plen, name); | |
| 236 | + return ident; | |
| 237 | +} | |
| 238 | + | |
| 239 | +static int | |
| 240 | +compiler_init(struct compiler *c) | |
| 241 | +{ | |
| 242 | + memset(c, 0, sizeof(struct compiler)); | |
| 243 | + | |
| 244 | + c->c_stack = PyList_New(0); | |
| 245 | + if (!c->c_stack) | |
| 246 | + return 0; | |
| 247 | + | |
| 248 | + return 1; | |
| 249 | +} | |
| 250 | + | |
| 251 | +PyCodeObject * | |
| 252 | +PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags, | |
| 253 | + PyArena *arena) | |
| 254 | +{ | |
| 255 | + struct compiler c; | |
| 256 | + PyCodeObject *co = NULL; | |
| 257 | + PyCompilerFlags local_flags; | |
| 258 | + int merged; | |
| 259 | + | |
| 260 | + if (!__doc__) { | |
| 261 | + __doc__ = PyString_InternFromString("__doc__"); | |
| 262 | + if (!__doc__) | |
| 263 | + return NULL; | |
| 264 | + } | |
| 265 | + | |
| 266 | + if (!compiler_init(&c)) | |
| 267 | + return NULL; | |
| 268 | + c.c_filename = filename; | |
| 269 | + c.c_arena = arena; | |
| 270 | + c.c_future = PyFuture_FromAST(mod, filename); | |
| 271 | + if (c.c_future == NULL) | |
| 272 | + goto finally; | |
| 273 | + if (!flags) { | |
| 274 | + local_flags.cf_flags = 0; | |
| 275 | + flags = &local_flags; | |
| 276 | + } | |
| 277 | + merged = c.c_future->ff_features | flags->cf_flags; | |
| 278 | + c.c_future->ff_features = merged; | |
| 279 | + flags->cf_flags = merged; | |
| 280 | + c.c_flags = flags; | |
| 281 | + c.c_nestlevel = 0; | |
| 282 | + | |
| 283 | + c.c_st = PySymtable_Build(mod, filename, c.c_future); | |
| 284 | + if (c.c_st == NULL) { | |
| 285 | + if (!PyErr_Occurred()) | |
| 286 | + PyErr_SetString(PyExc_SystemError, "no symtable"); | |
| 287 | + goto finally; | |
| 288 | + } | |
| 289 | + | |
| 290 | + /* XXX initialize to NULL for now, need to handle */ | |
| 291 | + c.c_encoding = NULL; | |
| 292 | + | |
| 293 | + co = compiler_mod(&c, mod); | |
| 294 | + | |
| 295 | + finally: | |
| 296 | + compiler_free(&c); | |
| 297 | + assert(co || PyErr_Occurred()); | |
| 298 | + return co; | |
| 299 | +} | |
| 300 | + | |
| 301 | +PyCodeObject * | |
| 302 | +PyNode_Compile(struct _node *n, const char *filename) | |
| 303 | +{ | |
| 304 | + PyCodeObject *co = NULL; | |
| 305 | + mod_ty mod; | |
| 306 | + PyArena *arena = PyArena_New(); | |
| 307 | + if (!arena) | |
| 308 | + return NULL; | |
| 309 | + mod = PyAST_FromNode(n, NULL, filename, arena); | |
| 310 | + if (mod) | |
| 311 | + co = PyAST_Compile(mod, filename, NULL, arena); | |
| 312 | + PyArena_Free(arena); | |
| 313 | + return co; | |
| 314 | +} | |
| 315 | + | |
| 316 | +static void | |
| 317 | +compiler_free(struct compiler *c) | |
| 318 | +{ | |
| 319 | + if (c->c_st) | |
| 320 | + PySymtable_Free(c->c_st); | |
| 321 | + if (c->c_future) | |
| 322 | + PyObject_Free(c->c_future); | |
| 323 | + Py_DECREF(c->c_stack); | |
| 324 | +} | |
| 325 | + | |
| 326 | +static PyObject * | |
| 327 | +list2dict(PyObject *list) | |
| 328 | +{ | |
| 329 | + Py_ssize_t i, n; | |
| 330 | + PyObject *v, *k; | |
| 331 | + PyObject *dict = PyDict_New(); | |
| 332 | + if (!dict) return NULL; | |
| 333 | + | |
| 334 | + n = PyList_Size(list); | |
| 335 | + for (i = 0; i < n; i++) { | |
| 336 | + v = PyInt_FromLong(i); | |
| 337 | + if (!v) { | |
| 338 | + Py_DECREF(dict); | |
| 339 | + return NULL; | |
| 340 | + } | |
| 341 | + k = PyList_GET_ITEM(list, i); | |
| 342 | + k = PyTuple_Pack(2, k, k->ob_type); | |
| 343 | + if (k == NULL || PyDict_SetItem(dict, k, v) < 0) { | |
| 344 | + Py_XDECREF(k); | |
| 345 | + Py_DECREF(v); | |
| 346 | + Py_DECREF(dict); | |
| 347 | + return NULL; | |
| 348 | + } | |
| 349 | + Py_DECREF(k); | |
| 350 | + Py_DECREF(v); | |
| 351 | + } | |
| 352 | + return dict; | |
| 353 | +} | |
| 354 | + | |
| 355 | +/* Return new dict containing names from src that match scope(s). | |
| 356 | + | |
| 357 | +src is a symbol table dictionary. If the scope of a name matches | |
| 358 | +either scope_type or flag is set, insert it into the new dict. The | |
| 359 | +values are integers, starting at offset and increasing by one for | |
| 360 | +each key. | |
| 361 | +*/ | |
| 362 | + | |
| 363 | +static PyObject * | |
| 364 | +dictbytype(PyObject *src, int scope_type, int flag, int offset) | |
| 365 | +{ | |
| 366 | + Py_ssize_t pos = 0, i = offset, scope; | |
| 367 | + PyObject *k, *v, *dest = PyDict_New(); | |
| 368 | + | |
| 369 | + assert(offset >= 0); | |
| 370 | + if (dest == NULL) | |
| 371 | + return NULL; | |
| 372 | + | |
| 373 | + while (PyDict_Next(src, &pos, &k, &v)) { | |
| 374 | + /* XXX this should probably be a macro in symtable.h */ | |
| 375 | + assert(PyInt_Check(v)); | |
| 376 | + scope = (PyInt_AS_LONG(v) >> SCOPE_OFF) & SCOPE_MASK; | |
| 377 | + | |
| 378 | + if (scope == scope_type || PyInt_AS_LONG(v) & flag) { | |
| 379 | + PyObject *tuple, *item = PyInt_FromLong(i); | |
| 380 | + if (item == NULL) { | |
| 381 | + Py_DECREF(dest); | |
| 382 | + return NULL; | |
| 383 | + } | |
| 384 | + i++; | |
| 385 | + tuple = PyTuple_Pack(2, k, k->ob_type); | |
| 386 | + if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) { | |
| 387 | + Py_DECREF(item); | |
| 388 | + Py_DECREF(dest); | |
| 389 | + Py_XDECREF(tuple); | |
| 390 | + return NULL; | |
| 391 | + } | |
| 392 | + Py_DECREF(item); | |
| 393 | + Py_DECREF(tuple); | |
| 394 | + } | |
| 395 | + } | |
| 396 | + return dest; | |
| 397 | +} | |
| 398 | + | |
| 399 | +/* Begin: Peephole optimizations ----------------------------------------- */ | |
| 400 | + | |
| 401 | +#define GETARG(arr, i) ((int)((arr[i+2]<<8) + arr[i+1])) | |
| 402 | +#define UNCONDITIONAL_JUMP(op) (op==JUMP_ABSOLUTE || op==JUMP_FORWARD) | |
| 403 | +#define ABSOLUTE_JUMP(op) (op==JUMP_ABSOLUTE || op==CONTINUE_LOOP) | |
| 404 | +#define GETJUMPTGT(arr, i) (GETARG(arr,i) + (ABSOLUTE_JUMP(arr[i]) ? 0 : i+3)) | |
| 405 | +#define SETARG(arr, i, val) arr[i+2] = val>>8; arr[i+1] = val & 255 | |
| 406 | +#define CODESIZE(op) (HAS_ARG(op) ? 3 : 1) | |
| 407 | +#define ISBASICBLOCK(blocks, start, bytes) \ | |
| 408 | + (blocks[start]==blocks[start+bytes-1]) | |
| 409 | + | |
| 410 | +/* Replace LOAD_CONST c1. LOAD_CONST c2 ... LOAD_CONST cn BUILD_TUPLE n | |
| 411 | + with LOAD_CONST (c1, c2, ... cn). | |
| 412 | + The consts table must still be in list form so that the | |
| 413 | + new constant (c1, c2, ... cn) can be appended. | |
| 414 | + Called with codestr pointing to the first LOAD_CONST. | |
| 415 | + Bails out with no change if one or more of the LOAD_CONSTs is missing. | |
| 416 | + Also works for BUILD_LIST when followed by an "in" or "not in" test. | |
| 417 | +*/ | |
| 418 | +static int | |
| 419 | +tuple_of_constants(unsigned char *codestr, int n, PyObject *consts) | |
| 420 | +{ | |
| 421 | + PyObject *newconst, *constant; | |
| 422 | + Py_ssize_t i, arg, len_consts; | |
| 423 | + | |
| 424 | + /* Pre-conditions */ | |
| 425 | + assert(PyList_CheckExact(consts)); | |
| 426 | + assert(codestr[n*3] == BUILD_TUPLE || codestr[n*3] == BUILD_LIST); | |
| 427 | + assert(GETARG(codestr, (n*3)) == n); | |
| 428 | + for (i=0 ; i<n ; i++) | |
| 429 | + assert(codestr[i*3] == LOAD_CONST); | |
| 430 | + | |
| 431 | + /* Buildup new tuple of constants */ | |
| 432 | + newconst = PyTuple_New(n); | |
| 433 | + if (newconst == NULL) | |
| 434 | + return 0; | |
| 435 | + len_consts = PyList_GET_SIZE(consts); | |
| 436 | + for (i=0 ; i<n ; i++) { | |
| 437 | + arg = GETARG(codestr, (i*3)); | |
| 438 | + assert(arg < len_consts); | |
| 439 | + constant = PyList_GET_ITEM(consts, arg); | |
| 440 | + Py_INCREF(constant); | |
| 441 | + PyTuple_SET_ITEM(newconst, i, constant); | |
| 442 | + } | |
| 443 | + | |
| 444 | + /* Append folded constant onto consts */ | |
| 445 | + if (PyList_Append(consts, newconst)) { | |
| 446 | + Py_DECREF(newconst); | |
| 447 | + return 0; | |
| 448 | + } | |
| 449 | + Py_DECREF(newconst); | |
| 450 | + | |
| 451 | + /* Write NOPs over old LOAD_CONSTS and | |
| 452 | + add a new LOAD_CONST newconst on top of the BUILD_TUPLE n */ | |
| 453 | + memset(codestr, NOP, n*3); | |
| 454 | + codestr[n*3] = LOAD_CONST; | |
| 455 | + SETARG(codestr, (n*3), len_consts); | |
| 456 | + return 1; | |
| 457 | +} | |
| 458 | + | |
| 459 | +/* Replace LOAD_CONST c1. LOAD_CONST c2 BINOP | |
| 460 | + with LOAD_CONST binop(c1,c2) | |
| 461 | + The consts table must still be in list form so that the | |
| 462 | + new constant can be appended. | |
| 463 | + Called with codestr pointing to the first LOAD_CONST. | |
| 464 | + Abandons the transformation if the folding fails (i.e. 1+'a'). | |
| 465 | + If the new constant is a sequence, only folds when the size | |
| 466 | + is below a threshold value. That keeps pyc files from | |
| 467 | + becoming large in the presence of code like: (None,)*1000. | |
| 468 | +*/ | |
| 469 | +static int | |
| 470 | +fold_binops_on_constants(unsigned char *codestr, PyObject *consts) | |
| 471 | +{ | |
| 472 | + PyObject *newconst, *v, *w; | |
| 473 | + Py_ssize_t len_consts, size; | |
| 474 | + int opcode; | |
| 475 | + | |
| 476 | + /* Pre-conditions */ | |
| 477 | + assert(PyList_CheckExact(consts)); | |
| 478 | + assert(codestr[0] == LOAD_CONST); | |
| 479 | + assert(codestr[3] == LOAD_CONST); | |
| 480 | + | |
| 481 | + /* Create new constant */ | |
| 482 | + v = PyList_GET_ITEM(consts, GETARG(codestr, 0)); | |
| 483 | + w = PyList_GET_ITEM(consts, GETARG(codestr, 3)); | |
| 484 | + opcode = codestr[6]; | |
| 485 | + switch (opcode) { | |
| 486 | + case BINARY_POWER: | |
| 487 | + newconst = PyNumber_Power(v, w, Py_None); | |
| 488 | + break; | |
| 489 | + case BINARY_MULTIPLY: | |
| 490 | + newconst = PyNumber_Multiply(v, w); | |
| 491 | + break; | |
| 492 | + case BINARY_DIVIDE: | |
| 493 | + /* Cannot fold this operation statically since | |
| 494 | + the result can depend on the run-time presence | |
| 495 | + of the -Qnew flag */ | |
| 496 | + return 0; | |
| 497 | + case BINARY_TRUE_DIVIDE: | |
| 498 | + newconst = PyNumber_TrueDivide(v, w); | |
| 499 | + break; | |
| 500 | + case BINARY_FLOOR_DIVIDE: | |
| 501 | + newconst = PyNumber_FloorDivide(v, w); | |
| 502 | + break; | |
| 503 | + case BINARY_MODULO: | |
| 504 | + newconst = PyNumber_Remainder(v, w); | |
| 505 | + break; | |
| 506 | + case BINARY_ADD: | |
| 507 | + newconst = PyNumber_Add(v, w); | |
| 508 | + break; | |
| 509 | + case BINARY_SUBTRACT: | |
| 510 | + newconst = PyNumber_Subtract(v, w); | |
| 511 | + break; | |
| 512 | + case BINARY_SUBSCR: | |
| 513 | + newconst = PyObject_GetItem(v, w); | |
| 514 | + break; | |
| 515 | + case BINARY_LSHIFT: | |
| 516 | + newconst = PyNumber_Lshift(v, w); | |
| 517 | + break; | |
| 518 | + case BINARY_RSHIFT: | |
| 519 | + newconst = PyNumber_Rshift(v, w); | |
| 520 | + break; | |
| 521 | + case BINARY_AND: | |
| 522 | + newconst = PyNumber_And(v, w); | |
| 523 | + break; | |
| 524 | + case BINARY_XOR: | |
| 525 | + newconst = PyNumber_Xor(v, w); | |
| 526 | + break; | |
| 527 | + case BINARY_OR: | |
| 528 | + newconst = PyNumber_Or(v, w); | |
| 529 | + break; | |
| 530 | + default: | |
| 531 | + /* Called with an unknown opcode */ | |
| 532 | + PyErr_Format(PyExc_SystemError, | |
| 533 | + "unexpected binary operation %d on a constant", | |
| 534 | + opcode); | |
| 535 | + return 0; | |
| 536 | + } | |
| 537 | + if (newconst == NULL) { | |
| 538 | + PyErr_Clear(); | |
| 539 | + return 0; | |
| 540 | + } | |
| 541 | + size = PyObject_Size(newconst); | |
| 542 | + if (size == -1) | |
| 543 | + PyErr_Clear(); | |
| 544 | + else if (size > 20) { | |
| 545 | + Py_DECREF(newconst); | |
| 546 | + return 0; | |
| 547 | + } | |
| 548 | + | |
| 549 | + /* Append folded constant into consts table */ | |
| 550 | + len_consts = PyList_GET_SIZE(consts); | |
| 551 | + if (PyList_Append(consts, newconst)) { | |
| 552 | + Py_DECREF(newconst); | |
| 553 | + return 0; | |
| 554 | + } | |
| 555 | + Py_DECREF(newconst); | |
| 556 | + | |
| 557 | + /* Write NOP NOP NOP NOP LOAD_CONST newconst */ | |
| 558 | + memset(codestr, NOP, 4); | |
| 559 | + codestr[4] = LOAD_CONST; | |
| 560 | + SETARG(codestr, 4, len_consts); | |
| 561 | + return 1; | |
| 562 | +} | |
| 563 | + | |
| 564 | +static int | |
| 565 | +fold_unaryops_on_constants(unsigned char *codestr, PyObject *consts) | |
| 566 | +{ | |
| 567 | + PyObject *newconst=NULL, *v; | |
| 568 | + Py_ssize_t len_consts; | |
| 569 | + int opcode; | |
| 570 | + | |
| 571 | + /* Pre-conditions */ | |
| 572 | + assert(PyList_CheckExact(consts)); | |
| 573 | + assert(codestr[0] == LOAD_CONST); | |
| 574 | + | |
| 575 | + /* Create new constant */ | |
| 576 | + v = PyList_GET_ITEM(consts, GETARG(codestr, 0)); | |
| 577 | + opcode = codestr[3]; | |
| 578 | + switch (opcode) { | |
| 579 | + case UNARY_NEGATIVE: | |
| 580 | + /* Preserve the sign of -0.0 */ | |
| 581 | + if (PyObject_IsTrue(v) == 1) | |
| 582 | + newconst = PyNumber_Negative(v); | |
| 583 | + break; | |
| 584 | + case UNARY_CONVERT: | |
| 585 | + newconst = PyObject_Repr(v); | |
| 586 | + break; | |
| 587 | + case UNARY_INVERT: | |
| 588 | + newconst = PyNumber_Invert(v); | |
| 589 | + break; | |
| 590 | + default: | |
| 591 | + /* Called with an unknown opcode */ | |
| 592 | + PyErr_Format(PyExc_SystemError, | |
| 593 | + "unexpected unary operation %d on a constant", | |
| 594 | + opcode); | |
| 595 | + return 0; | |
| 596 | + } | |
| 597 | + if (newconst == NULL) { | |
| 598 | + PyErr_Clear(); | |
| 599 | + return 0; | |
| 600 | + } | |
| 601 | + | |
| 602 | + /* Append folded constant into consts table */ | |
| 603 | + len_consts = PyList_GET_SIZE(consts); | |
| 604 | + if (PyList_Append(consts, newconst)) { | |
| 605 | + Py_DECREF(newconst); | |
| 606 | + return 0; | |
| 607 | + } | |
| 608 | + Py_DECREF(newconst); | |
| 609 | + | |
| 610 | + /* Write NOP LOAD_CONST newconst */ | |
| 611 | + codestr[0] = NOP; | |
| 612 | + codestr[1] = LOAD_CONST; | |
| 613 | + SETARG(codestr, 1, len_consts); | |
| 614 | + return 1; | |
| 615 | +} | |
| 616 | + | |
| 617 | +static unsigned int * | |
| 618 | +markblocks(unsigned char *code, int len) | |
| 619 | +{ | |
| 620 | + unsigned int *blocks = (unsigned int *)PyMem_Malloc(len*sizeof(int)); | |
| 621 | + int i,j, opcode, blockcnt = 0; | |
| 622 | + | |
| 623 | + if (blocks == NULL) { | |
| 624 | + PyErr_NoMemory(); | |
| 625 | + return NULL; | |
| 626 | + } | |
| 627 | + memset(blocks, 0, len*sizeof(int)); | |
| 628 | + | |
| 629 | + /* Mark labels in the first pass */ | |
| 630 | + for (i=0 ; i<len ; i+=CODESIZE(opcode)) { | |
| 631 | + opcode = code[i]; | |
| 632 | + switch (opcode) { | |
| 633 | + case FOR_ITER: | |
| 634 | + case JUMP_FORWARD: | |
| 635 | + case JUMP_IF_FALSE: | |
| 636 | + case JUMP_IF_TRUE: | |
| 637 | + case JUMP_ABSOLUTE: | |
| 638 | + case CONTINUE_LOOP: | |
| 639 | + case SETUP_LOOP: | |
| 640 | + case SETUP_EXCEPT: | |
| 641 | + case SETUP_FINALLY: | |
| 642 | + j = GETJUMPTGT(code, i); | |
| 643 | + blocks[j] = 1; | |
| 644 | + break; | |
| 645 | + } | |
| 646 | + } | |
| 647 | + /* Build block numbers in the second pass */ | |
| 648 | + for (i=0 ; i<len ; i++) { | |
| 649 | + blockcnt += blocks[i]; /* increment blockcnt over labels */ | |
| 650 | + blocks[i] = blockcnt; | |
| 651 | + } | |
| 652 | + return blocks; | |
| 653 | +} | |
| 654 | + | |
| 655 | +/* Perform basic peephole optimizations to components of a code object. | |
| 656 | + The consts object should still be in list form to allow new constants | |
| 657 | + to be appended. | |
| 658 | + | |
| 659 | + To keep the optimizer simple, it bails out (does nothing) for code | |
| 660 | + containing extended arguments or that has a length over 32,700. That | |
| 661 | + allows us to avoid overflow and sign issues. Likewise, it bails when | |
| 662 | + the lineno table has complex encoding for gaps >= 255. | |
| 663 | + | |
| 664 | + Optimizations are restricted to simple transformations occuring within a | |
| 665 | + single basic block. All transformations keep the code size the same or | |
| 666 | + smaller. For those that reduce size, the gaps are initially filled with | |
| 667 | + NOPs. Later those NOPs are removed and the jump addresses retargeted in | |
| 668 | + a single pass. Line numbering is adjusted accordingly. */ | |
| 669 | + | |
| 670 | +static PyObject * | |
| 671 | +optimize_code(PyObject *code, PyObject* consts, PyObject *names, | |
| 672 | + PyObject *lineno_obj) | |
| 673 | +{ | |
| 674 | + Py_ssize_t i, j, codelen; | |
| 675 | + int nops, h, adj; | |
| 676 | + int tgt, tgttgt, opcode; | |
| 677 | + unsigned char *codestr = NULL; | |
| 678 | + unsigned char *lineno; | |
| 679 | + int *addrmap = NULL; | |
| 680 | + int new_line, cum_orig_line, last_line, tabsiz; | |
| 681 | + int cumlc=0, lastlc=0; /* Count runs of consecutive LOAD_CONSTs */ | |
| 682 | + unsigned int *blocks = NULL; | |
| 683 | + char *name; | |
| 684 | + | |
| 685 | + /* Bail out if an exception is set */ | |
| 686 | + if (PyErr_Occurred()) | |
| 687 | + goto exitUnchanged; | |
| 688 | + | |
| 689 | + /* Bypass optimization when the lineno table is too complex */ | |
| 690 | + assert(PyString_Check(lineno_obj)); | |
| 691 | + lineno = (unsigned char*)PyString_AS_STRING(lineno_obj); | |
| 692 | + tabsiz = PyString_GET_SIZE(lineno_obj); | |
| 693 | + if (memchr(lineno, 255, tabsiz) != NULL) | |
| 694 | + goto exitUnchanged; | |
| 695 | + | |
| 696 | + /* Avoid situations where jump retargeting could overflow */ | |
| 697 | + assert(PyString_Check(code)); | |
| 698 | + codelen = PyString_Size(code); | |
| 699 | + if (codelen > 32700) | |
| 700 | + goto exitUnchanged; | |
| 701 | + | |
| 702 | + /* Make a modifiable copy of the code string */ | |
| 703 | + codestr = (unsigned char *)PyMem_Malloc(codelen); | |
| 704 | + if (codestr == NULL) | |
| 705 | + goto exitUnchanged; | |
| 706 | + codestr = (unsigned char *)memcpy(codestr, | |
| 707 | + PyString_AS_STRING(code), codelen); | |
| 708 | + | |
| 709 | + /* Verify that RETURN_VALUE terminates the codestring. This allows | |
| 710 | + the various transformation patterns to look ahead several | |
| 711 | + instructions without additional checks to make sure they are not | |
| 712 | + looking beyond the end of the code string. | |
| 713 | + */ | |
| 714 | + if (codestr[codelen-1] != RETURN_VALUE) | |
| 715 | + goto exitUnchanged; | |
| 716 | + | |
| 717 | + /* Mapping to new jump targets after NOPs are removed */ | |
| 718 | + addrmap = (int *)PyMem_Malloc(codelen * sizeof(int)); | |
| 719 | + if (addrmap == NULL) | |
| 720 | + goto exitUnchanged; | |
| 721 | + | |
| 722 | + blocks = markblocks(codestr, codelen); | |
| 723 | + if (blocks == NULL) | |
| 724 | + goto exitUnchanged; | |
| 725 | + assert(PyList_Check(consts)); | |
| 726 | + | |
| 727 | + for (i=0 ; i<codelen ; i += CODESIZE(codestr[i])) { | |
| 728 | + opcode = codestr[i]; | |
| 729 | + | |
| 730 | + lastlc = cumlc; | |
| 731 | + cumlc = 0; | |
| 732 | + | |
| 733 | + switch (opcode) { | |
| 734 | + | |
| 735 | + /* Replace UNARY_NOT JUMP_IF_FALSE POP_TOP with | |
| 736 | + with JUMP_IF_TRUE POP_TOP */ | |
| 737 | + case UNARY_NOT: | |
| 738 | + if (codestr[i+1] != JUMP_IF_FALSE || | |
| 739 | + codestr[i+4] != POP_TOP || | |
| 740 | + !ISBASICBLOCK(blocks,i,5)) | |
| 741 | + continue; | |
| 742 | + tgt = GETJUMPTGT(codestr, (i+1)); | |
| 743 | + if (codestr[tgt] != POP_TOP) | |
| 744 | + continue; | |
| 745 | + j = GETARG(codestr, i+1) + 1; | |
| 746 | + codestr[i] = JUMP_IF_TRUE; | |
| 747 | + SETARG(codestr, i, j); | |
| 748 | + codestr[i+3] = POP_TOP; | |
| 749 | + codestr[i+4] = NOP; | |
| 750 | + break; | |
| 751 | + | |
| 752 | + /* not a is b --> a is not b | |
| 753 | + not a in b --> a not in b | |
| 754 | + not a is not b --> a is b | |
| 755 | + not a not in b --> a in b | |
| 756 | + */ | |
| 757 | + case COMPARE_OP: | |
| 758 | + j = GETARG(codestr, i); | |
| 759 | + if (j < 6 || j > 9 || | |
| 760 | + codestr[i+3] != UNARY_NOT || | |
| 761 | + !ISBASICBLOCK(blocks,i,4)) | |
| 762 | + continue; | |
| 763 | + SETARG(codestr, i, (j^1)); | |
| 764 | + codestr[i+3] = NOP; | |
| 765 | + break; | |
| 766 | + | |
| 767 | + /* Replace LOAD_GLOBAL/LOAD_NAME None | |
| 768 | + with LOAD_CONST None */ | |
| 769 | + case LOAD_NAME: | |
| 770 | + case LOAD_GLOBAL: | |
| 771 | + j = GETARG(codestr, i); | |
| 772 | + name = PyString_AsString(PyTuple_GET_ITEM(names, j)); | |
| 773 | + if (name == NULL || strcmp(name, "None") != 0) | |
| 774 | + continue; | |
| 775 | + for (j=0 ; j < PyList_GET_SIZE(consts) ; j++) { | |
| 776 | + if (PyList_GET_ITEM(consts, j) == Py_None) | |
| 777 | + break; | |
| 778 | + } | |
| 779 | + if (j == PyList_GET_SIZE(consts)) { | |
| 780 | + if (PyList_Append(consts, Py_None) == -1) | |
| 781 | + goto exitUnchanged; | |
| 782 | + } | |
| 783 | + assert(PyList_GET_ITEM(consts, j) == Py_None); | |
| 784 | + codestr[i] = LOAD_CONST; | |
| 785 | + SETARG(codestr, i, j); | |
| 786 | + cumlc = lastlc + 1; | |
| 787 | + break; | |
| 788 | + | |
| 789 | + /* Skip over LOAD_CONST trueconst | |
| 790 | + JUMP_IF_FALSE xx POP_TOP */ | |
| 791 | + case LOAD_CONST: | |
| 792 | + cumlc = lastlc + 1; | |
| 793 | + j = GETARG(codestr, i); | |
| 794 | + if (codestr[i+3] != JUMP_IF_FALSE || | |
| 795 | + codestr[i+6] != POP_TOP || | |
| 796 | + !ISBASICBLOCK(blocks,i,7) || | |
| 797 | + !PyObject_IsTrue(PyList_GET_ITEM(consts, j))) | |
| 798 | + continue; | |
| 799 | + memset(codestr+i, NOP, 7); | |
| 800 | + cumlc = 0; | |
| 801 | + break; | |
| 802 | + | |
| 803 | + /* Try to fold tuples of constants (includes a case for lists | |
| 804 | + which are only used for "in" and "not in" tests). | |
| 805 | + Skip over BUILD_SEQN 1 UNPACK_SEQN 1. | |
| 806 | + Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2. | |
| 807 | + Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */ | |
| 808 | + case BUILD_TUPLE: | |
| 809 | + case BUILD_LIST: | |
| 810 | + j = GETARG(codestr, i); | |
| 811 | + h = i - 3 * j; | |
| 812 | + if (h >= 0 && | |
| 813 | + j <= lastlc && | |
| 814 | + ((opcode == BUILD_TUPLE && | |
| 815 | + ISBASICBLOCK(blocks, h, 3*(j+1))) || | |
| 816 | + (opcode == BUILD_LIST && | |
| 817 | + codestr[i+3]==COMPARE_OP && | |
| 818 | + ISBASICBLOCK(blocks, h, 3*(j+2)) && | |
| 819 | + (GETARG(codestr,i+3)==6 || | |
| 820 | + GETARG(codestr,i+3)==7))) && | |
| 821 | + tuple_of_constants(&codestr[h], j, consts)) { | |
| 822 | + assert(codestr[i] == LOAD_CONST); | |
| 823 | + cumlc = 1; | |
| 824 | + break; | |
| 825 | + } | |
| 826 | + if (codestr[i+3] != UNPACK_SEQUENCE || | |
| 827 | + !ISBASICBLOCK(blocks,i,6) || | |
| 828 | + j != GETARG(codestr, i+3)) | |
| 829 | + continue; | |
| 830 | + if (j == 1) { | |
| 831 | + memset(codestr+i, NOP, 6); | |
| 832 | + } else if (j == 2) { | |
| 833 | + codestr[i] = ROT_TWO; | |
| 834 | + memset(codestr+i+1, NOP, 5); | |
| 835 | + } else if (j == 3) { | |
| 836 | + codestr[i] = ROT_THREE; | |
| 837 | + codestr[i+1] = ROT_TWO; | |
| 838 | + memset(codestr+i+2, NOP, 4); | |
| 839 | + } | |
| 840 | + break; | |
| 841 | + | |
| 842 | + /* Fold binary ops on constants. | |
| 843 | + LOAD_CONST c1 LOAD_CONST c2 BINOP --> LOAD_CONST binop(c1,c2) */ | |
| 844 | + case BINARY_POWER: | |
| 845 | + case BINARY_MULTIPLY: | |
| 846 | + case BINARY_TRUE_DIVIDE: | |
| 847 | + case BINARY_FLOOR_DIVIDE: | |
| 848 | + case BINARY_MODULO: | |
| 849 | + case BINARY_ADD: | |
| 850 | + case BINARY_SUBTRACT: | |
| 851 | + case BINARY_SUBSCR: | |
| 852 | + case BINARY_LSHIFT: | |
| 853 | + case BINARY_RSHIFT: | |
| 854 | + case BINARY_AND: | |
| 855 | + case BINARY_XOR: | |
| 856 | + case BINARY_OR: | |
| 857 | + if (lastlc >= 2 && | |
| 858 | + ISBASICBLOCK(blocks, i-6, 7) && | |
| 859 | + fold_binops_on_constants(&codestr[i-6], consts)) { | |
| 860 | + i -= 2; | |
| 861 | + assert(codestr[i] == LOAD_CONST); | |
| 862 | + cumlc = 1; | |
| 863 | + } | |
| 864 | + break; | |
| 865 | + | |
| 866 | + /* Fold unary ops on constants. | |
| 867 | + LOAD_CONST c1 UNARY_OP --> LOAD_CONST unary_op(c) */ | |
| 868 | + case UNARY_NEGATIVE: | |
| 869 | + case UNARY_CONVERT: | |
| 870 | + case UNARY_INVERT: | |
| 871 | + if (lastlc >= 1 && | |
| 872 | + ISBASICBLOCK(blocks, i-3, 4) && | |
| 873 | + fold_unaryops_on_constants(&codestr[i-3], consts)) { | |
| 874 | + i -= 2; | |
| 875 | + assert(codestr[i] == LOAD_CONST); | |
| 876 | + cumlc = 1; | |
| 877 | + } | |
| 878 | + break; | |
| 879 | + | |
| 880 | + /* Simplify conditional jump to conditional jump where the | |
| 881 | + result of the first test implies the success of a similar | |
| 882 | + test or the failure of the opposite test. | |
| 883 | + Arises in code like: | |
| 884 | + "if a and b:" | |
| 885 | + "if a or b:" | |
| 886 | + "a and b or c" | |
| 887 | + "(a and b) and c" | |
| 888 | + x:JUMP_IF_FALSE y y:JUMP_IF_FALSE z --> x:JUMP_IF_FALSE z | |
| 889 | + x:JUMP_IF_FALSE y y:JUMP_IF_TRUE z --> x:JUMP_IF_FALSE y+3 | |
| 890 | + where y+3 is the instruction following the second test. | |
| 891 | + */ | |
| 892 | + case JUMP_IF_FALSE: | |
| 893 | + case JUMP_IF_TRUE: | |
| 894 | + tgt = GETJUMPTGT(codestr, i); | |
| 895 | + j = codestr[tgt]; | |
| 896 | + if (j == JUMP_IF_FALSE || j == JUMP_IF_TRUE) { | |
| 897 | + if (j == opcode) { | |
| 898 | + tgttgt = GETJUMPTGT(codestr, tgt) - i - 3; | |
| 899 | + SETARG(codestr, i, tgttgt); | |
| 900 | + } else { | |
| 901 | + tgt -= i; | |
| 902 | + SETARG(codestr, i, tgt); | |
| 903 | + } | |
| 904 | + break; | |
| 905 | + } | |
| 906 | + /* Intentional fallthrough */ | |
| 907 | + | |
| 908 | + /* Replace jumps to unconditional jumps */ | |
| 909 | + case FOR_ITER: | |
| 910 | + case JUMP_FORWARD: | |
| 911 | + case JUMP_ABSOLUTE: | |
| 912 | + case CONTINUE_LOOP: | |
| 913 | + case SETUP_LOOP: | |
| 914 | + case SETUP_EXCEPT: | |
| 915 | + case SETUP_FINALLY: | |
| 916 | + tgt = GETJUMPTGT(codestr, i); | |
| 917 | + if (!UNCONDITIONAL_JUMP(codestr[tgt])) | |
| 918 | + continue; | |
| 919 | + tgttgt = GETJUMPTGT(codestr, tgt); | |
| 920 | + if (opcode == JUMP_FORWARD) /* JMP_ABS can go backwards */ | |
| 921 | + opcode = JUMP_ABSOLUTE; | |
| 922 | + if (!ABSOLUTE_JUMP(opcode)) | |
| 923 | + tgttgt -= i + 3; /* Calc relative jump addr */ | |
| 924 | + if (tgttgt < 0) /* No backward relative jumps */ | |
| 925 | + continue; | |
| 926 | + codestr[i] = opcode; | |
| 927 | + SETARG(codestr, i, tgttgt); | |
| 928 | + break; | |
| 929 | + | |
| 930 | + case EXTENDED_ARG: | |
| 931 | + goto exitUnchanged; | |
| 932 | + | |
| 933 | + /* Replace RETURN LOAD_CONST None RETURN with just RETURN */ | |
| 934 | + case RETURN_VALUE: | |
| 935 | + if (i+4 >= codelen || | |
| 936 | + codestr[i+4] != RETURN_VALUE || | |
| 937 | + !ISBASICBLOCK(blocks,i,5)) | |
| 938 | + continue; | |
| 939 | + memset(codestr+i+1, NOP, 4); | |
| 940 | + break; | |
| 941 | + } | |
| 942 | + } | |
| 943 | + | |
| 944 | + /* Fixup linenotab */ | |
| 945 | + for (i=0, nops=0 ; i<codelen ; i += CODESIZE(codestr[i])) { | |
| 946 | + addrmap[i] = i - nops; | |
| 947 | + if (codestr[i] == NOP) | |
| 948 | + nops++; | |
| 949 | + } | |
| 950 | + cum_orig_line = 0; | |
| 951 | + last_line = 0; | |
| 952 | + for (i=0 ; i < tabsiz ; i+=2) { | |
| 953 | + cum_orig_line += lineno[i]; | |
| 954 | + new_line = addrmap[cum_orig_line]; | |
| 955 | + assert (new_line - last_line < 255); | |
| 956 | + lineno[i] =((unsigned char)(new_line - last_line)); | |
| 957 | + last_line = new_line; | |
| 958 | + } | |
| 959 | + | |
| 960 | + /* Remove NOPs and fixup jump targets */ | |
| 961 | + for (i=0, h=0 ; i<codelen ; ) { | |
| 962 | + opcode = codestr[i]; | |
| 963 | + switch (opcode) { | |
| 964 | + case NOP: | |
| 965 | + i++; | |
| 966 | + continue; | |
| 967 | + | |
| 968 | + case JUMP_ABSOLUTE: | |
| 969 | + case CONTINUE_LOOP: | |
| 970 | + j = addrmap[GETARG(codestr, i)]; | |
| 971 | + SETARG(codestr, i, j); | |
| 972 | + break; | |
| 973 | + | |
| 974 | + case FOR_ITER: | |
| 975 | + case JUMP_FORWARD: | |
| 976 | + case JUMP_IF_FALSE: | |
| 977 | + case JUMP_IF_TRUE: | |
| 978 | + case SETUP_LOOP: | |
| 979 | + case SETUP_EXCEPT: | |
| 980 | + case SETUP_FINALLY: | |
| 981 | + j = addrmap[GETARG(codestr, i) + i + 3] - addrmap[i] - 3; | |
| 982 | + SETARG(codestr, i, j); | |
| 983 | + break; | |
| 984 | + } | |
| 985 | + adj = CODESIZE(opcode); | |
| 986 | + while (adj--) | |
| 987 | + codestr[h++] = codestr[i++]; | |
| 988 | + } | |
| 989 | + assert(h + nops == codelen); | |
| 990 | + | |
| 991 | + code = PyString_FromStringAndSize((char *)codestr, h); | |
| 992 | + PyMem_Free(addrmap); | |
| 993 | + PyMem_Free(codestr); | |
| 994 | + PyMem_Free(blocks); | |
| 995 | + return code; | |
| 996 | + | |
| 997 | + exitUnchanged: | |
| 998 | + if (blocks != NULL) | |
| 999 | + PyMem_Free(blocks); | |
| 1000 | + if (addrmap != NULL) | |
| 1001 | + PyMem_Free(addrmap); | |
| 1002 | + if (codestr != NULL) | |
| 1003 | + PyMem_Free(codestr); | |
| 1004 | + Py_INCREF(code); | |
| 1005 | + return code; | |
| 1006 | +} | |
| 1007 | + | |
| 1008 | +/* End: Peephole optimizations ----------------------------------------- */ | |
| 1009 | + | |
| 1010 | +/* | |
| 1011 | + | |
| 1012 | +Leave this debugging code for just a little longer. | |
| 1013 | + | |
| 1014 | +static void | |
| 1015 | +compiler_display_symbols(PyObject *name, PyObject *symbols) | |
| 1016 | +{ | |
| 1017 | +PyObject *key, *value; | |
| 1018 | +int flags; | |
| 1019 | +Py_ssize_t pos = 0; | |
| 1020 | + | |
| 1021 | +fprintf(stderr, "block %s\n", PyString_AS_STRING(name)); | |
| 1022 | +while (PyDict_Next(symbols, &pos, &key, &value)) { | |
| 1023 | +flags = PyInt_AsLong(value); | |
| 1024 | +fprintf(stderr, "var %s:", PyString_AS_STRING(key)); | |
| 1025 | +if (flags & DEF_GLOBAL) | |
| 1026 | +fprintf(stderr, " declared_global"); | |
| 1027 | +if (flags & DEF_LOCAL) | |
| 1028 | +fprintf(stderr, " local"); | |
| 1029 | +if (flags & DEF_PARAM) | |
| 1030 | +fprintf(stderr, " param"); | |
| 1031 | +if (flags & DEF_STAR) | |
| 1032 | +fprintf(stderr, " stararg"); | |
| 1033 | +if (flags & DEF_DOUBLESTAR) | |
| 1034 | +fprintf(stderr, " starstar"); | |
| 1035 | +if (flags & DEF_INTUPLE) | |
| 1036 | +fprintf(stderr, " tuple"); | |
| 1037 | +if (flags & DEF_FREE) | |
| 1038 | +fprintf(stderr, " free"); | |
| 1039 | +if (flags & DEF_FREE_GLOBAL) | |
| 1040 | +fprintf(stderr, " global"); | |
| 1041 | +if (flags & DEF_FREE_CLASS) | |
| 1042 | +fprintf(stderr, " free/class"); | |
| 1043 | +if (flags & DEF_IMPORT) | |
| 1044 | +fprintf(stderr, " import"); | |
| 1045 | +fprintf(stderr, "\n"); | |
| 1046 | +} | |
| 1047 | + fprintf(stderr, "\n"); | |
| 1048 | +} | |
| 1049 | +*/ | |
| 1050 | + | |
| 1051 | +static void | |
| 1052 | +compiler_unit_check(struct compiler_unit *u) | |
| 1053 | +{ | |
| 1054 | + basicblock *block; | |
| 1055 | + for (block = u->u_blocks; block != NULL; block = block->b_list) { | |
| 1056 | + assert(block != (void *)0xcbcbcbcb); | |
| 1057 | + assert(block != (void *)0xfbfbfbfb); | |
| 1058 | + assert(block != (void *)0xdbdbdbdb); | |
| 1059 | + if (block->b_instr != NULL) { | |
| 1060 | + assert(block->b_ialloc > 0); | |
| 1061 | + assert(block->b_iused > 0); | |
| 1062 | + assert(block->b_ialloc >= block->b_iused); | |
| 1063 | + } | |
| 1064 | + else { | |
| 1065 | + assert (block->b_iused == 0); | |
| 1066 | + assert (block->b_ialloc == 0); | |
| 1067 | + } | |
| 1068 | + } | |
| 1069 | +} | |
| 1070 | + | |
| 1071 | +static void | |
| 1072 | +compiler_unit_free(struct compiler_unit *u) | |
| 1073 | +{ | |
| 1074 | + basicblock *b, *next; | |
| 1075 | + | |
| 1076 | + compiler_unit_check(u); | |
| 1077 | + b = u->u_blocks; | |
| 1078 | + while (b != NULL) { | |
| 1079 | + if (b->b_instr) | |
| 1080 | + PyObject_Free((void *)b->b_instr); | |
| 1081 | + next = b->b_list; | |
| 1082 | + PyObject_Free((void *)b); | |
| 1083 | + b = next; | |
| 1084 | + } | |
| 1085 | + Py_CLEAR(u->u_ste); | |
| 1086 | + Py_CLEAR(u->u_name); | |
| 1087 | + Py_CLEAR(u->u_consts); | |
| 1088 | + Py_CLEAR(u->u_names); | |
| 1089 | + Py_CLEAR(u->u_varnames); | |
| 1090 | + Py_CLEAR(u->u_freevars); | |
| 1091 | + Py_CLEAR(u->u_cellvars); | |
| 1092 | + Py_CLEAR(u->u_private); | |
| 1093 | + PyObject_Free(u); | |
| 1094 | +} | |
| 1095 | + | |
| 1096 | +static int | |
| 1097 | +compiler_enter_scope(struct compiler *c, identifier name, void *key, | |
| 1098 | + int lineno) | |
| 1099 | +{ | |
| 1100 | + struct compiler_unit *u; | |
| 1101 | + | |
| 1102 | + u = (struct compiler_unit *)PyObject_Malloc(sizeof( | |
| 1103 | + struct compiler_unit)); | |
| 1104 | + if (!u) { | |
| 1105 | + PyErr_NoMemory(); | |
| 1106 | + return 0; | |
| 1107 | + } | |
| 1108 | + memset(u, 0, sizeof(struct compiler_unit)); | |
| 1109 | + u->u_argcount = 0; | |
| 1110 | + u->u_ste = PySymtable_Lookup(c->c_st, key); | |
| 1111 | + if (!u->u_ste) { | |
| 1112 | + compiler_unit_free(u); | |
| 1113 | + return 0; | |
| 1114 | + } | |
| 1115 | + Py_INCREF(name); | |
| 1116 | + u->u_name = name; | |
| 1117 | + u->u_varnames = list2dict(u->u_ste->ste_varnames); | |
| 1118 | + u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0); | |
| 1119 | + if (!u->u_varnames || !u->u_cellvars) { | |
| 1120 | + compiler_unit_free(u); | |
| 1121 | + return 0; | |
| 1122 | + } | |
| 1123 | + | |
| 1124 | + u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS, | |
| 1125 | + PyDict_Size(u->u_cellvars)); | |
| 1126 | + if (!u->u_freevars) { | |
| 1127 | + compiler_unit_free(u); | |
| 1128 | + return 0; | |
| 1129 | + } | |
| 1130 | + | |
| 1131 | + u->u_blocks = NULL; | |
| 1132 | + u->u_tmpname = 0; | |
| 1133 | + u->u_nfblocks = 0; | |
| 1134 | + u->u_firstlineno = lineno; | |
| 1135 | + u->u_lineno = 0; | |
| 1136 | + u->u_lineno_set = false; | |
| 1137 | + u->u_consts = PyDict_New(); | |
| 1138 | + if (!u->u_consts) { | |
| 1139 | + compiler_unit_free(u); | |
| 1140 | + return 0; | |
| 1141 | + } | |
| 1142 | + u->u_names = PyDict_New(); | |
| 1143 | + if (!u->u_names) { | |
| 1144 | + compiler_unit_free(u); | |
| 1145 | + return 0; | |
| 1146 | + } | |
| 1147 | + | |
| 1148 | + u->u_private = NULL; | |
| 1149 | + | |
| 1150 | + /* Push the old compiler_unit on the stack. */ | |
| 1151 | + if (c->u) { | |
| 1152 | + PyObject *wrapper = PyCObject_FromVoidPtr(c->u, NULL); | |
| 1153 | + if (!wrapper || PyList_Append(c->c_stack, wrapper) < 0) { | |
| 1154 | + Py_XDECREF(wrapper); | |
| 1155 | + compiler_unit_free(u); | |
| 1156 | + return 0; | |
| 1157 | + } | |
| 1158 | + Py_DECREF(wrapper); | |
| 1159 | + u->u_private = c->u->u_private; | |
| 1160 | + Py_XINCREF(u->u_private); | |
| 1161 | + } | |
| 1162 | + c->u = u; | |
| 1163 | + | |
| 1164 | + c->c_nestlevel++; | |
| 1165 | + if (compiler_use_new_block(c) == NULL) | |
| 1166 | + return 0; | |
| 1167 | + | |
| 1168 | + return 1; | |
| 1169 | +} | |
| 1170 | + | |
| 1171 | +static void | |
| 1172 | +compiler_exit_scope(struct compiler *c) | |
| 1173 | +{ | |
| 1174 | + int n; | |
| 1175 | + PyObject *wrapper; | |
| 1176 | + | |
| 1177 | + c->c_nestlevel--; | |
| 1178 | + compiler_unit_free(c->u); | |
| 1179 | + /* Restore c->u to the parent unit. */ | |
| 1180 | + n = PyList_GET_SIZE(c->c_stack) - 1; | |
| 1181 | + if (n >= 0) { | |
| 1182 | + wrapper = PyList_GET_ITEM(c->c_stack, n); | |
| 1183 | + c->u = (struct compiler_unit *)PyCObject_AsVoidPtr(wrapper); | |
| 1184 | + /* we are deleting from a list so this really shouldn't fail */ | |
| 1185 | + if (PySequence_DelItem(c->c_stack, n) < 0) | |
| 1186 | + Py_FatalError("compiler_exit_scope()"); | |
| 1187 | + compiler_unit_check(c->u); | |
| 1188 | + } | |
| 1189 | + else | |
| 1190 | + c->u = NULL; | |
| 1191 | + | |
| 1192 | +} | |
| 1193 | + | |
| 1194 | +/* Allocate a new "anonymous" local variable. | |
| 1195 | + Used by list comprehensions and with statements. | |
| 1196 | +*/ | |
| 1197 | + | |
| 1198 | +static PyObject * | |
| 1199 | +compiler_new_tmpname(struct compiler *c) | |
| 1200 | +{ | |
| 1201 | + char tmpname[256]; | |
| 1202 | + PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", ++c->u->u_tmpname); | |
| 1203 | + return PyString_FromString(tmpname); | |
| 1204 | +} | |
| 1205 | + | |
| 1206 | +/* Allocate a new block and return a pointer to it. | |
| 1207 | + Returns NULL on error. | |
| 1208 | +*/ | |
| 1209 | + | |
| 1210 | +static basicblock * | |
| 1211 | +compiler_new_block(struct compiler *c) | |
| 1212 | +{ | |
| 1213 | + basicblock *b; | |
| 1214 | + struct compiler_unit *u; | |
| 1215 | + | |
| 1216 | + u = c->u; | |
| 1217 | + b = (basicblock *)PyObject_Malloc(sizeof(basicblock)); | |
| 1218 | + if (b == NULL) { | |
| 1219 | + PyErr_NoMemory(); | |
| 1220 | + return NULL; | |
| 1221 | + } | |
| 1222 | + memset((void *)b, 0, sizeof(basicblock)); | |
| 1223 | + /* Extend the singly linked list of blocks with new block. */ | |
| 1224 | + b->b_list = u->u_blocks; | |
| 1225 | + u->u_blocks = b; | |
| 1226 | + return b; | |
| 1227 | +} | |
| 1228 | + | |
| 1229 | +static basicblock * | |
| 1230 | +compiler_use_new_block(struct compiler *c) | |
| 1231 | +{ | |
| 1232 | + basicblock *block = compiler_new_block(c); | |
| 1233 | + if (block == NULL) | |
| 1234 | + return NULL; | |
| 1235 | + c->u->u_curblock = block; | |
| 1236 | + return block; | |
| 1237 | +} | |
| 1238 | + | |
| 1239 | +static basicblock * | |
| 1240 | +compiler_next_block(struct compiler *c) | |
| 1241 | +{ | |
| 1242 | + basicblock *block = compiler_new_block(c); | |
| 1243 | + if (block == NULL) | |
| 1244 | + return NULL; | |
| 1245 | + c->u->u_curblock->b_next = block; | |
| 1246 | + c->u->u_curblock = block; | |
| 1247 | + return block; | |
| 1248 | +} | |
| 1249 | + | |
| 1250 | +static basicblock * | |
| 1251 | +compiler_use_next_block(struct compiler *c, basicblock *block) | |
| 1252 | +{ | |
| 1253 | + assert(block != NULL); | |
| 1254 | + c->u->u_curblock->b_next = block; | |
| 1255 | + c->u->u_curblock = block; | |
| 1256 | + return block; | |
| 1257 | +} | |
| 1258 | + | |
| 1259 | +/* Returns the offset of the next instruction in the current block's | |
| 1260 | + b_instr array. Resizes the b_instr as necessary. | |
| 1261 | + Returns -1 on failure. | |
| 1262 | + */ | |
| 1263 | + | |
| 1264 | +static int | |
| 1265 | +compiler_next_instr(struct compiler *c, basicblock *b) | |
| 1266 | +{ | |
| 1267 | + assert(b != NULL); | |
| 1268 | + if (b->b_instr == NULL) { | |
| 1269 | + b->b_instr = (struct instr *)PyObject_Malloc( | |
| 1270 | + sizeof(struct instr) * DEFAULT_BLOCK_SIZE); | |
| 1271 | + if (b->b_instr == NULL) { | |
| 1272 | + PyErr_NoMemory(); | |
| 1273 | + return -1; | |
| 1274 | + } | |
| 1275 | + b->b_ialloc = DEFAULT_BLOCK_SIZE; | |
| 1276 | + memset((char *)b->b_instr, 0, | |
| 1277 | + sizeof(struct instr) * DEFAULT_BLOCK_SIZE); | |
| 1278 | + } | |
| 1279 | + else if (b->b_iused == b->b_ialloc) { | |
| 1280 | + struct instr *tmp; | |
| 1281 | + size_t oldsize, newsize; | |
| 1282 | + oldsize = b->b_ialloc * sizeof(struct instr); | |
| 1283 | + newsize = oldsize << 1; | |
| 1284 | + if (newsize == 0) { | |
| 1285 | + PyErr_NoMemory(); | |
| 1286 | + return -1; | |
| 1287 | + } | |
| 1288 | + b->b_ialloc <<= 1; | |
| 1289 | + tmp = (struct instr *)PyObject_Realloc( | |
| 1290 | + (void *)b->b_instr, newsize); | |
| 1291 | + if (tmp == NULL) { | |
| 1292 | + PyErr_NoMemory(); | |
| 1293 | + return -1; | |
| 1294 | + } | |
| 1295 | + b->b_instr = tmp; | |
| 1296 | + memset((char *)b->b_instr + oldsize, 0, newsize - oldsize); | |
| 1297 | + } | |
| 1298 | + return b->b_iused++; | |
| 1299 | +} | |
| 1300 | + | |
| 1301 | +/* Set the i_lineno member of the instruction at offse off if the | |
| 1302 | + line number for the current expression/statement (?) has not | |
| 1303 | + already been set. If it has been set, the call has no effect. | |
| 1304 | + | |
| 1305 | + Every time a new node is b | |
| 1306 | + */ | |
| 1307 | + | |
| 1308 | +static void | |
| 1309 | +compiler_set_lineno(struct compiler *c, int off) | |
| 1310 | +{ | |
| 1311 | + basicblock *b; | |
| 1312 | + if (c->u->u_lineno_set) | |
| 1313 | + return; | |
| 1314 | + c->u->u_lineno_set = true; | |
| 1315 | + b = c->u->u_curblock; | |
| 1316 | + b->b_instr[off].i_lineno = c->u->u_lineno; | |
| 1317 | +} | |
| 1318 | + | |
| 1319 | +static int | |
| 1320 | +opcode_stack_effect(int opcode, int oparg) | |
| 1321 | +{ | |
| 1322 | + switch (opcode) { | |
| 1323 | + case POP_TOP: | |
| 1324 | + return -1; | |
| 1325 | + case ROT_TWO: | |
| 1326 | + case ROT_THREE: | |
| 1327 | + return 0; | |
| 1328 | + case DUP_TOP: | |
| 1329 | + return 1; | |
| 1330 | + case ROT_FOUR: | |
| 1331 | + return 0; | |
| 1332 | + | |
| 1333 | + case UNARY_POSITIVE: | |
| 1334 | + case UNARY_NEGATIVE: | |
| 1335 | + case UNARY_NOT: | |
| 1336 | + case UNARY_CONVERT: | |
| 1337 | + case UNARY_INVERT: | |
| 1338 | + return 0; | |
| 1339 | + | |
| 1340 | + case LIST_APPEND: | |
| 1341 | + return -2; | |
| 1342 | + | |
| 1343 | + case BINARY_POWER: | |
| 1344 | + case BINARY_MULTIPLY: | |
| 1345 | + case BINARY_DIVIDE: | |
| 1346 | + case BINARY_MODULO: | |
| 1347 | + case BINARY_ADD: | |
| 1348 | + case BINARY_SUBTRACT: | |
| 1349 | + case BINARY_SUBSCR: | |
| 1350 | + case BINARY_FLOOR_DIVIDE: | |
| 1351 | + case BINARY_TRUE_DIVIDE: | |
| 1352 | + return -1; | |
| 1353 | + case INPLACE_FLOOR_DIVIDE: | |
| 1354 | + case INPLACE_TRUE_DIVIDE: | |
| 1355 | + return -1; | |
| 1356 | + | |
| 1357 | + case SLICE+0: | |
| 1358 | + return 1; | |
| 1359 | + case SLICE+1: | |
| 1360 | + return 0; | |
| 1361 | + case SLICE+2: | |
| 1362 | + return 0; | |
| 1363 | + case SLICE+3: | |
| 1364 | + return -1; | |
| 1365 | + | |
| 1366 | + case STORE_SLICE+0: | |
| 1367 | + return -2; | |
| 1368 | + case STORE_SLICE+1: | |
| 1369 | + return -3; | |
| 1370 | + case STORE_SLICE+2: | |
| 1371 | + return -3; | |
| 1372 | + case STORE_SLICE+3: | |
| 1373 | + return -4; | |
| 1374 | + | |
| 1375 | + case DELETE_SLICE+0: | |
| 1376 | + return -1; | |
| 1377 | + case DELETE_SLICE+1: | |
| 1378 | + return -2; | |
| 1379 | + case DELETE_SLICE+2: | |
| 1380 | + return -2; | |
| 1381 | + case DELETE_SLICE+3: | |
| 1382 | + return -3; | |
| 1383 | + | |
| 1384 | + case INPLACE_ADD: | |
| 1385 | + case INPLACE_SUBTRACT: | |
| 1386 | + case INPLACE_MULTIPLY: | |
| 1387 | + case INPLACE_DIVIDE: | |
| 1388 | + case INPLACE_MODULO: | |
| 1389 | + return -1; | |
| 1390 | + case STORE_SUBSCR: | |
| 1391 | + return -3; | |
| 1392 | + case DELETE_SUBSCR: | |
| 1393 | + return -2; | |
| 1394 | + | |
| 1395 | + case BINARY_LSHIFT: | |
| 1396 | + case BINARY_RSHIFT: | |
| 1397 | + case BINARY_AND: | |
| 1398 | + case BINARY_XOR: | |
| 1399 | + case BINARY_OR: | |
| 1400 | + return -1; | |
| 1401 | + case INPLACE_POWER: | |
| 1402 | + return -1; | |
| 1403 | + case GET_ITER: | |
| 1404 | + return 0; | |
| 1405 | + | |
| 1406 | + case PRINT_EXPR: | |
| 1407 | + return -1; | |
| 1408 | + case PRINT_ITEM: | |
| 1409 | + return -1; | |
| 1410 | + case PRINT_NEWLINE: | |
| 1411 | + return 0; | |
| 1412 | + case PRINT_ITEM_TO: | |
| 1413 | + return -2; | |
| 1414 | + case PRINT_NEWLINE_TO: | |
| 1415 | + return -1; | |
| 1416 | + case INPLACE_LSHIFT: | |
| 1417 | + case INPLACE_RSHIFT: | |
| 1418 | + case INPLACE_AND: | |
| 1419 | + case INPLACE_XOR: | |
| 1420 | + case INPLACE_OR: | |
| 1421 | + return -1; | |
| 1422 | + case BREAK_LOOP: | |
| 1423 | + return 0; | |
| 1424 | + case WITH_CLEANUP: | |
| 1425 | + return -1; /* XXX Sometimes more */ | |
| 1426 | + case LOAD_LOCALS: | |
| 1427 | + return 1; | |
| 1428 | + case RETURN_VALUE: | |
| 1429 | + return -1; | |
| 1430 | + case IMPORT_STAR: | |
| 1431 | + return -1; | |
| 1432 | + case EXEC_STMT: | |
| 1433 | + return -3; | |
| 1434 | + case YIELD_VALUE: | |
| 1435 | + return 0; | |
| 1436 | + | |
| 1437 | + case POP_BLOCK: | |
| 1438 | + return 0; | |
| 1439 | + case END_FINALLY: | |
| 1440 | + return -1; /* or -2 or -3 if exception occurred */ | |
| 1441 | + case BUILD_CLASS: | |
| 1442 | + return -2; | |
| 1443 | + | |
| 1444 | + case STORE_NAME: | |
| 1445 | + return -1; | |
| 1446 | + case DELETE_NAME: | |
| 1447 | + return 0; | |
| 1448 | + case UNPACK_SEQUENCE: | |
| 1449 | + return oparg-1; | |
| 1450 | + case FOR_ITER: | |
| 1451 | + return 1; | |
| 1452 | + | |
| 1453 | + case STORE_ATTR: | |
| 1454 | + return -2; | |
| 1455 | + case DELETE_ATTR: | |
| 1456 | + return -1; | |
| 1457 | + case STORE_GLOBAL: | |
| 1458 | + return -1; | |
| 1459 | + case DELETE_GLOBAL: | |
| 1460 | + return 0; | |
| 1461 | + case DUP_TOPX: | |
| 1462 | + return oparg; | |
| 1463 | + case LOAD_CONST: | |
| 1464 | + return 1; | |
| 1465 | + case LOAD_NAME: | |
| 1466 | + return 1; | |
| 1467 | + case BUILD_TUPLE: | |
| 1468 | + case BUILD_LIST: | |
| 1469 | + return 1-oparg; | |
| 1470 | + case BUILD_MAP: | |
| 1471 | + return 1; | |
| 1472 | + case LOAD_ATTR: | |
| 1473 | + return 0; | |
| 1474 | + case COMPARE_OP: | |
| 1475 | + return -1; | |
| 1476 | + case IMPORT_NAME: | |
| 1477 | + return 0; | |
| 1478 | + case IMPORT_FROM: | |
| 1479 | + return 1; | |
| 1480 | + | |
| 1481 | + case JUMP_FORWARD: | |
| 1482 | + case JUMP_IF_FALSE: | |
| 1483 | + case JUMP_IF_TRUE: | |
| 1484 | + case JUMP_ABSOLUTE: | |
| 1485 | + return 0; | |
| 1486 | + | |
| 1487 | + case LOAD_GLOBAL: | |
| 1488 | + return 1; | |
| 1489 | + | |
| 1490 | + case CONTINUE_LOOP: | |
| 1491 | + return 0; | |
| 1492 | + case SETUP_LOOP: | |
| 1493 | + return 0; | |
| 1494 | + case SETUP_EXCEPT: | |
| 1495 | + case SETUP_FINALLY: | |
| 1496 | + return 3; /* actually pushed by an exception */ | |
| 1497 | + | |
| 1498 | + case LOAD_FAST: | |
| 1499 | + return 1; | |
| 1500 | + case STORE_FAST: | |
| 1501 | + return -1; | |
| 1502 | + case DELETE_FAST: | |
| 1503 | + return 0; | |
| 1504 | + | |
| 1505 | + case RAISE_VARARGS: | |
| 1506 | + return -oparg; | |
| 1507 | +#define NARGS(o) (((o) % 256) + 2*((o) / 256)) | |
| 1508 | + case CALL_FUNCTION: | |
| 1509 | + return -NARGS(oparg); | |
| 1510 | + case CALL_FUNCTION_VAR: | |
| 1511 | + case CALL_FUNCTION_KW: | |
| 1512 | + return -NARGS(oparg)-1; | |
| 1513 | + case CALL_FUNCTION_VAR_KW: | |
| 1514 | + return -NARGS(oparg)-2; | |
| 1515 | +#undef NARGS | |
| 1516 | + case MAKE_FUNCTION: | |
| 1517 | + return -oparg; | |
| 1518 | + case BUILD_SLICE: | |
| 1519 | + if (oparg == 3) | |
| 1520 | + return -2; | |
| 1521 | + else | |
| 1522 | + return -1; | |
| 1523 | + | |
| 1524 | + case MAKE_CLOSURE: | |
| 1525 | + return -oparg; | |
| 1526 | + case LOAD_CLOSURE: | |
| 1527 | + return 1; | |
| 1528 | + case LOAD_DEREF: | |
| 1529 | + return 1; | |
| 1530 | + case STORE_DEREF: | |
| 1531 | + return -1; | |
| 1532 | + default: | |
| 1533 | + fprintf(stderr, "opcode = %d\n", opcode); | |
| 1534 | + Py_FatalError("opcode_stack_effect()"); | |
| 1535 | + | |
| 1536 | + } | |
| 1537 | + return 0; /* not reachable */ | |
| 1538 | +} | |
| 1539 | + | |
| 1540 | +/* Add an opcode with no argument. | |
| 1541 | + Returns 0 on failure, 1 on success. | |
| 1542 | +*/ | |
| 1543 | + | |
| 1544 | +static int | |
| 1545 | +compiler_addop(struct compiler *c, int opcode) | |
| 1546 | +{ | |
| 1547 | + basicblock *b; | |
| 1548 | + struct instr *i; | |
| 1549 | + int off; | |
| 1550 | + off = compiler_next_instr(c, c->u->u_curblock); | |
| 1551 | + if (off < 0) | |
| 1552 | + return 0; | |
| 1553 | + b = c->u->u_curblock; | |
| 1554 | + i = &b->b_instr[off]; | |
| 1555 | + i->i_opcode = opcode; | |
| 1556 | + i->i_hasarg = 0; | |
| 1557 | + if (opcode == RETURN_VALUE) | |
| 1558 | + b->b_return = 1; | |
| 1559 | + compiler_set_lineno(c, off); | |
| 1560 | + return 1; | |
| 1561 | +} | |
| 1562 | + | |
| 1563 | +static int | |
| 1564 | +compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o) | |
| 1565 | +{ | |
| 1566 | + PyObject *t, *v; | |
| 1567 | + Py_ssize_t arg; | |
| 1568 | + | |
| 1569 | + /* necessary to make sure types aren't coerced (e.g., int and long) */ | |
| 1570 | + t = PyTuple_Pack(2, o, o->ob_type); | |
| 1571 | + if (t == NULL) | |
| 1572 | + return -1; | |
| 1573 | + | |
| 1574 | + v = PyDict_GetItem(dict, t); | |
| 1575 | + if (!v) { | |
| 1576 | + arg = PyDict_Size(dict); | |
| 1577 | + v = PyInt_FromLong(arg); | |
| 1578 | + if (!v) { | |
| 1579 | + Py_DECREF(t); | |
| 1580 | + return -1; | |
| 1581 | + } | |
| 1582 | + if (PyDict_SetItem(dict, t, v) < 0) { | |
| 1583 | + Py_DECREF(t); | |
| 1584 | + Py_DECREF(v); | |
| 1585 | + return -1; | |
| 1586 | + } | |
| 1587 | + Py_DECREF(v); | |
| 1588 | + } | |
| 1589 | + else | |
| 1590 | + arg = PyInt_AsLong(v); | |
| 1591 | + Py_DECREF(t); | |
| 1592 | + return arg; | |
| 1593 | +} | |
| 1594 | + | |
| 1595 | +static int | |
| 1596 | +compiler_addop_o(struct compiler *c, int opcode, PyObject *dict, | |
| 1597 | + PyObject *o) | |
| 1598 | +{ | |
| 1599 | + int arg = compiler_add_o(c, dict, o); | |
| 1600 | + if (arg < 0) | |
| 1601 | + return 0; | |
| 1602 | + return compiler_addop_i(c, opcode, arg); | |
| 1603 | +} | |
| 1604 | + | |
| 1605 | +static int | |
| 1606 | +compiler_addop_name(struct compiler *c, int opcode, PyObject *dict, | |
| 1607 | + PyObject *o) | |
| 1608 | +{ | |
| 1609 | + int arg; | |
| 1610 | + PyObject *mangled = _Py_Mangle(c->u->u_private, o); | |
| 1611 | + if (!mangled) | |
| 1612 | + return 0; | |
| 1613 | + arg = compiler_add_o(c, dict, mangled); | |
| 1614 | + Py_DECREF(mangled); | |
| 1615 | + if (arg < 0) | |
| 1616 | + return 0; | |
| 1617 | + return compiler_addop_i(c, opcode, arg); | |
| 1618 | +} | |
| 1619 | + | |
| 1620 | +/* Add an opcode with an integer argument. | |
| 1621 | + Returns 0 on failure, 1 on success. | |
| 1622 | +*/ | |
| 1623 | + | |
| 1624 | +static int | |
| 1625 | +compiler_addop_i(struct compiler *c, int opcode, int oparg) | |
| 1626 | +{ | |
| 1627 | + struct instr *i; | |
| 1628 | + int off; | |
| 1629 | + off = compiler_next_instr(c, c->u->u_curblock); | |
| 1630 | + if (off < 0) | |
| 1631 | + return 0; | |
| 1632 | + i = &c->u->u_curblock->b_instr[off]; | |
| 1633 | + i->i_opcode = opcode; | |
| 1634 | + i->i_oparg = oparg; | |
| 1635 | + i->i_hasarg = 1; | |
| 1636 | + compiler_set_lineno(c, off); | |
| 1637 | + return 1; | |
| 1638 | +} | |
| 1639 | + | |
| 1640 | +static int | |
| 1641 | +compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute) | |
| 1642 | +{ | |
| 1643 | + struct instr *i; | |
| 1644 | + int off; | |
| 1645 | + | |
| 1646 | + assert(b != NULL); | |
| 1647 | + off = compiler_next_instr(c, c->u->u_curblock); | |
| 1648 | + if (off < 0) | |
| 1649 | + return 0; | |
| 1650 | + i = &c->u->u_curblock->b_instr[off]; | |
| 1651 | + i->i_opcode = opcode; | |
| 1652 | + i->i_target = b; | |
| 1653 | + i->i_hasarg = 1; | |
| 1654 | + if (absolute) | |
| 1655 | + i->i_jabs = 1; | |
| 1656 | + else | |
| 1657 | + i->i_jrel = 1; | |
| 1658 | + compiler_set_lineno(c, off); | |
| 1659 | + return 1; | |
| 1660 | +} | |
| 1661 | + | |
| 1662 | +/* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd | |
| 1663 | + like to find better names.) NEW_BLOCK() creates a new block and sets | |
| 1664 | + it as the current block. NEXT_BLOCK() also creates an implicit jump | |
| 1665 | + from the current block to the new block. | |
| 1666 | +*/ | |
| 1667 | + | |
| 1668 | +/* XXX The returns inside these macros make it impossible to decref | |
| 1669 | + objects created in the local function. | |
| 1670 | +*/ | |
| 1671 | + | |
| 1672 | + | |
| 1673 | +#define NEW_BLOCK(C) { \ | |
| 1674 | + if (compiler_use_new_block((C)) == NULL) \ | |
| 1675 | + return 0; \ | |
| 1676 | +} | |
| 1677 | + | |
| 1678 | +#define NEXT_BLOCK(C) { \ | |
| 1679 | + if (compiler_next_block((C)) == NULL) \ | |
| 1680 | + return 0; \ | |
| 1681 | +} | |
| 1682 | + | |
| 1683 | +#define ADDOP(C, OP) { \ | |
| 1684 | + if (!compiler_addop((C), (OP))) \ | |
| 1685 | + return 0; \ | |
| 1686 | +} | |
| 1687 | + | |
| 1688 | +#define ADDOP_IN_SCOPE(C, OP) { \ | |
| 1689 | + if (!compiler_addop((C), (OP))) { \ | |
| 1690 | + compiler_exit_scope(c); \ | |
| 1691 | + return 0; \ | |
| 1692 | + } \ | |
| 1693 | +} | |
| 1694 | + | |
| 1695 | +#define ADDOP_O(C, OP, O, TYPE) { \ | |
| 1696 | + if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \ | |
| 1697 | + return 0; \ | |
| 1698 | +} | |
| 1699 | + | |
| 1700 | +#define ADDOP_NAME(C, OP, O, TYPE) { \ | |
| 1701 | + if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \ | |
| 1702 | + return 0; \ | |
| 1703 | +} | |
| 1704 | + | |
| 1705 | +#define ADDOP_I(C, OP, O) { \ | |
| 1706 | + if (!compiler_addop_i((C), (OP), (O))) \ | |
| 1707 | + return 0; \ | |
| 1708 | +} | |
| 1709 | + | |
| 1710 | +#define ADDOP_JABS(C, OP, O) { \ | |
| 1711 | + if (!compiler_addop_j((C), (OP), (O), 1)) \ | |
| 1712 | + return 0; \ | |
| 1713 | +} | |
| 1714 | + | |
| 1715 | +#define ADDOP_JREL(C, OP, O) { \ | |
| 1716 | + if (!compiler_addop_j((C), (OP), (O), 0)) \ | |
| 1717 | + return 0; \ | |
| 1718 | +} | |
| 1719 | + | |
| 1720 | +/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use | |
| 1721 | + the ASDL name to synthesize the name of the C type and the visit function. | |
| 1722 | +*/ | |
| 1723 | + | |
| 1724 | +#define VISIT(C, TYPE, V) {\ | |
| 1725 | + if (!compiler_visit_ ## TYPE((C), (V))) \ | |
| 1726 | + return 0; \ | |
| 1727 | +} | |
| 1728 | + | |
| 1729 | +#define VISIT_IN_SCOPE(C, TYPE, V) {\ | |
| 1730 | + if (!compiler_visit_ ## TYPE((C), (V))) { \ | |
| 1731 | + compiler_exit_scope(c); \ | |
| 1732 | + return 0; \ | |
| 1733 | + } \ | |
| 1734 | +} | |
| 1735 | + | |
| 1736 | +#define VISIT_SLICE(C, V, CTX) {\ | |
| 1737 | + if (!compiler_visit_slice((C), (V), (CTX))) \ | |
| 1738 | + return 0; \ | |
| 1739 | +} | |
| 1740 | + | |
| 1741 | +#define VISIT_SEQ(C, TYPE, SEQ) { \ | |
| 1742 | + int _i; \ | |
| 1743 | + asdl_seq *seq = (SEQ); /* avoid variable capture */ \ | |
| 1744 | + for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ | |
| 1745 | + TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \ | |
| 1746 | + if (!compiler_visit_ ## TYPE((C), elt)) \ | |
| 1747 | + return 0; \ | |
| 1748 | + } \ | |
| 1749 | +} | |
| 1750 | + | |
| 1751 | +#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \ | |
| 1752 | + int _i; \ | |
| 1753 | + asdl_seq *seq = (SEQ); /* avoid variable capture */ \ | |
| 1754 | + for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ | |
| 1755 | + TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \ | |
| 1756 | + if (!compiler_visit_ ## TYPE((C), elt)) { \ | |
| 1757 | + compiler_exit_scope(c); \ | |
| 1758 | + return 0; \ | |
| 1759 | + } \ | |
| 1760 | + } \ | |
| 1761 | +} | |
| 1762 | + | |
| 1763 | +static int | |
| 1764 | +compiler_isdocstring(stmt_ty s) | |
| 1765 | +{ | |
| 1766 | + if (s->kind != Expr_kind) | |
| 1767 | + return 0; | |
| 1768 | + return s->v.Expr.value->kind == Str_kind; | |
| 1769 | +} | |
| 1770 | + | |
| 1771 | +/* Compile a sequence of statements, checking for a docstring. */ | |
| 1772 | + | |
| 1773 | +static int | |
| 1774 | +compiler_body(struct compiler *c, asdl_seq *stmts) | |
| 1775 | +{ | |
| 1776 | + int i = 0; | |
| 1777 | + stmt_ty st; | |
| 1778 | + | |
| 1779 | + if (!asdl_seq_LEN(stmts)) | |
| 1780 | + return 1; | |
| 1781 | + st = (stmt_ty)asdl_seq_GET(stmts, 0); | |
| 1782 | + if (compiler_isdocstring(st)) { | |
| 1783 | + i = 1; | |
| 1784 | + VISIT(c, expr, st->v.Expr.value); | |
| 1785 | + if (!compiler_nameop(c, __doc__, Store)) | |
| 1786 | + return 0; | |
| 1787 | + } | |
| 1788 | + for (; i < asdl_seq_LEN(stmts); i++) | |
| 1789 | + VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i)); | |
| 1790 | + return 1; | |
| 1791 | +} | |
| 1792 | + | |
| 1793 | +static PyCodeObject * | |
| 1794 | +compiler_mod(struct compiler *c, mod_ty mod) | |
| 1795 | +{ | |
| 1796 | + PyCodeObject *co; | |
| 1797 | + int addNone = 1; | |
| 1798 | + static PyObject *module; | |
| 1799 | + if (!module) { | |
| 1800 | + module = PyString_FromString("<module>"); | |
| 1801 | + if (!module) | |
| 1802 | + return NULL; | |
| 1803 | + } | |
| 1804 | + /* Use 0 for firstlineno initially, will fixup in assemble(). */ | |
| 1805 | + if (!compiler_enter_scope(c, module, mod, 0)) | |
| 1806 | + return NULL; | |
| 1807 | + switch (mod->kind) { | |
| 1808 | + case Module_kind: | |
| 1809 | + if (!compiler_body(c, mod->v.Module.body)) { | |
| 1810 | + compiler_exit_scope(c); | |
| 1811 | + return 0; | |
| 1812 | + } | |
| 1813 | + break; | |
| 1814 | + case Interactive_kind: | |
| 1815 | + c->c_interactive = 1; | |
| 1816 | + VISIT_SEQ_IN_SCOPE(c, stmt, | |
| 1817 | + mod->v.Interactive.body); | |
| 1818 | + break; | |
| 1819 | + case Expression_kind: | |
| 1820 | + VISIT_IN_SCOPE(c, expr, mod->v.Expression.body); | |
| 1821 | + addNone = 0; | |
| 1822 | + break; | |
| 1823 | + case Suite_kind: | |
| 1824 | + PyErr_SetString(PyExc_SystemError, | |
| 1825 | + "suite should not be possible"); | |
| 1826 | + return 0; | |
| 1827 | + default: | |
| 1828 | + PyErr_Format(PyExc_SystemError, | |
| 1829 | + "module kind %d should not be possible", | |
| 1830 | + mod->kind); | |
| 1831 | + return 0; | |
| 1832 | + } | |
| 1833 | + co = assemble(c, addNone); | |
| 1834 | + compiler_exit_scope(c); | |
| 1835 | + return co; | |
| 1836 | +} | |
| 1837 | + | |
| 1838 | +/* The test for LOCAL must come before the test for FREE in order to | |
| 1839 | + handle classes where name is both local and free. The local var is | |
| 1840 | + a method and the free var is a free var referenced within a method. | |
| 1841 | +*/ | |
| 1842 | + | |
| 1843 | +static int | |
| 1844 | +get_ref_type(struct compiler *c, PyObject *name) | |
| 1845 | +{ | |
| 1846 | + int scope = PyST_GetScope(c->u->u_ste, name); | |
| 1847 | + if (scope == 0) { | |
| 1848 | + char buf[350]; | |
| 1849 | + PyOS_snprintf(buf, sizeof(buf), | |
| 1850 | + "unknown scope for %.100s in %.100s(%s) in %s\n" | |
| 1851 | + "symbols: %s\nlocals: %s\nglobals: %s\n", | |
| 1852 | + PyString_AS_STRING(name), | |
| 1853 | + PyString_AS_STRING(c->u->u_name), | |
| 1854 | + PyObject_REPR(c->u->u_ste->ste_id), | |
| 1855 | + c->c_filename, | |
| 1856 | + PyObject_REPR(c->u->u_ste->ste_symbols), | |
| 1857 | + PyObject_REPR(c->u->u_varnames), | |
| 1858 | + PyObject_REPR(c->u->u_names) | |
| 1859 | + ); | |
| 1860 | + Py_FatalError(buf); | |
| 1861 | + } | |
| 1862 | + | |
| 1863 | + return scope; | |
| 1864 | +} | |
| 1865 | + | |
| 1866 | +static int | |
| 1867 | +compiler_lookup_arg(PyObject *dict, PyObject *name) | |
| 1868 | +{ | |
| 1869 | + PyObject *k, *v; | |
| 1870 | + k = PyTuple_Pack(2, name, name->ob_type); | |
| 1871 | + if (k == NULL) | |
| 1872 | + return -1; | |
| 1873 | + v = PyDict_GetItem(dict, k); | |
| 1874 | + Py_DECREF(k); | |
| 1875 | + if (v == NULL) | |
| 1876 | + return -1; | |
| 1877 | + return PyInt_AS_LONG(v); | |
| 1878 | +} | |
| 1879 | + | |
| 1880 | +static int | |
| 1881 | +compiler_make_closure(struct compiler *c, PyCodeObject *co, int args) | |
| 1882 | +{ | |
| 1883 | + int i, free = PyCode_GetNumFree(co); | |
| 1884 | + if (free == 0) { | |
| 1885 | + ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts); | |
| 1886 | + ADDOP_I(c, MAKE_FUNCTION, args); | |
| 1887 | + return 1; | |
| 1888 | + } | |
| 1889 | + for (i = 0; i < free; ++i) { | |
| 1890 | + /* Bypass com_addop_varname because it will generate | |
| 1891 | + LOAD_DEREF but LOAD_CLOSURE is needed. | |
| 1892 | + */ | |
| 1893 | + PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); | |
| 1894 | + int arg, reftype; | |
| 1895 | + | |
| 1896 | + /* Special case: If a class contains a method with a | |
| 1897 | + free variable that has the same name as a method, | |
| 1898 | + the name will be considered free *and* local in the | |
| 1899 | + class. It should be handled by the closure, as | |
| 1900 | + well as by the normal name loookup logic. | |
| 1901 | + */ | |
| 1902 | + reftype = get_ref_type(c, name); | |
| 1903 | + if (reftype == CELL) | |
| 1904 | + arg = compiler_lookup_arg(c->u->u_cellvars, name); | |
| 1905 | + else /* (reftype == FREE) */ | |
| 1906 | + arg = compiler_lookup_arg(c->u->u_freevars, name); | |
| 1907 | + if (arg == -1) { | |
| 1908 | + printf("lookup %s in %s %d %d\n" | |
| 1909 | + "freevars of %s: %s\n", | |
| 1910 | + PyObject_REPR(name), | |
| 1911 | + PyString_AS_STRING(c->u->u_name), | |
| 1912 | + reftype, arg, | |
| 1913 | + PyString_AS_STRING(co->co_name), | |
| 1914 | + PyObject_REPR(co->co_freevars)); | |
| 1915 | + Py_FatalError("compiler_make_closure()"); | |
| 1916 | + } | |
| 1917 | + ADDOP_I(c, LOAD_CLOSURE, arg); | |
| 1918 | + } | |
| 1919 | + ADDOP_I(c, BUILD_TUPLE, free); | |
| 1920 | + ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts); | |
| 1921 | + ADDOP_I(c, MAKE_CLOSURE, args); | |
| 1922 | + return 1; | |
| 1923 | +} | |
| 1924 | + | |
| 1925 | +static int | |
| 1926 | +compiler_decorators(struct compiler *c, asdl_seq* decos) | |
| 1927 | +{ | |
| 1928 | + int i; | |
| 1929 | + | |
| 1930 | + if (!decos) | |
| 1931 | + return 1; | |
| 1932 | + | |
| 1933 | + for (i = 0; i < asdl_seq_LEN(decos); i++) { | |
| 1934 | + VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i)); | |
| 1935 | + } | |
| 1936 | + return 1; | |
| 1937 | +} | |
| 1938 | + | |
| 1939 | +static int | |
| 1940 | +compiler_arguments(struct compiler *c, arguments_ty args) | |
| 1941 | +{ | |
| 1942 | + int i; | |
| 1943 | + int n = asdl_seq_LEN(args->args); | |
| 1944 | + /* Correctly handle nested argument lists */ | |
| 1945 | + for (i = 0; i < n; i++) { | |
| 1946 | + expr_ty arg = (expr_ty)asdl_seq_GET(args->args, i); | |
| 1947 | + if (arg->kind == Tuple_kind) { | |
| 1948 | + PyObject *id = PyString_FromFormat(".%d", i); | |
| 1949 | + if (id == NULL) { | |
| 1950 | + return 0; | |
| 1951 | + } | |
| 1952 | + if (!compiler_nameop(c, id, Load)) { | |
| 1953 | + Py_DECREF(id); | |
| 1954 | + return 0; | |
| 1955 | + } | |
| 1956 | + Py_DECREF(id); | |
| 1957 | + VISIT(c, expr, arg); | |
| 1958 | + } | |
| 1959 | + } | |
| 1960 | + return 1; | |
| 1961 | +} | |
| 1962 | + | |
| 1963 | +static int | |
| 1964 | +compiler_function(struct compiler *c, stmt_ty s) | |
| 1965 | +{ | |
| 1966 | + PyCodeObject *co; | |
| 1967 | + PyObject *first_const = Py_None; | |
| 1968 | + arguments_ty args = s->v.FunctionDef.args; | |
| 1969 | + asdl_seq* decos = s->v.FunctionDef.decorators; | |
| 1970 | + stmt_ty st; | |
| 1971 | + int i, n, docstring; | |
| 1972 | + | |
| 1973 | + assert(s->kind == FunctionDef_kind); | |
| 1974 | + | |
| 1975 | + if (!compiler_decorators(c, decos)) | |
| 1976 | + return 0; | |
| 1977 | + if (args->defaults) | |
| 1978 | + VISIT_SEQ(c, expr, args->defaults); | |
| 1979 | + if (!compiler_enter_scope(c, s->v.FunctionDef.name, (void *)s, | |
| 1980 | + s->lineno)) | |
| 1981 | + return 0; | |
| 1982 | + | |
| 1983 | + st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, 0); | |
| 1984 | + docstring = compiler_isdocstring(st); | |
| 1985 | + if (docstring) | |
| 1986 | + first_const = st->v.Expr.value->v.Str.s; | |
| 1987 | + if (compiler_add_o(c, c->u->u_consts, first_const) < 0) { | |
| 1988 | + compiler_exit_scope(c); | |
| 1989 | + return 0; | |
| 1990 | + } | |
| 1991 | + | |
| 1992 | + /* unpack nested arguments */ | |
| 1993 | + compiler_arguments(c, args); | |
| 1994 | + | |
| 1995 | + c->u->u_argcount = asdl_seq_LEN(args->args); | |
| 1996 | + n = asdl_seq_LEN(s->v.FunctionDef.body); | |
| 1997 | + /* if there was a docstring, we need to skip the first statement */ | |
| 1998 | + for (i = docstring; i < n; i++) { | |
| 1999 | + st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i); | |
| 2000 | + VISIT_IN_SCOPE(c, stmt, st); | |
| 2001 | + } | |
| 2002 | + co = assemble(c, 1); | |
| 2003 | + compiler_exit_scope(c); | |
| 2004 | + if (co == NULL) | |
| 2005 | + return 0; | |
| 2006 | + | |
| 2007 | + compiler_make_closure(c, co, asdl_seq_LEN(args->defaults)); | |
| 2008 | + Py_DECREF(co); | |
| 2009 | + | |
| 2010 | + for (i = 0; i < asdl_seq_LEN(decos); i++) { | |
| 2011 | + ADDOP_I(c, CALL_FUNCTION, 1); | |
| 2012 | + } | |
| 2013 | + | |
| 2014 | + return compiler_nameop(c, s->v.FunctionDef.name, Store); | |
| 2015 | +} | |
| 2016 | + | |
| 2017 | +static int | |
| 2018 | +compiler_class(struct compiler *c, stmt_ty s) | |
| 2019 | +{ | |
| 2020 | + int n; | |
| 2021 | + PyCodeObject *co; | |
| 2022 | + PyObject *str; | |
| 2023 | + /* push class name on stack, needed by BUILD_CLASS */ | |
| 2024 | + ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts); | |
| 2025 | + /* push the tuple of base classes on the stack */ | |
| 2026 | + n = asdl_seq_LEN(s->v.ClassDef.bases); | |
| 2027 | + if (n > 0) | |
| 2028 | + VISIT_SEQ(c, expr, s->v.ClassDef.bases); | |
| 2029 | + ADDOP_I(c, BUILD_TUPLE, n); | |
| 2030 | + if (!compiler_enter_scope(c, s->v.ClassDef.name, (void *)s, | |
| 2031 | + s->lineno)) | |
| 2032 | + return 0; | |
| 2033 | + c->u->u_private = s->v.ClassDef.name; | |
| 2034 | + Py_INCREF(c->u->u_private); | |
| 2035 | + str = PyString_InternFromString("__name__"); | |
| 2036 | + if (!str || !compiler_nameop(c, str, Load)) { | |
| 2037 | + Py_XDECREF(str); | |
| 2038 | + compiler_exit_scope(c); | |
| 2039 | + return 0; | |
| 2040 | + } | |
| 2041 | + | |
| 2042 | + Py_DECREF(str); | |
| 2043 | + str = PyString_InternFromString("__module__"); | |
| 2044 | + if (!str || !compiler_nameop(c, str, Store)) { | |
| 2045 | + Py_XDECREF(str); | |
| 2046 | + compiler_exit_scope(c); | |
| 2047 | + return 0; | |
| 2048 | + } | |
| 2049 | + Py_DECREF(str); | |
| 2050 | + | |
| 2051 | + if (!compiler_body(c, s->v.ClassDef.body)) { | |
| 2052 | + compiler_exit_scope(c); | |
| 2053 | + return 0; | |
| 2054 | + } | |
| 2055 | + | |
| 2056 | + ADDOP_IN_SCOPE(c, LOAD_LOCALS); | |
| 2057 | + ADDOP_IN_SCOPE(c, RETURN_VALUE); | |
| 2058 | + co = assemble(c, 1); | |
| 2059 | + compiler_exit_scope(c); | |
| 2060 | + if (co == NULL) | |
| 2061 | + return 0; | |
| 2062 | + | |
| 2063 | + compiler_make_closure(c, co, 0); | |
| 2064 | + Py_DECREF(co); | |
| 2065 | + | |
| 2066 | + ADDOP_I(c, CALL_FUNCTION, 0); | |
| 2067 | + ADDOP(c, BUILD_CLASS); | |
| 2068 | + if (!compiler_nameop(c, s->v.ClassDef.name, Store)) | |
| 2069 | + return 0; | |
| 2070 | + return 1; | |
| 2071 | +} | |
| 2072 | + | |
| 2073 | +static int | |
| 2074 | +compiler_ifexp(struct compiler *c, expr_ty e) | |
| 2075 | +{ | |
| 2076 | + basicblock *end, *next; | |
| 2077 | + | |
| 2078 | + assert(e->kind == IfExp_kind); | |
| 2079 | + end = compiler_new_block(c); | |
| 2080 | + if (end == NULL) | |
| 2081 | + return 0; | |
| 2082 | + next = compiler_new_block(c); | |
| 2083 | + if (next == NULL) | |
| 2084 | + return 0; | |
| 2085 | + VISIT(c, expr, e->v.IfExp.test); | |
| 2086 | + ADDOP_JREL(c, JUMP_IF_FALSE, next); | |
| 2087 | + ADDOP(c, POP_TOP); | |
| 2088 | + VISIT(c, expr, e->v.IfExp.body); | |
| 2089 | + ADDOP_JREL(c, JUMP_FORWARD, end); | |
| 2090 | + compiler_use_next_block(c, next); | |
| 2091 | + ADDOP(c, POP_TOP); | |
| 2092 | + VISIT(c, expr, e->v.IfExp.orelse); | |
| 2093 | + compiler_use_next_block(c, end); | |
| 2094 | + return 1; | |
| 2095 | +} | |
| 2096 | + | |
| 2097 | +static int | |
| 2098 | +compiler_lambda(struct compiler *c, expr_ty e) | |
| 2099 | +{ | |
| 2100 | + PyCodeObject *co; | |
| 2101 | + static identifier name; | |
| 2102 | + arguments_ty args = e->v.Lambda.args; | |
| 2103 | + assert(e->kind == Lambda_kind); | |
| 2104 | + | |
| 2105 | + if (!name) { | |
| 2106 | + name = PyString_InternFromString("<lambda>"); | |
| 2107 | + if (!name) | |
| 2108 | + return 0; | |
| 2109 | + } | |
| 2110 | + | |
| 2111 | + if (args->defaults) | |
| 2112 | + VISIT_SEQ(c, expr, args->defaults); | |
| 2113 | + if (!compiler_enter_scope(c, name, (void *)e, e->lineno)) | |
| 2114 | + return 0; | |
| 2115 | + | |
| 2116 | + /* unpack nested arguments */ | |
| 2117 | + compiler_arguments(c, args); | |
| 2118 | + | |
| 2119 | + c->u->u_argcount = asdl_seq_LEN(args->args); | |
| 2120 | + VISIT_IN_SCOPE(c, expr, e->v.Lambda.body); | |
| 2121 | + ADDOP_IN_SCOPE(c, RETURN_VALUE); | |
| 2122 | + co = assemble(c, 1); | |
| 2123 | + compiler_exit_scope(c); | |
| 2124 | + if (co == NULL) | |
| 2125 | + return 0; | |
| 2126 | + | |
| 2127 | + compiler_make_closure(c, co, asdl_seq_LEN(args->defaults)); | |
| 2128 | + Py_DECREF(co); | |
| 2129 | + | |
| 2130 | + return 1; | |
| 2131 | +} | |
| 2132 | + | |
| 2133 | +static int | |
| 2134 | +compiler_print(struct compiler *c, stmt_ty s) | |
| 2135 | +{ | |
| 2136 | + int i, n; | |
| 2137 | + bool dest; | |
| 2138 | + | |
| 2139 | + assert(s->kind == Print_kind); | |
| 2140 | + n = asdl_seq_LEN(s->v.Print.values); | |
| 2141 | + dest = false; | |
| 2142 | + if (s->v.Print.dest) { | |
| 2143 | + VISIT(c, expr, s->v.Print.dest); | |
| 2144 | + dest = true; | |
| 2145 | + } | |
| 2146 | + for (i = 0; i < n; i++) { | |
| 2147 | + expr_ty e = (expr_ty)asdl_seq_GET(s->v.Print.values, i); | |
| 2148 | + if (dest) { | |
| 2149 | + ADDOP(c, DUP_TOP); | |
| 2150 | + VISIT(c, expr, e); | |
| 2151 | + ADDOP(c, ROT_TWO); | |
| 2152 | + ADDOP(c, PRINT_ITEM_TO); | |
| 2153 | + } | |
| 2154 | + else { | |
| 2155 | + VISIT(c, expr, e); | |
| 2156 | + ADDOP(c, PRINT_ITEM); | |
| 2157 | + } | |
| 2158 | + } | |
| 2159 | + if (s->v.Print.nl) { | |
| 2160 | + if (dest) | |
| 2161 | + ADDOP(c, PRINT_NEWLINE_TO) | |
| 2162 | + else | |
| 2163 | + ADDOP(c, PRINT_NEWLINE) | |
| 2164 | + } | |
| 2165 | + else if (dest) | |
| 2166 | + ADDOP(c, POP_TOP); | |
| 2167 | + return 1; | |
| 2168 | +} | |
| 2169 | + | |
| 2170 | +static int | |
| 2171 | +compiler_if(struct compiler *c, stmt_ty s) | |
| 2172 | +{ | |
| 2173 | + basicblock *end, *next; | |
| 2174 | + int constant; | |
| 2175 | + assert(s->kind == If_kind); | |
| 2176 | + end = compiler_new_block(c); | |
| 2177 | + if (end == NULL) | |
| 2178 | + return 0; | |
| 2179 | + next = compiler_new_block(c); | |
| 2180 | + if (next == NULL) | |
| 2181 | + return 0; | |
| 2182 | + | |
| 2183 | + constant = expr_constant(s->v.If.test); | |
| 2184 | + /* constant = 0: "if 0" | |
| 2185 | + * constant = 1: "if 1", "if 2", ... | |
| 2186 | + * constant = -1: rest */ | |
| 2187 | + if (constant == 0) { | |
| 2188 | + if (s->v.If.orelse) | |
| 2189 | + VISIT_SEQ(c, stmt, s->v.If.orelse); | |
| 2190 | + } else if (constant == 1) { | |
| 2191 | + VISIT_SEQ(c, stmt, s->v.If.body); | |
| 2192 | + } else { | |
| 2193 | + VISIT(c, expr, s->v.If.test); | |
| 2194 | + ADDOP_JREL(c, JUMP_IF_FALSE, next); | |
| 2195 | + ADDOP(c, POP_TOP); | |
| 2196 | + VISIT_SEQ(c, stmt, s->v.If.body); | |
| 2197 | + ADDOP_JREL(c, JUMP_FORWARD, end); | |
| 2198 | + compiler_use_next_block(c, next); | |
| 2199 | + ADDOP(c, POP_TOP); | |
| 2200 | + if (s->v.If.orelse) | |
| 2201 | + VISIT_SEQ(c, stmt, s->v.If.orelse); | |
| 2202 | + } | |
| 2203 | + compiler_use_next_block(c, end); | |
| 2204 | + return 1; | |
| 2205 | +} | |
| 2206 | + | |
| 2207 | +static int | |
| 2208 | +compiler_for(struct compiler *c, stmt_ty s) | |
| 2209 | +{ | |
| 2210 | + basicblock *start, *cleanup, *end; | |
| 2211 | + | |
| 2212 | + start = compiler_new_block(c); | |
| 2213 | + cleanup = compiler_new_block(c); | |
| 2214 | + end = compiler_new_block(c); | |
| 2215 | + if (start == NULL || end == NULL || cleanup == NULL) | |
| 2216 | + return 0; | |
| 2217 | + ADDOP_JREL(c, SETUP_LOOP, end); | |
| 2218 | + if (!compiler_push_fblock(c, LOOP, start)) | |
| 2219 | + return 0; | |
| 2220 | + VISIT(c, expr, s->v.For.iter); | |
| 2221 | + ADDOP(c, GET_ITER); | |
| 2222 | + compiler_use_next_block(c, start); | |
| 2223 | + /* XXX(nnorwitz): is there a better way to handle this? | |
| 2224 | + for loops are special, we want to be able to trace them | |
| 2225 | + each time around, so we need to set an extra line number. */ | |
| 2226 | + c->u->u_lineno_set = false; | |
| 2227 | + ADDOP_JREL(c, FOR_ITER, cleanup); | |
| 2228 | + VISIT(c, expr, s->v.For.target); | |
| 2229 | + VISIT_SEQ(c, stmt, s->v.For.body); | |
| 2230 | + ADDOP_JABS(c, JUMP_ABSOLUTE, start); | |
| 2231 | + compiler_use_next_block(c, cleanup); | |
| 2232 | + ADDOP(c, POP_BLOCK); | |
| 2233 | + compiler_pop_fblock(c, LOOP, start); | |
| 2234 | + VISIT_SEQ(c, stmt, s->v.For.orelse); | |
| 2235 | + compiler_use_next_block(c, end); | |
| 2236 | + return 1; | |
| 2237 | +} | |
| 2238 | + | |
| 2239 | +static int | |
| 2240 | +compiler_while(struct compiler *c, stmt_ty s) | |
| 2241 | +{ | |
| 2242 | + basicblock *loop, *orelse, *end, *anchor = NULL; | |
| 2243 | + int constant = expr_constant(s->v.While.test); | |
| 2244 | + | |
| 2245 | + if (constant == 0) | |
| 2246 | + return 1; | |
| 2247 | + loop = compiler_new_block(c); | |
| 2248 | + end = compiler_new_block(c); | |
| 2249 | + if (constant == -1) { | |
| 2250 | + anchor = compiler_new_block(c); | |
| 2251 | + if (anchor == NULL) | |
| 2252 | + return 0; | |
| 2253 | + } | |
| 2254 | + if (loop == NULL || end == NULL) | |
| 2255 | + return 0; | |
| 2256 | + if (s->v.While.orelse) { | |
| 2257 | + orelse = compiler_new_block(c); | |
| 2258 | + if (orelse == NULL) | |
| 2259 | + return 0; | |
| 2260 | + } | |
| 2261 | + else | |
| 2262 | + orelse = NULL; | |
| 2263 | + | |
| 2264 | + ADDOP_JREL(c, SETUP_LOOP, end); | |
| 2265 | + compiler_use_next_block(c, loop); | |
| 2266 | + if (!compiler_push_fblock(c, LOOP, loop)) | |
| 2267 | + return 0; | |
| 2268 | + if (constant == -1) { | |
| 2269 | + VISIT(c, expr, s->v.While.test); | |
| 2270 | + ADDOP_JREL(c, JUMP_IF_FALSE, anchor); | |
| 2271 | + ADDOP(c, POP_TOP); | |
| 2272 | + } | |
| 2273 | + VISIT_SEQ(c, stmt, s->v.While.body); | |
| 2274 | + ADDOP_JABS(c, JUMP_ABSOLUTE, loop); | |
| 2275 | + | |
| 2276 | + /* XXX should the two POP instructions be in a separate block | |
| 2277 | + if there is no else clause ? | |
| 2278 | + */ | |
| 2279 | + | |
| 2280 | + if (constant == -1) { | |
| 2281 | + compiler_use_next_block(c, anchor); | |
| 2282 | + ADDOP(c, POP_TOP); | |
| 2283 | + ADDOP(c, POP_BLOCK); | |
| 2284 | + } | |
| 2285 | + compiler_pop_fblock(c, LOOP, loop); | |
| 2286 | + if (orelse != NULL) /* what if orelse is just pass? */ | |
| 2287 | + VISIT_SEQ(c, stmt, s->v.While.orelse); | |
| 2288 | + compiler_use_next_block(c, end); | |
| 2289 | + | |
| 2290 | + return 1; | |
| 2291 | +} | |
| 2292 | + | |
| 2293 | +static int | |
| 2294 | +compiler_continue(struct compiler *c) | |
| 2295 | +{ | |
| 2296 | + static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop"; | |
| 2297 | + static const char IN_FINALLY_ERROR_MSG[] = | |
| 2298 | + "'continue' not supported inside 'finally' clause"; | |
| 2299 | + int i; | |
| 2300 | + | |
| 2301 | + if (!c->u->u_nfblocks) | |
| 2302 | + return compiler_error(c, LOOP_ERROR_MSG); | |
| 2303 | + i = c->u->u_nfblocks - 1; | |
| 2304 | + switch (c->u->u_fblock[i].fb_type) { | |
| 2305 | + case LOOP: | |
| 2306 | + ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block); | |
| 2307 | + break; | |
| 2308 | + case EXCEPT: | |
| 2309 | + case FINALLY_TRY: | |
| 2310 | + while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP) { | |
| 2311 | + /* Prevent try: ... finally: | |
| 2312 | + try: continue ... or | |
| 2313 | + try: ... except: continue */ | |
| 2314 | + if (c->u->u_fblock[i].fb_type == FINALLY_END) | |
| 2315 | + return compiler_error(c, IN_FINALLY_ERROR_MSG); | |
| 2316 | + } | |
| 2317 | + if (i == -1) | |
| 2318 | + return compiler_error(c, LOOP_ERROR_MSG); | |
| 2319 | + ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block); | |
| 2320 | + break; | |
| 2321 | + case FINALLY_END: | |
| 2322 | + return compiler_error(c, IN_FINALLY_ERROR_MSG); | |
| 2323 | + } | |
| 2324 | + | |
| 2325 | + return 1; | |
| 2326 | +} | |
| 2327 | + | |
| 2328 | +/* Code generated for "try: <body> finally: <finalbody>" is as follows: | |
| 2329 | + | |
| 2330 | + SETUP_FINALLY L | |
| 2331 | + <code for body> | |
| 2332 | + POP_BLOCK | |
| 2333 | + LOAD_CONST <None> | |
| 2334 | + L: <code for finalbody> | |
| 2335 | + END_FINALLY | |
| 2336 | + | |
| 2337 | + The special instructions use the block stack. Each block | |
| 2338 | + stack entry contains the instruction that created it (here | |
| 2339 | + SETUP_FINALLY), the level of the value stack at the time the | |
| 2340 | + block stack entry was created, and a label (here L). | |
| 2341 | + | |
| 2342 | + SETUP_FINALLY: | |
| 2343 | + Pushes the current value stack level and the label | |
| 2344 | + onto the block stack. | |
| 2345 | + POP_BLOCK: | |
| 2346 | + Pops en entry from the block stack, and pops the value | |
| 2347 | + stack until its level is the same as indicated on the | |
| 2348 | + block stack. (The label is ignored.) | |
| 2349 | + END_FINALLY: | |
| 2350 | + Pops a variable number of entries from the *value* stack | |
| 2351 | + and re-raises the exception they specify. The number of | |
| 2352 | + entries popped depends on the (pseudo) exception type. | |
| 2353 | + | |
| 2354 | + The block stack is unwound when an exception is raised: | |
| 2355 | + when a SETUP_FINALLY entry is found, the exception is pushed | |
| 2356 | + onto the value stack (and the exception condition is cleared), | |
| 2357 | + and the interpreter jumps to the label gotten from the block | |
| 2358 | + stack. | |
| 2359 | +*/ | |
| 2360 | + | |
| 2361 | +static int | |
| 2362 | +compiler_try_finally(struct compiler *c, stmt_ty s) | |
| 2363 | +{ | |
| 2364 | + basicblock *body, *end; | |
| 2365 | + body = compiler_new_block(c); | |
| 2366 | + end = compiler_new_block(c); | |
| 2367 | + if (body == NULL || end == NULL) | |
| 2368 | + return 0; | |
| 2369 | + | |
| 2370 | + ADDOP_JREL(c, SETUP_FINALLY, end); | |
| 2371 | + compiler_use_next_block(c, body); | |
| 2372 | + if (!compiler_push_fblock(c, FINALLY_TRY, body)) | |
| 2373 | + return 0; | |
| 2374 | + VISIT_SEQ(c, stmt, s->v.TryFinally.body); | |
| 2375 | + ADDOP(c, POP_BLOCK); | |
| 2376 | + compiler_pop_fblock(c, FINALLY_TRY, body); | |
| 2377 | + | |
| 2378 | + ADDOP_O(c, LOAD_CONST, Py_None, consts); | |
| 2379 | + compiler_use_next_block(c, end); | |
| 2380 | + if (!compiler_push_fblock(c, FINALLY_END, end)) | |
| 2381 | + return 0; | |
| 2382 | + VISIT_SEQ(c, stmt, s->v.TryFinally.finalbody); | |
| 2383 | + ADDOP(c, END_FINALLY); | |
| 2384 | + compiler_pop_fblock(c, FINALLY_END, end); | |
| 2385 | + | |
| 2386 | + return 1; | |
| 2387 | +} | |
| 2388 | + | |
| 2389 | +/* | |
| 2390 | + Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...": | |
| 2391 | + (The contents of the value stack is shown in [], with the top | |
| 2392 | + at the right; 'tb' is trace-back info, 'val' the exception's | |
| 2393 | + associated value, and 'exc' the exception.) | |
| 2394 | + | |
| 2395 | + Value stack Label Instruction Argument | |
| 2396 | + [] SETUP_EXCEPT L1 | |
| 2397 | + [] <code for S> | |
| 2398 | + [] POP_BLOCK | |
| 2399 | + [] JUMP_FORWARD L0 | |
| 2400 | + | |
| 2401 | + [tb, val, exc] L1: DUP ) | |
| 2402 | + [tb, val, exc, exc] <evaluate E1> ) | |
| 2403 | + [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1 | |
| 2404 | + [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 ) | |
| 2405 | + [tb, val, exc, 1] POP ) | |
| 2406 | + [tb, val, exc] POP | |
| 2407 | + [tb, val] <assign to V1> (or POP if no V1) | |
| 2408 | + [tb] POP | |
| 2409 | + [] <code for S1> | |
| 2410 | + JUMP_FORWARD L0 | |
| 2411 | + | |
| 2412 | + [tb, val, exc, 0] L2: POP | |
| 2413 | + [tb, val, exc] DUP | |
| 2414 | + .............................etc....................... | |
| 2415 | + | |
| 2416 | + [tb, val, exc, 0] Ln+1: POP | |
| 2417 | + [tb, val, exc] END_FINALLY # re-raise exception | |
| 2418 | + | |
| 2419 | + [] L0: <next statement> | |
| 2420 | + | |
| 2421 | + Of course, parts are not generated if Vi or Ei is not present. | |
| 2422 | +*/ | |
| 2423 | +static int | |
| 2424 | +compiler_try_except(struct compiler *c, stmt_ty s) | |
| 2425 | +{ | |
| 2426 | + basicblock *body, *orelse, *except, *end; | |
| 2427 | + int i, n; | |
| 2428 | + | |
| 2429 | + body = compiler_new_block(c); | |
| 2430 | + except = compiler_new_block(c); | |
| 2431 | + orelse = compiler_new_block(c); | |
| 2432 | + end = compiler_new_block(c); | |
| 2433 | + if (body == NULL || except == NULL || orelse == NULL || end == NULL) | |
| 2434 | + return 0; | |
| 2435 | + ADDOP_JREL(c, SETUP_EXCEPT, except); | |
| 2436 | + compiler_use_next_block(c, body); | |
| 2437 | + if (!compiler_push_fblock(c, EXCEPT, body)) | |
| 2438 | + return 0; | |
| 2439 | + VISIT_SEQ(c, stmt, s->v.TryExcept.body); | |
| 2440 | + ADDOP(c, POP_BLOCK); | |
| 2441 | + compiler_pop_fblock(c, EXCEPT, body); | |
| 2442 | + ADDOP_JREL(c, JUMP_FORWARD, orelse); | |
| 2443 | + n = asdl_seq_LEN(s->v.TryExcept.handlers); | |
| 2444 | + compiler_use_next_block(c, except); | |
| 2445 | + for (i = 0; i < n; i++) { | |
| 2446 | + excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( | |
| 2447 | + s->v.TryExcept.handlers, i); | |
| 2448 | + if (!handler->type && i < n-1) | |
| 2449 | + return compiler_error(c, "default 'except:' must be last"); | |
| 2450 | + c->u->u_lineno_set = false; | |
| 2451 | + c->u->u_lineno = handler->lineno; | |
| 2452 | + except = compiler_new_block(c); | |
| 2453 | + if (except == NULL) | |
| 2454 | + return 0; | |
| 2455 | + if (handler->type) { | |
| 2456 | + ADDOP(c, DUP_TOP); | |
| 2457 | + VISIT(c, expr, handler->type); | |
| 2458 | + ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH); | |
| 2459 | + ADDOP_JREL(c, JUMP_IF_FALSE, except); | |
| 2460 | + ADDOP(c, POP_TOP); | |
| 2461 | + } | |
| 2462 | + ADDOP(c, POP_TOP); | |
| 2463 | + if (handler->name) { | |
| 2464 | + VISIT(c, expr, handler->name); | |
| 2465 | + } | |
| 2466 | + else { | |
| 2467 | + ADDOP(c, POP_TOP); | |
| 2468 | + } | |
| 2469 | + ADDOP(c, POP_TOP); | |
| 2470 | + VISIT_SEQ(c, stmt, handler->body); | |
| 2471 | + ADDOP_JREL(c, JUMP_FORWARD, end); | |
| 2472 | + compiler_use_next_block(c, except); | |
| 2473 | + if (handler->type) | |
| 2474 | + ADDOP(c, POP_TOP); | |
| 2475 | + } | |
| 2476 | + ADDOP(c, END_FINALLY); | |
| 2477 | + compiler_use_next_block(c, orelse); | |
| 2478 | + VISIT_SEQ(c, stmt, s->v.TryExcept.orelse); | |
| 2479 | + compiler_use_next_block(c, end); | |
| 2480 | + return 1; | |
| 2481 | +} | |
| 2482 | + | |
| 2483 | +static int | |
| 2484 | +compiler_import_as(struct compiler *c, identifier name, identifier asname) | |
| 2485 | +{ | |
| 2486 | + /* The IMPORT_NAME opcode was already generated. This function | |
| 2487 | + merely needs to bind the result to a name. | |
| 2488 | + | |
| 2489 | + If there is a dot in name, we need to split it and emit a | |
| 2490 | + LOAD_ATTR for each name. | |
| 2491 | + */ | |
| 2492 | + const char *src = PyString_AS_STRING(name); | |
| 2493 | + const char *dot = strchr(src, '.'); | |
| 2494 | + if (dot) { | |
| 2495 | + /* Consume the base module name to get the first attribute */ | |
| 2496 | + src = dot + 1; | |
| 2497 | + while (dot) { | |
| 2498 | + /* NB src is only defined when dot != NULL */ | |
| 2499 | + PyObject *attr; | |
| 2500 | + dot = strchr(src, '.'); | |
| 2501 | + attr = PyString_FromStringAndSize(src, | |
| 2502 | + dot ? dot - src : strlen(src)); | |
| 2503 | + if (!attr) | |
| 2504 | + return -1; | |
| 2505 | + ADDOP_O(c, LOAD_ATTR, attr, names); | |
| 2506 | + Py_DECREF(attr); | |
| 2507 | + src = dot + 1; | |
| 2508 | + } | |
| 2509 | + } | |
| 2510 | + return compiler_nameop(c, asname, Store); | |
| 2511 | +} | |
| 2512 | + | |
| 2513 | +static int | |
| 2514 | +compiler_import(struct compiler *c, stmt_ty s) | |
| 2515 | +{ | |
| 2516 | + /* The Import node stores a module name like a.b.c as a single | |
| 2517 | + string. This is convenient for all cases except | |
| 2518 | + import a.b.c as d | |
| 2519 | + where we need to parse that string to extract the individual | |
| 2520 | + module names. | |
| 2521 | + XXX Perhaps change the representation to make this case simpler? | |
| 2522 | + */ | |
| 2523 | + int i, n = asdl_seq_LEN(s->v.Import.names); | |
| 2524 | + | |
| 2525 | + for (i = 0; i < n; i++) { | |
| 2526 | + alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i); | |
| 2527 | + int r; | |
| 2528 | + PyObject *level; | |
| 2529 | + | |
| 2530 | + if (c->c_flags && (c->c_flags->cf_flags & CO_FUTURE_ABSOLUTE_IMPORT)) | |
| 2531 | + level = PyInt_FromLong(0); | |
| 2532 | + else | |
| 2533 | + level = PyInt_FromLong(-1); | |
| 2534 | + | |
| 2535 | + if (level == NULL) | |
| 2536 | + return 0; | |
| 2537 | + | |
| 2538 | + ADDOP_O(c, LOAD_CONST, level, consts); | |
| 2539 | + Py_DECREF(level); | |
| 2540 | + ADDOP_O(c, LOAD_CONST, Py_None, consts); | |
| 2541 | + ADDOP_NAME(c, IMPORT_NAME, alias->name, names); | |
| 2542 | + | |
| 2543 | + if (alias->asname) { | |
| 2544 | + r = compiler_import_as(c, alias->name, alias->asname); | |
| 2545 | + if (!r) | |
| 2546 | + return r; | |
| 2547 | + } | |
| 2548 | + else { | |
| 2549 | + identifier tmp = alias->name; | |
| 2550 | + const char *base = PyString_AS_STRING(alias->name); | |
| 2551 | + char *dot = strchr(base, '.'); | |
| 2552 | + if (dot) | |
| 2553 | + tmp = PyString_FromStringAndSize(base, | |
| 2554 | + dot - base); | |
| 2555 | + r = compiler_nameop(c, tmp, Store); | |
| 2556 | + if (dot) { | |
| 2557 | + Py_DECREF(tmp); | |
| 2558 | + } | |
| 2559 | + if (!r) | |
| 2560 | + return r; | |
| 2561 | + } | |
| 2562 | + } | |
| 2563 | + return 1; | |
| 2564 | +} | |
| 2565 | + | |
| 2566 | +static int | |
| 2567 | +compiler_from_import(struct compiler *c, stmt_ty s) | |
| 2568 | +{ | |
| 2569 | + int i, n = asdl_seq_LEN(s->v.ImportFrom.names); | |
| 2570 | + | |
| 2571 | + PyObject *names = PyTuple_New(n); | |
| 2572 | + PyObject *level; | |
| 2573 | + | |
| 2574 | + if (!names) | |
| 2575 | + return 0; | |
| 2576 | + | |
| 2577 | + if (s->v.ImportFrom.level == 0 && c->c_flags && | |
| 2578 | + !(c->c_flags->cf_flags & CO_FUTURE_ABSOLUTE_IMPORT)) | |
| 2579 | + level = PyInt_FromLong(-1); | |
| 2580 | + else | |
| 2581 | + level = PyInt_FromLong(s->v.ImportFrom.level); | |
| 2582 | + | |
| 2583 | + if (!level) { | |
| 2584 | + Py_DECREF(names); | |
| 2585 | + return 0; | |
| 2586 | + } | |
| 2587 | + | |
| 2588 | + /* build up the names */ | |
| 2589 | + for (i = 0; i < n; i++) { | |
| 2590 | + alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); | |
| 2591 | + Py_INCREF(alias->name); | |
| 2592 | + PyTuple_SET_ITEM(names, i, alias->name); | |
| 2593 | + } | |
| 2594 | + | |
| 2595 | + if (s->lineno > c->c_future->ff_lineno) { | |
| 2596 | + if (!strcmp(PyString_AS_STRING(s->v.ImportFrom.module), | |
| 2597 | + "__future__")) { | |
| 2598 | + Py_DECREF(level); | |
| 2599 | + Py_DECREF(names); | |
| 2600 | + return compiler_error(c, | |
| 2601 | + "from __future__ imports must occur " | |
| 2602 | + "at the beginning of the file"); | |
| 2603 | + | |
| 2604 | + } | |
| 2605 | + } | |
| 2606 | + | |
| 2607 | + ADDOP_O(c, LOAD_CONST, level, consts); | |
| 2608 | + Py_DECREF(level); | |
| 2609 | + ADDOP_O(c, LOAD_CONST, names, consts); | |
| 2610 | + Py_DECREF(names); | |
| 2611 | + ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names); | |
| 2612 | + for (i = 0; i < n; i++) { | |
| 2613 | + alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); | |
| 2614 | + identifier store_name; | |
| 2615 | + | |
| 2616 | + if (i == 0 && *PyString_AS_STRING(alias->name) == '*') { | |
| 2617 | + assert(n == 1); | |
| 2618 | + ADDOP(c, IMPORT_STAR); | |
| 2619 | + return 1; | |
| 2620 | + } | |
| 2621 | + | |
| 2622 | + ADDOP_NAME(c, IMPORT_FROM, alias->name, names); | |
| 2623 | + store_name = alias->name; | |
| 2624 | + if (alias->asname) | |
| 2625 | + store_name = alias->asname; | |
| 2626 | + | |
| 2627 | + if (!compiler_nameop(c, store_name, Store)) { | |
| 2628 | + Py_DECREF(names); | |
| 2629 | + return 0; | |
| 2630 | + } | |
| 2631 | + } | |
| 2632 | + /* remove imported module */ | |
| 2633 | + ADDOP(c, POP_TOP); | |
| 2634 | + return 1; | |
| 2635 | +} | |
| 2636 | + | |
| 2637 | +static int | |
| 2638 | +compiler_assert(struct compiler *c, stmt_ty s) | |
| 2639 | +{ | |
| 2640 | + static PyObject *assertion_error = NULL; | |
| 2641 | + basicblock *end; | |
| 2642 | + | |
| 2643 | + if (Py_OptimizeFlag) | |
| 2644 | + return 1; | |
| 2645 | + if (assertion_error == NULL) { | |
| 2646 | + assertion_error = PyString_FromString("AssertionError"); | |
| 2647 | + if (assertion_error == NULL) | |
| 2648 | + return 0; | |
| 2649 | + } | |
| 2650 | + VISIT(c, expr, s->v.Assert.test); | |
| 2651 | + end = compiler_new_block(c); | |
| 2652 | + if (end == NULL) | |
| 2653 | + return 0; | |
| 2654 | + ADDOP_JREL(c, JUMP_IF_TRUE, end); | |
| 2655 | + ADDOP(c, POP_TOP); | |
| 2656 | + ADDOP_O(c, LOAD_GLOBAL, assertion_error, names); | |
| 2657 | + if (s->v.Assert.msg) { | |
| 2658 | + VISIT(c, expr, s->v.Assert.msg); | |
| 2659 | + ADDOP_I(c, RAISE_VARARGS, 2); | |
| 2660 | + } | |
| 2661 | + else { | |
| 2662 | + ADDOP_I(c, RAISE_VARARGS, 1); | |
| 2663 | + } | |
| 2664 | + compiler_use_next_block(c, end); | |
| 2665 | + ADDOP(c, POP_TOP); | |
| 2666 | + return 1; | |
| 2667 | +} | |
| 2668 | + | |
| 2669 | +static int | |
| 2670 | +compiler_visit_stmt(struct compiler *c, stmt_ty s) | |
| 2671 | +{ | |
| 2672 | + int i, n; | |
| 2673 | + | |
| 2674 | + /* Always assign a lineno to the next instruction for a stmt. */ | |
| 2675 | + c->u->u_lineno = s->lineno; | |
| 2676 | + c->u->u_lineno_set = false; | |
| 2677 | + | |
| 2678 | + switch (s->kind) { | |
| 2679 | + case FunctionDef_kind: | |
| 2680 | + return compiler_function(c, s); | |
| 2681 | + case ClassDef_kind: | |
| 2682 | + return compiler_class(c, s); | |
| 2683 | + case Return_kind: | |
| 2684 | + if (c->u->u_ste->ste_type != FunctionBlock) | |
| 2685 | + return compiler_error(c, "'return' outside function"); | |
| 2686 | + if (s->v.Return.value) { | |
| 2687 | + VISIT(c, expr, s->v.Return.value); | |
| 2688 | + } | |
| 2689 | + else | |
| 2690 | + ADDOP_O(c, LOAD_CONST, Py_None, consts); | |
| 2691 | + ADDOP(c, RETURN_VALUE); | |
| 2692 | + break; | |
| 2693 | + case Delete_kind: | |
| 2694 | + VISIT_SEQ(c, expr, s->v.Delete.targets) | |
| 2695 | + break; | |
| 2696 | + case Assign_kind: | |
| 2697 | + n = asdl_seq_LEN(s->v.Assign.targets); | |
| 2698 | + VISIT(c, expr, s->v.Assign.value); | |
| 2699 | + for (i = 0; i < n; i++) { | |
| 2700 | + if (i < n - 1) | |
| 2701 | + ADDOP(c, DUP_TOP); | |
| 2702 | + VISIT(c, expr, | |
| 2703 | + (expr_ty)asdl_seq_GET(s->v.Assign.targets, i)); | |
| 2704 | + } | |
| 2705 | + break; | |
| 2706 | + case AugAssign_kind: | |
| 2707 | + return compiler_augassign(c, s); | |
| 2708 | + case Print_kind: | |
| 2709 | + return compiler_print(c, s); | |
| 2710 | + case For_kind: | |
| 2711 | + return compiler_for(c, s); | |
| 2712 | + case While_kind: | |
| 2713 | + return compiler_while(c, s); | |
| 2714 | + case If_kind: | |
| 2715 | + return compiler_if(c, s); | |
| 2716 | + case Raise_kind: | |
| 2717 | + n = 0; | |
| 2718 | + if (s->v.Raise.type) { | |
| 2719 | + VISIT(c, expr, s->v.Raise.type); | |
| 2720 | + n++; | |
| 2721 | + if (s->v.Raise.inst) { | |
| 2722 | + VISIT(c, expr, s->v.Raise.inst); | |
| 2723 | + n++; | |
| 2724 | + if (s->v.Raise.tback) { | |
| 2725 | + VISIT(c, expr, s->v.Raise.tback); | |
| 2726 | + n++; | |
| 2727 | + } | |
| 2728 | + } | |
| 2729 | + } | |
| 2730 | + ADDOP_I(c, RAISE_VARARGS, n); | |
| 2731 | + break; | |
| 2732 | + case TryExcept_kind: | |
| 2733 | + return compiler_try_except(c, s); | |
| 2734 | + case TryFinally_kind: | |
| 2735 | + return compiler_try_finally(c, s); | |
| 2736 | + case Assert_kind: | |
| 2737 | + return compiler_assert(c, s); | |
| 2738 | + case Import_kind: | |
| 2739 | + return compiler_import(c, s); | |
| 2740 | + case ImportFrom_kind: | |
| 2741 | + return compiler_from_import(c, s); | |
| 2742 | + case Exec_kind: | |
| 2743 | + VISIT(c, expr, s->v.Exec.body); | |
| 2744 | + if (s->v.Exec.globals) { | |
| 2745 | + VISIT(c, expr, s->v.Exec.globals); | |
| 2746 | + if (s->v.Exec.locals) { | |
| 2747 | + VISIT(c, expr, s->v.Exec.locals); | |
| 2748 | + } else { | |
| 2749 | + ADDOP(c, DUP_TOP); | |
| 2750 | + } | |
| 2751 | + } else { | |
| 2752 | + ADDOP_O(c, LOAD_CONST, Py_None, consts); | |
| 2753 | + ADDOP(c, DUP_TOP); | |
| 2754 | + } | |
| 2755 | + ADDOP(c, EXEC_STMT); | |
| 2756 | + break; | |
| 2757 | + case Global_kind: | |
| 2758 | + break; | |
| 2759 | + case Expr_kind: | |
| 2760 | + if (c->c_interactive && c->c_nestlevel <= 1) { | |
| 2761 | + VISIT(c, expr, s->v.Expr.value); | |
| 2762 | + ADDOP(c, PRINT_EXPR); | |
| 2763 | + } | |
| 2764 | + else if (s->v.Expr.value->kind != Str_kind && | |
| 2765 | + s->v.Expr.value->kind != Num_kind) { | |
| 2766 | + VISIT(c, expr, s->v.Expr.value); | |
| 2767 | + ADDOP(c, POP_TOP); | |
| 2768 | + } | |
| 2769 | + break; | |
| 2770 | + case Pass_kind: | |
| 2771 | + break; | |
| 2772 | + case Break_kind: | |
| 2773 | + if (!compiler_in_loop(c)) | |
| 2774 | + return compiler_error(c, "'break' outside loop"); | |
| 2775 | + ADDOP(c, BREAK_LOOP); | |
| 2776 | + break; | |
| 2777 | + case Continue_kind: | |
| 2778 | + return compiler_continue(c); | |
| 2779 | + case With_kind: | |
| 2780 | + return compiler_with(c, s); | |
| 2781 | + } | |
| 2782 | + return 1; | |
| 2783 | +} | |
| 2784 | + | |
| 2785 | +static int | |
| 2786 | +unaryop(unaryop_ty op) | |
| 2787 | +{ | |
| 2788 | + switch (op) { | |
| 2789 | + case Invert: | |
| 2790 | + return UNARY_INVERT; | |
| 2791 | + case Not: | |
| 2792 | + return UNARY_NOT; | |
| 2793 | + case UAdd: | |
| 2794 | + return UNARY_POSITIVE; | |
| 2795 | + case USub: | |
| 2796 | + return UNARY_NEGATIVE; | |
| 2797 | + } | |
| 2798 | + return 0; | |
| 2799 | +} | |
| 2800 | + | |
| 2801 | +static int | |
| 2802 | +binop(struct compiler *c, operator_ty op) | |
| 2803 | +{ | |
| 2804 | + switch (op) { | |
| 2805 | + case Add: | |
| 2806 | + return BINARY_ADD; | |
| 2807 | + case Sub: | |
| 2808 | + return BINARY_SUBTRACT; | |
| 2809 | + case Mult: | |
| 2810 | + return BINARY_MULTIPLY; | |
| 2811 | + case Div: | |
| 2812 | + if (c->c_flags && c->c_flags->cf_flags & CO_FUTURE_DIVISION) | |
| 2813 | + return BINARY_TRUE_DIVIDE; | |
| 2814 | + else | |
| 2815 | + return BINARY_DIVIDE; | |
| 2816 | + case Mod: | |
| 2817 | + return BINARY_MODULO; | |
| 2818 | + case Pow: | |
| 2819 | + return BINARY_POWER; | |
| 2820 | + case LShift: | |
| 2821 | + return BINARY_LSHIFT; | |
| 2822 | + case RShift: | |
| 2823 | + return BINARY_RSHIFT; | |
| 2824 | + case BitOr: | |
| 2825 | + return BINARY_OR; | |
| 2826 | + case BitXor: | |
| 2827 | + return BINARY_XOR; | |
| 2828 | + case BitAnd: | |
| 2829 | + return BINARY_AND; | |
| 2830 | + case FloorDiv: | |
| 2831 | + return BINARY_FLOOR_DIVIDE; | |
| 2832 | + } | |
| 2833 | + return 0; | |
| 2834 | +} | |
| 2835 | + | |
| 2836 | +static int | |
| 2837 | +cmpop(cmpop_ty op) | |
| 2838 | +{ | |
| 2839 | + switch (op) { | |
| 2840 | + case Eq: | |
| 2841 | + return PyCmp_EQ; | |
| 2842 | + case NotEq: | |
| 2843 | + return PyCmp_NE; | |
| 2844 | + case Lt: | |
| 2845 | + return PyCmp_LT; | |
| 2846 | + case LtE: | |
| 2847 | + return PyCmp_LE; | |
| 2848 | + case Gt: | |
| 2849 | + return PyCmp_GT; | |
| 2850 | + case GtE: | |
| 2851 | + return PyCmp_GE; | |
| 2852 | + case Is: | |
| 2853 | + return PyCmp_IS; | |
| 2854 | + case IsNot: | |
| 2855 | + return PyCmp_IS_NOT; | |
| 2856 | + case In: | |
| 2857 | + return PyCmp_IN; | |
| 2858 | + case NotIn: | |
| 2859 | + return PyCmp_NOT_IN; | |
| 2860 | + } | |
| 2861 | + return PyCmp_BAD; | |
| 2862 | +} | |
| 2863 | + | |
| 2864 | +static int | |
| 2865 | +inplace_binop(struct compiler *c, operator_ty op) | |
| 2866 | +{ | |
| 2867 | + switch (op) { | |
| 2868 | + case Add: | |
| 2869 | + return INPLACE_ADD; | |
| 2870 | + case Sub: | |
| 2871 | + return INPLACE_SUBTRACT; | |
| 2872 | + case Mult: | |
| 2873 | + return INPLACE_MULTIPLY; | |
| 2874 | + case Div: | |
| 2875 | + if (c->c_flags && c->c_flags->cf_flags & CO_FUTURE_DIVISION) | |
| 2876 | + return INPLACE_TRUE_DIVIDE; | |
| 2877 | + else | |
| 2878 | + return INPLACE_DIVIDE; | |
| 2879 | + case Mod: | |
| 2880 | + return INPLACE_MODULO; | |
| 2881 | + case Pow: | |
| 2882 | + return INPLACE_POWER; | |
| 2883 | + case LShift: | |
| 2884 | + return INPLACE_LSHIFT; | |
| 2885 | + case RShift: | |
| 2886 | + return INPLACE_RSHIFT; | |
| 2887 | + case BitOr: | |
| 2888 | + return INPLACE_OR; | |
| 2889 | + case BitXor: | |
| 2890 | + return INPLACE_XOR; | |
| 2891 | + case BitAnd: | |
| 2892 | + return INPLACE_AND; | |
| 2893 | + case FloorDiv: | |
| 2894 | + return INPLACE_FLOOR_DIVIDE; | |
| 2895 | + } | |
| 2896 | + PyErr_Format(PyExc_SystemError, | |
| 2897 | + "inplace binary op %d should not be possible", op); | |
| 2898 | + return 0; | |
| 2899 | +} | |
| 2900 | + | |
| 2901 | +static int | |
| 2902 | +compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx) | |
| 2903 | +{ | |
| 2904 | + int op, scope, arg; | |
| 2905 | + enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype; | |
| 2906 | + | |
| 2907 | + PyObject *dict = c->u->u_names; | |
| 2908 | + PyObject *mangled; | |
| 2909 | + /* XXX AugStore isn't used anywhere! */ | |
| 2910 | + | |
| 2911 | + /* First check for assignment to __debug__. Param? */ | |
| 2912 | + if ((ctx == Store || ctx == AugStore || ctx == Del) | |
| 2913 | + && !strcmp(PyString_AS_STRING(name), "__debug__")) { | |
| 2914 | + return compiler_error(c, "can not assign to __debug__"); | |
| 2915 | + } | |
| 2916 | + | |
| 2917 | + mangled = _Py_Mangle(c->u->u_private, name); | |
| 2918 | + if (!mangled) | |
| 2919 | + return 0; | |
| 2920 | + | |
| 2921 | + op = 0; | |
| 2922 | + optype = OP_NAME; | |
| 2923 | + scope = PyST_GetScope(c->u->u_ste, mangled); | |
| 2924 | + switch (scope) { | |
| 2925 | + case FREE: | |
| 2926 | + dict = c->u->u_freevars; | |
| 2927 | + optype = OP_DEREF; | |
| 2928 | + break; | |
| 2929 | + case CELL: | |
| 2930 | + dict = c->u->u_cellvars; | |
| 2931 | + optype = OP_DEREF; | |
| 2932 | + break; | |
| 2933 | + case LOCAL: | |
| 2934 | + if (c->u->u_ste->ste_type == FunctionBlock) | |
| 2935 | + optype = OP_FAST; | |
| 2936 | + break; | |
| 2937 | + case GLOBAL_IMPLICIT: | |
| 2938 | + if (c->u->u_ste->ste_type == FunctionBlock && | |
| 2939 | + !c->u->u_ste->ste_unoptimized) | |
| 2940 | + optype = OP_GLOBAL; | |
| 2941 | + break; | |
| 2942 | + case GLOBAL_EXPLICIT: | |
| 2943 | + optype = OP_GLOBAL; | |
| 2944 | + break; | |
| 2945 | + default: | |
| 2946 | + /* scope can be 0 */ | |
| 2947 | + break; | |
| 2948 | + } | |
| 2949 | + | |
| 2950 | + /* XXX Leave assert here, but handle __doc__ and the like better */ | |
| 2951 | + assert(scope || PyString_AS_STRING(name)[0] == '_'); | |
| 2952 | + | |
| 2953 | + switch (optype) { | |
| 2954 | + case OP_DEREF: | |
| 2955 | + switch (ctx) { | |
| 2956 | + case Load: op = LOAD_DEREF; break; | |
| 2957 | + case Store: op = STORE_DEREF; break; | |
| 2958 | + case AugLoad: | |
| 2959 | + case AugStore: | |
| 2960 | + break; | |
| 2961 | + case Del: | |
| 2962 | + PyErr_Format(PyExc_SyntaxError, | |
| 2963 | + "can not delete variable '%s' referenced " | |
| 2964 | + "in nested scope", | |
| 2965 | + PyString_AS_STRING(name)); | |
| 2966 | + Py_DECREF(mangled); | |
| 2967 | + return 0; | |
| 2968 | + case Param: | |
| 2969 | + default: | |
| 2970 | + PyErr_SetString(PyExc_SystemError, | |
| 2971 | + "param invalid for deref variable"); | |
| 2972 | + return 0; | |
| 2973 | + } | |
| 2974 | + break; | |
| 2975 | + case OP_FAST: | |
| 2976 | + switch (ctx) { | |
| 2977 | + case Load: op = LOAD_FAST; break; | |
| 2978 | + case Store: op = STORE_FAST; break; | |
| 2979 | + case Del: op = DELETE_FAST; break; | |
| 2980 | + case AugLoad: | |
| 2981 | + case AugStore: | |
| 2982 | + break; | |
| 2983 | + case Param: | |
| 2984 | + default: | |
| 2985 | + PyErr_SetString(PyExc_SystemError, | |
| 2986 | + "param invalid for local variable"); | |
| 2987 | + return 0; | |
| 2988 | + } | |
| 2989 | + ADDOP_O(c, op, mangled, varnames); | |
| 2990 | + Py_DECREF(mangled); | |
| 2991 | + return 1; | |
| 2992 | + case OP_GLOBAL: | |
| 2993 | + switch (ctx) { | |
| 2994 | + case Load: op = LOAD_GLOBAL; break; | |
| 2995 | + case Store: op = STORE_GLOBAL; break; | |
| 2996 | + case Del: op = DELETE_GLOBAL; break; | |
| 2997 | + case AugLoad: | |
| 2998 | + case AugStore: | |
| 2999 | + break; | |
| 3000 | + case Param: | |
| 3001 | + default: | |
| 3002 | + PyErr_SetString(PyExc_SystemError, | |
| 3003 | + "param invalid for global variable"); | |
| 3004 | + return 0; | |
| 3005 | + } | |
| 3006 | + break; | |
| 3007 | + case OP_NAME: | |
| 3008 | + switch (ctx) { | |
| 3009 | + case Load: op = LOAD_NAME; break; | |
| 3010 | + case Store: op = STORE_NAME; break; | |
| 3011 | + case Del: op = DELETE_NAME; break; | |
| 3012 | + case AugLoad: | |
| 3013 | + case AugStore: | |
| 3014 | + break; | |
| 3015 | + case Param: | |
| 3016 | + default: | |
| 3017 | + PyErr_SetString(PyExc_SystemError, | |
| 3018 | + "param invalid for name variable"); | |
| 3019 | + return 0; | |
| 3020 | + } | |
| 3021 | + break; | |
| 3022 | + } | |
| 3023 | + | |
| 3024 | + assert(op); | |
| 3025 | + arg = compiler_add_o(c, dict, mangled); | |
| 3026 | + Py_DECREF(mangled); | |
| 3027 | + if (arg < 0) | |
| 3028 | + return 0; | |
| 3029 | + return compiler_addop_i(c, op, arg); | |
| 3030 | +} | |
| 3031 | + | |
| 3032 | +static int | |
| 3033 | +compiler_boolop(struct compiler *c, expr_ty e) | |
| 3034 | +{ | |
| 3035 | + basicblock *end; | |
| 3036 | + int jumpi, i, n; | |
| 3037 | + asdl_seq *s; | |
| 3038 | + | |
| 3039 | + assert(e->kind == BoolOp_kind); | |
| 3040 | + if (e->v.BoolOp.op == And) | |
| 3041 | + jumpi = JUMP_IF_FALSE; | |
| 3042 | + else | |
| 3043 | + jumpi = JUMP_IF_TRUE; | |
| 3044 | + end = compiler_new_block(c); | |
| 3045 | + if (end == NULL) | |
| 3046 | + return 0; | |
| 3047 | + s = e->v.BoolOp.values; | |
| 3048 | + n = asdl_seq_LEN(s) - 1; | |
| 3049 | + assert(n >= 0); | |
| 3050 | + for (i = 0; i < n; ++i) { | |
| 3051 | + VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i)); | |
| 3052 | + ADDOP_JREL(c, jumpi, end); | |
| 3053 | + ADDOP(c, POP_TOP) | |
| 3054 | + } | |
| 3055 | + VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n)); | |
| 3056 | + compiler_use_next_block(c, end); | |
| 3057 | + return 1; | |
| 3058 | +} | |
| 3059 | + | |
| 3060 | +static int | |
| 3061 | +compiler_list(struct compiler *c, expr_ty e) | |
| 3062 | +{ | |
| 3063 | + int n = asdl_seq_LEN(e->v.List.elts); | |
| 3064 | + if (e->v.List.ctx == Store) { | |
| 3065 | + ADDOP_I(c, UNPACK_SEQUENCE, n); | |
| 3066 | + } | |
| 3067 | + VISIT_SEQ(c, expr, e->v.List.elts); | |
| 3068 | + if (e->v.List.ctx == Load) { | |
| 3069 | + ADDOP_I(c, BUILD_LIST, n); | |
| 3070 | + } | |
| 3071 | + return 1; | |
| 3072 | +} | |
| 3073 | + | |
| 3074 | +static int | |
| 3075 | +compiler_tuple(struct compiler *c, expr_ty e) | |
| 3076 | +{ | |
| 3077 | + int n = asdl_seq_LEN(e->v.Tuple.elts); | |
| 3078 | + if (e->v.Tuple.ctx == Store) { | |
| 3079 | + ADDOP_I(c, UNPACK_SEQUENCE, n); | |
| 3080 | + } | |
| 3081 | + VISIT_SEQ(c, expr, e->v.Tuple.elts); | |
| 3082 | + if (e->v.Tuple.ctx == Load) { | |
| 3083 | + ADDOP_I(c, BUILD_TUPLE, n); | |
| 3084 | + } | |
| 3085 | + return 1; | |
| 3086 | +} | |
| 3087 | + | |
| 3088 | +static int | |
| 3089 | +compiler_compare(struct compiler *c, expr_ty e) | |
| 3090 | +{ | |
| 3091 | + int i, n; | |
| 3092 | + basicblock *cleanup = NULL; | |
| 3093 | + | |
| 3094 | + /* XXX the logic can be cleaned up for 1 or multiple comparisons */ | |
| 3095 | + VISIT(c, expr, e->v.Compare.left); | |
| 3096 | + n = asdl_seq_LEN(e->v.Compare.ops); | |
| 3097 | + assert(n > 0); | |
| 3098 | + if (n > 1) { | |
| 3099 | + cleanup = compiler_new_block(c); | |
| 3100 | + if (cleanup == NULL) | |
| 3101 | + return 0; | |
| 3102 | + VISIT(c, expr, | |
| 3103 | + (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0)); | |
| 3104 | + } | |
| 3105 | + for (i = 1; i < n; i++) { | |
| 3106 | + ADDOP(c, DUP_TOP); | |
| 3107 | + ADDOP(c, ROT_THREE); | |
| 3108 | + ADDOP_I(c, COMPARE_OP, | |
| 3109 | + cmpop((cmpop_ty)(asdl_seq_GET( | |
| 3110 | + e->v.Compare.ops, i - 1)))); | |
| 3111 | + ADDOP_JREL(c, JUMP_IF_FALSE, cleanup); | |
| 3112 | + NEXT_BLOCK(c); | |
| 3113 | + ADDOP(c, POP_TOP); | |
| 3114 | + if (i < (n - 1)) | |
| 3115 | + VISIT(c, expr, | |
| 3116 | + (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); | |
| 3117 | + } | |
| 3118 | + VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n - 1)); | |
| 3119 | + ADDOP_I(c, COMPARE_OP, | |
| 3120 | + cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n - 1)))); | |
| 3121 | + if (n > 1) { | |
| 3122 | + basicblock *end = compiler_new_block(c); | |
| 3123 | + if (end == NULL) | |
| 3124 | + return 0; | |
| 3125 | + ADDOP_JREL(c, JUMP_FORWARD, end); | |
| 3126 | + compiler_use_next_block(c, cleanup); | |
| 3127 | + ADDOP(c, ROT_TWO); | |
| 3128 | + ADDOP(c, POP_TOP); | |
| 3129 | + compiler_use_next_block(c, end); | |
| 3130 | + } | |
| 3131 | + return 1; | |
| 3132 | +} | |
| 3133 | +#undef CMPCAST | |
| 3134 | + | |
| 3135 | +static int | |
| 3136 | +compiler_call(struct compiler *c, expr_ty e) | |
| 3137 | +{ | |
| 3138 | + int n, code = 0; | |
| 3139 | + | |
| 3140 | + VISIT(c, expr, e->v.Call.func); | |
| 3141 | + n = asdl_seq_LEN(e->v.Call.args); | |
| 3142 | + VISIT_SEQ(c, expr, e->v.Call.args); | |
| 3143 | + if (e->v.Call.keywords) { | |
| 3144 | + VISIT_SEQ(c, keyword, e->v.Call.keywords); | |
| 3145 | + n |= asdl_seq_LEN(e->v.Call.keywords) << 8; | |
| 3146 | + } | |
| 3147 | + if (e->v.Call.starargs) { | |
| 3148 | + VISIT(c, expr, e->v.Call.starargs); | |
| 3149 | + code |= 1; | |
| 3150 | + } | |
| 3151 | + if (e->v.Call.kwargs) { | |
| 3152 | + VISIT(c, expr, e->v.Call.kwargs); | |
| 3153 | + code |= 2; | |
| 3154 | + } | |
| 3155 | + switch (code) { | |
| 3156 | + case 0: | |
| 3157 | + ADDOP_I(c, CALL_FUNCTION, n); | |
| 3158 | + break; | |
| 3159 | + case 1: | |
| 3160 | + ADDOP_I(c, CALL_FUNCTION_VAR, n); | |
| 3161 | + break; | |
| 3162 | + case 2: | |
| 3163 | + ADDOP_I(c, CALL_FUNCTION_KW, n); | |
| 3164 | + break; | |
| 3165 | + case 3: | |
| 3166 | + ADDOP_I(c, CALL_FUNCTION_VAR_KW, n); | |
| 3167 | + break; | |
| 3168 | + } | |
| 3169 | + return 1; | |
| 3170 | +} | |
| 3171 | + | |
| 3172 | +static int | |
| 3173 | +compiler_listcomp_generator(struct compiler *c, PyObject *tmpname, | |
| 3174 | + asdl_seq *generators, int gen_index, | |
| 3175 | + expr_ty elt) | |
| 3176 | +{ | |
| 3177 | + /* generate code for the iterator, then each of the ifs, | |
| 3178 | + and then write to the element */ | |
| 3179 | + | |
| 3180 | + comprehension_ty l; | |
| 3181 | + basicblock *start, *anchor, *skip, *if_cleanup; | |
| 3182 | + int i, n; | |
| 3183 | + | |
| 3184 | + start = compiler_new_block(c); | |
| 3185 | + skip = compiler_new_block(c); | |
| 3186 | + if_cleanup = compiler_new_block(c); | |
| 3187 | + anchor = compiler_new_block(c); | |
| 3188 | + | |
| 3189 | + if (start == NULL || skip == NULL || if_cleanup == NULL || | |
| 3190 | + anchor == NULL) | |
| 3191 | + return 0; | |
| 3192 | + | |
| 3193 | + l = (comprehension_ty)asdl_seq_GET(generators, gen_index); | |
| 3194 | + VISIT(c, expr, l->iter); | |
| 3195 | + ADDOP(c, GET_ITER); | |
| 3196 | + compiler_use_next_block(c, start); | |
| 3197 | + ADDOP_JREL(c, FOR_ITER, anchor); | |
| 3198 | + NEXT_BLOCK(c); | |
| 3199 | + VISIT(c, expr, l->target); | |
| 3200 | + | |
| 3201 | + /* XXX this needs to be cleaned up...a lot! */ | |
| 3202 | + n = asdl_seq_LEN(l->ifs); | |
| 3203 | + for (i = 0; i < n; i++) { | |
| 3204 | + expr_ty e = (expr_ty)asdl_seq_GET(l->ifs, i); | |
| 3205 | + VISIT(c, expr, e); | |
| 3206 | + ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup); | |
| 3207 | + NEXT_BLOCK(c); | |
| 3208 | + ADDOP(c, POP_TOP); | |
| 3209 | + } | |
| 3210 | + | |
| 3211 | + if (++gen_index < asdl_seq_LEN(generators)) | |
| 3212 | + if (!compiler_listcomp_generator(c, tmpname, | |
| 3213 | + generators, gen_index, elt)) | |
| 3214 | + return 0; | |
| 3215 | + | |
| 3216 | + /* only append after the last for generator */ | |
| 3217 | + if (gen_index >= asdl_seq_LEN(generators)) { | |
| 3218 | + if (!compiler_nameop(c, tmpname, Load)) | |
| 3219 | + return 0; | |
| 3220 | + VISIT(c, expr, elt); | |
| 3221 | + ADDOP(c, LIST_APPEND); | |
| 3222 | + | |
| 3223 | + compiler_use_next_block(c, skip); | |
| 3224 | + } | |
| 3225 | + for (i = 0; i < n; i++) { | |
| 3226 | + ADDOP_I(c, JUMP_FORWARD, 1); | |
| 3227 | + if (i == 0) | |
| 3228 | + compiler_use_next_block(c, if_cleanup); | |
| 3229 | + ADDOP(c, POP_TOP); | |
| 3230 | + } | |
| 3231 | + ADDOP_JABS(c, JUMP_ABSOLUTE, start); | |
| 3232 | + compiler_use_next_block(c, anchor); | |
| 3233 | + /* delete the append method added to locals */ | |
| 3234 | + if (gen_index == 1) | |
| 3235 | + if (!compiler_nameop(c, tmpname, Del)) | |
| 3236 | + return 0; | |
| 3237 | + | |
| 3238 | + return 1; | |
| 3239 | +} | |
| 3240 | + | |
| 3241 | +static int | |
| 3242 | +compiler_listcomp(struct compiler *c, expr_ty e) | |
| 3243 | +{ | |
| 3244 | + identifier tmp; | |
| 3245 | + int rc = 0; | |
| 3246 | + static identifier append; | |
| 3247 | + asdl_seq *generators = e->v.ListComp.generators; | |
| 3248 | + | |
| 3249 | + assert(e->kind == ListComp_kind); | |
| 3250 | + if (!append) { | |
| 3251 | + append = PyString_InternFromString("append"); | |
| 3252 | + if (!append) | |
| 3253 | + return 0; | |
| 3254 | + } | |
| 3255 | + tmp = compiler_new_tmpname(c); | |
| 3256 | + if (!tmp) | |
| 3257 | + return 0; | |
| 3258 | + ADDOP_I(c, BUILD_LIST, 0); | |
| 3259 | + ADDOP(c, DUP_TOP); | |
| 3260 | + if (compiler_nameop(c, tmp, Store)) | |
| 3261 | + rc = compiler_listcomp_generator(c, tmp, generators, 0, | |
| 3262 | + e->v.ListComp.elt); | |
| 3263 | + Py_DECREF(tmp); | |
| 3264 | + return rc; | |
| 3265 | +} | |
| 3266 | + | |
| 3267 | +static int | |
| 3268 | +compiler_genexp_generator(struct compiler *c, | |
| 3269 | + asdl_seq *generators, int gen_index, | |
| 3270 | + expr_ty elt) | |
| 3271 | +{ | |
| 3272 | + /* generate code for the iterator, then each of the ifs, | |
| 3273 | + and then write to the element */ | |
| 3274 | + | |
| 3275 | + comprehension_ty ge; | |
| 3276 | + basicblock *start, *anchor, *skip, *if_cleanup, *end; | |
| 3277 | + int i, n; | |
| 3278 | + | |
| 3279 | + start = compiler_new_block(c); | |
| 3280 | + skip = compiler_new_block(c); | |
| 3281 | + if_cleanup = compiler_new_block(c); | |
| 3282 | + anchor = compiler_new_block(c); | |
| 3283 | + end = compiler_new_block(c); | |
| 3284 | + | |
| 3285 | + if (start == NULL || skip == NULL || if_cleanup == NULL || | |
| 3286 | + anchor == NULL || end == NULL) | |
| 3287 | + return 0; | |
| 3288 | + | |
| 3289 | + ge = (comprehension_ty)asdl_seq_GET(generators, gen_index); | |
| 3290 | + ADDOP_JREL(c, SETUP_LOOP, end); | |
| 3291 | + if (!compiler_push_fblock(c, LOOP, start)) | |
| 3292 | + return 0; | |
| 3293 | + | |
| 3294 | + if (gen_index == 0) { | |
| 3295 | + /* Receive outermost iter as an implicit argument */ | |
| 3296 | + c->u->u_argcount = 1; | |
| 3297 | + ADDOP_I(c, LOAD_FAST, 0); | |
| 3298 | + } | |
| 3299 | + else { | |
| 3300 | + /* Sub-iter - calculate on the fly */ | |
| 3301 | + VISIT(c, expr, ge->iter); | |
| 3302 | + ADDOP(c, GET_ITER); | |
| 3303 | + } | |
| 3304 | + compiler_use_next_block(c, start); | |
| 3305 | + ADDOP_JREL(c, FOR_ITER, anchor); | |
| 3306 | + NEXT_BLOCK(c); | |
| 3307 | + VISIT(c, expr, ge->target); | |
| 3308 | + | |
| 3309 | + /* XXX this needs to be cleaned up...a lot! */ | |
| 3310 | + n = asdl_seq_LEN(ge->ifs); | |
| 3311 | + for (i = 0; i < n; i++) { | |
| 3312 | + expr_ty e = (expr_ty)asdl_seq_GET(ge->ifs, i); | |
| 3313 | + VISIT(c, expr, e); | |
| 3314 | + ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup); | |
| 3315 | + NEXT_BLOCK(c); | |
| 3316 | + ADDOP(c, POP_TOP); | |
| 3317 | + } | |
| 3318 | + | |
| 3319 | + if (++gen_index < asdl_seq_LEN(generators)) | |
| 3320 | + if (!compiler_genexp_generator(c, generators, gen_index, elt)) | |
| 3321 | + return 0; | |
| 3322 | + | |
| 3323 | + /* only append after the last 'for' generator */ | |
| 3324 | + if (gen_index >= asdl_seq_LEN(generators)) { | |
| 3325 | + VISIT(c, expr, elt); | |
| 3326 | + ADDOP(c, YIELD_VALUE); | |
| 3327 | + ADDOP(c, POP_TOP); | |
| 3328 | + | |
| 3329 | + compiler_use_next_block(c, skip); | |
| 3330 | + } | |
| 3331 | + for (i = 0; i < n; i++) { | |
| 3332 | + ADDOP_I(c, JUMP_FORWARD, 1); | |
| 3333 | + if (i == 0) | |
| 3334 | + compiler_use_next_block(c, if_cleanup); | |
| 3335 | + | |
| 3336 | + ADDOP(c, POP_TOP); | |
| 3337 | + } | |
| 3338 | + ADDOP_JABS(c, JUMP_ABSOLUTE, start); | |
| 3339 | + compiler_use_next_block(c, anchor); | |
| 3340 | + ADDOP(c, POP_BLOCK); | |
| 3341 | + compiler_pop_fblock(c, LOOP, start); | |
| 3342 | + compiler_use_next_block(c, end); | |
| 3343 | + | |
| 3344 | + return 1; | |
| 3345 | +} | |
| 3346 | + | |
| 3347 | +static int | |
| 3348 | +compiler_genexp(struct compiler *c, expr_ty e) | |
| 3349 | +{ | |
| 3350 | + static identifier name; | |
| 3351 | + PyCodeObject *co; | |
| 3352 | + expr_ty outermost_iter = ((comprehension_ty) | |
| 3353 | + (asdl_seq_GET(e->v.GeneratorExp.generators, | |
| 3354 | + 0)))->iter; | |
| 3355 | + | |
| 3356 | + if (!name) { | |
| 3357 | + name = PyString_FromString("<genexpr>"); | |
| 3358 | + if (!name) | |
| 3359 | + return 0; | |
| 3360 | + } | |
| 3361 | + | |
| 3362 | + if (!compiler_enter_scope(c, name, (void *)e, e->lineno)) | |
| 3363 | + return 0; | |
| 3364 | + compiler_genexp_generator(c, e->v.GeneratorExp.generators, 0, | |
| 3365 | + e->v.GeneratorExp.elt); | |
| 3366 | + co = assemble(c, 1); | |
| 3367 | + compiler_exit_scope(c); | |
| 3368 | + if (co == NULL) | |
| 3369 | + return 0; | |
| 3370 | + | |
| 3371 | + compiler_make_closure(c, co, 0); | |
| 3372 | + Py_DECREF(co); | |
| 3373 | + | |
| 3374 | + VISIT(c, expr, outermost_iter); | |
| 3375 | + ADDOP(c, GET_ITER); | |
| 3376 | + ADDOP_I(c, CALL_FUNCTION, 1); | |
| 3377 | + | |
| 3378 | + return 1; | |
| 3379 | +} | |
| 3380 | + | |
| 3381 | +static int | |
| 3382 | +compiler_visit_keyword(struct compiler *c, keyword_ty k) | |
| 3383 | +{ | |
| 3384 | + ADDOP_O(c, LOAD_CONST, k->arg, consts); | |
| 3385 | + VISIT(c, expr, k->value); | |
| 3386 | + return 1; | |
| 3387 | +} | |
| 3388 | + | |
| 3389 | +/* Test whether expression is constant. For constants, report | |
| 3390 | + whether they are true or false. | |
| 3391 | + | |
| 3392 | + Return values: 1 for true, 0 for false, -1 for non-constant. | |
| 3393 | + */ | |
| 3394 | + | |
| 3395 | +static int | |
| 3396 | +expr_constant(expr_ty e) | |
| 3397 | +{ | |
| 3398 | + switch (e->kind) { | |
| 3399 | + case Num_kind: | |
| 3400 | + return PyObject_IsTrue(e->v.Num.n); | |
| 3401 | + case Str_kind: | |
| 3402 | + return PyObject_IsTrue(e->v.Str.s); | |
| 3403 | + case Name_kind: | |
| 3404 | + /* __debug__ is not assignable, so we can optimize | |
| 3405 | + * it away in if and while statements */ | |
| 3406 | + if (strcmp(PyString_AS_STRING(e->v.Name.id), | |
| 3407 | + "__debug__") == 0) | |
| 3408 | + return ! Py_OptimizeFlag; | |
| 3409 | + /* fall through */ | |
| 3410 | + default: | |
| 3411 | + return -1; | |
| 3412 | + } | |
| 3413 | +} | |
| 3414 | + | |
| 3415 | +/* | |
| 3416 | + Implements the with statement from PEP 343. | |
| 3417 | + | |
| 3418 | + The semantics outlined in that PEP are as follows: | |
| 3419 | + | |
| 3420 | + with EXPR as VAR: | |
| 3421 | + BLOCK | |
| 3422 | + | |
| 3423 | + It is implemented roughly as: | |
| 3424 | + | |
| 3425 | + context = EXPR | |
| 3426 | + exit = context.__exit__ # not calling it | |
| 3427 | + value = context.__enter__() | |
| 3428 | + try: | |
| 3429 | + VAR = value # if VAR present in the syntax | |
| 3430 | + BLOCK | |
| 3431 | + finally: | |
| 3432 | + if an exception was raised: | |
| 3433 | + exc = copy of (exception, instance, traceback) | |
| 3434 | + else: | |
| 3435 | + exc = (None, None, None) | |
| 3436 | + exit(*exc) | |
| 3437 | + */ | |
| 3438 | +static int | |
| 3439 | +compiler_with(struct compiler *c, stmt_ty s) | |
| 3440 | +{ | |
| 3441 | + static identifier enter_attr, exit_attr; | |
| 3442 | + basicblock *block, *finally; | |
| 3443 | + identifier tmpexit, tmpvalue = NULL; | |
| 3444 | + | |
| 3445 | + assert(s->kind == With_kind); | |
| 3446 | + | |
| 3447 | + if (!enter_attr) { | |
| 3448 | + enter_attr = PyString_InternFromString("__enter__"); | |
| 3449 | + if (!enter_attr) | |
| 3450 | + return 0; | |
| 3451 | + } | |
| 3452 | + if (!exit_attr) { | |
| 3453 | + exit_attr = PyString_InternFromString("__exit__"); | |
| 3454 | + if (!exit_attr) | |
| 3455 | + return 0; | |
| 3456 | + } | |
| 3457 | + | |
| 3458 | + block = compiler_new_block(c); | |
| 3459 | + finally = compiler_new_block(c); | |
| 3460 | + if (!block || !finally) | |
| 3461 | + return 0; | |
| 3462 | + | |
| 3463 | + /* Create a temporary variable to hold context.__exit__ */ | |
| 3464 | + tmpexit = compiler_new_tmpname(c); | |
| 3465 | + if (tmpexit == NULL) | |
| 3466 | + return 0; | |
| 3467 | + PyArena_AddPyObject(c->c_arena, tmpexit); | |
| 3468 | + | |
| 3469 | + if (s->v.With.optional_vars) { | |
| 3470 | + /* Create a temporary variable to hold context.__enter__(). | |
| 3471 | + We need to do this rather than preserving it on the stack | |
| 3472 | + because SETUP_FINALLY remembers the stack level. | |
| 3473 | + We need to do the assignment *inside* the try/finally | |
| 3474 | + so that context.__exit__() is called when the assignment | |
| 3475 | + fails. But we need to call context.__enter__() *before* | |
| 3476 | + the try/finally so that if it fails we won't call | |
| 3477 | + context.__exit__(). | |
| 3478 | + */ | |
| 3479 | + tmpvalue = compiler_new_tmpname(c); | |
| 3480 | + if (tmpvalue == NULL) | |
| 3481 | + return 0; | |
| 3482 | + PyArena_AddPyObject(c->c_arena, tmpvalue); | |
| 3483 | + } | |
| 3484 | + | |
| 3485 | + /* Evaluate EXPR */ | |
| 3486 | + VISIT(c, expr, s->v.With.context_expr); | |
| 3487 | + | |
| 3488 | + /* Squirrel away context.__exit__ */ | |
| 3489 | + ADDOP(c, DUP_TOP); | |
| 3490 | + ADDOP_O(c, LOAD_ATTR, exit_attr, names); | |
| 3491 | + if (!compiler_nameop(c, tmpexit, Store)) | |
| 3492 | + return 0; | |
| 3493 | + | |
| 3494 | + /* Call context.__enter__() */ | |
| 3495 | + ADDOP_O(c, LOAD_ATTR, enter_attr, names); | |
| 3496 | + ADDOP_I(c, CALL_FUNCTION, 0); | |
| 3497 | + | |
| 3498 | + if (s->v.With.optional_vars) { | |
| 3499 | + /* Store it in tmpvalue */ | |
| 3500 | + if (!compiler_nameop(c, tmpvalue, Store)) | |
| 3501 | + return 0; | |
| 3502 | + } | |
| 3503 | + else { | |
| 3504 | + /* Discard result from context.__enter__() */ | |
| 3505 | + ADDOP(c, POP_TOP); | |
| 3506 | + } | |
| 3507 | + | |
| 3508 | + /* Start the try block */ | |
| 3509 | + ADDOP_JREL(c, SETUP_FINALLY, finally); | |
| 3510 | + | |
| 3511 | + compiler_use_next_block(c, block); | |
| 3512 | + if (!compiler_push_fblock(c, FINALLY_TRY, block)) { | |
| 3513 | + return 0; | |
| 3514 | + } | |
| 3515 | + | |
| 3516 | + if (s->v.With.optional_vars) { | |
| 3517 | + /* Bind saved result of context.__enter__() to VAR */ | |
| 3518 | + if (!compiler_nameop(c, tmpvalue, Load) || | |
| 3519 | + !compiler_nameop(c, tmpvalue, Del)) | |
| 3520 | + return 0; | |
| 3521 | + VISIT(c, expr, s->v.With.optional_vars); | |
| 3522 | + } | |
| 3523 | + | |
| 3524 | + /* BLOCK code */ | |
| 3525 | + VISIT_SEQ(c, stmt, s->v.With.body); | |
| 3526 | + | |
| 3527 | + /* End of try block; start the finally block */ | |
| 3528 | + ADDOP(c, POP_BLOCK); | |
| 3529 | + compiler_pop_fblock(c, FINALLY_TRY, block); | |
| 3530 | + | |
| 3531 | + ADDOP_O(c, LOAD_CONST, Py_None, consts); | |
| 3532 | + compiler_use_next_block(c, finally); | |
| 3533 | + if (!compiler_push_fblock(c, FINALLY_END, finally)) | |
| 3534 | + return 0; | |
| 3535 | + | |
| 3536 | + /* Finally block starts; push tmpexit and issue our magic opcode. */ | |
| 3537 | + if (!compiler_nameop(c, tmpexit, Load) || | |
| 3538 | + !compiler_nameop(c, tmpexit, Del)) | |
| 3539 | + return 0; | |
| 3540 | + ADDOP(c, WITH_CLEANUP); | |
| 3541 | + | |
| 3542 | + /* Finally block ends. */ | |
| 3543 | + ADDOP(c, END_FINALLY); | |
| 3544 | + compiler_pop_fblock(c, FINALLY_END, finally); | |
| 3545 | + return 1; | |
| 3546 | +} | |
| 3547 | + | |
| 3548 | +static int | |
| 3549 | +compiler_visit_expr(struct compiler *c, expr_ty e) | |
| 3550 | +{ | |
| 3551 | + int i, n; | |
| 3552 | + | |
| 3553 | + /* If expr e has a different line number than the last expr/stmt, | |
| 3554 | + set a new line number for the next instruction. | |
| 3555 | + */ | |
| 3556 | + if (e->lineno > c->u->u_lineno) { | |
| 3557 | + c->u->u_lineno = e->lineno; | |
| 3558 | + c->u->u_lineno_set = false; | |
| 3559 | + } | |
| 3560 | + switch (e->kind) { | |
| 3561 | + case BoolOp_kind: | |
| 3562 | + return compiler_boolop(c, e); | |
| 3563 | + case BinOp_kind: | |
| 3564 | + VISIT(c, expr, e->v.BinOp.left); | |
| 3565 | + VISIT(c, expr, e->v.BinOp.right); | |
| 3566 | + ADDOP(c, binop(c, e->v.BinOp.op)); | |
| 3567 | + break; | |
| 3568 | + case UnaryOp_kind: | |
| 3569 | + VISIT(c, expr, e->v.UnaryOp.operand); | |
| 3570 | + ADDOP(c, unaryop(e->v.UnaryOp.op)); | |
| 3571 | + break; | |
| 3572 | + case Lambda_kind: | |
| 3573 | + return compiler_lambda(c, e); | |
| 3574 | + case IfExp_kind: | |
| 3575 | + return compiler_ifexp(c, e); | |
| 3576 | + case Dict_kind: | |
| 3577 | + /* XXX get rid of arg? */ | |
| 3578 | + ADDOP_I(c, BUILD_MAP, 0); | |
| 3579 | + n = asdl_seq_LEN(e->v.Dict.values); | |
| 3580 | + /* We must arrange things just right for STORE_SUBSCR. | |
| 3581 | + It wants the stack to look like (value) (dict) (key) */ | |
| 3582 | + for (i = 0; i < n; i++) { | |
| 3583 | + ADDOP(c, DUP_TOP); | |
| 3584 | + VISIT(c, expr, | |
| 3585 | + (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); | |
| 3586 | + ADDOP(c, ROT_TWO); | |
| 3587 | + VISIT(c, expr, | |
| 3588 | + (expr_ty)asdl_seq_GET(e->v.Dict.keys, i)); | |
| 3589 | + ADDOP(c, STORE_SUBSCR); | |
| 3590 | + } | |
| 3591 | + break; | |
| 3592 | + case ListComp_kind: | |
| 3593 | + return compiler_listcomp(c, e); | |
| 3594 | + case GeneratorExp_kind: | |
| 3595 | + return compiler_genexp(c, e); | |
| 3596 | + case Yield_kind: | |
| 3597 | + if (c->u->u_ste->ste_type != FunctionBlock) | |
| 3598 | + return compiler_error(c, "'yield' outside function"); | |
| 3599 | + /* | |
| 3600 | + for (i = 0; i < c->u->u_nfblocks; i++) { | |
| 3601 | + if (c->u->u_fblock[i].fb_type == FINALLY_TRY) | |
| 3602 | + return compiler_error( | |
| 3603 | + c, "'yield' not allowed in a 'try' " | |
| 3604 | + "block with a 'finally' clause"); | |
| 3605 | + } | |
| 3606 | + */ | |
| 3607 | + if (e->v.Yield.value) { | |
| 3608 | + VISIT(c, expr, e->v.Yield.value); | |
| 3609 | + } | |
| 3610 | + else { | |
| 3611 | + ADDOP_O(c, LOAD_CONST, Py_None, consts); | |
| 3612 | + } | |
| 3613 | + ADDOP(c, YIELD_VALUE); | |
| 3614 | + break; | |
| 3615 | + case Compare_kind: | |
| 3616 | + return compiler_compare(c, e); | |
| 3617 | + case Call_kind: | |
| 3618 | + return compiler_call(c, e); | |
| 3619 | + case Repr_kind: | |
| 3620 | + VISIT(c, expr, e->v.Repr.value); | |
| 3621 | + ADDOP(c, UNARY_CONVERT); | |
| 3622 | + break; | |
| 3623 | + case Num_kind: | |
| 3624 | + ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts); | |
| 3625 | + break; | |
| 3626 | + case Str_kind: | |
| 3627 | + ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts); | |
| 3628 | + break; | |
| 3629 | + /* The following exprs can be assignment targets. */ | |
| 3630 | + case Attribute_kind: | |
| 3631 | + if (e->v.Attribute.ctx != AugStore) | |
| 3632 | + VISIT(c, expr, e->v.Attribute.value); | |
| 3633 | + switch (e->v.Attribute.ctx) { | |
| 3634 | + case AugLoad: | |
| 3635 | + ADDOP(c, DUP_TOP); | |
| 3636 | + /* Fall through to load */ | |
| 3637 | + case Load: | |
| 3638 | + ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names); | |
| 3639 | + break; | |
| 3640 | + case AugStore: | |
| 3641 | + ADDOP(c, ROT_TWO); | |
| 3642 | + /* Fall through to save */ | |
| 3643 | + case Store: | |
| 3644 | + ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names); | |
| 3645 | + break; | |
| 3646 | + case Del: | |
| 3647 | + ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names); | |
| 3648 | + break; | |
| 3649 | + case Param: | |
| 3650 | + default: | |
| 3651 | + PyErr_SetString(PyExc_SystemError, | |
| 3652 | + "param invalid in attribute expression"); | |
| 3653 | + return 0; | |
| 3654 | + } | |
| 3655 | + break; | |
| 3656 | + case Subscript_kind: | |
| 3657 | + switch (e->v.Subscript.ctx) { | |
| 3658 | + case AugLoad: | |
| 3659 | + VISIT(c, expr, e->v.Subscript.value); | |
| 3660 | + VISIT_SLICE(c, e->v.Subscript.slice, AugLoad); | |
| 3661 | + break; | |
| 3662 | + case Load: | |
| 3663 | + VISIT(c, expr, e->v.Subscript.value); | |
| 3664 | + VISIT_SLICE(c, e->v.Subscript.slice, Load); | |
| 3665 | + break; | |
| 3666 | + case AugStore: | |
| 3667 | + VISIT_SLICE(c, e->v.Subscript.slice, AugStore); | |
| 3668 | + break; | |
| 3669 | + case Store: | |
| 3670 | + VISIT(c, expr, e->v.Subscript.value); | |
| 3671 | + VISIT_SLICE(c, e->v.Subscript.slice, Store); | |
| 3672 | + break; | |
| 3673 | + case Del: | |
| 3674 | + VISIT(c, expr, e->v.Subscript.value); | |
| 3675 | + VISIT_SLICE(c, e->v.Subscript.slice, Del); | |
| 3676 | + break; | |
| 3677 | + case Param: | |
| 3678 | + default: | |
| 3679 | + PyErr_SetString(PyExc_SystemError, | |
| 3680 | + "param invalid in subscript expression"); | |
| 3681 | + return 0; | |
| 3682 | + } | |
| 3683 | + break; | |
| 3684 | + case Name_kind: | |
| 3685 | + return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx); | |
| 3686 | + /* child nodes of List and Tuple will have expr_context set */ | |
| 3687 | + case List_kind: | |
| 3688 | + return compiler_list(c, e); | |
| 3689 | + case Tuple_kind: | |
| 3690 | + return compiler_tuple(c, e); | |
| 3691 | + } | |
| 3692 | + return 1; | |
| 3693 | +} | |
| 3694 | + | |
| 3695 | +static int | |
| 3696 | +compiler_augassign(struct compiler *c, stmt_ty s) | |
| 3697 | +{ | |
| 3698 | + expr_ty e = s->v.AugAssign.target; | |
| 3699 | + expr_ty auge; | |
| 3700 | + | |
| 3701 | + assert(s->kind == AugAssign_kind); | |
| 3702 | + | |
| 3703 | + switch (e->kind) { | |
| 3704 | + case Attribute_kind: | |
| 3705 | + auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr, | |
| 3706 | + AugLoad, e->lineno, e->col_offset, c->c_arena); | |
| 3707 | + if (auge == NULL) | |
| 3708 | + return 0; | |
| 3709 | + VISIT(c, expr, auge); | |
| 3710 | + VISIT(c, expr, s->v.AugAssign.value); | |
| 3711 | + ADDOP(c, inplace_binop(c, s->v.AugAssign.op)); | |
| 3712 | + auge->v.Attribute.ctx = AugStore; | |
| 3713 | + VISIT(c, expr, auge); | |
| 3714 | + break; | |
| 3715 | + case Subscript_kind: | |
| 3716 | + auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice, | |
| 3717 | + AugLoad, e->lineno, e->col_offset, c->c_arena); | |
| 3718 | + if (auge == NULL) | |
| 3719 | + return 0; | |
| 3720 | + VISIT(c, expr, auge); | |
| 3721 | + VISIT(c, expr, s->v.AugAssign.value); | |
| 3722 | + ADDOP(c, inplace_binop(c, s->v.AugAssign.op)); | |
| 3723 | + auge->v.Subscript.ctx = AugStore; | |
| 3724 | + VISIT(c, expr, auge); | |
| 3725 | + break; | |
| 3726 | + case Name_kind: | |
| 3727 | + if (!compiler_nameop(c, e->v.Name.id, Load)) | |
| 3728 | + return 0; | |
| 3729 | + VISIT(c, expr, s->v.AugAssign.value); | |
| 3730 | + ADDOP(c, inplace_binop(c, s->v.AugAssign.op)); | |
| 3731 | + return compiler_nameop(c, e->v.Name.id, Store); | |
| 3732 | + default: | |
| 3733 | + PyErr_Format(PyExc_SystemError, | |
| 3734 | + "invalid node type (%d) for augmented assignment", | |
| 3735 | + e->kind); | |
| 3736 | + return 0; | |
| 3737 | + } | |
| 3738 | + return 1; | |
| 3739 | +} | |
| 3740 | + | |
| 3741 | +static int | |
| 3742 | +compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b) | |
| 3743 | +{ | |
| 3744 | + struct fblockinfo *f; | |
| 3745 | + if (c->u->u_nfblocks >= CO_MAXBLOCKS) { | |
| 3746 | + PyErr_SetString(PyExc_SystemError, | |
| 3747 | + "too many statically nested blocks"); | |
| 3748 | + return 0; | |
| 3749 | + } | |
| 3750 | + f = &c->u->u_fblock[c->u->u_nfblocks++]; | |
| 3751 | + f->fb_type = t; | |
| 3752 | + f->fb_block = b; | |
| 3753 | + return 1; | |
| 3754 | +} | |
| 3755 | + | |
| 3756 | +static void | |
| 3757 | +compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b) | |
| 3758 | +{ | |
| 3759 | + struct compiler_unit *u = c->u; | |
| 3760 | + assert(u->u_nfblocks > 0); | |
| 3761 | + u->u_nfblocks--; | |
| 3762 | + assert(u->u_fblock[u->u_nfblocks].fb_type == t); | |
| 3763 | + assert(u->u_fblock[u->u_nfblocks].fb_block == b); | |
| 3764 | +} | |
| 3765 | + | |
| 3766 | +static int | |
| 3767 | +compiler_in_loop(struct compiler *c) { | |
| 3768 | + int i; | |
| 3769 | + struct compiler_unit *u = c->u; | |
| 3770 | + for (i = 0; i < u->u_nfblocks; ++i) { | |
| 3771 | + if (u->u_fblock[i].fb_type == LOOP) | |
| 3772 | + return 1; | |
| 3773 | + } | |
| 3774 | + return 0; | |
| 3775 | +} | |
| 3776 | +/* Raises a SyntaxError and returns 0. | |
| 3777 | + If something goes wrong, a different exception may be raised. | |
| 3778 | +*/ | |
| 3779 | + | |
| 3780 | +static int | |
| 3781 | +compiler_error(struct compiler *c, const char *errstr) | |
| 3782 | +{ | |
| 3783 | + PyObject *loc; | |
| 3784 | + PyObject *u = NULL, *v = NULL; | |
| 3785 | + | |
| 3786 | + loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno); | |
| 3787 | + if (!loc) { | |
| 3788 | + Py_INCREF(Py_None); | |
| 3789 | + loc = Py_None; | |
| 3790 | + } | |
| 3791 | + u = Py_BuildValue("(ziOO)", c->c_filename, c->u->u_lineno, | |
| 3792 | + Py_None, loc); | |
| 3793 | + if (!u) | |
| 3794 | + goto exit; | |
| 3795 | + v = Py_BuildValue("(zO)", errstr, u); | |
| 3796 | + if (!v) | |
| 3797 | + goto exit; | |
| 3798 | + PyErr_SetObject(PyExc_SyntaxError, v); | |
| 3799 | + exit: | |
| 3800 | + Py_DECREF(loc); | |
| 3801 | + Py_XDECREF(u); | |
| 3802 | + Py_XDECREF(v); | |
| 3803 | + return 0; | |
| 3804 | +} | |
| 3805 | + | |
| 3806 | +static int | |
| 3807 | +compiler_handle_subscr(struct compiler *c, const char *kind, | |
| 3808 | + expr_context_ty ctx) | |
| 3809 | +{ | |
| 3810 | + int op = 0; | |
| 3811 | + | |
| 3812 | + /* XXX this code is duplicated */ | |
| 3813 | + switch (ctx) { | |
| 3814 | + case AugLoad: /* fall through to Load */ | |
| 3815 | + case Load: op = BINARY_SUBSCR; break; | |
| 3816 | + case AugStore:/* fall through to Store */ | |
| 3817 | + case Store: op = STORE_SUBSCR; break; | |
| 3818 | + case Del: op = DELETE_SUBSCR; break; | |
| 3819 | + case Param: | |
| 3820 | + PyErr_Format(PyExc_SystemError, | |
| 3821 | + "invalid %s kind %d in subscript\n", | |
| 3822 | + kind, ctx); | |
| 3823 | + return 0; | |
| 3824 | + } | |
| 3825 | + if (ctx == AugLoad) { | |
| 3826 | + ADDOP_I(c, DUP_TOPX, 2); | |
| 3827 | + } | |
| 3828 | + else if (ctx == AugStore) { | |
| 3829 | + ADDOP(c, ROT_THREE); | |
| 3830 | + } | |
| 3831 | + ADDOP(c, op); | |
| 3832 | + return 1; | |
| 3833 | +} | |
| 3834 | + | |
| 3835 | +static int | |
| 3836 | +compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx) | |
| 3837 | +{ | |
| 3838 | + int n = 2; | |
| 3839 | + assert(s->kind == Slice_kind); | |
| 3840 | + | |
| 3841 | + /* only handles the cases where BUILD_SLICE is emitted */ | |
| 3842 | + if (s->v.Slice.lower) { | |
| 3843 | + VISIT(c, expr, s->v.Slice.lower); | |
| 3844 | + } | |
| 3845 | + else { | |
| 3846 | + ADDOP_O(c, LOAD_CONST, Py_None, consts); | |
| 3847 | + } | |
| 3848 | + | |
| 3849 | + if (s->v.Slice.upper) { | |
| 3850 | + VISIT(c, expr, s->v.Slice.upper); | |
| 3851 | + } | |
| 3852 | + else { | |
| 3853 | + ADDOP_O(c, LOAD_CONST, Py_None, consts); | |
| 3854 | + } | |
| 3855 | + | |
| 3856 | + if (s->v.Slice.step) { | |
| 3857 | + n++; | |
| 3858 | + VISIT(c, expr, s->v.Slice.step); | |
| 3859 | + } | |
| 3860 | + ADDOP_I(c, BUILD_SLICE, n); | |
| 3861 | + return 1; | |
| 3862 | +} | |
| 3863 | + | |
| 3864 | +static int | |
| 3865 | +compiler_simple_slice(struct compiler *c, slice_ty s, expr_context_ty ctx) | |
| 3866 | +{ | |
| 3867 | + int op = 0, slice_offset = 0, stack_count = 0; | |
| 3868 | + | |
| 3869 | + assert(s->v.Slice.step == NULL); | |
| 3870 | + if (s->v.Slice.lower) { | |
| 3871 | + slice_offset++; | |
| 3872 | + stack_count++; | |
| 3873 | + if (ctx != AugStore) | |
| 3874 | + VISIT(c, expr, s->v.Slice.lower); | |
| 3875 | + } | |
| 3876 | + if (s->v.Slice.upper) { | |
| 3877 | + slice_offset += 2; | |
| 3878 | + stack_count++; | |
| 3879 | + if (ctx != AugStore) | |
| 3880 | + VISIT(c, expr, s->v.Slice.upper); | |
| 3881 | + } | |
| 3882 | + | |
| 3883 | + if (ctx == AugLoad) { | |
| 3884 | + switch (stack_count) { | |
| 3885 | + case 0: ADDOP(c, DUP_TOP); break; | |
| 3886 | + case 1: ADDOP_I(c, DUP_TOPX, 2); break; | |
| 3887 | + case 2: ADDOP_I(c, DUP_TOPX, 3); break; | |
| 3888 | + } | |
| 3889 | + } | |
| 3890 | + else if (ctx == AugStore) { | |
| 3891 | + switch (stack_count) { | |
| 3892 | + case 0: ADDOP(c, ROT_TWO); break; | |
| 3893 | + case 1: ADDOP(c, ROT_THREE); break; | |
| 3894 | + case 2: ADDOP(c, ROT_FOUR); break; | |
| 3895 | + } | |
| 3896 | + } | |
| 3897 | + | |
| 3898 | + switch (ctx) { | |
| 3899 | + case AugLoad: /* fall through to Load */ | |
| 3900 | + case Load: op = SLICE; break; | |
| 3901 | + case AugStore:/* fall through to Store */ | |
| 3902 | + case Store: op = STORE_SLICE; break; | |
| 3903 | + case Del: op = DELETE_SLICE; break; | |
| 3904 | + case Param: | |
| 3905 | + default: | |
| 3906 | + PyErr_SetString(PyExc_SystemError, | |
| 3907 | + "param invalid in simple slice"); | |
| 3908 | + return 0; | |
| 3909 | + } | |
| 3910 | + | |
| 3911 | + ADDOP(c, op + slice_offset); | |
| 3912 | + return 1; | |
| 3913 | +} | |
| 3914 | + | |
| 3915 | +static int | |
| 3916 | +compiler_visit_nested_slice(struct compiler *c, slice_ty s, | |
| 3917 | + expr_context_ty ctx) | |
| 3918 | +{ | |
| 3919 | + switch (s->kind) { | |
| 3920 | + case Ellipsis_kind: | |
| 3921 | + ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts); | |
| 3922 | + break; | |
| 3923 | + case Slice_kind: | |
| 3924 | + return compiler_slice(c, s, ctx); | |
| 3925 | + case Index_kind: | |
| 3926 | + VISIT(c, expr, s->v.Index.value); | |
| 3927 | + break; | |
| 3928 | + case ExtSlice_kind: | |
| 3929 | + default: | |
| 3930 | + PyErr_SetString(PyExc_SystemError, | |
| 3931 | + "extended slice invalid in nested slice"); | |
| 3932 | + return 0; | |
| 3933 | + } | |
| 3934 | + return 1; | |
| 3935 | +} | |
| 3936 | + | |
| 3937 | + | |
| 3938 | +static int | |
| 3939 | +compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx) | |
| 3940 | +{ | |
| 3941 | + char * kindname = NULL; | |
| 3942 | + switch (s->kind) { | |
| 3943 | + case Index_kind: | |
| 3944 | + kindname = "index"; | |
| 3945 | + if (ctx != AugStore) { | |
| 3946 | + VISIT(c, expr, s->v.Index.value); | |
| 3947 | + } | |
| 3948 | + break; | |
| 3949 | + case Ellipsis_kind: | |
| 3950 | + kindname = "ellipsis"; | |
| 3951 | + if (ctx != AugStore) { | |
| 3952 | + ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts); | |
| 3953 | + } | |
| 3954 | + break; | |
| 3955 | + case Slice_kind: | |
| 3956 | + kindname = "slice"; | |
| 3957 | + if (!s->v.Slice.step) | |
| 3958 | + return compiler_simple_slice(c, s, ctx); | |
| 3959 | + if (ctx != AugStore) { | |
| 3960 | + if (!compiler_slice(c, s, ctx)) | |
| 3961 | + return 0; | |
| 3962 | + } | |
| 3963 | + break; | |
| 3964 | + case ExtSlice_kind: | |
| 3965 | + kindname = "extended slice"; | |
| 3966 | + if (ctx != AugStore) { | |
| 3967 | + int i, n = asdl_seq_LEN(s->v.ExtSlice.dims); | |
| 3968 | + for (i = 0; i < n; i++) { | |
| 3969 | + slice_ty sub = (slice_ty)asdl_seq_GET( | |
| 3970 | + s->v.ExtSlice.dims, i); | |
| 3971 | + if (!compiler_visit_nested_slice(c, sub, ctx)) | |
| 3972 | + return 0; | |
| 3973 | + } | |
| 3974 | + ADDOP_I(c, BUILD_TUPLE, n); | |
| 3975 | + } | |
| 3976 | + break; | |
| 3977 | + default: | |
| 3978 | + PyErr_Format(PyExc_SystemError, | |
| 3979 | + "invalid subscript kind %d", s->kind); | |
| 3980 | + return 0; | |
| 3981 | + } | |
| 3982 | + return compiler_handle_subscr(c, kindname, ctx); | |
| 3983 | +} | |
| 3984 | + | |
| 3985 | +/* do depth-first search of basic block graph, starting with block. | |
| 3986 | + post records the block indices in post-order. | |
| 3987 | + | |
| 3988 | + XXX must handle implicit jumps from one block to next | |
| 3989 | +*/ | |
| 3990 | + | |
| 3991 | +static void | |
| 3992 | +dfs(struct compiler *c, basicblock *b, struct assembler *a) | |
| 3993 | +{ | |
| 3994 | + int i; | |
| 3995 | + struct instr *instr = NULL; | |
| 3996 | + | |
| 3997 | + if (b->b_seen) | |
| 3998 | + return; | |
| 3999 | + b->b_seen = 1; | |
| 4000 | + if (b->b_next != NULL) | |
| 4001 | + dfs(c, b->b_next, a); | |
| 4002 | + for (i = 0; i < b->b_iused; i++) { | |
| 4003 | + instr = &b->b_instr[i]; | |
| 4004 | + if (instr->i_jrel || instr->i_jabs) | |
| 4005 | + dfs(c, instr->i_target, a); | |
| 4006 | + } | |
| 4007 | + a->a_postorder[a->a_nblocks++] = b; | |
| 4008 | +} | |
| 4009 | + | |
| 4010 | +static int | |
| 4011 | +stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth) | |
| 4012 | +{ | |
| 4013 | + int i; | |
| 4014 | + struct instr *instr; | |
| 4015 | + if (b->b_seen || b->b_startdepth >= depth) | |
| 4016 | + return maxdepth; | |
| 4017 | + b->b_seen = 1; | |
| 4018 | + b->b_startdepth = depth; | |
| 4019 | + for (i = 0; i < b->b_iused; i++) { | |
| 4020 | + instr = &b->b_instr[i]; | |
| 4021 | + depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg); | |
| 4022 | + if (depth > maxdepth) | |
| 4023 | + maxdepth = depth; | |
| 4024 | + assert(depth >= 0); /* invalid code or bug in stackdepth() */ | |
| 4025 | + if (instr->i_jrel || instr->i_jabs) { | |
| 4026 | + maxdepth = stackdepth_walk(c, instr->i_target, | |
| 4027 | + depth, maxdepth); | |
| 4028 | + if (instr->i_opcode == JUMP_ABSOLUTE || | |
| 4029 | + instr->i_opcode == JUMP_FORWARD) { | |
| 4030 | + goto out; /* remaining code is dead */ | |
| 4031 | + } | |
| 4032 | + } | |
| 4033 | + } | |
| 4034 | + if (b->b_next) | |
| 4035 | + maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth); | |
| 4036 | +out: | |
| 4037 | + b->b_seen = 0; | |
| 4038 | + return maxdepth; | |
| 4039 | +} | |
| 4040 | + | |
| 4041 | +/* Find the flow path that needs the largest stack. We assume that | |
| 4042 | + * cycles in the flow graph have no net effect on the stack depth. | |
| 4043 | + */ | |
| 4044 | +static int | |
| 4045 | +stackdepth(struct compiler *c) | |
| 4046 | +{ | |
| 4047 | + basicblock *b, *entryblock; | |
| 4048 | + entryblock = NULL; | |
| 4049 | + for (b = c->u->u_blocks; b != NULL; b = b->b_list) { | |
| 4050 | + b->b_seen = 0; | |
| 4051 | + b->b_startdepth = INT_MIN; | |
| 4052 | + entryblock = b; | |
| 4053 | + } | |
| 4054 | + if (!entryblock) | |
| 4055 | + return 0; | |
| 4056 | + return stackdepth_walk(c, entryblock, 0, 0); | |
| 4057 | +} | |
| 4058 | + | |
| 4059 | +static int | |
| 4060 | +assemble_init(struct assembler *a, int nblocks, int firstlineno) | |
| 4061 | +{ | |
| 4062 | + memset(a, 0, sizeof(struct assembler)); | |
| 4063 | + a->a_lineno = firstlineno; | |
| 4064 | + a->a_bytecode = PyString_FromStringAndSize(NULL, DEFAULT_CODE_SIZE); | |
| 4065 | + if (!a->a_bytecode) | |
| 4066 | + return 0; | |
| 4067 | + a->a_lnotab = PyString_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE); | |
| 4068 | + if (!a->a_lnotab) | |
| 4069 | + return 0; | |
| 4070 | + a->a_postorder = (basicblock **)PyObject_Malloc( | |
| 4071 | + sizeof(basicblock *) * nblocks); | |
| 4072 | + if (!a->a_postorder) { | |
| 4073 | + PyErr_NoMemory(); | |
| 4074 | + return 0; | |
| 4075 | + } | |
| 4076 | + return 1; | |
| 4077 | +} | |
| 4078 | + | |
| 4079 | +static void | |
| 4080 | +assemble_free(struct assembler *a) | |
| 4081 | +{ | |
| 4082 | + Py_XDECREF(a->a_bytecode); | |
| 4083 | + Py_XDECREF(a->a_lnotab); | |
| 4084 | + if (a->a_postorder) | |
| 4085 | + PyObject_Free(a->a_postorder); | |
| 4086 | +} | |
| 4087 | + | |
| 4088 | +/* Return the size of a basic block in bytes. */ | |
| 4089 | + | |
| 4090 | +static int | |
| 4091 | +instrsize(struct instr *instr) | |
| 4092 | +{ | |
| 4093 | + if (!instr->i_hasarg) | |
| 4094 | + return 1; | |
| 4095 | + if (instr->i_oparg > 0xffff) | |
| 4096 | + return 6; | |
| 4097 | + return 3; | |
| 4098 | +} | |
| 4099 | + | |
| 4100 | +static int | |
| 4101 | +blocksize(basicblock *b) | |
| 4102 | +{ | |
| 4103 | + int i; | |
| 4104 | + int size = 0; | |
| 4105 | + | |
| 4106 | + for (i = 0; i < b->b_iused; i++) | |
| 4107 | + size += instrsize(&b->b_instr[i]); | |
| 4108 | + return size; | |
| 4109 | +} | |
| 4110 | + | |
| 4111 | +/* All about a_lnotab. | |
| 4112 | + | |
| 4113 | +c_lnotab is an array of unsigned bytes disguised as a Python string. | |
| 4114 | +It is used to map bytecode offsets to source code line #s (when needed | |
| 4115 | +for tracebacks). | |
| 4116 | + | |
| 4117 | +The array is conceptually a list of | |
| 4118 | + (bytecode offset increment, line number increment) | |
| 4119 | +pairs. The details are important and delicate, best illustrated by example: | |
| 4120 | + | |
| 4121 | + byte code offset source code line number | |
| 4122 | + 0 1 | |
| 4123 | + 6 2 | |
| 4124 | + 50 7 | |
| 4125 | + 350 307 | |
| 4126 | + 361 308 | |
| 4127 | + | |
| 4128 | +The first trick is that these numbers aren't stored, only the increments | |
| 4129 | +from one row to the next (this doesn't really work, but it's a start): | |
| 4130 | + | |
| 4131 | + 0, 1, 6, 1, 44, 5, 300, 300, 11, 1 | |
| 4132 | + | |
| 4133 | +The second trick is that an unsigned byte can't hold negative values, or | |
| 4134 | +values larger than 255, so (a) there's a deep assumption that byte code | |
| 4135 | +offsets and their corresponding line #s both increase monotonically, and (b) | |
| 4136 | +if at least one column jumps by more than 255 from one row to the next, more | |
| 4137 | +than one pair is written to the table. In case #b, there's no way to know | |
| 4138 | +from looking at the table later how many were written. That's the delicate | |
| 4139 | +part. A user of c_lnotab desiring to find the source line number | |
| 4140 | +corresponding to a bytecode address A should do something like this | |
| 4141 | + | |
| 4142 | + lineno = addr = 0 | |
| 4143 | + for addr_incr, line_incr in c_lnotab: | |
| 4144 | + addr += addr_incr | |
| 4145 | + if addr > A: | |
| 4146 | + return lineno | |
| 4147 | + lineno += line_incr | |
| 4148 | + | |
| 4149 | +In order for this to work, when the addr field increments by more than 255, | |
| 4150 | +the line # increment in each pair generated must be 0 until the remaining addr | |
| 4151 | +increment is < 256. So, in the example above, assemble_lnotab (it used | |
| 4152 | +to be called com_set_lineno) should not (as was actually done until 2.2) | |
| 4153 | +expand 300, 300 to 255, 255, 45, 45, | |
| 4154 | + but to 255, 0, 45, 255, 0, 45. | |
| 4155 | +*/ | |
| 4156 | + | |
| 4157 | +static int | |
| 4158 | +assemble_lnotab(struct assembler *a, struct instr *i) | |
| 4159 | +{ | |
| 4160 | + int d_bytecode, d_lineno; | |
| 4161 | + int len; | |
| 4162 | + unsigned char *lnotab; | |
| 4163 | + | |
| 4164 | + d_bytecode = a->a_offset - a->a_lineno_off; | |
| 4165 | + d_lineno = i->i_lineno - a->a_lineno; | |
| 4166 | + | |
| 4167 | + assert(d_bytecode >= 0); | |
| 4168 | + assert(d_lineno >= 0); | |
| 4169 | + | |
| 4170 | + /* XXX(nnorwitz): is there a better way to handle this? | |
| 4171 | + for loops are special, we want to be able to trace them | |
| 4172 | + each time around, so we need to set an extra line number. */ | |
| 4173 | + if (d_lineno == 0 && i->i_opcode != FOR_ITER) | |
| 4174 | + return 1; | |
| 4175 | + | |
| 4176 | + if (d_bytecode > 255) { | |
| 4177 | + int j, nbytes, ncodes = d_bytecode / 255; | |
| 4178 | + nbytes = a->a_lnotab_off + 2 * ncodes; | |
| 4179 | + len = PyString_GET_SIZE(a->a_lnotab); | |
| 4180 | + if (nbytes >= len) { | |
| 4181 | + if (len * 2 < nbytes) | |
| 4182 | + len = nbytes; | |
| 4183 | + else | |
| 4184 | + len *= 2; | |
| 4185 | + if (_PyString_Resize(&a->a_lnotab, len) < 0) | |
| 4186 | + return 0; | |
| 4187 | + } | |
| 4188 | + lnotab = (unsigned char *) | |
| 4189 | + PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off; | |
| 4190 | + for (j = 0; j < ncodes; j++) { | |
| 4191 | + *lnotab++ = 255; | |
| 4192 | + *lnotab++ = 0; | |
| 4193 | + } | |
| 4194 | + d_bytecode -= ncodes * 255; | |
| 4195 | + a->a_lnotab_off += ncodes * 2; | |
| 4196 | + } | |
| 4197 | + assert(d_bytecode <= 255); | |
| 4198 | + if (d_lineno > 255) { | |
| 4199 | + int j, nbytes, ncodes = d_lineno / 255; | |
| 4200 | + nbytes = a->a_lnotab_off + 2 * ncodes; | |
| 4201 | + len = PyString_GET_SIZE(a->a_lnotab); | |
| 4202 | + if (nbytes >= len) { | |
| 4203 | + if (len * 2 < nbytes) | |
| 4204 | + len = nbytes; | |
| 4205 | + else | |
| 4206 | + len *= 2; | |
| 4207 | + if (_PyString_Resize(&a->a_lnotab, len) < 0) | |
| 4208 | + return 0; | |
| 4209 | + } | |
| 4210 | + lnotab = (unsigned char *) | |
| 4211 | + PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off; | |
| 4212 | + *lnotab++ = d_bytecode; | |
| 4213 | + *lnotab++ = 255; | |
| 4214 | + d_bytecode = 0; | |
| 4215 | + for (j = 1; j < ncodes; j++) { | |
| 4216 | + *lnotab++ = 0; | |
| 4217 | + *lnotab++ = 255; | |
| 4218 | + } | |
| 4219 | + d_lineno -= ncodes * 255; | |
| 4220 | + a->a_lnotab_off += ncodes * 2; | |
| 4221 | + } | |
| 4222 | + | |
| 4223 | + len = PyString_GET_SIZE(a->a_lnotab); | |
| 4224 | + if (a->a_lnotab_off + 2 >= len) { | |
| 4225 | + if (_PyString_Resize(&a->a_lnotab, len * 2) < 0) | |
| 4226 | + return 0; | |
| 4227 | + } | |
| 4228 | + lnotab = (unsigned char *) | |
| 4229 | + PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off; | |
| 4230 | + | |
| 4231 | + a->a_lnotab_off += 2; | |
| 4232 | + if (d_bytecode) { | |
| 4233 | + *lnotab++ = d_bytecode; | |
| 4234 | + *lnotab++ = d_lineno; | |
| 4235 | + } | |
| 4236 | + else { /* First line of a block; def stmt, etc. */ | |
| 4237 | + *lnotab++ = 0; | |
| 4238 | + *lnotab++ = d_lineno; | |
| 4239 | + } | |
| 4240 | + a->a_lineno = i->i_lineno; | |
| 4241 | + a->a_lineno_off = a->a_offset; | |
| 4242 | + return 1; | |
| 4243 | +} | |
| 4244 | + | |
| 4245 | +/* assemble_emit() | |
| 4246 | + Extend the bytecode with a new instruction. | |
| 4247 | + Update lnotab if necessary. | |
| 4248 | +*/ | |
| 4249 | + | |
| 4250 | +static int | |
| 4251 | +assemble_emit(struct assembler *a, struct instr *i) | |
| 4252 | +{ | |
| 4253 | + int size, arg = 0, ext = 0; | |
| 4254 | + Py_ssize_t len = PyString_GET_SIZE(a->a_bytecode); | |
| 4255 | + char *code; | |
| 4256 | + | |
| 4257 | + size = instrsize(i); | |
| 4258 | + if (i->i_hasarg) { | |
| 4259 | + arg = i->i_oparg; | |
| 4260 | + ext = arg >> 16; | |
| 4261 | + } | |
| 4262 | + if (i->i_lineno && !assemble_lnotab(a, i)) | |
| 4263 | + return 0; | |
| 4264 | + if (a->a_offset + size >= len) { | |
| 4265 | + if (_PyString_Resize(&a->a_bytecode, len * 2) < 0) | |
| 4266 | + return 0; | |
| 4267 | + } | |
| 4268 | + code = PyString_AS_STRING(a->a_bytecode) + a->a_offset; | |
| 4269 | + a->a_offset += size; | |
| 4270 | + if (size == 6) { | |
| 4271 | + assert(i->i_hasarg); | |
| 4272 | + *code++ = (char)EXTENDED_ARG; | |
| 4273 | + *code++ = ext & 0xff; | |
| 4274 | + *code++ = ext >> 8; | |
| 4275 | + arg &= 0xffff; | |
| 4276 | + } | |
| 4277 | + *code++ = i->i_opcode; | |
| 4278 | + if (i->i_hasarg) { | |
| 4279 | + assert(size == 3 || size == 6); | |
| 4280 | + *code++ = arg & 0xff; | |
| 4281 | + *code++ = arg >> 8; | |
| 4282 | + } | |
| 4283 | + return 1; | |
| 4284 | +} | |
| 4285 | + | |
| 4286 | +static void | |
| 4287 | +assemble_jump_offsets(struct assembler *a, struct compiler *c) | |
| 4288 | +{ | |
| 4289 | + basicblock *b; | |
| 4290 | + int bsize, totsize, extended_arg_count, last_extended_arg_count = 0; | |
| 4291 | + int i; | |
| 4292 | + | |
| 4293 | + /* Compute the size of each block and fixup jump args. | |
| 4294 | + Replace block pointer with position in bytecode. */ | |
| 4295 | +start: | |
| 4296 | + totsize = 0; | |
| 4297 | + for (i = a->a_nblocks - 1; i >= 0; i--) { | |
| 4298 | + b = a->a_postorder[i]; | |
| 4299 | + bsize = blocksize(b); | |
| 4300 | + b->b_offset = totsize; | |
| 4301 | + totsize += bsize; | |
| 4302 | + } | |
| 4303 | + extended_arg_count = 0; | |
| 4304 | + for (b = c->u->u_blocks; b != NULL; b = b->b_list) { | |
| 4305 | + bsize = b->b_offset; | |
| 4306 | + for (i = 0; i < b->b_iused; i++) { | |
| 4307 | + struct instr *instr = &b->b_instr[i]; | |
| 4308 | + /* Relative jumps are computed relative to | |
| 4309 | + the instruction pointer after fetching | |
| 4310 | + the jump instruction. | |
| 4311 | + */ | |
| 4312 | + bsize += instrsize(instr); | |
| 4313 | + if (instr->i_jabs) | |
| 4314 | + instr->i_oparg = instr->i_target->b_offset; | |
| 4315 | + else if (instr->i_jrel) { | |
| 4316 | + int delta = instr->i_target->b_offset - bsize; | |
| 4317 | + instr->i_oparg = delta; | |
| 4318 | + } | |
| 4319 | + else | |
| 4320 | + continue; | |
| 4321 | + if (instr->i_oparg > 0xffff) | |
| 4322 | + extended_arg_count++; | |
| 4323 | + } | |
| 4324 | + } | |
| 4325 | + | |
| 4326 | + /* XXX: This is an awful hack that could hurt performance, but | |
| 4327 | + on the bright side it should work until we come up | |
| 4328 | + with a better solution. | |
| 4329 | + | |
| 4330 | + In the meantime, should the goto be dropped in favor | |
| 4331 | + of a loop? | |
| 4332 | + | |
| 4333 | + The issue is that in the first loop blocksize() is called | |
| 4334 | + which calls instrsize() which requires i_oparg be set | |
| 4335 | + appropriately. There is a bootstrap problem because | |
| 4336 | + i_oparg is calculated in the second loop above. | |
| 4337 | + | |
| 4338 | + So we loop until we stop seeing new EXTENDED_ARGs. | |
| 4339 | + The only EXTENDED_ARGs that could be popping up are | |
| 4340 | + ones in jump instructions. So this should converge | |
| 4341 | + fairly quickly. | |
| 4342 | + */ | |
| 4343 | + if (last_extended_arg_count != extended_arg_count) { | |
| 4344 | + last_extended_arg_count = extended_arg_count; | |
| 4345 | + goto start; | |
| 4346 | + } | |
| 4347 | +} | |
| 4348 | + | |
| 4349 | +static PyObject * | |
| 4350 | +dict_keys_inorder(PyObject *dict, int offset) | |
| 4351 | +{ | |
| 4352 | + PyObject *tuple, *k, *v; | |
| 4353 | + Py_ssize_t i, pos = 0, size = PyDict_Size(dict); | |
| 4354 | + | |
| 4355 | + tuple = PyTuple_New(size); | |
| 4356 | + if (tuple == NULL) | |
| 4357 | + return NULL; | |
| 4358 | + while (PyDict_Next(dict, &pos, &k, &v)) { | |
| 4359 | + i = PyInt_AS_LONG(v); | |
| 4360 | + k = PyTuple_GET_ITEM(k, 0); | |
| 4361 | + Py_INCREF(k); | |
| 4362 | + assert((i - offset) < size); | |
| 4363 | + assert((i - offset) >= 0); | |
| 4364 | + PyTuple_SET_ITEM(tuple, i - offset, k); | |
| 4365 | + } | |
| 4366 | + return tuple; | |
| 4367 | +} | |
| 4368 | + | |
| 4369 | +static int | |
| 4370 | +compute_code_flags(struct compiler *c) | |
| 4371 | +{ | |
| 4372 | + PySTEntryObject *ste = c->u->u_ste; | |
| 4373 | + int flags = 0, n; | |
| 4374 | + if (ste->ste_type != ModuleBlock) | |
| 4375 | + flags |= CO_NEWLOCALS; | |
| 4376 | + if (ste->ste_type == FunctionBlock) { | |
| 4377 | + if (!ste->ste_unoptimized) | |
| 4378 | + flags |= CO_OPTIMIZED; | |
| 4379 | + if (ste->ste_nested) | |
| 4380 | + flags |= CO_NESTED; | |
| 4381 | + if (ste->ste_generator) | |
| 4382 | + flags |= CO_GENERATOR; | |
| 4383 | + } | |
| 4384 | + if (ste->ste_varargs) | |
| 4385 | + flags |= CO_VARARGS; | |
| 4386 | + if (ste->ste_varkeywords) | |
| 4387 | + flags |= CO_VARKEYWORDS; | |
| 4388 | + if (ste->ste_generator) | |
| 4389 | + flags |= CO_GENERATOR; | |
| 4390 | + | |
| 4391 | + /* (Only) inherit compilerflags in PyCF_MASK */ | |
| 4392 | + flags |= (c->c_flags->cf_flags & PyCF_MASK); | |
| 4393 | + | |
| 4394 | + n = PyDict_Size(c->u->u_freevars); | |
| 4395 | + if (n < 0) | |
| 4396 | + return -1; | |
| 4397 | + if (n == 0) { | |
| 4398 | + n = PyDict_Size(c->u->u_cellvars); | |
| 4399 | + if (n < 0) | |
| 4400 | + return -1; | |
| 4401 | + if (n == 0) { | |
| 4402 | + flags |= CO_NOFREE; | |
| 4403 | + } | |
| 4404 | + } | |
| 4405 | + | |
| 4406 | + return flags; | |
| 4407 | +} | |
| 4408 | + | |
| 4409 | +static PyCodeObject * | |
| 4410 | +makecode(struct compiler *c, struct assembler *a) | |
| 4411 | +{ | |
| 4412 | + PyObject *tmp; | |
| 4413 | + PyCodeObject *co = NULL; | |
| 4414 | + PyObject *consts = NULL; | |
| 4415 | + PyObject *names = NULL; | |
| 4416 | + PyObject *varnames = NULL; | |
| 4417 | + PyObject *filename = NULL; | |
| 4418 | + PyObject *name = NULL; | |
| 4419 | + PyObject *freevars = NULL; | |
| 4420 | + PyObject *cellvars = NULL; | |
| 4421 | + PyObject *bytecode = NULL; | |
| 4422 | + int nlocals, flags; | |
| 4423 | + | |
| 4424 | + tmp = dict_keys_inorder(c->u->u_consts, 0); | |
| 4425 | + if (!tmp) | |
| 4426 | + goto error; | |
| 4427 | + consts = PySequence_List(tmp); /* optimize_code requires a list */ | |
| 4428 | + Py_DECREF(tmp); | |
| 4429 | + | |
| 4430 | + names = dict_keys_inorder(c->u->u_names, 0); | |
| 4431 | + varnames = dict_keys_inorder(c->u->u_varnames, 0); | |
| 4432 | + if (!consts || !names || !varnames) | |
| 4433 | + goto error; | |
| 4434 | + | |
| 4435 | + cellvars = dict_keys_inorder(c->u->u_cellvars, 0); | |
| 4436 | + if (!cellvars) | |
| 4437 | + goto error; | |
| 4438 | + freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars)); | |
| 4439 | + if (!freevars) | |
| 4440 | + goto error; | |
| 4441 | + filename = PyString_FromString(c->c_filename); | |
| 4442 | + if (!filename) | |
| 4443 | + goto error; | |
| 4444 | + | |
| 4445 | + nlocals = PyDict_Size(c->u->u_varnames); | |
| 4446 | + flags = compute_code_flags(c); | |
| 4447 | + if (flags < 0) | |
| 4448 | + goto error; | |
| 4449 | + | |
| 4450 | + bytecode = optimize_code(a->a_bytecode, consts, names, a->a_lnotab); | |
| 4451 | + if (!bytecode) | |
| 4452 | + goto error; | |
| 4453 | + | |
| 4454 | + tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */ | |
| 4455 | + if (!tmp) | |
| 4456 | + goto error; | |
| 4457 | + Py_DECREF(consts); | |
| 4458 | + consts = tmp; | |
| 4459 | + | |
| 4460 | + co = PyCode_New(c->u->u_argcount, nlocals, stackdepth(c), flags, | |
| 4461 | + bytecode, consts, names, varnames, | |
| 4462 | + freevars, cellvars, | |
| 4463 | + filename, c->u->u_name, | |
| 4464 | + c->u->u_firstlineno, | |
| 4465 | + a->a_lnotab); | |
| 4466 | + error: | |
| 4467 | + Py_XDECREF(consts); | |
| 4468 | + Py_XDECREF(names); | |
| 4469 | + Py_XDECREF(varnames); | |
| 4470 | + Py_XDECREF(filename); | |
| 4471 | + Py_XDECREF(name); | |
| 4472 | + Py_XDECREF(freevars); | |
| 4473 | + Py_XDECREF(cellvars); | |
| 4474 | + Py_XDECREF(bytecode); | |
| 4475 | + return co; | |
| 4476 | +} | |
| 4477 | + | |
| 4478 | + | |
| 4479 | +/* For debugging purposes only */ | |
| 4480 | +#if 0 | |
| 4481 | +static void | |
| 4482 | +dump_instr(const struct instr *i) | |
| 4483 | +{ | |
| 4484 | + const char *jrel = i->i_jrel ? "jrel " : ""; | |
| 4485 | + const char *jabs = i->i_jabs ? "jabs " : ""; | |
| 4486 | + char arg[128]; | |
| 4487 | + | |
| 4488 | + *arg = '\0'; | |
| 4489 | + if (i->i_hasarg) | |
| 4490 | + sprintf(arg, "arg: %d ", i->i_oparg); | |
| 4491 | + | |
| 4492 | + fprintf(stderr, "line: %d, opcode: %d %s%s%s\n", | |
| 4493 | + i->i_lineno, i->i_opcode, arg, jabs, jrel); | |
| 4494 | +} | |
| 4495 | + | |
| 4496 | +static void | |
| 4497 | +dump_basicblock(const basicblock *b) | |
| 4498 | +{ | |
| 4499 | + const char *seen = b->b_seen ? "seen " : ""; | |
| 4500 | + const char *b_return = b->b_return ? "return " : ""; | |
| 4501 | + fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n", | |
| 4502 | + b->b_iused, b->b_startdepth, b->b_offset, seen, b_return); | |
| 4503 | + if (b->b_instr) { | |
| 4504 | + int i; | |
| 4505 | + for (i = 0; i < b->b_iused; i++) { | |
| 4506 | + fprintf(stderr, " [%02d] ", i); | |
| 4507 | + dump_instr(b->b_instr + i); | |
| 4508 | + } | |
| 4509 | + } | |
| 4510 | +} | |
| 4511 | +#endif | |
| 4512 | + | |
| 4513 | +static PyCodeObject * | |
| 4514 | +assemble(struct compiler *c, int addNone) | |
| 4515 | +{ | |
| 4516 | + basicblock *b, *entryblock; | |
| 4517 | + struct assembler a; | |
| 4518 | + int i, j, nblocks; | |
| 4519 | + PyCodeObject *co = NULL; | |
| 4520 | + | |
| 4521 | + /* Make sure every block that falls off the end returns None. | |
| 4522 | + XXX NEXT_BLOCK() isn't quite right, because if the last | |
| 4523 | + block ends with a jump or return b_next shouldn't set. | |
| 4524 | + */ | |
| 4525 | + if (!c->u->u_curblock->b_return) { | |
| 4526 | + NEXT_BLOCK(c); | |
| 4527 | + if (addNone) | |
| 4528 | + ADDOP_O(c, LOAD_CONST, Py_None, consts); | |
| 4529 | + ADDOP(c, RETURN_VALUE); | |
| 4530 | + } | |
| 4531 | + | |
| 4532 | + nblocks = 0; | |
| 4533 | + entryblock = NULL; | |
| 4534 | + for (b = c->u->u_blocks; b != NULL; b = b->b_list) { | |
| 4535 | + nblocks++; | |
| 4536 | + entryblock = b; | |
| 4537 | + } | |
| 4538 | + | |
| 4539 | + /* Set firstlineno if it wasn't explicitly set. */ | |
| 4540 | + if (!c->u->u_firstlineno) { | |
| 4541 | + if (entryblock && entryblock->b_instr) | |
| 4542 | + c->u->u_firstlineno = entryblock->b_instr->i_lineno; | |
| 4543 | + else | |
| 4544 | + c->u->u_firstlineno = 1; | |
| 4545 | + } | |
| 4546 | + if (!assemble_init(&a, nblocks, c->u->u_firstlineno)) | |
| 4547 | + goto error; | |
| 4548 | + dfs(c, entryblock, &a); | |
| 4549 | + | |
| 4550 | + /* Can't modify the bytecode after computing jump offsets. */ | |
| 4551 | + assemble_jump_offsets(&a, c); | |
| 4552 | + | |
| 4553 | + /* Emit code in reverse postorder from dfs. */ | |
| 4554 | + for (i = a.a_nblocks - 1; i >= 0; i--) { | |
| 4555 | + b = a.a_postorder[i]; | |
| 4556 | + for (j = 0; j < b->b_iused; j++) | |
| 4557 | + if (!assemble_emit(&a, &b->b_instr[j])) | |
| 4558 | + goto error; | |
| 4559 | + } | |
| 4560 | + | |
| 4561 | + if (_PyString_Resize(&a.a_lnotab, a.a_lnotab_off) < 0) | |
| 4562 | + goto error; | |
| 4563 | + if (_PyString_Resize(&a.a_bytecode, a.a_offset) < 0) | |
| 4564 | + goto error; | |
| 4565 | + | |
| 4566 | + co = makecode(c, &a); | |
| 4567 | + error: | |
| 4568 | + assemble_free(&a); | |
| 4569 | + return co; | |
| 4570 | +} | |
| @@ -0,0 +1,30 @@ | ||
| 1 | +/* | |
| 2 | + * Public domain dup2() lookalike | |
| 3 | + * by Curtis Jackson @ AT&T Technologies, Burlington, NC | |
| 4 | + * electronic address: burl!rcj | |
| 5 | + * | |
| 6 | + * dup2 performs the following functions: | |
| 7 | + * | |
| 8 | + * Check to make sure that fd1 is a valid open file descriptor. | |
| 9 | + * Check to see if fd2 is already open; if so, close it. | |
| 10 | + * Duplicate fd1 onto fd2; checking to make sure fd2 is a valid fd. | |
| 11 | + * Return fd2 if all went well; return BADEXIT otherwise. | |
| 12 | + */ | |
| 13 | + | |
| 14 | +#include <fcntl.h> | |
| 15 | + | |
| 16 | +#define BADEXIT -1 | |
| 17 | + | |
| 18 | +int | |
| 19 | +dup2(int fd1, int fd2) | |
| 20 | +{ | |
| 21 | + if (fd1 != fd2) { | |
| 22 | + if (fcntl(fd1, F_GETFL) < 0) | |
| 23 | + return BADEXIT; | |
| 24 | + if (fcntl(fd2, F_GETFL) >= 0) | |
| 25 | + close(fd2); | |
| 26 | + if (fcntl(fd1, F_DUPFD, fd2) < 0) | |
| 27 | + return BADEXIT; | |
| 28 | + } | |
| 29 | + return fd2; | |
| 30 | +} | |
| @@ -0,0 +1,183 @@ | ||
| 1 | + | |
| 2 | +/* Support for dynamic loading of extension modules */ | |
| 3 | + | |
| 4 | +#include "Python.h" | |
| 5 | +#include "importdl.h" | |
| 6 | + | |
| 7 | +#include <ctype.h> /* for isdigit() */ | |
| 8 | +#include <errno.h> /* for global errno */ | |
| 9 | +#include <string.h> /* for strerror() */ | |
| 10 | +#include <stdlib.h> /* for malloc(), free() */ | |
| 11 | +#include <sys/ldr.h> | |
| 12 | + | |
| 13 | + | |
| 14 | +#ifdef AIX_GENUINE_CPLUSPLUS | |
| 15 | +#include "/usr/lpp/xlC/include/load.h" | |
| 16 | +#define aix_load loadAndInit | |
| 17 | +#else | |
| 18 | +#define aix_load load | |
| 19 | +#endif | |
| 20 | + | |
| 21 | + | |
| 22 | +extern char *Py_GetProgramName(void); | |
| 23 | + | |
| 24 | +typedef struct Module { | |
| 25 | + struct Module *next; | |
| 26 | + void *entry; | |
| 27 | +} Module, *ModulePtr; | |
| 28 | + | |
| 29 | +const struct filedescr _PyImport_DynLoadFiletab[] = { | |
| 30 | + {".so", "rb", C_EXTENSION}, | |
| 31 | + {"module.so", "rb", C_EXTENSION}, | |
| 32 | + {0, 0} | |
| 33 | +}; | |
| 34 | + | |
| 35 | +static int | |
| 36 | +aix_getoldmodules(void **modlistptr) | |
| 37 | +{ | |
| 38 | + register ModulePtr modptr, prevmodptr; | |
| 39 | + register struct ld_info *ldiptr; | |
| 40 | + register char *ldibuf; | |
| 41 | + register int errflag, bufsize = 1024; | |
| 42 | + register unsigned int offset; | |
| 43 | + char *progname = Py_GetProgramName(); | |
| 44 | + | |
| 45 | + /* | |
| 46 | + -- Get the list of loaded modules into ld_info structures. | |
| 47 | + */ | |
| 48 | + if ((ldibuf = malloc(bufsize)) == NULL) { | |
| 49 | + PyErr_SetString(PyExc_ImportError, strerror(errno)); | |
| 50 | + return -1; | |
| 51 | + } | |
| 52 | + while ((errflag = loadquery(L_GETINFO, ldibuf, bufsize)) == -1 | |
| 53 | + && errno == ENOMEM) { | |
| 54 | + free(ldibuf); | |
| 55 | + bufsize += 1024; | |
| 56 | + if ((ldibuf = malloc(bufsize)) == NULL) { | |
| 57 | + PyErr_SetString(PyExc_ImportError, strerror(errno)); | |
| 58 | + return -1; | |
| 59 | + } | |
| 60 | + } | |
| 61 | + if (errflag == -1) { | |
| 62 | + PyErr_SetString(PyExc_ImportError, strerror(errno)); | |
| 63 | + return -1; | |
| 64 | + } | |
| 65 | + /* | |
| 66 | + -- Make the modules list from the ld_info structures. | |
| 67 | + */ | |
| 68 | + ldiptr = (struct ld_info *)ldibuf; | |
| 69 | + prevmodptr = NULL; | |
| 70 | + do { | |
| 71 | + if (strstr(progname, ldiptr->ldinfo_filename) == NULL && | |
| 72 | + strstr(ldiptr->ldinfo_filename, "python") == NULL) { | |
| 73 | + /* | |
| 74 | + -- Extract only the modules belonging to the main | |
| 75 | + -- executable + those containing "python" as a | |
| 76 | + -- substring (like the "python[version]" binary or | |
| 77 | + -- "libpython[version].a" in case it's a shared lib). | |
| 78 | + */ | |
| 79 | + offset = (unsigned int)ldiptr->ldinfo_next; | |
| 80 | + ldiptr = (struct ld_info *)((char*)ldiptr + offset); | |
| 81 | + continue; | |
| 82 | + } | |
| 83 | + if ((modptr = (ModulePtr)malloc(sizeof(Module))) == NULL) { | |
| 84 | + PyErr_SetString(PyExc_ImportError, strerror(errno)); | |
| 85 | + while (*modlistptr) { | |
| 86 | + modptr = (ModulePtr)*modlistptr; | |
| 87 | + *modlistptr = (void *)modptr->next; | |
| 88 | + free(modptr); | |
| 89 | + } | |
| 90 | + return -1; | |
| 91 | + } | |
| 92 | + modptr->entry = ldiptr->ldinfo_dataorg; | |
| 93 | + modptr->next = NULL; | |
| 94 | + if (prevmodptr == NULL) | |
| 95 | + *modlistptr = (void *)modptr; | |
| 96 | + else | |
| 97 | + prevmodptr->next = modptr; | |
| 98 | + prevmodptr = modptr; | |
| 99 | + offset = (unsigned int)ldiptr->ldinfo_next; | |
| 100 | + ldiptr = (struct ld_info *)((char*)ldiptr + offset); | |
| 101 | + } while (offset); | |
| 102 | + free(ldibuf); | |
| 103 | + return 0; | |
| 104 | +} | |
| 105 | + | |
| 106 | + | |
| 107 | +static void | |
| 108 | +aix_loaderror(const char *pathname) | |
| 109 | +{ | |
| 110 | + | |
| 111 | + char *message[1024], errbuf[1024]; | |
| 112 | + register int i,j; | |
| 113 | + | |
| 114 | + struct errtab { | |
| 115 | + int errNo; | |
| 116 | + char *errstr; | |
| 117 | + } load_errtab[] = { | |
| 118 | + {L_ERROR_TOOMANY, "too many errors, rest skipped."}, | |
| 119 | + {L_ERROR_NOLIB, "can't load library:"}, | |
| 120 | + {L_ERROR_UNDEF, "can't find symbol in library:"}, | |
| 121 | + {L_ERROR_RLDBAD, | |
| 122 | + "RLD index out of range or bad relocation type:"}, | |
| 123 | + {L_ERROR_FORMAT, "not a valid, executable xcoff file:"}, | |
| 124 | + {L_ERROR_MEMBER, | |
| 125 | + "file not an archive or does not contain requested member:"}, | |
| 126 | + {L_ERROR_TYPE, "symbol table mismatch:"}, | |
| 127 | + {L_ERROR_ALIGN, "text alignment in file is wrong."}, | |
| 128 | + {L_ERROR_SYSTEM, "System error:"}, | |
| 129 | + {L_ERROR_ERRNO, NULL} | |
| 130 | + }; | |
| 131 | + | |
| 132 | +#define LOAD_ERRTAB_LEN (sizeof(load_errtab)/sizeof(load_errtab[0])) | |
| 133 | +#define ERRBUF_APPEND(s) strncat(errbuf, s, sizeof(errbuf)-strlen(errbuf)-1) | |
| 134 | + | |
| 135 | + PyOS_snprintf(errbuf, sizeof(errbuf), "from module %.200s ", pathname); | |
| 136 | + | |
| 137 | + if (!loadquery(L_GETMESSAGES, &message[0], sizeof(message))) { | |
| 138 | + ERRBUF_APPEND(strerror(errno)); | |
| 139 | + ERRBUF_APPEND("\n"); | |
| 140 | + } | |
| 141 | + for(i = 0; message[i] && *message[i]; i++) { | |
| 142 | + int nerr = atoi(message[i]); | |
| 143 | + for (j=0; j<LOAD_ERRTAB_LEN ; j++) { | |
| 144 | + if (nerr == load_errtab[j].errNo && load_errtab[j].errstr) | |
| 145 | + ERRBUF_APPEND(load_errtab[j].errstr); | |
| 146 | + } | |
| 147 | + while (isdigit(Py_CHARMASK(*message[i]))) message[i]++ ; | |
| 148 | + ERRBUF_APPEND(message[i]); | |
| 149 | + ERRBUF_APPEND("\n"); | |
| 150 | + } | |
| 151 | + errbuf[strlen(errbuf)-1] = '\0'; /* trim off last newline */ | |
| 152 | + PyErr_SetString(PyExc_ImportError, errbuf); | |
| 153 | + return; | |
| 154 | +} | |
| 155 | + | |
| 156 | + | |
| 157 | +dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname, | |
| 158 | + const char *pathname, FILE *fp) | |
| 159 | +{ | |
| 160 | + dl_funcptr p; | |
| 161 | + | |
| 162 | + /* | |
| 163 | + -- Invoke load() with L_NOAUTODEFER leaving the imported symbols | |
| 164 | + -- of the shared module unresolved. Thus we have to resolve them | |
| 165 | + -- explicitly with loadbind. The new module is loaded, then we | |
| 166 | + -- resolve its symbols using the list of already loaded modules | |
| 167 | + -- (only those that belong to the python executable). Get these | |
| 168 | + -- with loadquery(L_GETINFO). | |
| 169 | + */ | |
| 170 | + | |
| 171 | + static void *staticmodlistptr = NULL; | |
| 172 | + | |
| 173 | + if (!staticmodlistptr) | |
| 174 | + if (aix_getoldmodules(&staticmodlistptr) == -1) | |
| 175 | + return NULL; | |
| 176 | + p = (dl_funcptr) aix_load((char *)pathname, L_NOAUTODEFER, 0); | |
| 177 | + if (p == NULL) { | |
| 178 | + aix_loaderror(pathname); | |
| 179 | + return NULL; | |
| 180 | + } | |
| 181 | + | |
| 182 | + return p; | |
| 183 | +} | |
| @@ -0,0 +1,47 @@ | ||
| 1 | + | |
| 2 | +/* Support for dynamic loading of extension modules */ | |
| 3 | + | |
| 4 | +#include <atheos/image.h> | |
| 5 | +#include <errno.h> | |
| 6 | + | |
| 7 | +#include "Python.h" | |
| 8 | +#include "importdl.h" | |
| 9 | + | |
| 10 | + | |
| 11 | +const struct filedescr _PyImport_DynLoadFiletab[] = { | |
| 12 | + {".so", "rb", C_EXTENSION}, | |
| 13 | + {"module.so", "rb", C_EXTENSION}, | |
| 14 | + {0, 0} | |
| 15 | +}; | |
| 16 | + | |
| 17 | +dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname, | |
| 18 | + const char *pathname, FILE *fp) | |
| 19 | +{ | |
| 20 | + void *p; | |
| 21 | + int lib; | |
| 22 | + char funcname[258]; | |
| 23 | + | |
| 24 | + if (Py_VerboseFlag) | |
| 25 | + printf("load_library %s\n", pathname); | |
| 26 | + | |
| 27 | + lib = load_library(pathname, 0); | |
| 28 | + if (lib < 0) { | |
| 29 | + char buf[512]; | |
| 30 | + if (Py_VerboseFlag) | |
| 31 | + perror(pathname); | |
| 32 | + PyOS_snprintf(buf, sizeof(buf), "Failed to load %.200s: %.200s", | |
| 33 | + pathname, strerror(errno)); | |
| 34 | + PyErr_SetString(PyExc_ImportError, buf); | |
| 35 | + return NULL; | |
| 36 | + } | |
| 37 | + PyOS_snprintf(funcname, sizeof(funcname), "init%.200s", shortname); | |
| 38 | + if (Py_VerboseFlag) | |
| 39 | + printf("get_symbol_address %s\n", funcname); | |
| 40 | + if (get_symbol_address(lib, funcname, -1, &p) < 0) { | |
| 41 | + p = NULL; | |
| 42 | + if (Py_VerboseFlag) | |
| 43 | + perror(funcname); | |
| 44 | + } | |
| 45 | + | |
| 46 | + return (dl_funcptr) p; | |
| 47 | +} | |
| @@ -0,0 +1,254 @@ | ||
| 1 | + | |
| 2 | +/* Support for dynamic loading of extension modules */ | |
| 3 | + | |
| 4 | +#include <kernel/image.h> | |
| 5 | +#include <kernel/OS.h> | |
| 6 | +#include <stdlib.h> | |
| 7 | + | |
| 8 | +#include "Python.h" | |
| 9 | +#include "importdl.h" | |
| 10 | + | |
| 11 | +const struct filedescr _PyImport_DynLoadFiletab[] = { | |
| 12 | + {".so", "rb", C_EXTENSION}, | |
| 13 | + {"module.so", "rb", C_EXTENSION}, | |
| 14 | + {0, 0} | |
| 15 | +}; | |
| 16 | + | |
| 17 | +#if defined(MAXPATHLEN) && !defined(_SYS_PARAM_H) | |
| 18 | +#undef MAXPATHLEN | |
| 19 | +#endif | |
| 20 | + | |
| 21 | +#ifdef WITH_THREAD | |
| 22 | +#include "pythread.h" | |
| 23 | +static PyThread_type_lock beos_dyn_lock; | |
| 24 | +#endif | |
| 25 | + | |
| 26 | +static PyObject *beos_dyn_images = NULL; | |
| 27 | + | |
| 28 | +/* ---------------------------------------------------------------------- | |
| 29 | + * BeOS dynamic loading support | |
| 30 | + * | |
| 31 | + * This uses shared libraries, but BeOS has its own way of doing things | |
| 32 | + * (much easier than dlfnc.h, from the look of things). We'll use a | |
| 33 | + * Python Dictionary object to store the images_ids so we can be very | |
| 34 | + * nice and unload them when we exit. | |
| 35 | + * | |
| 36 | + * Note that this is thread-safe. Probably irrelevent, because of losing | |
| 37 | + * systems... Python probably disables threads while loading modules. | |
| 38 | + * Note the use of "probably"! Better to be safe than sorry. [chrish] | |
| 39 | + * | |
| 40 | + * As of 1.5.1 this should also work properly when you've configured | |
| 41 | + * Python without thread support; the 1.5 version required it, which wasn't | |
| 42 | + * very friendly. Note that I haven't tested it without threading... why | |
| 43 | + * would you want to avoid threads on BeOS? [chrish] | |
| 44 | + * | |
| 45 | + * As of 1.5.2, the PyImport_BeImageID() function has been removed; Donn | |
| 46 | + * tells me it's not necessary anymore because of PyCObject_Import(). | |
| 47 | + * [chrish] | |
| 48 | + */ | |
| 49 | + | |
| 50 | +/* Whack an item; the item is an image_id in disguise, so we'll call | |
| 51 | + * unload_add_on() for it. | |
| 52 | + */ | |
| 53 | +static void beos_nuke_dyn( PyObject *item ) | |
| 54 | +{ | |
| 55 | + status_t retval; | |
| 56 | + | |
| 57 | + if( item ) { | |
| 58 | + image_id id = (image_id)PyInt_AsLong( item ); | |
| 59 | + | |
| 60 | + retval = unload_add_on( id ); | |
| 61 | + } | |
| 62 | +} | |
| 63 | + | |
| 64 | +/* atexit() handler that'll call unload_add_on() for every item in the | |
| 65 | + * dictionary. | |
| 66 | + */ | |
| 67 | +static void beos_cleanup_dyn( void ) | |
| 68 | +{ | |
| 69 | + if( beos_dyn_images ) { | |
| 70 | + int idx; | |
| 71 | + int list_size; | |
| 72 | + PyObject *id_list; | |
| 73 | + | |
| 74 | +#ifdef WITH_THREAD | |
| 75 | + PyThread_acquire_lock( beos_dyn_lock, 1 ); | |
| 76 | +#endif | |
| 77 | + | |
| 78 | + id_list = PyDict_Values( beos_dyn_images ); | |
| 79 | + | |
| 80 | + list_size = PyList_Size( id_list ); | |
| 81 | + for( idx = 0; idx < list_size; idx++ ) { | |
| 82 | + PyObject *the_item; | |
| 83 | + | |
| 84 | + the_item = PyList_GetItem( id_list, idx ); | |
| 85 | + beos_nuke_dyn( the_item ); | |
| 86 | + } | |
| 87 | + | |
| 88 | + PyDict_Clear( beos_dyn_images ); | |
| 89 | + | |
| 90 | +#ifdef WITH_THREAD | |
| 91 | + PyThread_free_lock( beos_dyn_lock ); | |
| 92 | +#endif | |
| 93 | + } | |
| 94 | +} | |
| 95 | + | |
| 96 | +/* | |
| 97 | + * Initialize our dictionary, and the dictionary mutex. | |
| 98 | + */ | |
| 99 | +static void beos_init_dyn( void ) | |
| 100 | +{ | |
| 101 | + /* We're protected from a race condition here by the atomic init_count | |
| 102 | + * variable. | |
| 103 | + */ | |
| 104 | + static int32 init_count = 0; | |
| 105 | + int32 val; | |
| 106 | + | |
| 107 | + val = atomic_add( &init_count, 1 ); | |
| 108 | + if( beos_dyn_images == NULL && val == 0 ) { | |
| 109 | + beos_dyn_images = PyDict_New(); | |
| 110 | +#ifdef WITH_THREAD | |
| 111 | + beos_dyn_lock = PyThread_allocate_lock(); | |
| 112 | +#endif | |
| 113 | + atexit( beos_cleanup_dyn ); | |
| 114 | + } | |
| 115 | +} | |
| 116 | + | |
| 117 | +/* | |
| 118 | + * Add an image_id to the dictionary; the module name of the loaded image | |
| 119 | + * is the key. Note that if the key is already in the dict, we unload | |
| 120 | + * that image; this should allow reload() to work on dynamically loaded | |
| 121 | + * modules (super-keen!). | |
| 122 | + */ | |
| 123 | +static void beos_add_dyn( char *name, image_id id ) | |
| 124 | +{ | |
| 125 | + int retval; | |
| 126 | + PyObject *py_id; | |
| 127 | + | |
| 128 | + if( beos_dyn_images == NULL ) { | |
| 129 | + beos_init_dyn(); | |
| 130 | + } | |
| 131 | + | |
| 132 | +#ifdef WITH_THREAD | |
| 133 | + retval = PyThread_acquire_lock( beos_dyn_lock, 1 ); | |
| 134 | +#endif | |
| 135 | + | |
| 136 | + /* If there's already an object with this key in the dictionary, | |
| 137 | + * we're doing a reload(), so let's nuke it. | |
| 138 | + */ | |
| 139 | + py_id = PyDict_GetItemString( beos_dyn_images, name ); | |
| 140 | + if( py_id ) { | |
| 141 | + beos_nuke_dyn( py_id ); | |
| 142 | + retval = PyDict_DelItemString( beos_dyn_images, name ); | |
| 143 | + } | |
| 144 | + | |
| 145 | + py_id = PyInt_FromLong( (long)id ); | |
| 146 | + if( py_id ) { | |
| 147 | + retval = PyDict_SetItemString( beos_dyn_images, name, py_id ); | |
| 148 | + } | |
| 149 | + | |
| 150 | +#ifdef WITH_THREAD | |
| 151 | + PyThread_release_lock( beos_dyn_lock ); | |
| 152 | +#endif | |
| 153 | +} | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | +dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname, | |
| 158 | + const char *pathname, FILE *fp) | |
| 159 | +{ | |
| 160 | + dl_funcptr p; | |
| 161 | + image_id the_id; | |
| 162 | + status_t retval; | |
| 163 | + char fullpath[PATH_MAX]; | |
| 164 | + char funcname[258]; | |
| 165 | + | |
| 166 | + if( Py_VerboseFlag ) { | |
| 167 | + printf( "load_add_on( %s )\n", pathname ); | |
| 168 | + } | |
| 169 | + | |
| 170 | + /* Hmm, this old bug appears to have regenerated itself; if the | |
| 171 | + * path isn't absolute, load_add_on() will fail. Reported to Be | |
| 172 | + * April 21, 1998. | |
| 173 | + */ | |
| 174 | + if( pathname[0] != '/' ) { | |
| 175 | + (void)getcwd( fullpath, PATH_MAX ); | |
| 176 | + (void)strncat( fullpath, "/", PATH_MAX ); | |
| 177 | + (void)strncat( fullpath, pathname, PATH_MAX ); | |
| 178 | + | |
| 179 | + if( Py_VerboseFlag ) { | |
| 180 | + printf( "load_add_on( %s )\n", fullpath ); | |
| 181 | + } | |
| 182 | + } else { | |
| 183 | + (void)strcpy( fullpath, pathname ); | |
| 184 | + } | |
| 185 | + | |
| 186 | + the_id = load_add_on( fullpath ); | |
| 187 | + if( the_id < B_NO_ERROR ) { | |
| 188 | + /* It's too bad load_add_on() doesn't set errno or something... | |
| 189 | + */ | |
| 190 | + char buff[256]; /* hate hard-coded string sizes... */ | |
| 191 | + | |
| 192 | + if( Py_VerboseFlag ) { | |
| 193 | + printf( "load_add_on( %s ) failed", fullpath ); | |
| 194 | + } | |
| 195 | + | |
| 196 | + if( the_id == B_ERROR ) | |
| 197 | + PyOS_snprintf( buff, sizeof(buff), | |
| 198 | + "BeOS: Failed to load %.200s", | |
| 199 | + fullpath ); | |
| 200 | + else | |
| 201 | + PyOS_snprintf( buff, sizeof(buff), | |
| 202 | + "Unknown error loading %.200s", | |
| 203 | + fullpath ); | |
| 204 | + | |
| 205 | + PyErr_SetString( PyExc_ImportError, buff ); | |
| 206 | + return NULL; | |
| 207 | + } | |
| 208 | + | |
| 209 | + PyOS_snprintf(funcname, sizeof(funcname), "init%.200s", shortname); | |
| 210 | + if( Py_VerboseFlag ) { | |
| 211 | + printf( "get_image_symbol( %s )\n", funcname ); | |
| 212 | + } | |
| 213 | + | |
| 214 | + retval = get_image_symbol( the_id, funcname, B_SYMBOL_TYPE_TEXT, &p ); | |
| 215 | + if( retval != B_NO_ERROR || p == NULL ) { | |
| 216 | + /* That's bad, we can't find that symbol in the module... | |
| 217 | + */ | |
| 218 | + char buff[256]; /* hate hard-coded string sizes... */ | |
| 219 | + | |
| 220 | + if( Py_VerboseFlag ) { | |
| 221 | + printf( "get_image_symbol( %s ) failed", funcname ); | |
| 222 | + } | |
| 223 | + | |
| 224 | + switch( retval ) { | |
| 225 | + case B_BAD_IMAGE_ID: | |
| 226 | + PyOS_snprintf( buff, sizeof(buff), | |
| 227 | + "can't load init function for dynamic module: " | |
| 228 | + "Invalid image ID for %.180s", fullpath ); | |
| 229 | + break; | |
| 230 | + case B_BAD_INDEX: | |
| 231 | + PyOS_snprintf( buff, sizeof(buff), | |
| 232 | + "can't load init function for dynamic module: " | |
| 233 | + "Bad index for %.180s", funcname ); | |
| 234 | + break; | |
| 235 | + default: | |
| 236 | + PyOS_snprintf( buff, sizeof(buff), | |
| 237 | + "can't load init function for dynamic module: " | |
| 238 | + "Unknown error looking up %.180s", funcname ); | |
| 239 | + break; | |
| 240 | + } | |
| 241 | + | |
| 242 | + retval = unload_add_on( the_id ); | |
| 243 | + | |
| 244 | + PyErr_SetString( PyExc_ImportError, buff ); | |
| 245 | + return NULL; | |
| 246 | + } | |
| 247 | + | |
| 248 | + /* Save the module name and image ID for later so we can clean up | |
| 249 | + * gracefully. | |
| 250 | + */ | |
| 251 | + beos_add_dyn( fqname, the_id ); | |
| 252 | + | |
| 253 | + return p; | |
| 254 | +} | |
| @@ -0,0 +1,26 @@ | ||
| 1 | + | |
| 2 | +/* Support for dynamic loading of extension modules */ | |
| 3 | + | |
| 4 | +#include "dl.h" | |
| 5 | + | |
| 6 | +#include "Python.h" | |
| 7 | +#include "importdl.h" | |
| 8 | + | |
| 9 | + | |
| 10 | +extern char *Py_GetProgramName(void); | |
| 11 | + | |
| 12 | +const struct filedescr _PyImport_DynLoadFiletab[] = { | |
| 13 | + {".o", "rb", C_EXTENSION}, | |
| 14 | + {"module.o", "rb", C_EXTENSION}, | |
| 15 | + {0, 0} | |
| 16 | +}; | |
| 17 | + | |
| 18 | + | |
| 19 | +dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname, | |
| 20 | + const char *pathname, FILE *fp) | |
| 21 | +{ | |
| 22 | + char funcname[258]; | |
| 23 | + | |
| 24 | + PyOS_snprintf(funcname, sizeof(funcname), "init%.200s", shortname); | |
| 25 | + return dl_loadmod(Py_GetProgramName(), pathname, funcname); | |
| 26 | +} | |
| @@ -0,0 +1,58 @@ | ||
| 1 | + | |
| 2 | +/* Support for dynamic loading of extension modules */ | |
| 3 | + | |
| 4 | +#include "dl.h" | |
| 5 | +#include <errno.h> | |
| 6 | + | |
| 7 | +#include "Python.h" | |
| 8 | +#include "importdl.h" | |
| 9 | + | |
| 10 | +#if defined(__hp9000s300) | |
| 11 | +#define FUNCNAME_PATTERN "_init%.200s" | |
| 12 | +#else | |
| 13 | +#define FUNCNAME_PATTERN "init%.200s" | |
| 14 | +#endif | |
| 15 | + | |
| 16 | +const struct filedescr _PyImport_DynLoadFiletab[] = { | |
| 17 | + {SHLIB_EXT, "rb", C_EXTENSION}, | |
| 18 | + {"module"SHLIB_EXT, "rb", C_EXTENSION}, | |
| 19 | + {0, 0} | |
| 20 | +}; | |
| 21 | + | |
| 22 | +dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname, | |
| 23 | + const char *pathname, FILE *fp) | |
| 24 | +{ | |
| 25 | + dl_funcptr p; | |
| 26 | + shl_t lib; | |
| 27 | + int flags; | |
| 28 | + char funcname[258]; | |
| 29 | + | |
| 30 | + flags = BIND_FIRST | BIND_DEFERRED; | |
| 31 | + if (Py_VerboseFlag) { | |
| 32 | + flags = BIND_FIRST | BIND_IMMEDIATE | | |
| 33 | + BIND_NONFATAL | BIND_VERBOSE; | |
| 34 | + printf("shl_load %s\n",pathname); | |
| 35 | + } | |
| 36 | + lib = shl_load(pathname, flags, 0); | |
| 37 | + /* XXX Chuck Blake once wrote that 0 should be BIND_NOSTART? */ | |
| 38 | + if (lib == NULL) { | |
| 39 | + char buf[256]; | |
| 40 | + if (Py_VerboseFlag) | |
| 41 | + perror(pathname); | |
| 42 | + PyOS_snprintf(buf, sizeof(buf), "Failed to load %.200s", | |
| 43 | + pathname); | |
| 44 | + PyErr_SetString(PyExc_ImportError, buf); | |
| 45 | + return NULL; | |
| 46 | + } | |
| 47 | + PyOS_snprintf(funcname, sizeof(funcname), FUNCNAME_PATTERN, shortname); | |
| 48 | + if (Py_VerboseFlag) | |
| 49 | + printf("shl_findsym %s\n", funcname); | |
| 50 | + if (shl_findsym(&lib, funcname, TYPE_UNDEFINED, (void *) &p) == -1) { | |
| 51 | + shl_unload(lib); | |
| 52 | + p = NULL; | |
| 53 | + } | |
| 54 | + if (p == NULL && Py_VerboseFlag) | |
| 55 | + perror(funcname); | |
| 56 | + | |
| 57 | + return p; | |
| 58 | +} | |
| @@ -0,0 +1,114 @@ | ||
| 1 | + | |
| 2 | +/* Support for dynamic loading of extension modules on Mac OS X | |
| 3 | +** All references to "NeXT" are for historical reasons. | |
| 4 | +*/ | |
| 5 | + | |
| 6 | +#include "Python.h" | |
| 7 | +#include "importdl.h" | |
| 8 | + | |
| 9 | +#include <mach-o/dyld.h> | |
| 10 | + | |
| 11 | +const struct filedescr _PyImport_DynLoadFiletab[] = { | |
| 12 | + {".so", "rb", C_EXTENSION}, | |
| 13 | + {"module.so", "rb", C_EXTENSION}, | |
| 14 | + {0, 0} | |
| 15 | +}; | |
| 16 | + | |
| 17 | +/* | |
| 18 | +** Python modules are Mach-O MH_BUNDLE files. The best way to load these | |
| 19 | +** is each in a private namespace, so you can load, say, a module bar and a | |
| 20 | +** module foo.bar. If we load everything in the global namespace the two | |
| 21 | +** initbar() symbols will conflict. | |
| 22 | +** However, it seems some extension packages depend upon being able to access | |
| 23 | +** each others' global symbols. There seems to be no way to eat our cake and | |
| 24 | +** have it, so the USE_DYLD_GLOBAL_NAMESPACE define determines which behaviour | |
| 25 | +** you get. | |
| 26 | +*/ | |
| 27 | + | |
| 28 | +#ifdef USE_DYLD_GLOBAL_NAMESPACE | |
| 29 | +#define LINKOPTIONS NSLINKMODULE_OPTION_BINDNOW|NSLINKMODULE_OPTION_RETURN_ON_ERROR | |
| 30 | +#else | |
| 31 | +#define LINKOPTIONS NSLINKMODULE_OPTION_BINDNOW| \ | |
| 32 | + NSLINKMODULE_OPTION_RETURN_ON_ERROR|NSLINKMODULE_OPTION_PRIVATE | |
| 33 | +#endif | |
| 34 | +dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname, | |
| 35 | + const char *pathname, FILE *fp) | |
| 36 | +{ | |
| 37 | + dl_funcptr p = NULL; | |
| 38 | + char funcname[258]; | |
| 39 | + NSObjectFileImageReturnCode rc; | |
| 40 | + NSObjectFileImage image; | |
| 41 | + NSModule newModule; | |
| 42 | + NSSymbol theSym; | |
| 43 | + const char *errString; | |
| 44 | + char errBuf[512]; | |
| 45 | + | |
| 46 | + PyOS_snprintf(funcname, sizeof(funcname), "_init%.200s", shortname); | |
| 47 | + | |
| 48 | +#ifdef USE_DYLD_GLOBAL_NAMESPACE | |
| 49 | + if (NSIsSymbolNameDefined(funcname)) { | |
| 50 | + theSym = NSLookupAndBindSymbol(funcname); | |
| 51 | + p = (dl_funcptr)NSAddressOfSymbol(theSym); | |
| 52 | + return p; | |
| 53 | + } | |
| 54 | +#endif | |
| 55 | + rc = NSCreateObjectFileImageFromFile(pathname, &image); | |
| 56 | + switch(rc) { | |
| 57 | + default: | |
| 58 | + case NSObjectFileImageFailure: | |
| 59 | + case NSObjectFileImageFormat: | |
| 60 | + /* for these a message is printed on stderr by dyld */ | |
| 61 | + errString = "Can't create object file image"; | |
| 62 | + break; | |
| 63 | + case NSObjectFileImageSuccess: | |
| 64 | + errString = NULL; | |
| 65 | + break; | |
| 66 | + case NSObjectFileImageInappropriateFile: | |
| 67 | + errString = "Inappropriate file type for dynamic loading"; | |
| 68 | + break; | |
| 69 | + case NSObjectFileImageArch: | |
| 70 | + errString = "Wrong CPU type in object file"; | |
| 71 | + break; | |
| 72 | + case NSObjectFileImageAccess: | |
| 73 | + errString = "Can't read object file (no access)"; | |
| 74 | + break; | |
| 75 | + } | |
| 76 | + if (errString == NULL) { | |
| 77 | + newModule = NSLinkModule(image, pathname, LINKOPTIONS); | |
| 78 | + if (newModule == NULL) { | |
| 79 | + int errNo; | |
| 80 | + const char *fileName, *moreErrorStr; | |
| 81 | + NSLinkEditErrors c; | |
| 82 | + NSLinkEditError( &c, &errNo, &fileName, &moreErrorStr ); | |
| 83 | + PyOS_snprintf(errBuf, 512, "Failure linking new module: %s: %s", | |
| 84 | + fileName, moreErrorStr); | |
| 85 | + errString = errBuf; | |
| 86 | + } | |
| 87 | + } | |
| 88 | + if (errString != NULL) { | |
| 89 | + PyErr_SetString(PyExc_ImportError, errString); | |
| 90 | + return NULL; | |
| 91 | + } | |
| 92 | +#ifdef USE_DYLD_GLOBAL_NAMESPACE | |
| 93 | + if (!NSIsSymbolNameDefined(funcname)) { | |
| 94 | + /* UnlinkModule() isn't implemented in current versions, but calling it does no harm */ | |
| 95 | + /* NSUnLinkModule(newModule, FALSE); removed: causes problems for ObjC code */ | |
| 96 | + PyErr_Format(PyExc_ImportError, | |
| 97 | + "Loaded module does not contain symbol %.200s", | |
| 98 | + funcname); | |
| 99 | + return NULL; | |
| 100 | + } | |
| 101 | + theSym = NSLookupAndBindSymbol(funcname); | |
| 102 | +#else | |
| 103 | + theSym = NSLookupSymbolInModule(newModule, funcname); | |
| 104 | + if ( theSym == NULL ) { | |
| 105 | + /* NSUnLinkModule(newModule, FALSE); removed: causes problems for ObjC code */ | |
| 106 | + PyErr_Format(PyExc_ImportError, | |
| 107 | + "Loaded module does not contain symbol %.200s", | |
| 108 | + funcname); | |
| 109 | + return NULL; | |
| 110 | + } | |
| 111 | +#endif | |
| 112 | + p = (dl_funcptr)NSAddressOfSymbol(theSym); | |
| 113 | + return p; | |
| 114 | +} | |
| @@ -0,0 +1,46 @@ | ||
| 1 | + | |
| 2 | +/* Support for dynamic loading of extension modules */ | |
| 3 | + | |
| 4 | +#define INCL_DOSERRORS | |
| 5 | +#define INCL_DOSMODULEMGR | |
| 6 | +#include <os2.h> | |
| 7 | + | |
| 8 | +#include "Python.h" | |
| 9 | +#include "importdl.h" | |
| 10 | + | |
| 11 | + | |
| 12 | +const struct filedescr _PyImport_DynLoadFiletab[] = { | |
| 13 | + {".pyd", "rb", C_EXTENSION}, | |
| 14 | + {".dll", "rb", C_EXTENSION}, | |
| 15 | + {0, 0} | |
| 16 | +}; | |
| 17 | + | |
| 18 | +dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname, | |
| 19 | + const char *pathname, FILE *fp) | |
| 20 | +{ | |
| 21 | + dl_funcptr p; | |
| 22 | + APIRET rc; | |
| 23 | + HMODULE hDLL; | |
| 24 | + char failreason[256]; | |
| 25 | + char funcname[258]; | |
| 26 | + | |
| 27 | + rc = DosLoadModule(failreason, | |
| 28 | + sizeof(failreason), | |
| 29 | + pathname, | |
| 30 | + &hDLL); | |
| 31 | + | |
| 32 | + if (rc != NO_ERROR) { | |
| 33 | + char errBuf[256]; | |
| 34 | + PyOS_snprintf(errBuf, sizeof(errBuf), | |
| 35 | + "DLL load failed, rc = %d: %.200s", | |
| 36 | + rc, failreason); | |
| 37 | + PyErr_SetString(PyExc_ImportError, errBuf); | |
| 38 | + return NULL; | |
| 39 | + } | |
| 40 | + | |
| 41 | + PyOS_snprintf(funcname, sizeof(funcname), "init%.200s", shortname); | |
| 42 | + rc = DosQueryProcAddr(hDLL, 0L, funcname, &p); | |
| 43 | + if (rc != NO_ERROR) | |
| 44 | + p = NULL; /* Signify Failure to Acquire Entrypoint */ | |
| 45 | + return p; | |
| 46 | +} | |
| @@ -0,0 +1,143 @@ | ||
| 1 | + | |
| 2 | +/* Support for dynamic loading of extension modules */ | |
| 3 | + | |
| 4 | +#include "Python.h" | |
| 5 | +#include "importdl.h" | |
| 6 | + | |
| 7 | +#include <sys/types.h> | |
| 8 | +#include <sys/stat.h> | |
| 9 | + | |
| 10 | +#if defined(__NetBSD__) | |
| 11 | +#include <sys/param.h> | |
| 12 | +#if (NetBSD < 199712) | |
| 13 | +#include <nlist.h> | |
| 14 | +#include <link.h> | |
| 15 | +#define dlerror() "error in dynamic linking" | |
| 16 | +#endif | |
| 17 | +#endif /* NetBSD */ | |
| 18 | + | |
| 19 | +#ifdef HAVE_DLFCN_H | |
| 20 | +#include <dlfcn.h> | |
| 21 | +#else | |
| 22 | +#if defined(PYOS_OS2) && defined(PYCC_GCC) | |
| 23 | +#include "dlfcn.h" | |
| 24 | +#endif | |
| 25 | +#endif | |
| 26 | + | |
| 27 | +#if (defined(__OpenBSD__) || defined(__NetBSD__)) && !defined(__ELF__) | |
| 28 | +#define LEAD_UNDERSCORE "_" | |
| 29 | +#else | |
| 30 | +#define LEAD_UNDERSCORE "" | |
| 31 | +#endif | |
| 32 | + | |
| 33 | + | |
| 34 | +const struct filedescr _PyImport_DynLoadFiletab[] = { | |
| 35 | +#ifdef __CYGWIN__ | |
| 36 | + {".dll", "rb", C_EXTENSION}, | |
| 37 | + {"module.dll", "rb", C_EXTENSION}, | |
| 38 | +#else | |
| 39 | +#if defined(PYOS_OS2) && defined(PYCC_GCC) | |
| 40 | + {".pyd", "rb", C_EXTENSION}, | |
| 41 | + {".dll", "rb", C_EXTENSION}, | |
| 42 | +#else | |
| 43 | +#ifdef __VMS | |
| 44 | + {".exe", "rb", C_EXTENSION}, | |
| 45 | + {".EXE", "rb", C_EXTENSION}, | |
| 46 | + {"module.exe", "rb", C_EXTENSION}, | |
| 47 | + {"MODULE.EXE", "rb", C_EXTENSION}, | |
| 48 | +#else | |
| 49 | + {".so", "rb", C_EXTENSION}, | |
| 50 | + {"module.so", "rb", C_EXTENSION}, | |
| 51 | +#endif | |
| 52 | +#endif | |
| 53 | +#endif | |
| 54 | + {0, 0} | |
| 55 | +}; | |
| 56 | + | |
| 57 | +static struct { | |
| 58 | + dev_t dev; | |
| 59 | +#ifdef __VMS | |
| 60 | + ino_t ino[3]; | |
| 61 | +#else | |
| 62 | + ino_t ino; | |
| 63 | +#endif | |
| 64 | + void *handle; | |
| 65 | +} handles[128]; | |
| 66 | +static int nhandles = 0; | |
| 67 | + | |
| 68 | + | |
| 69 | +dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname, | |
| 70 | + const char *pathname, FILE *fp) | |
| 71 | +{ | |
| 72 | + dl_funcptr p; | |
| 73 | + void *handle; | |
| 74 | + char funcname[258]; | |
| 75 | + char pathbuf[260]; | |
| 76 | + int dlopenflags=0; | |
| 77 | + | |
| 78 | + if (strchr(pathname, '/') == NULL) { | |
| 79 | + /* Prefix bare filename with "./" */ | |
| 80 | + PyOS_snprintf(pathbuf, sizeof(pathbuf), "./%-.255s", pathname); | |
| 81 | + pathname = pathbuf; | |
| 82 | + } | |
| 83 | + | |
| 84 | + PyOS_snprintf(funcname, sizeof(funcname), | |
| 85 | + LEAD_UNDERSCORE "init%.200s", shortname); | |
| 86 | + | |
| 87 | + if (fp != NULL) { | |
| 88 | + int i; | |
| 89 | + struct stat statb; | |
| 90 | + fstat(fileno(fp), &statb); | |
| 91 | + for (i = 0; i < nhandles; i++) { | |
| 92 | + if (statb.st_dev == handles[i].dev && | |
| 93 | + statb.st_ino == handles[i].ino) { | |
| 94 | + p = (dl_funcptr) dlsym(handles[i].handle, | |
| 95 | + funcname); | |
| 96 | + return p; | |
| 97 | + } | |
| 98 | + } | |
| 99 | + if (nhandles < 128) { | |
| 100 | + handles[nhandles].dev = statb.st_dev; | |
| 101 | +#ifdef __VMS | |
| 102 | + handles[nhandles].ino[0] = statb.st_ino[0]; | |
| 103 | + handles[nhandles].ino[1] = statb.st_ino[1]; | |
| 104 | + handles[nhandles].ino[2] = statb.st_ino[2]; | |
| 105 | +#else | |
| 106 | + handles[nhandles].ino = statb.st_ino; | |
| 107 | +#endif | |
| 108 | + } | |
| 109 | + } | |
| 110 | + | |
| 111 | +#if !(defined(PYOS_OS2) && defined(PYCC_GCC)) | |
| 112 | + dlopenflags = PyThreadState_GET()->interp->dlopenflags; | |
| 113 | +#endif | |
| 114 | + | |
| 115 | + if (Py_VerboseFlag) | |
| 116 | + PySys_WriteStderr("dlopen(\"%s\", %x);\n", pathname, | |
| 117 | + dlopenflags); | |
| 118 | + | |
| 119 | +#ifdef __VMS | |
| 120 | + /* VMS currently don't allow a pathname, use a logical name instead */ | |
| 121 | + /* Concatenate 'python_module_' and shortname */ | |
| 122 | + /* so "import vms.bar" will use the logical python_module_bar */ | |
| 123 | + /* As C module use only one name space this is probably not a */ | |
| 124 | + /* important limitation */ | |
| 125 | + PyOS_snprintf(pathbuf, sizeof(pathbuf), "python_module_%-.200s", | |
| 126 | + shortname); | |
| 127 | + pathname = pathbuf; | |
| 128 | +#endif | |
| 129 | + | |
| 130 | + handle = dlopen(pathname, dlopenflags); | |
| 131 | + | |
| 132 | + if (handle == NULL) { | |
| 133 | + const char *error = dlerror(); | |
| 134 | + if (error == NULL) | |
| 135 | + error = "unknown dlopen() error"; | |
| 136 | + PyErr_SetString(PyExc_ImportError, error); | |
| 137 | + return NULL; | |
| 138 | + } | |
| 139 | + if (fp != NULL && nhandles < 128) | |
| 140 | + handles[nhandles++].handle = handle; | |
| 141 | + p = (dl_funcptr) dlsym(handle, funcname); | |
| 142 | + return p; | |
| 143 | +} | |
| @@ -0,0 +1,11 @@ | ||
| 1 | + | |
| 2 | +/* This module provides the necessary stubs for when dynamic loading is | |
| 3 | + not present. */ | |
| 4 | + | |
| 5 | +#include "Python.h" | |
| 6 | +#include "importdl.h" | |
| 7 | + | |
| 8 | + | |
| 9 | +const struct filedescr _PyImport_DynLoadFiletab[] = { | |
| 10 | + {0, 0} | |
| 11 | +}; | |
| @@ -0,0 +1,263 @@ | ||
| 1 | + | |
| 2 | +/* Support for dynamic loading of extension modules */ | |
| 3 | + | |
| 4 | +#include <windows.h> | |
| 5 | +#ifdef HAVE_DIRECT_H | |
| 6 | +#include <direct.h> | |
| 7 | +#endif | |
| 8 | +#include <ctype.h> | |
| 9 | + | |
| 10 | +#include "Python.h" | |
| 11 | +#include "importdl.h" | |
| 12 | + | |
| 13 | +const struct filedescr _PyImport_DynLoadFiletab[] = { | |
| 14 | +#ifdef _DEBUG | |
| 15 | + {"_d.pyd", "rb", C_EXTENSION}, | |
| 16 | + /* Temporarily disable .dll, to avoid conflicts between sqlite3.dll | |
| 17 | + and the sqlite3 package. If this needs to be reverted for 2.5, | |
| 18 | + some other solution for the naming conflict must be found. | |
| 19 | + {"_d.dll", "rb", C_EXTENSION}, | |
| 20 | + */ | |
| 21 | +#else | |
| 22 | + {".pyd", "rb", C_EXTENSION}, | |
| 23 | + /* Likewise | |
| 24 | + {".dll", "rb", C_EXTENSION}, | |
| 25 | + */ | |
| 26 | +#endif | |
| 27 | + {0, 0} | |
| 28 | +}; | |
| 29 | + | |
| 30 | + | |
| 31 | +/* Case insensitive string compare, to avoid any dependencies on particular | |
| 32 | + C RTL implementations */ | |
| 33 | + | |
| 34 | +static int strcasecmp (char *string1, char *string2) | |
| 35 | +{ | |
| 36 | + int first, second; | |
| 37 | + | |
| 38 | + do { | |
| 39 | + first = tolower(*string1); | |
| 40 | + second = tolower(*string2); | |
| 41 | + string1++; | |
| 42 | + string2++; | |
| 43 | + } while (first && first == second); | |
| 44 | + | |
| 45 | + return (first - second); | |
| 46 | +} | |
| 47 | + | |
| 48 | + | |
| 49 | +/* Function to return the name of the "python" DLL that the supplied module | |
| 50 | + directly imports. Looks through the list of imported modules and | |
| 51 | + returns the first entry that starts with "python" (case sensitive) and | |
| 52 | + is followed by nothing but numbers until the separator (period). | |
| 53 | + | |
| 54 | + Returns a pointer to the import name, or NULL if no matching name was | |
| 55 | + located. | |
| 56 | + | |
| 57 | + This function parses through the PE header for the module as loaded in | |
| 58 | + memory by the system loader. The PE header is accessed as documented by | |
| 59 | + Microsoft in the MSDN PE and COFF specification (2/99), and handles | |
| 60 | + both PE32 and PE32+. It only worries about the direct import table and | |
| 61 | + not the delay load import table since it's unlikely an extension is | |
| 62 | + going to be delay loading Python (after all, it's already loaded). | |
| 63 | + | |
| 64 | + If any magic values are not found (e.g., the PE header or optional | |
| 65 | + header magic), then this function simply returns NULL. */ | |
| 66 | + | |
| 67 | +#define DWORD_AT(mem) (*(DWORD *)(mem)) | |
| 68 | +#define WORD_AT(mem) (*(WORD *)(mem)) | |
| 69 | + | |
| 70 | +static char *GetPythonImport (HINSTANCE hModule) | |
| 71 | +{ | |
| 72 | + unsigned char *dllbase, *import_data, *import_name; | |
| 73 | + DWORD pe_offset, opt_offset; | |
| 74 | + WORD opt_magic; | |
| 75 | + int num_dict_off, import_off; | |
| 76 | + | |
| 77 | + /* Safety check input */ | |
| 78 | + if (hModule == NULL) { | |
| 79 | + return NULL; | |
| 80 | + } | |
| 81 | + | |
| 82 | + /* Module instance is also the base load address. First portion of | |
| 83 | + memory is the MS-DOS loader, which holds the offset to the PE | |
| 84 | + header (from the load base) at 0x3C */ | |
| 85 | + dllbase = (unsigned char *)hModule; | |
| 86 | + pe_offset = DWORD_AT(dllbase + 0x3C); | |
| 87 | + | |
| 88 | + /* The PE signature must be "PE\0\0" */ | |
175 more lines aren’t shown here.
This diff is large: its 823 lines aren’t shown here.
This diff is large: its 28 lines aren’t shown here.
This diff is large: its 39 lines aren’t shown here.
This diff is large: its 69 lines aren’t shown here.
This diff is large: its 137 lines aren’t shown here.
This diff is large: its 1772 lines aren’t shown here.
This diff is large: its 29 lines aren’t shown here.
This diff is large: its 24 lines aren’t shown here.
This diff is large: its 84 lines aren’t shown here.
This diff is large: its 27 lines aren’t shown here.
This diff is large: its 116 lines aren’t shown here.
This diff is large: its 13 lines aren’t shown here.
This diff is large: its 16 lines aren’t shown here.
This diff is large: its 2130 lines aren’t shown here.
This diff is large: its 24 lines aren’t shown here.
This diff is large: its 3161 lines aren’t shown here.
This diff is large: its 79 lines aren’t shown here.
This diff is large: its 54 lines aren’t shown here.
This diff is large: its 471 lines aren’t shown here.
This diff is large: its 1156 lines aren’t shown here.
This diff is large: its 26 lines aren’t shown here.
This diff is large: its 65 lines aren’t shown here.
This diff is large: its 632 lines aren’t shown here.
This diff is large: its 94 lines aren’t shown here.
This diff is large: its 233 lines aren’t shown here.
This diff is large: its 221 lines aren’t shown here.
This diff is large: its 24 lines aren’t shown here.
This diff is large: its 633 lines aren’t shown here.
This diff is large: its 249 lines aren’t shown here.
This diff is large: its 1858 lines aren’t shown here.
This diff is large: its 20 lines aren’t shown here.
This diff is large: its 15 lines aren’t shown here.
This diff is large: its 20 lines aren’t shown here.
This diff is large: its 157 lines aren’t shown here.
This diff is large: its 313 lines aren’t shown here.
This diff is large: its 1426 lines aren’t shown here.
This diff is large: its 1469 lines aren’t shown here.
This diff is large: its 385 lines aren’t shown here.
This diff is large: its 301 lines aren’t shown here.
This diff is large: its 288 lines aren’t shown here.
This diff is large: its 157 lines aren’t shown here.
This diff is large: its 116 lines aren’t shown here.
This diff is large: its 150 lines aren’t shown here.
This diff is large: its 365 lines aren’t shown here.
This diff is large: its 308 lines aren’t shown here.
This diff is large: its 136 lines aren’t shown here.
This diff is large: its 214 lines aren’t shown here.
This diff is large: its 534 lines aren’t shown here.
This diff is large: its 376 lines aren’t shown here.
This diff is large: its 175 lines aren’t shown here.
This diff is large: its 172 lines aren’t shown here.
This diff is large: its 263 lines aren’t shown here.