comparison Python/Python-ast.c @ 46202:42c5d287a852 legacy-trunk

#2505: allow easier creation of AST nodes.
author Georg Brandl <georg@python.org>
date Sun, 30 Mar 2008 07:01:47 +0000
parents 2ff88ae9850c
children 29ea507fd998
comparison
equal deleted inserted replaced
46201:e370d9cd149e 46202:42c5d287a852
1 /* File automatically generated by Parser/asdl_c.py. */ 1 /* File automatically generated by Parser/asdl_c.py. */
2 2
3 3
4 /* 4 /*
5 __version__ 60978. 5 __version__ 62047.
6 6
7 This module must be committed separately after each AST grammar change; 7 This module must be committed separately after each AST grammar change;
8 The __version__ number is set to the revision number of the commit 8 The __version__ number is set to the revision number of the commit
9 containing the grammar change. 9 containing the grammar change.
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",
367 "name", 367 "name",
368 "asname", 368 "asname",
369 }; 369 };
370 370
371 371
372 static int
373 ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
374 {
375 Py_ssize_t i, numfields = 0;
376 int res = -1;
377 PyObject *key, *value, *fields;
378 fields = PyObject_GetAttrString((PyObject*)Py_TYPE(self), "_fields");
379 if (!fields)
380 PyErr_Clear();
381 if (fields) {
382 numfields = PySequence_Size(fields);
383 if (numfields == -1)
384 goto cleanup;
385 }
386 res = 0; /* if no error occurs, this stays 0 to the end */
387 if (PyTuple_GET_SIZE(args) > 0) {
388 if (numfields != PyTuple_GET_SIZE(args)) {
389 PyErr_Format(PyExc_TypeError, "%.400s constructor takes either 0 or "
390 "%d positional argument%s", Py_TYPE(self)->tp_name,
391 numfields, numfields == 1 ? "" : "s");
392 res = -1;
393 goto cleanup;
394 }
395 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
396 /* cannot be reached when fields is NULL */
397 PyObject *name = PySequence_GetItem(fields, i);
398 if (!name) {
399 res = -1;
400 goto cleanup;
401 }
402 res = PyObject_SetAttr(self, name, PyTuple_GET_ITEM(args, i));
403 Py_DECREF(name);
404 if (res < 0)
405 goto cleanup;
406 }
407 }
408 if (kw) {
409 i = 0; /* needed by PyDict_Next */
410 while (PyDict_Next(kw, &i, &key, &value)) {
411 res = PyObject_SetAttr(self, key, value);
412 if (res < 0)
413 goto cleanup;
414 }
415 }
416 cleanup:
417 Py_XDECREF(fields);
418 return res;
419 }
420
421 static PyTypeObject AST_type = {
422 PyVarObject_HEAD_INIT(&PyType_Type, 0)
423 "AST",
424 sizeof(PyObject),
425 0,
426 0, /* tp_dealloc */
427 0, /* tp_print */
428 0, /* tp_getattr */
429 0, /* tp_setattr */
430 0, /* tp_compare */
431 0, /* tp_repr */
432 0, /* tp_as_number */
433 0, /* tp_as_sequence */
434 0, /* tp_as_mapping */
435 0, /* tp_hash */
436 0, /* tp_call */
437 0, /* tp_str */
438 PyObject_GenericGetAttr, /* tp_getattro */
439 PyObject_GenericSetAttr, /* tp_setattro */
440 0, /* tp_as_buffer */
441 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
442 0, /* tp_doc */
443 0, /* tp_traverse */
444 0, /* tp_clear */
445 0, /* tp_richcompare */
446 0, /* tp_weaklistoffset */
447 0, /* tp_iter */
448 0, /* tp_iternext */
449 0, /* tp_methods */
450 0, /* tp_members */
451 0, /* tp_getset */
452 0, /* tp_base */
453 0, /* tp_dict */
454 0, /* tp_descr_get */
455 0, /* tp_descr_set */
456 0, /* tp_dictoffset */
457 (initproc)ast_type_init, /* tp_init */
458 PyType_GenericAlloc, /* tp_alloc */
459 PyType_GenericNew, /* tp_new */
460 PyObject_Del, /* tp_free */
461 };
462
463
372 static PyTypeObject* make_type(char *type, PyTypeObject* base, char**fields, int num_fields) 464 static PyTypeObject* make_type(char *type, PyTypeObject* base, char**fields, int num_fields)
373 { 465 {
374 PyObject *fnames, *result; 466 PyObject *fnames, *result;
375 int i; 467 int i;
376 if (num_fields) { 468 if (num_fields) {
395 } 487 }
396 488
397 static int add_attributes(PyTypeObject* type, char**attrs, int num_fields) 489 static int add_attributes(PyTypeObject* type, char**attrs, int num_fields)
398 { 490 {
399 int i, result; 491 int i, result;
400 PyObject *s, *l = PyList_New(num_fields); 492 PyObject *s, *l = PyTuple_New(num_fields);
401 if (!l) return 0; 493 if (!l) return 0;
402 for(i = 0; i < num_fields; i++) { 494 for(i = 0; i < num_fields; i++) {
403 s = PyString_FromString(attrs[i]); 495 s = PyString_FromString(attrs[i]);
404 if (!s) { 496 if (!s) {
405 Py_DECREF(l); 497 Py_DECREF(l);
406 return 0; 498 return 0;
407 } 499 }
408 PyList_SET_ITEM(l, i, s); 500 PyTuple_SET_ITEM(l, i, s);
409 } 501 }
410 result = PyObject_SetAttrString((PyObject*)type, "_attributes", l) >= 0; 502 result = PyObject_SetAttrString((PyObject*)type, "_attributes", l) >= 0;
411 Py_DECREF(l); 503 Py_DECREF(l);
412 return result; 504 return result;
413 } 505 }
504 596
505 static int init_types(void) 597 static int init_types(void)
506 { 598 {
507 static int initialized; 599 static int initialized;
508 if (initialized) return 1; 600 if (initialized) return 1;
509 AST_type = make_type("AST", &PyBaseObject_Type, NULL, 0); 601 mod_type = make_type("mod", &AST_type, NULL, 0);
510 mod_type = make_type("mod", AST_type, NULL, 0);
511 if (!mod_type) return 0; 602 if (!mod_type) return 0;
512 if (!add_attributes(mod_type, NULL, 0)) return 0; 603 if (!add_attributes(mod_type, NULL, 0)) return 0;
513 Module_type = make_type("Module", mod_type, Module_fields, 1); 604 Module_type = make_type("Module", mod_type, Module_fields, 1);
514 if (!Module_type) return 0; 605 if (!Module_type) return 0;
515 Interactive_type = make_type("Interactive", mod_type, 606 Interactive_type = make_type("Interactive", mod_type,
518 Expression_type = make_type("Expression", mod_type, Expression_fields, 609 Expression_type = make_type("Expression", mod_type, Expression_fields,
519 1); 610 1);
520 if (!Expression_type) return 0; 611 if (!Expression_type) return 0;
521 Suite_type = make_type("Suite", mod_type, Suite_fields, 1); 612 Suite_type = make_type("Suite", mod_type, Suite_fields, 1);
522 if (!Suite_type) return 0; 613 if (!Suite_type) return 0;
523 stmt_type = make_type("stmt", AST_type, NULL, 0); 614 stmt_type = make_type("stmt", &AST_type, NULL, 0);
524 if (!stmt_type) return 0; 615 if (!stmt_type) return 0;
525 if (!add_attributes(stmt_type, stmt_attributes, 2)) return 0; 616 if (!add_attributes(stmt_type, stmt_attributes, 2)) return 0;
526 FunctionDef_type = make_type("FunctionDef", stmt_type, 617 FunctionDef_type = make_type("FunctionDef", stmt_type,
527 FunctionDef_fields, 4); 618 FunctionDef_fields, 4);
528 if (!FunctionDef_type) return 0; 619 if (!FunctionDef_type) return 0;
570 if (!Pass_type) return 0; 661 if (!Pass_type) return 0;
571 Break_type = make_type("Break", stmt_type, NULL, 0); 662 Break_type = make_type("Break", stmt_type, NULL, 0);
572 if (!Break_type) return 0; 663 if (!Break_type) return 0;
573 Continue_type = make_type("Continue", stmt_type, NULL, 0); 664 Continue_type = make_type("Continue", stmt_type, NULL, 0);
574 if (!Continue_type) return 0; 665 if (!Continue_type) return 0;
575 expr_type = make_type("expr", AST_type, NULL, 0); 666 expr_type = make_type("expr", &AST_type, NULL, 0);
576 if (!expr_type) return 0; 667 if (!expr_type) return 0;
577 if (!add_attributes(expr_type, expr_attributes, 2)) return 0; 668 if (!add_attributes(expr_type, expr_attributes, 2)) return 0;
578 BoolOp_type = make_type("BoolOp", expr_type, BoolOp_fields, 2); 669 BoolOp_type = make_type("BoolOp", expr_type, BoolOp_fields, 2);
579 if (!BoolOp_type) return 0; 670 if (!BoolOp_type) return 0;
580 BinOp_type = make_type("BinOp", expr_type, BinOp_fields, 3); 671 BinOp_type = make_type("BinOp", expr_type, BinOp_fields, 3);
612 if (!Name_type) return 0; 703 if (!Name_type) return 0;
613 List_type = make_type("List", expr_type, List_fields, 2); 704 List_type = make_type("List", expr_type, List_fields, 2);
614 if (!List_type) return 0; 705 if (!List_type) return 0;
615 Tuple_type = make_type("Tuple", expr_type, Tuple_fields, 2); 706 Tuple_type = make_type("Tuple", expr_type, Tuple_fields, 2);
616 if (!Tuple_type) return 0; 707 if (!Tuple_type) return 0;
617 expr_context_type = make_type("expr_context", AST_type, NULL, 0); 708 expr_context_type = make_type("expr_context", &AST_type, NULL, 0);
618 if (!expr_context_type) return 0; 709 if (!expr_context_type) return 0;
619 if (!add_attributes(expr_context_type, NULL, 0)) return 0; 710 if (!add_attributes(expr_context_type, NULL, 0)) return 0;
620 Load_type = make_type("Load", expr_context_type, NULL, 0); 711 Load_type = make_type("Load", expr_context_type, NULL, 0);
621 if (!Load_type) return 0; 712 if (!Load_type) return 0;
622 Load_singleton = PyType_GenericNew(Load_type, NULL, NULL); 713 Load_singleton = PyType_GenericNew(Load_type, NULL, NULL);
639 if (!AugStore_singleton) return 0; 730 if (!AugStore_singleton) return 0;
640 Param_type = make_type("Param", expr_context_type, NULL, 0); 731 Param_type = make_type("Param", expr_context_type, NULL, 0);
641 if (!Param_type) return 0; 732 if (!Param_type) return 0;
642 Param_singleton = PyType_GenericNew(Param_type, NULL, NULL); 733 Param_singleton = PyType_GenericNew(Param_type, NULL, NULL);
643 if (!Param_singleton) return 0; 734 if (!Param_singleton) return 0;
644 slice_type = make_type("slice", AST_type, NULL, 0); 735 slice_type = make_type("slice", &AST_type, NULL, 0);
645 if (!slice_type) return 0; 736 if (!slice_type) return 0;
646 if (!add_attributes(slice_type, NULL, 0)) return 0; 737 if (!add_attributes(slice_type, NULL, 0)) return 0;
647 Ellipsis_type = make_type("Ellipsis", slice_type, NULL, 0); 738 Ellipsis_type = make_type("Ellipsis", slice_type, NULL, 0);
648 if (!Ellipsis_type) return 0; 739 if (!Ellipsis_type) return 0;
649 Slice_type = make_type("Slice", slice_type, Slice_fields, 3); 740 Slice_type = make_type("Slice", slice_type, Slice_fields, 3);
650 if (!Slice_type) return 0; 741 if (!Slice_type) return 0;
651 ExtSlice_type = make_type("ExtSlice", slice_type, ExtSlice_fields, 1); 742 ExtSlice_type = make_type("ExtSlice", slice_type, ExtSlice_fields, 1);
652 if (!ExtSlice_type) return 0; 743 if (!ExtSlice_type) return 0;
653 Index_type = make_type("Index", slice_type, Index_fields, 1); 744 Index_type = make_type("Index", slice_type, Index_fields, 1);
654 if (!Index_type) return 0; 745 if (!Index_type) return 0;
655 boolop_type = make_type("boolop", AST_type, NULL, 0); 746 boolop_type = make_type("boolop", &AST_type, NULL, 0);
656 if (!boolop_type) return 0; 747 if (!boolop_type) return 0;
657 if (!add_attributes(boolop_type, NULL, 0)) return 0; 748 if (!add_attributes(boolop_type, NULL, 0)) return 0;
658 And_type = make_type("And", boolop_type, NULL, 0); 749 And_type = make_type("And", boolop_type, NULL, 0);
659 if (!And_type) return 0; 750 if (!And_type) return 0;
660 And_singleton = PyType_GenericNew(And_type, NULL, NULL); 751 And_singleton = PyType_GenericNew(And_type, NULL, NULL);
661 if (!And_singleton) return 0; 752 if (!And_singleton) return 0;
662 Or_type = make_type("Or", boolop_type, NULL, 0); 753 Or_type = make_type("Or", boolop_type, NULL, 0);
663 if (!Or_type) return 0; 754 if (!Or_type) return 0;
664 Or_singleton = PyType_GenericNew(Or_type, NULL, NULL); 755 Or_singleton = PyType_GenericNew(Or_type, NULL, NULL);
665 if (!Or_singleton) return 0; 756 if (!Or_singleton) return 0;
666 operator_type = make_type("operator", AST_type, NULL, 0); 757 operator_type = make_type("operator", &AST_type, NULL, 0);
667 if (!operator_type) return 0; 758 if (!operator_type) return 0;
668 if (!add_attributes(operator_type, NULL, 0)) return 0; 759 if (!add_attributes(operator_type, NULL, 0)) return 0;
669 Add_type = make_type("Add", operator_type, NULL, 0); 760 Add_type = make_type("Add", operator_type, NULL, 0);
670 if (!Add_type) return 0; 761 if (!Add_type) return 0;
671 Add_singleton = PyType_GenericNew(Add_type, NULL, NULL); 762 Add_singleton = PyType_GenericNew(Add_type, NULL, NULL);
712 if (!BitAnd_singleton) return 0; 803 if (!BitAnd_singleton) return 0;
713 FloorDiv_type = make_type("FloorDiv", operator_type, NULL, 0); 804 FloorDiv_type = make_type("FloorDiv", operator_type, NULL, 0);
714 if (!FloorDiv_type) return 0; 805 if (!FloorDiv_type) return 0;
715 FloorDiv_singleton = PyType_GenericNew(FloorDiv_type, NULL, NULL); 806 FloorDiv_singleton = PyType_GenericNew(FloorDiv_type, NULL, NULL);
716 if (!FloorDiv_singleton) return 0; 807 if (!FloorDiv_singleton) return 0;
717 unaryop_type = make_type("unaryop", AST_type, NULL, 0); 808 unaryop_type = make_type("unaryop", &AST_type, NULL, 0);
718 if (!unaryop_type) return 0; 809 if (!unaryop_type) return 0;
719 if (!add_attributes(unaryop_type, NULL, 0)) return 0; 810 if (!add_attributes(unaryop_type, NULL, 0)) return 0;
720 Invert_type = make_type("Invert", unaryop_type, NULL, 0); 811 Invert_type = make_type("Invert", unaryop_type, NULL, 0);
721 if (!Invert_type) return 0; 812 if (!Invert_type) return 0;
722 Invert_singleton = PyType_GenericNew(Invert_type, NULL, NULL); 813 Invert_singleton = PyType_GenericNew(Invert_type, NULL, NULL);
731 if (!UAdd_singleton) return 0; 822 if (!UAdd_singleton) return 0;
732 USub_type = make_type("USub", unaryop_type, NULL, 0); 823 USub_type = make_type("USub", unaryop_type, NULL, 0);
733 if (!USub_type) return 0; 824 if (!USub_type) return 0;
734 USub_singleton = PyType_GenericNew(USub_type, NULL, NULL); 825 USub_singleton = PyType_GenericNew(USub_type, NULL, NULL);
735 if (!USub_singleton) return 0; 826 if (!USub_singleton) return 0;
736 cmpop_type = make_type("cmpop", AST_type, NULL, 0); 827 cmpop_type = make_type("cmpop", &AST_type, NULL, 0);
737 if (!cmpop_type) return 0; 828 if (!cmpop_type) return 0;
738 if (!add_attributes(cmpop_type, NULL, 0)) return 0; 829 if (!add_attributes(cmpop_type, NULL, 0)) return 0;
739 Eq_type = make_type("Eq", cmpop_type, NULL, 0); 830 Eq_type = make_type("Eq", cmpop_type, NULL, 0);
740 if (!Eq_type) return 0; 831 if (!Eq_type) return 0;
741 Eq_singleton = PyType_GenericNew(Eq_type, NULL, NULL); 832 Eq_singleton = PyType_GenericNew(Eq_type, NULL, NULL);
774 if (!In_singleton) return 0; 865 if (!In_singleton) return 0;
775 NotIn_type = make_type("NotIn", cmpop_type, NULL, 0); 866 NotIn_type = make_type("NotIn", cmpop_type, NULL, 0);
776 if (!NotIn_type) return 0; 867 if (!NotIn_type) return 0;
777 NotIn_singleton = PyType_GenericNew(NotIn_type, NULL, NULL); 868 NotIn_singleton = PyType_GenericNew(NotIn_type, NULL, NULL);
778 if (!NotIn_singleton) return 0; 869 if (!NotIn_singleton) return 0;
779 comprehension_type = make_type("comprehension", AST_type, 870 comprehension_type = make_type("comprehension", &AST_type,
780 comprehension_fields, 3); 871 comprehension_fields, 3);
781 if (!comprehension_type) return 0; 872 if (!comprehension_type) return 0;
782 excepthandler_type = make_type("excepthandler", AST_type, NULL, 0); 873 excepthandler_type = make_type("excepthandler", &AST_type, NULL, 0);
783 if (!excepthandler_type) return 0; 874 if (!excepthandler_type) return 0;
784 if (!add_attributes(excepthandler_type, excepthandler_attributes, 2)) 875 if (!add_attributes(excepthandler_type, excepthandler_attributes, 2))
785 return 0; 876 return 0;
786 ExceptHandler_type = make_type("ExceptHandler", excepthandler_type, 877 ExceptHandler_type = make_type("ExceptHandler", excepthandler_type,
787 ExceptHandler_fields, 3); 878 ExceptHandler_fields, 3);
788 if (!ExceptHandler_type) return 0; 879 if (!ExceptHandler_type) return 0;
789 arguments_type = make_type("arguments", AST_type, arguments_fields, 4); 880 arguments_type = make_type("arguments", &AST_type, arguments_fields, 4);
790 if (!arguments_type) return 0; 881 if (!arguments_type) return 0;
791 keyword_type = make_type("keyword", AST_type, keyword_fields, 2); 882 keyword_type = make_type("keyword", &AST_type, keyword_fields, 2);
792 if (!keyword_type) return 0; 883 if (!keyword_type) return 0;
793 alias_type = make_type("alias", AST_type, alias_fields, 2); 884 alias_type = make_type("alias", &AST_type, alias_fields, 2);
794 if (!alias_type) return 0; 885 if (!alias_type) return 0;
795 initialized = 1; 886 initialized = 1;
796 return 1; 887 return 1;
797 } 888 }
798 889
5814 PyObject *m, *d; 5905 PyObject *m, *d;
5815 if (!init_types()) return; 5906 if (!init_types()) return;
5816 m = Py_InitModule3("_ast", NULL, NULL); 5907 m = Py_InitModule3("_ast", NULL, NULL);
5817 if (!m) return; 5908 if (!m) return;
5818 d = PyModule_GetDict(m); 5909 d = PyModule_GetDict(m);
5819 if (PyDict_SetItemString(d, "AST", (PyObject*)AST_type) < 0) return; 5910 if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return;
5820 if (PyModule_AddIntConstant(m, "PyCF_ONLY_AST", PyCF_ONLY_AST) < 0) 5911 if (PyModule_AddIntConstant(m, "PyCF_ONLY_AST", PyCF_ONLY_AST) < 0)
5821 return; 5912 return;
5822 if (PyModule_AddStringConstant(m, "__version__", "60978") < 0) 5913 if (PyModule_AddStringConstant(m, "__version__", "62047") < 0)
5823 return; 5914 return;
5824 if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return; 5915 if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return;
5825 if (PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0) 5916 if (PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0)
5826 return; 5917 return;
5827 if (PyDict_SetItemString(d, "Interactive", (PyObject*)Interactive_type) 5918 if (PyDict_SetItemString(d, "Interactive", (PyObject*)Interactive_type)
5995 } 6086 }
5996 6087
5997 int PyAST_Check(PyObject* obj) 6088 int PyAST_Check(PyObject* obj)
5998 { 6089 {
5999 init_types(); 6090 init_types();
6000 return PyObject_IsInstance(obj, (PyObject*)AST_type); 6091 return PyObject_IsInstance(obj, (PyObject*)&AST_type);
6001 } 6092 }
6002 6093
6003 6094