diff options
author | Georg Brandl <georg@python.org> | 2007-03-10 22:13:27 (GMT) |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2007-03-10 22:13:27 (GMT) |
commit | e32b4224d0e5a5a2faa7398211ad859e8a4cb0c8 (patch) | |
tree | 89c1a560b9940d1c8c68bf451916c9143fe1a0b5 /Lib | |
parent | af334387d12e12677460b4f558ed0a670fdfcebf (diff) | |
download | cpython-e32b4224d0e5a5a2faa7398211ad859e8a4cb0c8.zip cpython-e32b4224d0e5a5a2faa7398211ad859e8a4cb0c8.tar.gz cpython-e32b4224d0e5a5a2faa7398211ad859e8a4cb0c8.tar.bz2 |
Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_builtin.py | 63 |
1 files changed, 59 insertions, 4 deletions
diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py index d2f70ff..05e80e4 100644 --- a/Lib/test/test_builtin.py +++ b/Lib/test/test_builtin.py @@ -223,12 +223,67 @@ class BuiltinTest(unittest.TestCase): self.assertRaises(TypeError, delattr) def test_dir(self): - x = 1 - self.assert_('x' in dir()) - import sys - self.assert_('modules' in dir(sys)) + # dir(wrong number of arguments) self.assertRaises(TypeError, dir, 42, 42) + # dir() - local scope + local_var = 1 + self.assert_('local_var' in dir()) + + # dir(module) + import sys + self.assert_('exit' in dir(sys)) + + # dir(module_with_invalid__dict__) + import types + class Foo(types.ModuleType): + __dict__ = 8 + f = Foo("foo") + self.assertRaises(TypeError, dir, f) + + # dir(type) + self.assert_("strip" in dir(str)) + self.assert_("__mro__" not in dir(str)) + + # dir(obj) + class Foo(object): + def __init__(self): + self.x = 7 + self.y = 8 + self.z = 9 + f = Foo() + self.assert_("y" in dir(f)) + + # dir(obj_no__dict__) + class Foo(object): + __slots__ = [] + f = Foo() + self.assert_("__repr__" in dir(f)) + + # dir(obj_no__class__with__dict__) + # (an ugly trick to cause getattr(f, "__class__") to fail) + class Foo(object): + __slots__ = ["__class__", "__dict__"] + def __init__(self): + self.bar = "wow" + f = Foo() + self.assert_("__repr__" not in dir(f)) + self.assert_("bar" in dir(f)) + + # dir(obj_using __dir__) + class Foo(object): + def __dir__(self): + return ["kan", "ga", "roo"] + f = Foo() + self.assert_(dir(f) == ["ga", "kan", "roo"]) + + # dir(obj__dir__not_list) + class Foo(object): + def __dir__(self): + return 7 + f = Foo() + self.assertRaises(TypeError, dir, f) + def test_divmod(self): self.assertEqual(divmod(12, 7), (1, 5)) self.assertEqual(divmod(-12, 7), (-2, 2)) |