mirror of
https://github.com/python/cpython.git
synced 2025-08-03 16:39:00 +00:00
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
This commit is contained in:
parent
0dceb91866
commit
f2c1aa1661
14 changed files with 401 additions and 44 deletions
|
@ -1,4 +1,8 @@
|
|||
-- ASDL's six builtin types are identifier, int, string, bytes, object, singleton
|
||||
-- ASDL's 7 builtin types are:
|
||||
-- identifier, int, string, bytes, object, singleton, constant
|
||||
--
|
||||
-- singleton: None, True or False
|
||||
-- constant can be None, whereas None means "no value" for object.
|
||||
|
||||
module Python
|
||||
{
|
||||
|
@ -76,6 +80,7 @@ module Python
|
|||
| Bytes(bytes s)
|
||||
| NameConstant(singleton value)
|
||||
| Ellipsis
|
||||
| Constant(constant value)
|
||||
|
||||
-- the following expression can appear in assignment context
|
||||
| Attribute(expr value, identifier attr, expr_context ctx)
|
||||
|
|
|
@ -33,7 +33,8 @@ __all__ = [
|
|||
# See the EBNF at the top of the file to understand the logical connection
|
||||
# between the various node types.
|
||||
|
||||
builtin_types = {'identifier', 'string', 'bytes', 'int', 'object', 'singleton'}
|
||||
builtin_types = {'identifier', 'string', 'bytes', 'int', 'object', 'singleton',
|
||||
'constant'}
|
||||
|
||||
class AST:
|
||||
def __repr__(self):
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue