Mercurial > cpython-lru_cache-weakref
comparison Python/Python-ast.c @ 80738:03f92a9f0875
create NameConstant AST class for None, True, and False literals (closes #16619)
| author | Benjamin Peterson <benjamin@python.org> |
|---|---|
| date | Thu, 06 Dec 2012 17:41:04 -0500 |
| parents | ffb2fa900cb7 |
| children | 7d69d04522e3 |
comparison
equal
deleted
inserted
replaced
| 80736:a58204570a7c | 80738:03f92a9f0875 |
|---|---|
| 269 }; | 269 }; |
| 270 static PyTypeObject *Bytes_type; | 270 static PyTypeObject *Bytes_type; |
| 271 static char *Bytes_fields[]={ | 271 static char *Bytes_fields[]={ |
| 272 "s", | 272 "s", |
| 273 }; | 273 }; |
| 274 static PyTypeObject *NameConstant_type; | |
| 275 static char *NameConstant_fields[]={ | |
| 276 "value", | |
| 277 }; | |
| 274 static PyTypeObject *Ellipsis_type; | 278 static PyTypeObject *Ellipsis_type; |
| 275 static PyTypeObject *Attribute_type; | 279 static PyTypeObject *Attribute_type; |
| 276 _Py_IDENTIFIER(attr); | 280 _Py_IDENTIFIER(attr); |
| 277 _Py_IDENTIFIER(ctx); | 281 _Py_IDENTIFIER(ctx); |
| 278 static char *Attribute_fields[]={ | 282 static char *Attribute_fields[]={ |
| 671 if (!o) | 675 if (!o) |
| 672 o = Py_None; | 676 o = Py_None; |
| 673 Py_INCREF((PyObject*)o); | 677 Py_INCREF((PyObject*)o); |
| 674 return (PyObject*)o; | 678 return (PyObject*)o; |
| 675 } | 679 } |
| 680 #define ast2obj_singleton ast2obj_object | |
| 676 #define ast2obj_identifier ast2obj_object | 681 #define ast2obj_identifier ast2obj_object |
| 677 #define ast2obj_string ast2obj_object | 682 #define ast2obj_string ast2obj_object |
| 678 #define ast2obj_bytes ast2obj_object | 683 #define ast2obj_bytes ast2obj_object |
| 679 | 684 |
| 680 static PyObject* ast2obj_int(long b) | 685 static PyObject* ast2obj_int(long b) |
| 681 { | 686 { |
| 682 return PyLong_FromLong(b); | 687 return PyLong_FromLong(b); |
| 683 } | 688 } |
| 684 | 689 |
| 685 /* Conversion Python -> AST */ | 690 /* Conversion Python -> AST */ |
| 691 | |
| 692 static int obj2ast_singleton(PyObject *obj, PyObject** out, PyArena* arena) | |
| 693 { | |
| 694 if (obj != Py_None && obj != Py_True && obj != Py_False) { | |
| 695 PyErr_SetString(PyExc_ValueError, | |
| 696 "AST singleton must be True, False, or None"); | |
| 697 return 1; | |
| 698 } | |
| 699 *out = obj; | |
| 700 return 0; | |
| 701 } | |
| 686 | 702 |
| 687 static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena) | 703 static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena) |
| 688 { | 704 { |
| 689 if (obj == Py_None) | 705 if (obj == Py_None) |
| 690 obj = NULL; | 706 obj = NULL; |
| 858 if (!Num_type) return 0; | 874 if (!Num_type) return 0; |
| 859 Str_type = make_type("Str", expr_type, Str_fields, 1); | 875 Str_type = make_type("Str", expr_type, Str_fields, 1); |
| 860 if (!Str_type) return 0; | 876 if (!Str_type) return 0; |
| 861 Bytes_type = make_type("Bytes", expr_type, Bytes_fields, 1); | 877 Bytes_type = make_type("Bytes", expr_type, Bytes_fields, 1); |
| 862 if (!Bytes_type) return 0; | 878 if (!Bytes_type) return 0; |
| 879 NameConstant_type = make_type("NameConstant", expr_type, | |
| 880 NameConstant_fields, 1); | |
| 881 if (!NameConstant_type) return 0; | |
| 863 Ellipsis_type = make_type("Ellipsis", expr_type, NULL, 0); | 882 Ellipsis_type = make_type("Ellipsis", expr_type, NULL, 0); |
| 864 if (!Ellipsis_type) return 0; | 883 if (!Ellipsis_type) return 0; |
| 865 Attribute_type = make_type("Attribute", expr_type, Attribute_fields, 3); | 884 Attribute_type = make_type("Attribute", expr_type, Attribute_fields, 3); |
| 866 if (!Attribute_type) return 0; | 885 if (!Attribute_type) return 0; |
| 867 Subscript_type = make_type("Subscript", expr_type, Subscript_fields, 3); | 886 Subscript_type = make_type("Subscript", expr_type, Subscript_fields, 3); |
| 1919 p->col_offset = col_offset; | 1938 p->col_offset = col_offset; |
| 1920 return p; | 1939 return p; |
| 1921 } | 1940 } |
| 1922 | 1941 |
| 1923 expr_ty | 1942 expr_ty |
| 1943 NameConstant(singleton value, int lineno, int col_offset, PyArena *arena) | |
| 1944 { | |
| 1945 expr_ty p; | |
| 1946 if (!value) { | |
| 1947 PyErr_SetString(PyExc_ValueError, | |
| 1948 "field value is required for NameConstant"); | |
| 1949 return NULL; | |
| 1950 } | |
| 1951 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 1952 if (!p) | |
| 1953 return NULL; | |
| 1954 p->kind = NameConstant_kind; | |
| 1955 p->v.NameConstant.value = value; | |
| 1956 p->lineno = lineno; | |
| 1957 p->col_offset = col_offset; | |
| 1958 return p; | |
| 1959 } | |
| 1960 | |
| 1961 expr_ty | |
| 1924 Ellipsis(int lineno, int col_offset, PyArena *arena) | 1962 Ellipsis(int lineno, int col_offset, PyArena *arena) |
| 1925 { | 1963 { |
| 1926 expr_ty p; | 1964 expr_ty p; |
| 1927 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); | 1965 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); |
| 1928 if (!p) | 1966 if (!p) |
| 2026 expr_ty | 2064 expr_ty |
| 2027 Name(identifier id, expr_context_ty ctx, int lineno, int col_offset, PyArena | 2065 Name(identifier id, expr_context_ty ctx, int lineno, int col_offset, PyArena |
| 2028 *arena) | 2066 *arena) |
| 2029 { | 2067 { |
| 2030 expr_ty p; | 2068 expr_ty p; |
| 2069 assert(PyUnicode_CompareWithASCIIString(id, "True") && PyUnicode_CompareWithASCIIString(id, "False") && PyUnicode_CompareWithASCIIString(id, "None")); | |
| 2031 if (!id) { | 2070 if (!id) { |
| 2032 PyErr_SetString(PyExc_ValueError, | 2071 PyErr_SetString(PyExc_ValueError, |
| 2033 "field id is required for Name"); | 2072 "field id is required for Name"); |
| 2034 return NULL; | 2073 return NULL; |
| 2035 } | 2074 } |
| 2943 result = PyType_GenericNew(Bytes_type, NULL, NULL); | 2982 result = PyType_GenericNew(Bytes_type, NULL, NULL); |
| 2944 if (!result) goto failed; | 2983 if (!result) goto failed; |
| 2945 value = ast2obj_bytes(o->v.Bytes.s); | 2984 value = ast2obj_bytes(o->v.Bytes.s); |
| 2946 if (!value) goto failed; | 2985 if (!value) goto failed; |
| 2947 if (_PyObject_SetAttrId(result, &PyId_s, value) == -1) | 2986 if (_PyObject_SetAttrId(result, &PyId_s, value) == -1) |
| 2987 goto failed; | |
| 2988 Py_DECREF(value); | |
| 2989 break; | |
| 2990 case NameConstant_kind: | |
| 2991 result = PyType_GenericNew(NameConstant_type, NULL, NULL); | |
| 2992 if (!result) goto failed; | |
| 2993 value = ast2obj_singleton(o->v.NameConstant.value); | |
| 2994 if (!value) goto failed; | |
| 2995 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1) | |
| 2948 goto failed; | 2996 goto failed; |
| 2949 Py_DECREF(value); | 2997 Py_DECREF(value); |
| 2950 break; | 2998 break; |
| 2951 case Ellipsis_kind: | 2999 case Ellipsis_kind: |
| 2952 result = PyType_GenericNew(Ellipsis_type, NULL, NULL); | 3000 result = PyType_GenericNew(Ellipsis_type, NULL, NULL); |
| 5683 } else { | 5731 } else { |
| 5684 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Bytes"); | 5732 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Bytes"); |
| 5685 return 1; | 5733 return 1; |
| 5686 } | 5734 } |
| 5687 *out = Bytes(s, lineno, col_offset, arena); | 5735 *out = Bytes(s, lineno, col_offset, arena); |
| 5736 if (*out == NULL) goto failed; | |
| 5737 return 0; | |
| 5738 } | |
| 5739 isinstance = PyObject_IsInstance(obj, (PyObject*)NameConstant_type); | |
| 5740 if (isinstance == -1) { | |
| 5741 return 1; | |
| 5742 } | |
| 5743 if (isinstance) { | |
| 5744 singleton value; | |
| 5745 | |
| 5746 if (_PyObject_HasAttrId(obj, &PyId_value)) { | |
| 5747 int res; | |
| 5748 tmp = _PyObject_GetAttrId(obj, &PyId_value); | |
| 5749 if (tmp == NULL) goto failed; | |
| 5750 res = obj2ast_singleton(tmp, &value, arena); | |
| 5751 if (res != 0) goto failed; | |
| 5752 Py_XDECREF(tmp); | |
| 5753 tmp = NULL; | |
| 5754 } else { | |
| 5755 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from NameConstant"); | |
| 5756 return 1; | |
| 5757 } | |
| 5758 *out = NameConstant(value, lineno, col_offset, arena); | |
| 5688 if (*out == NULL) goto failed; | 5759 if (*out == NULL) goto failed; |
| 5689 return 0; | 5760 return 0; |
| 5690 } | 5761 } |
| 5691 isinstance = PyObject_IsInstance(obj, (PyObject*)Ellipsis_type); | 5762 isinstance = PyObject_IsInstance(obj, (PyObject*)Ellipsis_type); |
| 5692 if (isinstance == -1) { | 5763 if (isinstance == -1) { |
| 7006 NULL; | 7077 NULL; |
| 7007 if (PyDict_SetItemString(d, "Str", (PyObject*)Str_type) < 0) return | 7078 if (PyDict_SetItemString(d, "Str", (PyObject*)Str_type) < 0) return |
| 7008 NULL; | 7079 NULL; |
| 7009 if (PyDict_SetItemString(d, "Bytes", (PyObject*)Bytes_type) < 0) return | 7080 if (PyDict_SetItemString(d, "Bytes", (PyObject*)Bytes_type) < 0) return |
| 7010 NULL; | 7081 NULL; |
| 7082 if (PyDict_SetItemString(d, "NameConstant", | |
| 7083 (PyObject*)NameConstant_type) < 0) return NULL; | |
| 7011 if (PyDict_SetItemString(d, "Ellipsis", (PyObject*)Ellipsis_type) < 0) | 7084 if (PyDict_SetItemString(d, "Ellipsis", (PyObject*)Ellipsis_type) < 0) |
| 7012 return NULL; | 7085 return NULL; |
| 7013 if (PyDict_SetItemString(d, "Attribute", (PyObject*)Attribute_type) < | 7086 if (PyDict_SetItemString(d, "Attribute", (PyObject*)Attribute_type) < |
| 7014 0) return NULL; | 7087 0) return NULL; |
| 7015 if (PyDict_SetItemString(d, "Subscript", (PyObject*)Subscript_type) < | 7088 if (PyDict_SetItemString(d, "Subscript", (PyObject*)Subscript_type) < |
