Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Plumbing for from __future__ import annotations (PEP 563)
The string form is recovered by unparsing the AST.
  • Loading branch information
ambv authored and Lukasz Langa committed Dec 31, 2017
commit 863d3e986fd0beec767383c302b06dd59436938f
4 changes: 4 additions & 0 deletions Include/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ PyAPI_FUNC(mod_ty) PyAST_FromNodeObject(
PyCompilerFlags *flags,
PyObject *filename,
PyArena *arena);
PyAPI_FUNC(PyObject *) PyAST_UnicodeFromAstExpr(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For uniformity with other names it would be better to name this function like PyAST_AsUnicode.

And I think it would be better to make it private and don't include in a limited API for now.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, since this functions supports only AST corresponding to expressions, PyAST_AsUnicode is not a good name. Maybe just add an underscore to PyAST_UnicodeFromAstExpr? Or _PyAST_ExprAsUnicode?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I like _PyAST_ExprAsUnicode.

expr_ty e,
int omit_parens,
int omit_string_brackets);

#ifdef __cplusplus
}
Expand Down
1 change: 1 addition & 0 deletions Include/code.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ typedef struct {

#define CO_FUTURE_BARRY_AS_BDFL 0x40000
#define CO_FUTURE_GENERATOR_STOP 0x80000
#define CO_FUTURE_ANNOTATIONS 0x100000

/* This value is found in the co_cell2arg array when the associated cell
variable does not correspond to an argument. */
Expand Down
3 changes: 2 additions & 1 deletion Include/compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ PyAPI_FUNC(PyCodeObject *) PyNode_Compile(struct _node *, const char *);
#define PyCF_MASK (CO_FUTURE_DIVISION | CO_FUTURE_ABSOLUTE_IMPORT | \
CO_FUTURE_WITH_STATEMENT | CO_FUTURE_PRINT_FUNCTION | \
CO_FUTURE_UNICODE_LITERALS | CO_FUTURE_BARRY_AS_BDFL | \
CO_FUTURE_GENERATOR_STOP)
CO_FUTURE_GENERATOR_STOP | CO_FUTURE_ANNOTATIONS)
#define PyCF_MASK_OBSOLETE (CO_NESTED)
#define PyCF_SOURCE_IS_UTF8 0x0100
#define PyCF_DONT_IMPLY_DEDENT 0x0200
Expand Down Expand Up @@ -45,6 +45,7 @@ typedef struct {
#define FUTURE_UNICODE_LITERALS "unicode_literals"
#define FUTURE_BARRY_AS_BDFL "barry_as_FLUFL"
#define FUTURE_GENERATOR_STOP "generator_stop"
#define FUTURE_ANNOTATIONS "annotations"

struct _mod; /* Declare the existence of this type */
#define PyAST_Compile(mod, s, f, ar) PyAST_CompileEx(mod, s, f, -1, ar)
Expand Down
20 changes: 13 additions & 7 deletions Lib/__future__.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,14 @@
"unicode_literals",
"barry_as_FLUFL",
"generator_stop",
"annotations",
]

__all__ = ["all_feature_names"] + all_feature_names

# The CO_xxx symbols are defined here under the same names used by
# compile.h, so that an editor search will find them here. However,
# they're not exported in __all__, because they don't really belong to
# The CO_xxx symbols are defined here under the same names defined in
# code.h and used by compile.h, so that an editor search will find them here.
# However, they're not exported in __all__, because they don't really belong to
# this module.
CO_NESTED = 0x0010 # nested_scopes
CO_GENERATOR_ALLOWED = 0 # generators (obsolete, was 0x1000)
Expand All @@ -74,6 +75,7 @@
CO_FUTURE_UNICODE_LITERALS = 0x20000 # unicode string literals
CO_FUTURE_BARRY_AS_BDFL = 0x40000
CO_FUTURE_GENERATOR_STOP = 0x80000 # StopIteration becomes RuntimeError in generators
CO_FUTURE_ANNOTATIONS = 0x100000 # annotations become strings at runtime

class _Feature:
def __init__(self, optionalRelease, mandatoryRelease, compiler_flag):
Expand Down Expand Up @@ -132,9 +134,13 @@ def __repr__(self):
CO_FUTURE_UNICODE_LITERALS)

barry_as_FLUFL = _Feature((3, 1, 0, "alpha", 2),
(3, 9, 0, "alpha", 0),
CO_FUTURE_BARRY_AS_BDFL)
(3, 9, 0, "alpha", 0),
CO_FUTURE_BARRY_AS_BDFL)

generator_stop = _Feature((3, 5, 0, "beta", 1),
(3, 7, 0, "alpha", 0),
CO_FUTURE_GENERATOR_STOP)
(3, 7, 0, "alpha", 0),
CO_FUTURE_GENERATOR_STOP)

annotations = _Feature((3, 7, 0, "beta", 1),
(4, 0, 0, "alpha", 0),
CO_FUTURE_ANNOTATIONS)
21 changes: 19 additions & 2 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -1658,13 +1658,27 @@ compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs,
return 0;
}

static int
compiler_visit_annexpr(struct compiler *c, expr_ty annotation)
{
static PyObject *ann_as_str;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why static?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A left-over from something else I tried before. Removed.

ann_as_str = PyAST_UnicodeFromAstExpr(annotation, 1, 1);
if (!ann_as_str)
return 0;
ADDOP_N(c, LOAD_CONST, ann_as_str, consts);
return 1;
}

static int
compiler_visit_argannotation(struct compiler *c, identifier id,
expr_ty annotation, PyObject *names)
{
if (annotation) {
PyObject *mangled;
VISIT(c, expr, annotation);
if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS)
VISIT(c, annexpr, annotation)
else
VISIT(c, expr, annotation);
mangled = _Py_Mangle(c->u->u_private, id);
if (!mangled)
return 0;
Expand Down Expand Up @@ -4646,7 +4660,10 @@ compiler_annassign(struct compiler *c, stmt_ty s)
if (!mangled) {
return 0;
}
VISIT(c, expr, s->v.AnnAssign.annotation);
if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS)
VISIT(c, annexpr, s->v.AnnAssign.annotation)
else
VISIT(c, expr, s->v.AnnAssign.annotation);
/* ADDOP_N decrefs its argument */
ADDOP_N(c, STORE_ANNOTATION, mangled, names);
}
Expand Down
2 changes: 2 additions & 0 deletions Python/future.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ future_check_features(PyFutureFeatures *ff, stmt_ty s, PyObject *filename)
ff->ff_features |= CO_FUTURE_BARRY_AS_BDFL;
} else if (strcmp(feature, FUTURE_GENERATOR_STOP) == 0) {
ff->ff_features |= CO_FUTURE_GENERATOR_STOP;
} else if (strcmp(feature, FUTURE_ANNOTATIONS) == 0) {
ff->ff_features |= CO_FUTURE_ANNOTATIONS;
} else if (strcmp(feature, "braces") == 0) {
PyErr_SetString(PyExc_SyntaxError,
"not a chance");
Expand Down