summaryrefslogtreecommitdiffstats
path: root/Parser/asdl_c.py
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2016-01-25 23:40:57 (GMT)
committerVictor Stinner <victor.stinner@gmail.com>2016-01-25 23:40:57 (GMT)
commitf2c1aa1661edb3e14ff8b7b9995f93e303c8acbb (patch)
tree2b05f347dd55b86059d70197ce3d21210a668f91 /Parser/asdl_c.py
parent0dceb918668399bdcbe27d1c59d01c4c9228c1a6 (diff)
downloadcpython-f2c1aa1661edb3e14ff8b7b9995f93e303c8acbb.zip
cpython-f2c1aa1661edb3e14ff8b7b9995f93e303c8acbb.tar.gz
cpython-f2c1aa1661edb3e14ff8b7b9995f93e303c8acbb.tar.bz2
Add ast.Constant
Issue #26146: Add a new kind of AST node: ast.Constant. It can be used by external AST optimizers, but the compiler does not emit directly such node. An optimizer can replace the following AST nodes with ast.Constant: * ast.NameConstant: None, False, True * ast.Num: int, float, complex * ast.Str: str * ast.Bytes: bytes * ast.Tuple if items are constants too: tuple * frozenset Update code to accept ast.Constant instead of ast.Num and/or ast.Str: * compiler * docstrings * ast.literal_eval() * Tools/parser/unparse.py
Diffstat (limited to 'Parser/asdl_c.py')
-rw-r--r--Parser/asdl_c.py21
1 files changed, 21 insertions, 0 deletions
diff --git a/Parser/asdl_c.py b/Parser/asdl_c.py
index eedd89b..a81644d 100644
--- a/Parser/asdl_c.py
+++ b/Parser/asdl_c.py
@@ -834,6 +834,7 @@ static PyObject* ast2obj_object(void *o)
return (PyObject*)o;
}
#define ast2obj_singleton ast2obj_object
+#define ast2obj_constant ast2obj_object
#define ast2obj_identifier ast2obj_object
#define ast2obj_string ast2obj_object
#define ast2obj_bytes ast2obj_object
@@ -871,6 +872,26 @@ static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena)
return 0;
}
+static int obj2ast_constant(PyObject* obj, PyObject** out, PyArena* arena)
+{
+ if (obj == Py_None || obj == Py_True || obj == Py_False) {
+ /* don't increment the reference counter, Constant uses a borrowed
+ * reference, not a strong reference */
+ *out = obj;
+ return 0;
+ }
+
+ if (obj) {
+ if (PyArena_AddPyObject(arena, obj) < 0) {
+ *out = NULL;
+ return -1;
+ }
+ Py_INCREF(obj);
+ }
+ *out = obj;
+ return 0;
+}
+
static int obj2ast_identifier(PyObject* obj, PyObject** out, PyArena* arena)
{
if (!PyUnicode_CheckExact(obj) && obj != Py_None) {