comparison Python/Python-ast.c @ 46229:07b36d2b7534

Merged revisions 62049,62054 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r62049 | georg.brandl | 2008-03-30 00:01:47 -0700 (Sun, 30 Mar 2008) | 2 lines #2505: allow easier creation of AST nodes. ........ r62054 | georg.brandl | 2008-03-30 12:43:27 -0700 (Sun, 30 Mar 2008) | 2 lines Fix error message -- "expects either 0 or 0 arguments" ........
author Neal Norwitz <nnorwitz@gmail.com>
date Mon, 31 Mar 2008 04:42:11 +0000
parents b2d93a960b98
children 81ee270a6eb5
comparison
equal deleted inserted replaced
46227:b2d93a960b98 46229:07b36d2b7534
10 */ 10 */
11 11
12 #include "Python.h" 12 #include "Python.h"
13 #include "Python-ast.h" 13 #include "Python-ast.h"
14 14
15 static PyTypeObject* AST_type; 15 static PyTypeObject AST_type;
16 static PyTypeObject *mod_type; 16 static PyTypeObject *mod_type;
17 static PyObject* ast2obj_mod(void*); 17 static PyObject* ast2obj_mod(void*);
18 static PyTypeObject *Module_type; 18 static PyTypeObject *Module_type;
19 static char *Module_fields[]={ 19 static char *Module_fields[]={
20 "body", 20 "body",
389 "name", 389 "name",
390 "asname", 390 "asname",
391 }; 391 };
392 392
393 393
394 static int
395 ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
396 {
397 Py_ssize_t i, numfields = 0;
398 int res = -1;
399 PyObject *key, *value, *fields;
400 fields = PyObject_GetAttrString((PyObject*)Py_TYPE(self), "_fields");
401 if (!fields)
402 PyErr_Clear();
403 if (fields) {
404 numfields = PySequence_Size(fields);
405 if (numfields == -1)
406 goto cleanup;
407 }
408 res = 0; /* if no error occurs, this stays 0 to the end */
409 if (PyTuple_GET_SIZE(args) > 0) {
410 if (numfields != PyTuple_GET_SIZE(args)) {
411 PyErr_Format(PyExc_TypeError, "%.400s constructor takes %s"
412 "%" PY_FORMAT_SIZE_T "d positional argument%s",
413 Py_TYPE(self)->tp_name,
414 numfields == 0 ? "" : "either 0 or ",
415 numfields, numfields == 1 ? "" : "s");
416 res = -1;
417 goto cleanup;
418 }
419 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
420 /* cannot be reached when fields is NULL */
421 PyObject *name = PySequence_GetItem(fields, i);
422 if (!name) {
423 res = -1;
424 goto cleanup;
425 }
426 res = PyObject_SetAttr(self, name, PyTuple_GET_ITEM(args, i));
427 Py_DECREF(name);
428 if (res < 0)
429 goto cleanup;
430 }
431 }
432 if (kw) {
433 i = 0; /* needed by PyDict_Next */
434 while (PyDict_Next(kw, &i, &key, &value)) {
435 res = PyObject_SetAttr(self, key, value);
436 if (res < 0)
437 goto cleanup;
438 }
439 }
440 cleanup:
441 Py_XDECREF(fields);
442 return res;
443 }
444
445 static PyTypeObject AST_type = {
446 PyVarObject_HEAD_INIT(&PyType_Type, 0)
447 "AST",
448 sizeof(PyObject),
449 0,
450 0, /* tp_dealloc */
451 0, /* tp_print */
452 0, /* tp_getattr */
453 0, /* tp_setattr */
454 0, /* tp_compare */
455 0, /* tp_repr */
456 0, /* tp_as_number */
457 0, /* tp_as_sequence */
458 0, /* tp_as_mapping */
459 0, /* tp_hash */
460 0, /* tp_call */
461 0, /* tp_str */
462 PyObject_GenericGetAttr, /* tp_getattro */
463 PyObject_GenericSetAttr, /* tp_setattro */
464 0, /* tp_as_buffer */
465 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
466 0, /* tp_doc */
467 0, /* tp_traverse */
468 0, /* tp_clear */
469 0, /* tp_richcompare */
470 0, /* tp_weaklistoffset */
471 0, /* tp_iter */
472 0, /* tp_iternext */
473 0, /* tp_methods */
474 0, /* tp_members */
475 0, /* tp_getset */
476 0, /* tp_base */
477 0, /* tp_dict */
478 0, /* tp_descr_get */
479 0, /* tp_descr_set */
480 0, /* tp_dictoffset */
481 (initproc)ast_type_init, /* tp_init */
482 PyType_GenericAlloc, /* tp_alloc */
483 PyType_GenericNew, /* tp_new */
484 PyObject_Del, /* tp_free */
485 };
486
487
394 static PyTypeObject* make_type(char *type, PyTypeObject* base, char**fields, int num_fields) 488 static PyTypeObject* make_type(char *type, PyTypeObject* base, char**fields, int num_fields)
395 { 489 {
396 PyObject *fnames, *result; 490 PyObject *fnames, *result;
397 int i; 491 int i;
398 if (num_fields) { 492 if (num_fields) {
417 } 511 }
418 512
419 static int add_attributes(PyTypeObject* type, char**attrs, int num_fields) 513 static int add_attributes(PyTypeObject* type, char**attrs, int num_fields)
420 { 514 {
421 int i, result; 515 int i, result;
422 PyObject *s, *l = PyList_New(num_fields); 516 PyObject *s, *l = PyTuple_New(num_fields);
423 if (!l) return 0; 517 if (!l) return 0;
424 for(i = 0; i < num_fields; i++) { 518 for(i = 0; i < num_fields; i++) {
425 s = PyUnicode_FromString(attrs[i]); 519 s = PyUnicode_FromString(attrs[i]);
426 if (!s) { 520 if (!s) {
427 Py_DECREF(l); 521 Py_DECREF(l);
428 return 0; 522 return 0;
429 } 523 }
430 PyList_SET_ITEM(l, i, s); 524 PyTuple_SET_ITEM(l, i, s);
431 } 525 }
432 result = PyObject_SetAttrString((PyObject*)type, "_attributes", l) >= 0; 526 result = PyObject_SetAttrString((PyObject*)type, "_attributes", l) >= 0;
433 Py_DECREF(l); 527 Py_DECREF(l);
434 return result; 528 return result;
435 } 529 }
507 601
508 static int init_types(void) 602 static int init_types(void)
509 { 603 {
510 static int initialized; 604 static int initialized;
511 if (initialized) return 1; 605 if (initialized) return 1;
512 AST_type = make_type("AST", &PyBaseObject_Type, NULL, 0); 606 mod_type = make_type("mod", &AST_type, NULL, 0);
513 mod_type = make_type("mod", AST_type, NULL, 0);
514 if (!mod_type) return 0; 607 if (!mod_type) return 0;
515 if (!add_attributes(mod_type, NULL, 0)) return 0; 608 if (!add_attributes(mod_type, NULL, 0)) return 0;
516 Module_type = make_type("Module", mod_type, Module_fields, 1); 609 Module_type = make_type("Module", mod_type, Module_fields, 1);
517 if (!Module_type) return 0; 610 if (!Module_type) return 0;
518 Interactive_type = make_type("Interactive", mod_type, 611 Interactive_type = make_type("Interactive", mod_type,
521 Expression_type = make_type("Expression", mod_type, Expression_fields, 614 Expression_type = make_type("Expression", mod_type, Expression_fields,
522 1); 615 1);
523 if (!Expression_type) return 0; 616 if (!Expression_type) return 0;
524 Suite_type = make_type("Suite", mod_type, Suite_fields, 1); 617 Suite_type = make_type("Suite", mod_type, Suite_fields, 1);
525 if (!Suite_type) return 0; 618 if (!Suite_type) return 0;
526 stmt_type = make_type("stmt", AST_type, NULL, 0); 619 stmt_type = make_type("stmt", &AST_type, NULL, 0);
527 if (!stmt_type) return 0; 620 if (!stmt_type) return 0;
528 if (!add_attributes(stmt_type, stmt_attributes, 2)) return 0; 621 if (!add_attributes(stmt_type, stmt_attributes, 2)) return 0;
529 FunctionDef_type = make_type("FunctionDef", stmt_type, 622 FunctionDef_type = make_type("FunctionDef", stmt_type,
530 FunctionDef_fields, 5); 623 FunctionDef_fields, 5);
531 if (!FunctionDef_type) return 0; 624 if (!FunctionDef_type) return 0;
571 if (!Pass_type) return 0; 664 if (!Pass_type) return 0;
572 Break_type = make_type("Break", stmt_type, NULL, 0); 665 Break_type = make_type("Break", stmt_type, NULL, 0);
573 if (!Break_type) return 0; 666 if (!Break_type) return 0;
574 Continue_type = make_type("Continue", stmt_type, NULL, 0); 667 Continue_type = make_type("Continue", stmt_type, NULL, 0);
575 if (!Continue_type) return 0; 668 if (!Continue_type) return 0;
576 expr_type = make_type("expr", AST_type, NULL, 0); 669 expr_type = make_type("expr", &AST_type, NULL, 0);
577 if (!expr_type) return 0; 670 if (!expr_type) return 0;
578 if (!add_attributes(expr_type, expr_attributes, 2)) return 0; 671 if (!add_attributes(expr_type, expr_attributes, 2)) return 0;
579 BoolOp_type = make_type("BoolOp", expr_type, BoolOp_fields, 2); 672 BoolOp_type = make_type("BoolOp", expr_type, BoolOp_fields, 2);
580 if (!BoolOp_type) return 0; 673 if (!BoolOp_type) return 0;
581 BinOp_type = make_type("BinOp", expr_type, BinOp_fields, 3); 674 BinOp_type = make_type("BinOp", expr_type, BinOp_fields, 3);
623 if (!Name_type) return 0; 716 if (!Name_type) return 0;
624 List_type = make_type("List", expr_type, List_fields, 2); 717 List_type = make_type("List", expr_type, List_fields, 2);
625 if (!List_type) return 0; 718 if (!List_type) return 0;
626 Tuple_type = make_type("Tuple", expr_type, Tuple_fields, 2); 719 Tuple_type = make_type("Tuple", expr_type, Tuple_fields, 2);
627 if (!Tuple_type) return 0; 720 if (!Tuple_type) return 0;
628 expr_context_type = make_type("expr_context", AST_type, NULL, 0); 721 expr_context_type = make_type("expr_context", &AST_type, NULL, 0);
629 if (!expr_context_type) return 0; 722 if (!expr_context_type) return 0;
630 if (!add_attributes(expr_context_type, NULL, 0)) return 0; 723 if (!add_attributes(expr_context_type, NULL, 0)) return 0;
631 Load_type = make_type("Load", expr_context_type, NULL, 0); 724 Load_type = make_type("Load", expr_context_type, NULL, 0);
632 if (!Load_type) return 0; 725 if (!Load_type) return 0;
633 Load_singleton = PyType_GenericNew(Load_type, NULL, NULL); 726 Load_singleton = PyType_GenericNew(Load_type, NULL, NULL);
650 if (!AugStore_singleton) return 0; 743 if (!AugStore_singleton) return 0;
651 Param_type = make_type("Param", expr_context_type, NULL, 0); 744 Param_type = make_type("Param", expr_context_type, NULL, 0);
652 if (!Param_type) return 0; 745 if (!Param_type) return 0;
653 Param_singleton = PyType_GenericNew(Param_type, NULL, NULL); 746 Param_singleton = PyType_GenericNew(Param_type, NULL, NULL);
654 if (!Param_singleton) return 0; 747 if (!Param_singleton) return 0;
655 slice_type = make_type("slice", AST_type, NULL, 0); 748 slice_type = make_type("slice", &AST_type, NULL, 0);
656 if (!slice_type) return 0; 749 if (!slice_type) return 0;
657 if (!add_attributes(slice_type, NULL, 0)) return 0; 750 if (!add_attributes(slice_type, NULL, 0)) return 0;
658 Slice_type = make_type("Slice", slice_type, Slice_fields, 3); 751 Slice_type = make_type("Slice", slice_type, Slice_fields, 3);
659 if (!Slice_type) return 0; 752 if (!Slice_type) return 0;
660 ExtSlice_type = make_type("ExtSlice", slice_type, ExtSlice_fields, 1); 753 ExtSlice_type = make_type("ExtSlice", slice_type, ExtSlice_fields, 1);
661 if (!ExtSlice_type) return 0; 754 if (!ExtSlice_type) return 0;
662 Index_type = make_type("Index", slice_type, Index_fields, 1); 755 Index_type = make_type("Index", slice_type, Index_fields, 1);
663 if (!Index_type) return 0; 756 if (!Index_type) return 0;
664 boolop_type = make_type("boolop", AST_type, NULL, 0); 757 boolop_type = make_type("boolop", &AST_type, NULL, 0);
665 if (!boolop_type) return 0; 758 if (!boolop_type) return 0;
666 if (!add_attributes(boolop_type, NULL, 0)) return 0; 759 if (!add_attributes(boolop_type, NULL, 0)) return 0;
667 And_type = make_type("And", boolop_type, NULL, 0); 760 And_type = make_type("And", boolop_type, NULL, 0);
668 if (!And_type) return 0; 761 if (!And_type) return 0;
669 And_singleton = PyType_GenericNew(And_type, NULL, NULL); 762 And_singleton = PyType_GenericNew(And_type, NULL, NULL);
670 if (!And_singleton) return 0; 763 if (!And_singleton) return 0;
671 Or_type = make_type("Or", boolop_type, NULL, 0); 764 Or_type = make_type("Or", boolop_type, NULL, 0);
672 if (!Or_type) return 0; 765 if (!Or_type) return 0;
673 Or_singleton = PyType_GenericNew(Or_type, NULL, NULL); 766 Or_singleton = PyType_GenericNew(Or_type, NULL, NULL);
674 if (!Or_singleton) return 0; 767 if (!Or_singleton) return 0;
675 operator_type = make_type("operator", AST_type, NULL, 0); 768 operator_type = make_type("operator", &AST_type, NULL, 0);
676 if (!operator_type) return 0; 769 if (!operator_type) return 0;
677 if (!add_attributes(operator_type, NULL, 0)) return 0; 770 if (!add_attributes(operator_type, NULL, 0)) return 0;
678 Add_type = make_type("Add", operator_type, NULL, 0); 771 Add_type = make_type("Add", operator_type, NULL, 0);
679 if (!Add_type) return 0; 772 if (!Add_type) return 0;
680 Add_singleton = PyType_GenericNew(Add_type, NULL, NULL); 773 Add_singleton = PyType_GenericNew(Add_type, NULL, NULL);
721 if (!BitAnd_singleton) return 0; 814 if (!BitAnd_singleton) return 0;
722 FloorDiv_type = make_type("FloorDiv", operator_type, NULL, 0); 815 FloorDiv_type = make_type("FloorDiv", operator_type, NULL, 0);
723 if (!FloorDiv_type) return 0; 816 if (!FloorDiv_type) return 0;
724 FloorDiv_singleton = PyType_GenericNew(FloorDiv_type, NULL, NULL); 817 FloorDiv_singleton = PyType_GenericNew(FloorDiv_type, NULL, NULL);
725 if (!FloorDiv_singleton) return 0; 818 if (!FloorDiv_singleton) return 0;
726 unaryop_type = make_type("unaryop", AST_type, NULL, 0); 819 unaryop_type = make_type("unaryop", &AST_type, NULL, 0);
727 if (!unaryop_type) return 0; 820 if (!unaryop_type) return 0;
728 if (!add_attributes(unaryop_type, NULL, 0)) return 0; 821 if (!add_attributes(unaryop_type, NULL, 0)) return 0;
729 Invert_type = make_type("Invert", unaryop_type, NULL, 0); 822 Invert_type = make_type("Invert", unaryop_type, NULL, 0);
730 if (!Invert_type) return 0; 823 if (!Invert_type) return 0;
731 Invert_singleton = PyType_GenericNew(Invert_type, NULL, NULL); 824 Invert_singleton = PyType_GenericNew(Invert_type, NULL, NULL);
740 if (!UAdd_singleton) return 0; 833 if (!UAdd_singleton) return 0;
741 USub_type = make_type("USub", unaryop_type, NULL, 0); 834 USub_type = make_type("USub", unaryop_type, NULL, 0);
742 if (!USub_type) return 0; 835 if (!USub_type) return 0;
743 USub_singleton = PyType_GenericNew(USub_type, NULL, NULL); 836 USub_singleton = PyType_GenericNew(USub_type, NULL, NULL);
744 if (!USub_singleton) return 0; 837 if (!USub_singleton) return 0;
745 cmpop_type = make_type("cmpop", AST_type, NULL, 0); 838 cmpop_type = make_type("cmpop", &AST_type, NULL, 0);
746 if (!cmpop_type) return 0; 839 if (!cmpop_type) return 0;
747 if (!add_attributes(cmpop_type, NULL, 0)) return 0; 840 if (!add_attributes(cmpop_type, NULL, 0)) return 0;
748 Eq_type = make_type("Eq", cmpop_type, NULL, 0); 841 Eq_type = make_type("Eq", cmpop_type, NULL, 0);
749 if (!Eq_type) return 0; 842 if (!Eq_type) return 0;
750 Eq_singleton = PyType_GenericNew(Eq_type, NULL, NULL); 843 Eq_singleton = PyType_GenericNew(Eq_type, NULL, NULL);
783 if (!In_singleton) return 0; 876 if (!In_singleton) return 0;
784 NotIn_type = make_type("NotIn", cmpop_type, NULL, 0); 877 NotIn_type = make_type("NotIn", cmpop_type, NULL, 0);
785 if (!NotIn_type) return 0; 878 if (!NotIn_type) return 0;
786 NotIn_singleton = PyType_GenericNew(NotIn_type, NULL, NULL); 879 NotIn_singleton = PyType_GenericNew(NotIn_type, NULL, NULL);
787 if (!NotIn_singleton) return 0; 880 if (!NotIn_singleton) return 0;
788 comprehension_type = make_type("comprehension", AST_type, 881 comprehension_type = make_type("comprehension", &AST_type,
789 comprehension_fields, 3); 882 comprehension_fields, 3);
790 if (!comprehension_type) return 0; 883 if (!comprehension_type) return 0;
791 excepthandler_type = make_type("excepthandler", AST_type, 884 excepthandler_type = make_type("excepthandler", &AST_type,
792 excepthandler_fields, 5); 885 excepthandler_fields, 5);
793 if (!excepthandler_type) return 0; 886 if (!excepthandler_type) return 0;
794 arguments_type = make_type("arguments", AST_type, arguments_fields, 8); 887 arguments_type = make_type("arguments", &AST_type, arguments_fields, 8);
795 if (!arguments_type) return 0; 888 if (!arguments_type) return 0;
796 arg_type = make_type("arg", AST_type, arg_fields, 2); 889 arg_type = make_type("arg", &AST_type, arg_fields, 2);
797 if (!arg_type) return 0; 890 if (!arg_type) return 0;
798 keyword_type = make_type("keyword", AST_type, keyword_fields, 2); 891 keyword_type = make_type("keyword", &AST_type, keyword_fields, 2);
799 if (!keyword_type) return 0; 892 if (!keyword_type) return 0;
800 alias_type = make_type("alias", AST_type, alias_fields, 2); 893 alias_type = make_type("alias", &AST_type, alias_fields, 2);
801 if (!alias_type) return 0; 894 if (!alias_type) return 0;
802 initialized = 1; 895 initialized = 1;
803 return 1; 896 return 1;
804 } 897 }
805 898
6251 PyObject *m, *d; 6344 PyObject *m, *d;
6252 if (!init_types()) return; 6345 if (!init_types()) return;
6253 m = Py_InitModule3("_ast", NULL, NULL); 6346 m = Py_InitModule3("_ast", NULL, NULL);
6254 if (!m) return; 6347 if (!m) return;
6255 d = PyModule_GetDict(m); 6348 d = PyModule_GetDict(m);
6256 if (PyDict_SetItemString(d, "AST", (PyObject*)AST_type) < 0) return; 6349 if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return;
6257 if (PyModule_AddIntConstant(m, "PyCF_ONLY_AST", PyCF_ONLY_AST) < 0) 6350 if (PyModule_AddIntConstant(m, "PyCF_ONLY_AST", PyCF_ONLY_AST) < 0)
6258 return; 6351 return;
6259 if (PyModule_AddStringConstant(m, "__version__", "57783") < 0) 6352 if (PyModule_AddStringConstant(m, "__version__", "57783") < 0)
6260 return; 6353 return;
6261 if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return; 6354 if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return;
6438 } 6531 }
6439 6532
6440 int PyAST_Check(PyObject* obj) 6533 int PyAST_Check(PyObject* obj)
6441 { 6534 {
6442 init_types(); 6535 init_types();
6443 return PyObject_IsInstance(obj, (PyObject*)AST_type); 6536 return PyObject_IsInstance(obj, (PyObject*)&AST_type);
6444 } 6537 }
6445 6538
6446 6539