8000 Fix symtable tests · python/cpython@355d3df · GitHub
[go: up one dir, main page]

Skip to content

Commit 355d3df

Browse files
committed
Fix symtable tests
1 parent 87baca2 commit 355d3df

File tree

4 files changed

+24
-13
lines changed

4 files changed

+24
-13
lines changed

Lib/symtable.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ def get_methods(self):
218218
if self.__methods is None:
219219
d = {}
220220
for st in self._table.children:
221+
if st.type == _symtable.TYPE_ANNOTATION:
222+
continue
221223
d[st.name] = 1
222224
self.__methods = tuple(d)
223225
return self.__methods

Lib/test/test_symtable.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,14 @@ def test_assigned(self):
205205

206206
def test_annotated(self):
207207
st1 = symtable.symtable('def f():\n x: int\n', 'test', 'exec')
208-
st2 = st1.get_children()[0]
208+
st2 = st1.get_children()[1]
209+
self.assertEqual(st2.get_type(), "function")
209210
self.assertTrue(st2.lookup('x').is_local())
210211
self.assertTrue(st2.lookup('x').is_annotated())
211212
self.assertFalse(st2.lookup('x').is_global())
212213
st3 = symtable.symtable('def f():\n x = 1\n', 'test', 'exec')
213-
st4 = st3.get_children()[0]
214+
st4 = st3.get_children()[1]
215+
self.assertEqual(st4.get_type(), "function")
214216
self.assertTrue(st4.lookup('x').is_local())
215217
self.assertFalse(st4.lookup('x').is_annotated())
216218

Python/compile.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1638,17 +1638,10 @@ compiler_setup_annotations_scope(struct compiler *c, location loc,
16381638
void *key, jump_target_label label,
16391639
PyObject *name)
16401640
{
1641-
PyObject *annotations_name = PyUnicode_FromFormat(
1642-
"<annotations of %U>", name);
1643-
if (!annotations_name) {
1644-
return ERROR;
1645-
}
1646-
if (compiler_enter_scope(c, annotations_name, COMPILER_SCOPE_ANNOTATIONS,
1641+
if (compiler_enter_scope(c, name, COMPILER_SCOPE_ANNOTATIONS,
16471642
key, loc.lineno) == -1) {
1648-
Py_DECREF(annotations_name);
16491643
return ERROR;
16501644
}
1651-
Py_DECREF(annotations_name);
16521645
c->u->u_metadata.u_posonlyargcount = 1;
16531646
_Py_DECLARE_STR(format, ".format");
16541647
ADDOP_I(c, loc, LOAD_FAST, 0);
@@ -1801,7 +1794,7 @@ compiler_body(struct compiler *c, location loc, asdl_stmt_seq *stmts)
18011794
NEW_JUMP_TARGET_LABEL(c, raise_notimp);
18021795
void *key = (void *)((uintptr_t)c->u->u_ste->ste_id + 1);
18031796
RETURN_IF_ERROR(compiler_setup_annotations_scope(c, loc, key, raise_notimp,
1804-
c->u->u_ste->ste_name));
1797+
c->u->u_ste->ste_annotation_block->ste_name));
18051798
int annotations_len = 0;
18061799
RETURN_IF_ERROR(
18071800
compiler_collect_annotations(c, stmts, &annotations_len)

Python/symtable.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2479,11 +2479,18 @@ symtable_visit_annotation(struct symtable *st, expr_ty annotation,
24792479
}
24802480
else {
24812481
if (st->st_cur->ste_annotation_block == NULL) {
2482+
PyObject *annotations_name = PyUnicode_FromFormat(
2483+
"<annotations of %U>", parent_ste->ste_name);
2484+
if (!annotations_name) {
2485+
VISIT_QUIT(st, 0);
2486+
}
24822487
_Py_block_ty current_type = st->st_cur->ste_type;
2483-
if (!symtable_enter_block(st, parent_ste->ste_name, AnnotationBlock,
2488+
if (!symtable_enter_block(st, annotations_name, AnnotationBlock,
24842489
key, LOCATION(annotation))) {
2490+
Py_DECREF(annotations_name);
24852491
VISIT_QUIT(st, 0);
24862492
}
2493+
Py_DECREF(annotations_name);
24872494
parent_ste->ste_annotation_block =
24882495
(struct _symtable_entry *)Py_NewRef(st->st_cur);
24892496
if (current_type == ClassBlock) {
@@ -2540,12 +2547,19 @@ static int
25402547
symtable_visit_annotations(struct symtable *st, stmt_ty o, arguments_ty a, expr_ty returns,
25412548
struct _symtable_entry *function_ste)
25422549
{
2550+
PyObject *annotations_name = PyUnicode_FromFormat(
2551+
"<annotations of %U>", function_ste->ste_name);
2552+
if (!annotations_name) {
2553+
VISIT_QUIT(st, 0);
2554+
}
25432555
int is_in_class = st->st_cur->ste_can_see_class_scope;
25442556
_Py_block_ty current_type = st->st_cur->ste_type;
2545-
if (!symtable_enter_block(st, function_ste->ste_name, AnnotationBlock,
2557+
if (!symtable_enter_block(st, annotations_name, AnnotationBlock,
25462558
(void *)a, LOCATION(o))) {
2559+
Py_DECREF(annotations_name);
25472560
VISIT_QUIT(st, 0);
25482561
}
2562+
Py_DECREF(annotations_name);
25492563
if (is_in_class || current_type == ClassBlock) {
25502564
st->st_cur->ste_can_see_class_scope = 1;
25512565
if (!symtable_add_def(st, &_Py_ID(__classdict__), USE, LOCATION(o))) {

0 commit comments

Comments
 (0)
0