[Python-checkins] CVS: python/dist/src/Lib/test test_descr.py,1.28,1.29

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


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

Modified Files:
	test_descr.py 
Log Message:
Make dictionary() a real constructor.  Accepts at most one argument, "a
mapping object", in the same sense dict.update(x) requires of x (that x
has a keys() method and a getitem).
Questionable:  The other type constructors accept a keyword argument, so I
did that here too (e.g., dictionary(mapping={1:2}) works).  But type_call
doesn't pass the keyword args to the tp_new slot (it passes NULL), it only
passes them to the tp_init slot, so getting at them required adding a
tp_init slot to dicts.  Looks like that makes the normal case (i.e., no
args at all) a little slower (the time it takes to call dict.tp_init and
have it figure out there's nothing to do).


Index: test_descr.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_descr.py,v
retrieving revision 1.28
retrieving revision 1.29
diff -C2 -d -r1.28 -r1.29
*** test_descr.py	2001/08/31 04:35:14	1.28
--- test_descr.py	2001/09/02 08:22:48	1.29
***************
*** 1,5 ****
  # Test descriptor-related enhancements
  
! from test_support import verify, verbose
  from copy import deepcopy
  
--- 1,5 ----
  # Test descriptor-related enhancements
  
! from test_support import verify, verbose, TestFailed
  from copy import deepcopy
  
***************
*** 124,127 ****
--- 124,176 ----
      testset2op({1:2,3:4}, 2, 3, {1:2,2:3,3:4}, "a[b]=c", "__setitem__")
  
+ def dict_constructor():
+     if verbose:
+         print "Testing dictionary constructor ..."
+     d = dictionary()
+     verify(d == {})
+     d = dictionary({})
+     verify(d == {})
+     d = dictionary(mapping={})
+     verify(d == {})
+     d = dictionary({1: 2, 'a': 'b'})
+     verify(d == {1: 2, 'a': 'b'})
+     for badarg in 0, 0L, 0j, "0", [0], (0,):
+         try:
+             dictionary(badarg)
+         except TypeError:
+             pass
+         else:
+             raise TestFailed("no TypeError from dictionary(%r)" % badarg)
+     try:
+         dictionary(senseless={})
+     except TypeError:
+         pass
+     else:
+         raise TestFailed("no TypeError from dictionary(senseless={}")
+ 
+     try:
+         dictionary({}, {})
+     except TypeError:
+         pass
+     else:
+         raise TestFailed("no TypeError from dictionary({}, {})")
+ 
+     class Mapping:
+         dict = {1:2, 3:4, 'a':1j}
+ 
+         def __getitem__(self, i):
+             return self.dict[i]
+ 
+     try:
+         dictionary(Mapping())
+     except TypeError:
+         pass
+     else:
+         raise TestFailed("no TypeError from dictionary(incomplete mapping)")
+ 
+     Mapping.keys = lambda self: self.dict.keys()
+     d = dictionary(mapping=Mapping())
+     verify(d == Mapping.dict)
+ 
  binops = {
      'add': '+',
***************
*** 1300,1303 ****
--- 1349,1353 ----
      lists()
      dicts()
+     dict_constructor()
      ints()
      longs()