Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2a7dede

Browse files
committed
SF bug #1004669: Type returned from .keys() is not checked
1 parent 61992ef commit 2a7dede

2 files changed

Lines changed: 14 additions & 0 deletions

File tree

Lib/test/test_builtin.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,15 @@ def __getitem__(self, key ):
322322
ss['a3'] = 'a2*7'
323323
self.assertEqual(ss['a3'], 210)
324324

325+
# Verify that dir() catches a non-list returned by eval
326+
# SF bug #1004669
327+
class C:
328+
def __getitem__(self, item):
329+
raise KeyError(item)
330+
def keys(self):
331+
return 'a'
332+
self.assertRaises(TypeError, eval, 'dir()', globals(), C())
333+
325334
# Done outside of the method test_z to get the correct scope
326335
z = 0
327336
f = open(TESTFN, 'w')

Objects/object.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1702,6 +1702,11 @@ PyObject_Dir(PyObject *arg)
17021702
}
17031703

17041704
assert(result);
1705+
if (!PyList_Check(result)) {
1706+
PyErr_SetString(PyExc_TypeError,
1707+
"Expected keys() to be a list.");
1708+
goto error;
1709+
}
17051710
if (PyList_Sort(result) != 0)
17061711
goto error;
17071712
else

0 commit comments

Comments
 (0)