comparison Python/Python-ast.c @ 82747:c8ccc0519247

refactor to fix refleaks
author Benjamin Peterson <benjamin@python.org>
date Mon, 18 Mar 2013 23:24:41 -0700
parents 7c5c678e4164
children cc7a94efb1b7
comparison
equal deleted inserted replaced
82746:6c0fca49b0fd 82747:c8ccc0519247
768 } 768 }
769 Py_DECREF(empty_tuple); 769 Py_DECREF(empty_tuple);
770 return 0; 770 return 0;
771 } 771 }
772 772
773 static int exists_not_none(PyObject *obj, _Py_Identifier *id)
774 {
775 PyObject *attr = _PyObject_GetAttrId(obj, id);
776 if (!attr) {
777 PyErr_Clear();
778 return 0;
779 }
780 int isnone = attr == Py_None;
781 Py_DECREF(attr);
782 return !isnone;
783 }
784
773 785
774 static int init_types(void) 786 static int init_types(void)
775 { 787 {
776 static int initialized; 788 static int initialized;
777 if (initialized) return 1; 789 if (initialized) return 1;
3844 tmp = NULL; 3856 tmp = NULL;
3845 } else { 3857 } else {
3846 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from FunctionDef"); 3858 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from FunctionDef");
3847 return 1; 3859 return 1;
3848 } 3860 }
3849 if (_PyObject_HasAttrId(obj, &PyId_returns) && _PyObject_GetAttrId(obj, &PyId_returns) != Py_None) { 3861 if (exists_not_none(obj, &PyId_returns)) {
3850 int res; 3862 int res;
3851 tmp = _PyObject_GetAttrId(obj, &PyId_returns); 3863 tmp = _PyObject_GetAttrId(obj, &PyId_returns);
3852 if (tmp == NULL) goto failed; 3864 if (tmp == NULL) goto failed;
3853 res = obj2ast_expr(tmp, &returns, arena); 3865 res = obj2ast_expr(tmp, &returns, arena);
3854 if (res != 0) goto failed; 3866 if (res != 0) goto failed;
3935 tmp = NULL; 3947 tmp = NULL;
3936 } else { 3948 } else {
3937 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from ClassDef"); 3949 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from ClassDef");
3938 return 1; 3950 return 1;
3939 } 3951 }
3940 if (_PyObject_HasAttrId(obj, &PyId_starargs) && _PyObject_GetAttrId(obj, &PyId_starargs) != Py_None) { 3952 if (exists_not_none(obj, &PyId_starargs)) {
3941 int res; 3953 int res;
3942 tmp = _PyObject_GetAttrId(obj, &PyId_starargs); 3954 tmp = _PyObject_GetAttrId(obj, &PyId_starargs);
3943 if (tmp == NULL) goto failed; 3955 if (tmp == NULL) goto failed;
3944 res = obj2ast_expr(tmp, &starargs, arena); 3956 res = obj2ast_expr(tmp, &starargs, arena);
3945 if (res != 0) goto failed; 3957 if (res != 0) goto failed;
3946 Py_XDECREF(tmp); 3958 Py_XDECREF(tmp);
3947 tmp = NULL; 3959 tmp = NULL;
3948 } else { 3960 } else {
3949 starargs = NULL; 3961 starargs = NULL;
3950 } 3962 }
3951 if (_PyObject_HasAttrId(obj, &PyId_kwargs) && _PyObject_GetAttrId(obj, &PyId_kwargs) != Py_None) { 3963 if (exists_not_none(obj, &PyId_kwargs)) {
3952 int res; 3964 int res;
3953 tmp = _PyObject_GetAttrId(obj, &PyId_kwargs); 3965 tmp = _PyObject_GetAttrId(obj, &PyId_kwargs);
3954 if (tmp == NULL) goto failed; 3966 if (tmp == NULL) goto failed;
3955 res = obj2ast_expr(tmp, &kwargs, arena); 3967 res = obj2ast_expr(tmp, &kwargs, arena);
3956 if (res != 0) goto failed; 3968 if (res != 0) goto failed;
4019 return 1; 4031 return 1;
4020 } 4032 }
4021 if (isinstance) { 4033 if (isinstance) {
4022 expr_ty value; 4034 expr_ty value;
4023 4035
4024 if (_PyObject_HasAttrId(obj, &PyId_value) && _PyObject_GetAttrId(obj, &PyId_value) != Py_None) { 4036 if (exists_not_none(obj, &PyId_value)) {
4025 int res; 4037 int res;
4026 tmp = _PyObject_GetAttrId(obj, &PyId_value); 4038 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4027 if (tmp == NULL) goto failed; 4039 if (tmp == NULL) goto failed;
4028 res = obj2ast_expr(tmp, &value, arena); 4040 res = obj2ast_expr(tmp, &value, arena);
4029 if (res != 0) goto failed; 4041 if (res != 0) goto failed;
4477 } 4489 }
4478 if (isinstance) { 4490 if (isinstance) {
4479 expr_ty exc; 4491 expr_ty exc;
4480 expr_ty cause; 4492 expr_ty cause;
4481 4493
4482 if (_PyObject_HasAttrId(obj, &PyId_exc) && _PyObject_GetAttrId(obj, &PyId_exc) != Py_None) { 4494 if (exists_not_none(obj, &PyId_exc)) {
4483 int res; 4495 int res;
4484 tmp = _PyObject_GetAttrId(obj, &PyId_exc); 4496 tmp = _PyObject_GetAttrId(obj, &PyId_exc);
4485 if (tmp == NULL) goto failed; 4497 if (tmp == NULL) goto failed;
4486 res = obj2ast_expr(tmp, &exc, arena); 4498 res = obj2ast_expr(tmp, &exc, arena);
4487 if (res != 0) goto failed; 4499 if (res != 0) goto failed;
4488 Py_XDECREF(tmp); 4500 Py_XDECREF(tmp);
4489 tmp = NULL; 4501 tmp = NULL;
4490 } else { 4502 } else {
4491 exc = NULL; 4503 exc = NULL;
4492 } 4504 }
4493 if (_PyObject_HasAttrId(obj, &PyId_cause) && _PyObject_GetAttrId(obj, &PyId_cause) != Py_None) { 4505 if (exists_not_none(obj, &PyId_cause)) {
4494 int res; 4506 int res;
4495 tmp = _PyObject_GetAttrId(obj, &PyId_cause); 4507 tmp = _PyObject_GetAttrId(obj, &PyId_cause);
4496 if (tmp == NULL) goto failed; 4508 if (tmp == NULL) goto failed;
4497 res = obj2ast_expr(tmp, &cause, arena); 4509 res = obj2ast_expr(tmp, &cause, arena);
4498 if (res != 0) goto failed; 4510 if (res != 0) goto failed;
4638 tmp = NULL; 4650 tmp = NULL;
4639 } else { 4651 } else {
4640 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from Assert"); 4652 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from Assert");
4641 return 1; 4653 return 1;
4642 } 4654 }
4643 if (_PyObject_HasAttrId(obj, &PyId_msg) && _PyObject_GetAttrId(obj, &PyId_msg) != Py_None) { 4655 if (exists_not_none(obj, &PyId_msg)) {
4644 int res; 4656 int res;
4645 tmp = _PyObject_GetAttrId(obj, &PyId_msg); 4657 tmp = _PyObject_GetAttrId(obj, &PyId_msg);
4646 if (tmp == NULL) goto failed; 4658 if (tmp == NULL) goto failed;
4647 res = obj2ast_expr(tmp, &msg, arena); 4659 res = obj2ast_expr(tmp, &msg, arena);
4648 if (res != 0) goto failed; 4660 if (res != 0) goto failed;
4698 if (isinstance) { 4710 if (isinstance) {
4699 identifier module; 4711 identifier module;
4700 asdl_seq* names; 4712 asdl_seq* names;
4701 int level; 4713 int level;
4702 4714
4703 if (_PyObject_HasAttrId(obj, &PyId_module) && _PyObject_GetAttrId(obj, &PyId_module) != Py_None) { 4715 if (exists_not_none(obj, &PyId_module)) {
4704 int res; 4716 int res;
4705 tmp = _PyObject_GetAttrId(obj, &PyId_module); 4717 tmp = _PyObject_GetAttrId(obj, &PyId_module);
4706 if (tmp == NULL) goto failed; 4718 if (tmp == NULL) goto failed;
4707 res = obj2ast_identifier(tmp, &module, arena); 4719 res = obj2ast_identifier(tmp, &module, arena);
4708 if (res != 0) goto failed; 4720 if (res != 0) goto failed;
4734 tmp = NULL; 4746 tmp = NULL;
4735 } else { 4747 } else {
4736 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from ImportFrom"); 4748 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from ImportFrom");
4737 return 1; 4749 return 1;
4738 } 4750 }
4739 if (_PyObject_HasAttrId(obj, &PyId_level) && _PyObject_GetAttrId(obj, &PyId_level) != Py_None) { 4751 if (exists_not_none(obj, &PyId_level)) {
4740 int res; 4752 int res;
4741 tmp = _PyObject_GetAttrId(obj, &PyId_level); 4753 tmp = _PyObject_GetAttrId(obj, &PyId_level);
4742 if (tmp == NULL) goto failed; 4754 if (tmp == NULL) goto failed;
4743 res = obj2ast_int(tmp, &level, arena); 4755 res = obj2ast_int(tmp, &level, arena);
4744 if (res != 0) goto failed; 4756 if (res != 0) goto failed;
5453 return 1; 5465 return 1;
5454 } 5466 }
5455 if (isinstance) { 5467 if (isinstance) {
5456 expr_ty value; 5468 expr_ty value;
5457 5469
5458 if (_PyObject_HasAttrId(obj, &PyId_value) && _PyObject_GetAttrId(obj, &PyId_value) != Py_None) { 5470 if (exists_not_none(obj, &PyId_value)) {
5459 int res; 5471 int res;
5460 tmp = _PyObject_GetAttrId(obj, &PyId_value); 5472 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5461 if (tmp == NULL) goto failed; 5473 if (tmp == NULL) goto failed;
5462 res = obj2ast_expr(tmp, &value, arena); 5474 res = obj2ast_expr(tmp, &value, arena);
5463 if (res != 0) goto failed; 5475 if (res != 0) goto failed;
5640 tmp = NULL; 5652 tmp = NULL;
5641 } else { 5653 } else {
5642 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from Call"); 5654 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from Call");
5643 return 1; 5655 return 1;
5644 } 5656 }
5645 if (_PyObject_HasAttrId(obj, &PyId_starargs) && _PyObject_GetAttrId(obj, &PyId_starargs) != Py_None) { 5657 if (exists_not_none(obj, &PyId_starargs)) {
5646 int res; 5658 int res;
5647 tmp = _PyObject_GetAttrId(obj, &PyId_starargs); 5659 tmp = _PyObject_GetAttrId(obj, &PyId_starargs);
5648 if (tmp == NULL) goto failed; 5660 if (tmp == NULL) goto failed;
5649 res = obj2ast_expr(tmp, &starargs, arena); 5661 res = obj2ast_expr(tmp, &starargs, arena);
5650 if (res != 0) goto failed; 5662 if (res != 0) goto failed;
5651 Py_XDECREF(tmp); 5663 Py_XDECREF(tmp);
5652 tmp = NULL; 5664 tmp = NULL;
5653 } else { 5665 } else {
5654 starargs = NULL; 5666 starargs = NULL;
5655 } 5667 }
5656 if (_PyObject_HasAttrId(obj, &PyId_kwargs) && _PyObject_GetAttrId(obj, &PyId_kwargs) != Py_None) { 5668 if (exists_not_none(obj, &PyId_kwargs)) {
5657 int res; 5669 int res;
5658 tmp = _PyObject_GetAttrId(obj, &PyId_kwargs); 5670 tmp = _PyObject_GetAttrId(obj, &PyId_kwargs);
5659 if (tmp == NULL) goto failed; 5671 if (tmp == NULL) goto failed;
5660 res = obj2ast_expr(tmp, &kwargs, arena); 5672 res = obj2ast_expr(tmp, &kwargs, arena);
5661 if (res != 0) goto failed; 5673 if (res != 0) goto failed;
6122 if (isinstance) { 6134 if (isinstance) {
6123 expr_ty lower; 6135 expr_ty lower;
6124 expr_ty upper; 6136 expr_ty upper;
6125 expr_ty step; 6137 expr_ty step;
6126 6138
6127 if (_PyObject_HasAttrId(obj, &PyId_lower) && _PyObject_GetAttrId(obj, &PyId_lower) != Py_None) { 6139 if (exists_not_none(obj, &PyId_lower)) {
6128 int res; 6140 int res;
6129 tmp = _PyObject_GetAttrId(obj, &PyId_lower); 6141 tmp = _PyObject_GetAttrId(obj, &PyId_lower);
6130 if (tmp == NULL) goto failed; 6142 if (tmp == NULL) goto failed;
6131 res = obj2ast_expr(tmp, &lower, arena); 6143 res = obj2ast_expr(tmp, &lower, arena);
6132 if (res != 0) goto failed; 6144 if (res != 0) goto failed;
6133 Py_XDECREF(tmp); 6145 Py_XDECREF(tmp);
6134 tmp = NULL; 6146 tmp = NULL;
6135 } else { 6147 } else {
6136 lower = NULL; 6148 lower = NULL;
6137 } 6149 }
6138 if (_PyObject_HasAttrId(obj, &PyId_upper) && _PyObject_GetAttrId(obj, &PyId_upper) != Py_None) { 6150 if (exists_not_none(obj, &PyId_upper)) {
6139 int res; 6151 int res;
6140 tmp = _PyObject_GetAttrId(obj, &PyId_upper); 6152 tmp = _PyObject_GetAttrId(obj, &PyId_upper);
6141 if (tmp == NULL) goto failed; 6153 if (tmp == NULL) goto failed;
6142 res = obj2ast_expr(tmp, &upper, arena); 6154 res = obj2ast_expr(tmp, &upper, arena);
6143 if (res != 0) goto failed; 6155 if (res != 0) goto failed;
6144 Py_XDECREF(tmp); 6156 Py_XDECREF(tmp);
6145 tmp = NULL; 6157 tmp = NULL;
6146 } else { 6158 } else {
6147 upper = NULL; 6159 upper = NULL;
6148 } 6160 }
6149 if (_PyObject_HasAttrId(obj, &PyId_step) && _PyObject_GetAttrId(obj, &PyId_step) != Py_None) { 6161 if (exists_not_none(obj, &PyId_step)) {
6150 int res; 6162 int res;
6151 tmp = _PyObject_GetAttrId(obj, &PyId_step); 6163 tmp = _PyObject_GetAttrId(obj, &PyId_step);
6152 if (tmp == NULL) goto failed; 6164 if (tmp == NULL) goto failed;
6153 res = obj2ast_expr(tmp, &step, arena); 6165 res = obj2ast_expr(tmp, &step, arena);
6154 if (res != 0) goto failed; 6166 if (res != 0) goto failed;
6599 if (isinstance) { 6611 if (isinstance) {
6600 expr_ty type; 6612 expr_ty type;
6601 identifier name; 6613 identifier name;
6602 asdl_seq* body; 6614 asdl_seq* body;
6603 6615
6604 if (_PyObject_HasAttrId(obj, &PyId_type) && _PyObject_GetAttrId(obj, &PyId_type) != Py_None) { 6616 if (exists_not_none(obj, &PyId_type)) {
6605 int res; 6617 int res;
6606 tmp = _PyObject_GetAttrId(obj, &PyId_type); 6618 tmp = _PyObject_GetAttrId(obj, &PyId_type);
6607 if (tmp == NULL) goto failed; 6619 if (tmp == NULL) goto failed;
6608 res = obj2ast_expr(tmp, &type, arena); 6620 res = obj2ast_expr(tmp, &type, arena);
6609 if (res != 0) goto failed; 6621 if (res != 0) goto failed;
6610 Py_XDECREF(tmp); 6622 Py_XDECREF(tmp);
6611 tmp = NULL; 6623 tmp = NULL;
6612 } else { 6624 } else {
6613 type = NULL; 6625 type = NULL;
6614 } 6626 }
6615 if (_PyObject_HasAttrId(obj, &PyId_name) && _PyObject_GetAttrId(obj, &PyId_name) != Py_None) { 6627 if (exists_not_none(obj, &PyId_name)) {
6616 int res; 6628 int res;
6617 tmp = _PyObject_GetAttrId(obj, &PyId_name); 6629 tmp = _PyObject_GetAttrId(obj, &PyId_name);
6618 if (tmp == NULL) goto failed; 6630 if (tmp == NULL) goto failed;
6619 res = obj2ast_identifier(tmp, &name, arena); 6631 res = obj2ast_identifier(tmp, &name, arena);
6620 if (res != 0) goto failed; 6632 if (res != 0) goto failed;
6694 tmp = NULL; 6706 tmp = NULL;
6695 } else { 6707 } else {
6696 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from arguments"); 6708 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from arguments");
6697 return 1; 6709 return 1;
6698 } 6710 }
6699 if (_PyObject_HasAttrId(obj, &PyId_vararg) && _PyObject_GetAttrId(obj, &PyId_vararg) != Py_None) { 6711 if (exists_not_none(obj, &PyId_vararg)) {
6700 int res; 6712 int res;
6701 tmp = _PyObject_GetAttrId(obj, &PyId_vararg); 6713 tmp = _PyObject_GetAttrId(obj, &PyId_vararg);
6702 if (tmp == NULL) goto failed; 6714 if (tmp == NULL) goto failed;
6703 res = obj2ast_arg(tmp, &vararg, arena); 6715 res = obj2ast_arg(tmp, &vararg, arena);
6704 if (res != 0) goto failed; 6716 if (res != 0) goto failed;
6755 tmp = NULL; 6767 tmp = NULL;
6756 } else { 6768 } else {
6757 PyErr_SetString(PyExc_TypeError, "required field \"kw_defaults\" missing from arguments"); 6769 PyErr_SetString(PyExc_TypeError, "required field \"kw_defaults\" missing from arguments");
6758 return 1; 6770 return 1;
6759 } 6771 }
6760 if (_PyObject_HasAttrId(obj, &PyId_kwarg) && _PyObject_GetAttrId(obj, &PyId_kwarg) != Py_None) { 6772 if (exists_not_none(obj, &PyId_kwarg)) {
6761 int res; 6773 int res;
6762 tmp = _PyObject_GetAttrId(obj, &PyId_kwarg); 6774 tmp = _PyObject_GetAttrId(obj, &PyId_kwarg);
6763 if (tmp == NULL) goto failed; 6775 if (tmp == NULL) goto failed;
6764 res = obj2ast_arg(tmp, &kwarg, arena); 6776 res = obj2ast_arg(tmp, &kwarg, arena);
6765 if (res != 0) goto failed; 6777 if (res != 0) goto failed;
6818 tmp = NULL; 6830 tmp = NULL;
6819 } else { 6831 } else {
6820 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from arg"); 6832 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from arg");
6821 return 1; 6833 return 1;
6822 } 6834 }
6823 if (_PyObject_HasAttrId(obj, &PyId_annotation) && _PyObject_GetAttrId(obj, &PyId_annotation) != Py_None) { 6835 if (exists_not_none(obj, &PyId_annotation)) {
6824 int res; 6836 int res;
6825 tmp = _PyObject_GetAttrId(obj, &PyId_annotation); 6837 tmp = _PyObject_GetAttrId(obj, &PyId_annotation);
6826 if (tmp == NULL) goto failed; 6838 if (tmp == NULL) goto failed;
6827 res = obj2ast_expr(tmp, &annotation, arena); 6839 res = obj2ast_expr(tmp, &annotation, arena);
6828 if (res != 0) goto failed; 6840 if (res != 0) goto failed;
6893 tmp = NULL; 6905 tmp = NULL;
6894 } else { 6906 } else {
6895 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from alias"); 6907 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from alias");
6896 return 1; 6908 return 1;
6897 } 6909 }
6898 if (_PyObject_HasAttrId(obj, &PyId_asname) && _PyObject_GetAttrId(obj, &PyId_asname) != Py_None) { 6910 if (exists_not_none(obj, &PyId_asname)) {
6899 int res; 6911 int res;
6900 tmp = _PyObject_GetAttrId(obj, &PyId_asname); 6912 tmp = _PyObject_GetAttrId(obj, &PyId_asname);
6901 if (tmp == NULL) goto failed; 6913 if (tmp == NULL) goto failed;
6902 res = obj2ast_identifier(tmp, &asname, arena); 6914 res = obj2ast_identifier(tmp, &asname, arena);
6903 if (res != 0) goto failed; 6915 if (res != 0) goto failed;
6930 tmp = NULL; 6942 tmp = NULL;
6931 } else { 6943 } else {
6932 PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from withitem"); 6944 PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from withitem");
6933 return 1; 6945 return 1;
6934 } 6946 }
6935 if (_PyObject_HasAttrId(obj, &PyId_optional_vars) && _PyObject_GetAttrId(obj, &PyId_optional_vars) != Py_None) { 6947 if (exists_not_none(obj, &PyId_optional_vars)) {
6936 int res; 6948 int res;
6937 tmp = _PyObject_GetAttrId(obj, &PyId_optional_vars); 6949 tmp = _PyObject_GetAttrId(obj, &PyId_optional_vars);
6938 if (tmp == NULL) goto failed; 6950 if (tmp == NULL) goto failed;
6939 res = obj2ast_expr(tmp, &optional_vars, arena); 6951 res = obj2ast_expr(tmp, &optional_vars, arena);
6940 if (res != 0) goto failed; 6952 if (res != 0) goto failed;