[Python-checkins] CVS: python/dist/src/Lib/test test_descr.py,1.29,1.30 test_descrtut.py,1.1,1.2 test_generators.py,1.27,1.28

Tim Peters tim_one@users.sourceforge.net
Sun, 02 Sep 2001 22:47:40 -0700


Update of /cvsroot/python/python/dist/src/Lib/test
In directory usw-pr-cvs1:/tmp/cvs-serv12547/python/Lib/test

Modified Files:
	test_descr.py test_descrtut.py test_generators.py 
Log Message:
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.


Index: test_descr.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_descr.py,v
retrieving revision 1.29
retrieving revision 1.30
diff -C2 -d -r1.29 -r1.30
*** test_descr.py	2001/09/02 08:22:48	1.29
--- test_descr.py	2001/09/03 05:47:38	1.30
***************
*** 173,176 ****
--- 173,224 ----
      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': '+',
***************
*** 1350,1353 ****
--- 1398,1402 ----
      dicts()
      dict_constructor()
+     test_dir()
      ints()
      longs()

Index: test_descrtut.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_descrtut.py,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** test_descrtut.py	2001/09/03 01:24:30	1.1
--- test_descrtut.py	2001/09/03 05:47:38	1.2
***************
*** 98,109 ****
      >>> print a["noway"]
      -1000
!     >>> print dir(a)
!     ['default']
      >>> a.x1 = 100
      >>> a.x2 = 200
      >>> print a.x1
      100
!     >>> print dir(a)
!     ['default', 'x1', 'x2']
      >>> print a.__dict__
      {'default': -1000, 'x2': 200, 'x1': 100}
--- 98,110 ----
      >>> print a["noway"]
      -1000
!     >>> 'default' in dir(a)
!     1
      >>> a.x1 = 100
      >>> a.x2 = 200
      >>> print a.x1
      100
!     >>> d = dir(a)
!     >>> 'default' in d and 'x1' in d and 'x2' in d
!     1
      >>> print a.__dict__
      {'default': -1000, 'x2': 200, 'x1': 100}

Index: test_generators.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_generators.py,v
retrieving revision 1.27
retrieving revision 1.28
diff -C2 -d -r1.27 -r1.28
*** test_generators.py	2001/08/16 08:30:10	1.27
--- test_generators.py	2001/09/03 05:47:38	1.28
***************
*** 384,395 ****
  >>> type(i)
  <type 'generator'>
! 
! XXX dir(object) *generally* doesn't return useful stuff in descr-branch.
! >>> dir(i)
! []
! 
! Was hoping to see this instead:
  ['gi_frame', 'gi_running', 'next']
- 
  >>> print i.next.__doc__
  x.next() -> the next value, or raise StopIteration
--- 384,389 ----
  >>> type(i)
  <type 'generator'>
! >>> [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