Skip to content

Commit 5e03c3e

Browse files
committed
Use co_annotations for module and class annotations
1 parent 4c09965 commit 5e03c3e

7 files changed

Lines changed: 83 additions & 65 deletions

File tree

‎Lib/test/test_dis.py‎

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -221,26 +221,17 @@ def bug1333982(x=[]):
221221
2 0 SETUP_ANNOTATIONS
222222
2 LOAD_CONST 0 (1)
223223
4 STORE_NAME 0 (x)
224-
6 LOAD_CONST 1 ('int')
225-
8 LOAD_NAME 1 (__annotations__)
226-
10 LOAD_CONST 2 ('x')
227-
12 STORE_SUBSCR
228-
229-
3 14 LOAD_CONST 3 ('fun(1)')
230-
16 LOAD_NAME 1 (__annotations__)
231-
18 LOAD_CONST 4 ('y')
232-
20 STORE_SUBSCR
233-
234-
4 22 LOAD_CONST 0 (1)
235-
24 LOAD_NAME 2 (lst)
236-
26 LOAD_NAME 3 (fun)
237-
28 LOAD_CONST 5 (0)
238-
30 CALL_FUNCTION 1
239-
32 STORE_SUBSCR
240-
34 LOAD_NAME 4 (int)
241-
36 POP_TOP
242-
38 LOAD_CONST 6 (None)
243-
40 RETURN_VALUE
224+
225+
4 6 LOAD_CONST 0 (1)
226+
8 LOAD_NAME 1 (lst)
227+
10 LOAD_NAME 2 (fun)
228+
12 LOAD_CONST 1 (0)
229+
14 CALL_FUNCTION 1
230+
16 STORE_SUBSCR
231+
18 LOAD_NAME 3 (int)
232+
20 POP_TOP
233+
22 LOAD_CONST 2 (None)
234+
24 RETURN_VALUE
244235
"""
245236

246237
compound_stmt_str = """\

‎Lib/test/test_opcodes.py‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,6 @@ def test_use_existing_annotations(self):
4141
exec('x: int', ns)
4242
self.assertEqual(ns['__annotations__'], {'x': 'int', 1: 2})
4343

44-
def test_do_not_recreate_annotations(self):
45-
# Don't rely on the existence of the '__annotations__' global.
46-
with support.swap_item(globals(), '__annotations__', {}):
47-
del globals()['__annotations__']
48-
class C:
49-
del __annotations__
50-
with self.assertRaises(NameError):
51-
x: int
52-
5344
def test_raise_class_exceptions(self):
5445

5546
class AClass(Exception): pass
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
Add ``co_annotations`` field to ``PyCodeObject`` in order to compute
2-
function annotations at compilation time. Patch provided by Yurii Karabas.
2+
all annotations at compilation time. Patch provided by Yurii Karabas.

‎Objects/codeobject.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@ PyCode_NewWithAnnotations(int argcount, int posonlyargcount, int kwonlyargcount,
165165
if (intern_string_constants(consts, NULL) < 0) {
166166
return NULL;
167167
}
168+
if (annotations != Py_None && intern_string_constants(annotations, NULL) < 0) {
169+
return NULL;
170+
}
168171

169172
/* Make sure that code is indexable with an int, this is
170173
a long running assumption in ceval.c and many parts of

‎Objects/funcobject.c‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -425,9 +425,8 @@ func_get_annotations(PyFunctionObject *op, void *Py_UNUSED(ignored))
425425
return NULL;
426426

427427
PyObject *annotations = ((PyCodeObject*) op->func_code )->co_annotations;
428-
if (annotations != Py_None) {
429-
PyDict_MergeFromSeq2(op->func_annotations, annotations, 0);
430-
}
428+
if (annotations != Py_None && PyDict_MergeFromSeq2(op->func_annotations, annotations, 1))
429+
return NULL;
431430
}
432431
Py_INCREF(op->func_annotations);
433432
return op->func_annotations;

‎Python/ceval.c‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3059,6 +3059,22 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
30593059
Py_DECREF(ann_dict);
30603060
}
30613061
}
3062+
if (ann_dict && f->f_code->co_annotations != Py_None) {
3063+
if (PyDict_CheckExact(ann_dict)) {
3064+
if (PyDict_MergeFromSeq2(ann_dict, f->f_code->co_annotations, 1))
3065+
goto error;
3066+
} else {
3067+
Py_ssize_t i;
3068+
PyObject *annotations = f->f_code->co_annotations;
3069+
3070+
for (i = 0; i < PyTuple_Size(annotations); i++) {
3071+
PyObject* pair = PyTuple_GET_ITEM(annotations, i);
3072+
3073+
if (PyObject_SetItem(ann_dict, PyTuple_GET_ITEM(pair, 0), PyTuple_GET_ITEM(pair, 1)))
3074+
goto error;
3075+
}
3076+
}
3077+
}
30623078
DISPATCH();
30633079
}
30643080

‎Python/compile.c‎

Lines changed: 50 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ static int compiler_async_comprehension_generator(
253253
expr_ty elt, expr_ty val, int type);
254254

255255
static PyCodeObject *assemble(struct compiler *, int addNone);
256-
static PyObject *__doc__, *__annotations__;
256+
static PyObject *__doc__;
257257

258258
#define CAPSULE_NAME "compile.c compiler unit"
259259

@@ -358,11 +358,6 @@ PyAST_CompileObject(mod_ty mod, PyObject *filename, PyCompilerFlags *flags,
358358
if (!__doc__)
359359
return NULL;
360360
}
361-
if (!__annotations__) {
362-
__annotations__ = PyUnicode_InternFromString("__annotations__");
363-
if (!__annotations__)
364-
return NULL;
365-
}
366361
if (!compiler_init(&c))
367362
return NULL;
368363
Py_INCREF(filename);
@@ -1786,9 +1781,9 @@ compiler_unwind_fblock_stack(struct compiler *c, int preserve_tos, struct fblock
17861781
static int
17871782
compiler_body(struct compiler *c, asdl_stmt_seq *stmts)
17881783
{
1789-
int i = 0;
1784+
int i = 0, has_annotations = 0;
17901785
stmt_ty st;
1791-
PyObject *docstring;
1786+
PyObject *docstring, *annotations;
17921787

17931788
/* Set current line number to the line number of first statement.
17941789
This way line number for SETUP_ANNOTATIONS will always
@@ -1801,6 +1796,10 @@ compiler_body(struct compiler *c, asdl_stmt_seq *stmts)
18011796
/* Every annotated class and module should have __annotations__. */
18021797
if (find_ann(stmts)) {
18031798
ADDOP(c, SETUP_ANNOTATIONS);
1799+
has_annotations = 1;
1800+
c->u->u_annotations = PyList_New(0);
1801+
if (!c->u->u_annotations)
1802+
return 0;
18041803
}
18051804
if (!asdl_seq_LEN(stmts))
18061805
return 1;
@@ -1818,6 +1817,16 @@ compiler_body(struct compiler *c, asdl_stmt_seq *stmts)
18181817
}
18191818
for (; i < asdl_seq_LEN(stmts); i++)
18201819
VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
1820+
1821+
if (has_annotations) {
1822+
annotations = PyList_AsTuple(c->u->u_annotations);
1823+
Py_DECREF(c->u->u_annotations);
1824+
c->u->u_annotations = Py_None;
1825+
if (annotations == NULL)
1826+
return 0;
1827+
c->u->u_annotations = annotations;
1828+
}
1829+
18211830
return 1;
18221831
}
18231832

@@ -2027,39 +2036,47 @@ compiler_visit_annexpr(struct compiler *c, expr_ty annotation)
20272036
return 1;
20282037
}
20292038

2039+
static int
2040+
compiler_add_annotation(struct compiler *c, PyObject* mangled,
2041+
expr_ty annotation, PyObject *annotations) {
2042+
PyObject *pair;
2043+
PyObject *value;
2044+
2045+
value = _PyAST_ExprAsUnicode(annotation);
2046+
if (value == NULL)
2047+
return 0;
2048+
2049+
pair = PyTuple_Pack(2, mangled, value);
2050+
if (pair == NULL) {
2051+
Py_DECREF(value);
2052+
return 0;
2053+
}
2054+
2055+
if (PyList_Append(annotations, pair) < 0) {
2056+
Py_DECREF(value);
2057+
Py_DECREF(pair);
2058+
return 0;
2059+
}
2060+
2061+
Py_DECREF(value);
2062+
Py_DECREF(pair);
2063+
return 1;
2064+
}
2065+
20302066
static int
20312067
compiler_visit_argannotation(struct compiler *c, identifier id,
20322068
expr_ty annotation, PyObject *annotations)
20332069
{
20342070
if (annotation) {
20352071
PyObject *mangled;
2036-
PyObject *pair;
2037-
PyObject *value;
20382072
mangled = _Py_Mangle(c->u->u_private, id);
20392073
if (!mangled)
20402074
return 0;
2041-
2042-
value = _PyAST_ExprAsUnicode(annotation);
2043-
if (value == NULL)
2044-
return 0;
2045-
2046-
pair = PyTuple_Pack(2, mangled, value);
2047-
if (pair == NULL) {
2048-
Py_DECREF(value);
2049-
Py_DECREF(mangled);
2050-
return 0;
2051-
}
2052-
2053-
if (PyList_Append(annotations, pair) < 0) {
2054-
Py_DECREF(value);
2075+
if(!compiler_add_annotation(c, mangled, annotation, annotations)){
20552076
Py_DECREF(mangled);
2056-
Py_DECREF(pair);
20572077
return 0;
20582078
}
2059-
2060-
Py_DECREF(value);
20612079
Py_DECREF(mangled);
2062-
Py_DECREF(pair);
20632080
}
20642081
return 1;
20652082
}
@@ -5284,11 +5301,12 @@ compiler_annassign(struct compiler *c, stmt_ty s)
52845301
if (s->v.AnnAssign.simple &&
52855302
(c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
52865303
c->u->u_scope_type == COMPILER_SCOPE_CLASS)) {
5287-
VISIT(c, annexpr, s->v.AnnAssign.annotation);
5288-
ADDOP_NAME(c, LOAD_NAME, __annotations__, names);
52895304
mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id);
5290-
ADDOP_LOAD_CONST_NEW(c, mangled);
5291-
ADDOP(c, STORE_SUBSCR);
5305+
if(!compiler_add_annotation(c, mangled, s->v.AnnAssign.annotation, c->u->u_annotations)){
5306+
Py_DECREF(mangled);
5307+
return 0;
5308+
}
5309+
Py_DECREF(mangled);
52925310
}
52935311
break;
52945312
case Attribute_kind:

0 commit comments

Comments
 (0)