Kaydet (Commit) 2a7dedef authored tarafından Raymond Hettinger's avatar Raymond Hettinger

SF bug #1004669: Type returned from .keys() is not checked

üst 61992efc
...@@ -322,6 +322,15 @@ class BuiltinTest(unittest.TestCase): ...@@ -322,6 +322,15 @@ class BuiltinTest(unittest.TestCase):
ss['a3'] = 'a2*7' ss['a3'] = 'a2*7'
self.assertEqual(ss['a3'], 210) self.assertEqual(ss['a3'], 210)
# Verify that dir() catches a non-list returned by eval
# SF bug #1004669
class C:
def __getitem__(self, item):
raise KeyError(item)
def keys(self):
return 'a'
self.assertRaises(TypeError, eval, 'dir()', globals(), C())
# Done outside of the method test_z to get the correct scope # Done outside of the method test_z to get the correct scope
z = 0 z = 0
f = open(TESTFN, 'w') f = open(TESTFN, 'w')
......
...@@ -1702,6 +1702,11 @@ PyObject_Dir(PyObject *arg) ...@@ -1702,6 +1702,11 @@ PyObject_Dir(PyObject *arg)
} }
assert(result); assert(result);
if (!PyList_Check(result)) {
PyErr_SetString(PyExc_TypeError,
"Expected keys() to be a list.");
goto error;
}
if (PyList_Sort(result) != 0) if (PyList_Sort(result) != 0)
goto error; goto error;
else else
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment