Skip to content

Commit 1e27721

Browse files
committed
also fix dir()
1 parent b096651 commit 1e27721

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

Lib/test/test_listcomps.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -545,12 +545,21 @@ def test_iter_var_available_in_locals(self):
545545
y = 0
546546
items = [locals()["x"] for x in l]
547547
items2 = [vars()["x"] for x in l]
548-
items3 = [eval("x") for x in l]
548+
items3 = [("x" in dir()) for x in l]
549+
items4 = [eval("x") for x in l]
549550
# x is available, and does not overwrite y
550551
[exec("y = x") for x in l]
551552
"""
552553
self._check_in_scopes(
553-
code, {"items": [1, 2], "items2": [1, 2], "items3": [1, 2], "y": 0})
554+
code,
555+
{
556+
"items": [1, 2],
557+
"items2": [1, 2],
558+
"items3": [True, True],
559+
"items4": [1, 2],
560+
"y": 0
561+
}
562+
)
554563

555564

556565
__test__ = {'doctests' : doctests}

Objects/object.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -1690,13 +1690,15 @@ _dir_locals(void)
16901690
PyObject *names;
16911691
PyObject *locals;
16921692

1693-
locals = PyEval_GetLocals();
1693+
locals = PyEval_GetFrameLocals();
16941694
if (locals == NULL)
16951695
return NULL;
16961696

16971697
names = PyMapping_Keys(locals);
1698-
if (!names)
1698+
Py_DECREF(locals);
1699+
if (!names) {
16991700
return NULL;
1701+
}
17001702
if (!PyList_Check(names)) {
17011703
PyErr_Format(PyExc_TypeError,
17021704
"dir(): expected keys() of locals to be a list, "
@@ -1708,7 +1710,6 @@ _dir_locals(void)
17081710
Py_DECREF(names);
17091711
return NULL;
17101712
}
1711-
/* the locals don't need to be DECREF'd */
17121713
return names;
17131714
}
17141715

0 commit comments

Comments
 (0)