]> granicus.if.org Git - python/commitdiff
SF bug #1004669: Type returned from .keys() is not checked
authorRaymond Hettinger <python@rcn.com>
Sat, 7 Aug 2004 04:55:30 +0000 (04:55 +0000)
committerRaymond Hettinger <python@rcn.com>
Sat, 7 Aug 2004 04:55:30 +0000 (04:55 +0000)
Lib/test/test_builtin.py
Objects/object.c

index 6654f565374c39d4ae5e55ad82db319b4fc0a7b0..bc5afdcb41e190ed17a09c103e503fdc08873b61 100644 (file)
@@ -322,6 +322,15 @@ class BuiltinTest(unittest.TestCase):
         ss['a3'] = 'a2*7'
         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
     z = 0
     f = open(TESTFN, 'w')
index 470da60e321312e647e08d347ca907dca5ae36ab..d8e403b52deeab4f81d05fafe41c80991a18c37f 100644 (file)
@@ -1702,6 +1702,11 @@ PyObject_Dir(PyObject *arg)
        }
 
        assert(result);
+       if (!PyList_Check(result)) {
+               PyErr_SetString(PyExc_TypeError, 
+                       "Expected keys() to be a list.");
+               goto error;
+       }
        if (PyList_Sort(result) != 0)
                goto error;
        else