summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorTim Peters <tim.peters@gmail.com>2001-09-03 05:47:38 (GMT)
committerTim Peters <tim.peters@gmail.com>2001-09-03 05:47:38 (GMT)
commit5d2b77cf31c5a3cbabc74936831480b9caea3a12 (patch)
treedabb2f29553f94a18e3c5ae96d6f232196415f50 /Lib
parent95c99e57b37ede725af1fdd1ff914c91284e3048 (diff)
downloadcpython-5d2b77cf31c5a3cbabc74936831480b9caea3a12.zip
cpython-5d2b77cf31c5a3cbabc74936831480b9caea3a12.tar.gz
cpython-5d2b77cf31c5a3cbabc74936831480b9caea3a12.tar.bz2
Make dir() wordier (see the new docstring). The new behavior is a mixed
bag. It's clearly wrong for classic classes, at heart because a classic class doesn't have a __class__ attribute, and I'm unclear on whether that's feature or bug. I'll repair this once I find out (in the meantime, dir() applied to classic classes won't find the base classes, while dir() applied to a classic-class instance *will* find the base classes but not *their* base classes). Please give the new dir() a try and see whether you love it or hate it. The new dir([]) behavior is something I could come to love. Here's something to hate: >>> class C: ... pass ... >>> c = C() >>> dir(c) ['__doc__', '__module__'] >>> The idea that an instance has a __doc__ attribute is jarring (of course it's really c.__class__.__doc__ == C.__doc__; likewise for __module__). OTOH, the code already has too many special cases, and dir(x) doesn't have a compelling or clear purpose when x isn't a module.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_descr.py49
-rw-r--r--Lib/test/test_descrtut.py9
-rw-r--r--Lib/test/test_generators.py8
3 files changed, 55 insertions, 11 deletions
diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py
index f6b9e1b..ee924dc 100644
--- a/Lib/test/test_descr.py
+++ b/Lib/test/test_descr.py
@@ -172,6 +172,54 @@ def dict_constructor():
d = dictionary(mapping=Mapping())
verify(d == Mapping.dict)
+def test_dir():
+ if verbose:
+ print "Testing dir() ..."
+ junk = 12
+ verify(dir() == ['junk'])
+ del junk
+
+ # Just make sure these don't blow up!
+ for arg in 2, 2L, 2j, 2e0, [2], "2", u"2", (2,), {2:2}, type, test_dir:
+ dir(arg)
+
+ # Check some details here because classic classes aren't working
+ # reasonably, and I want this to fail (eventually).
+ class C:
+ Cdata = 1
+ def Cmethod(self): pass
+
+ cstuff = ['Cdata', 'Cmethod', '__doc__', '__module__']
+ verify(dir(C) == cstuff)
+
+ c = C() # c.__doc__ is an odd thing to see here; ditto c.__module__.
+ verify(dir(c) == cstuff)
+
+ c.cdata = 2
+ c.cmethod = lambda self: 0
+ verify(dir(c) == cstuff + ['cdata', 'cmethod'])
+
+ class A(C):
+ Adata = 1
+ def Amethod(self): pass
+ astuff = ['Adata', 'Amethod', '__doc__', '__module__']
+ # This isn't finding C's stuff at all.
+ verify(dir(A) == astuff)
+ # But this is! It's because a.__class__ exists but A.__class__ doesn't.
+ a = A()
+ verify(dir(a) == astuff[:2] + cstuff)
+
+ # The story for new-style classes is quite different.
+ class C(object):
+ Cdata = 1
+ def Cmethod(self): pass
+ class A(C):
+ Adata = 1
+ def Amethod(self): pass
+ d = dir(A)
+ for expected in 'Cdata', 'Cmethod', 'Adata', 'Amethod':
+ verify(expected in d)
+
binops = {
'add': '+',
'sub': '-',
@@ -1349,6 +1397,7 @@ def all():
lists()
dicts()
dict_constructor()
+ test_dir()
ints()
longs()
floats()
diff --git a/Lib/test/test_descrtut.py b/Lib/test/test_descrtut.py
index edb0388..121eed5 100644
--- a/Lib/test/test_descrtut.py
+++ b/Lib/test/test_descrtut.py
@@ -97,14 +97,15 @@ just like classic classes:
>>> a.default = -1000
>>> print a["noway"]
-1000
- >>> print dir(a)
- ['default']
+ >>> 'default' in dir(a)
+ 1
>>> a.x1 = 100
>>> a.x2 = 200
>>> print a.x1
100
- >>> print dir(a)
- ['default', 'x1', 'x2']
+ >>> d = dir(a)
+ >>> 'default' in d and 'x1' in d and 'x2' in d
+ 1
>>> print a.__dict__
{'default': -1000, 'x2': 200, 'x1': 100}
>>>
diff --git a/Lib/test/test_generators.py b/Lib/test/test_generators.py
index 947e26f..0e9d060 100644
--- a/Lib/test/test_generators.py
+++ b/Lib/test/test_generators.py
@@ -383,14 +383,8 @@ From the Iterators list, about the types of these things.
>>> i = g()
>>> type(i)
<type 'generator'>
-
-XXX dir(object) *generally* doesn't return useful stuff in descr-branch.
->>> dir(i)
-[]
-
-Was hoping to see this instead:
+>>> [s for s in dir(i) if not s.startswith('_')]
['gi_frame', 'gi_running', 'next']
-
>>> print i.next.__doc__
x.next() -> the next value, or raise StopIteration
>>> iter(i) is i