blob: a8bc22adb8ef489575ec11526fbeea412046f6b3 [file] [log] [blame]
Guido van Rossum3bead091992-01-27 17:00:37 +00001# Python test set -- part 4b, built-in functions n-z
2
3from test_support import *
4
5print 'oct'
6if oct(100) != '0144': raise TestFailed, 'oct(100)'
7if oct(100L) != '0144L': raise TestFailed, 'oct(100L)'
Guido van Rossumd9c6f4f1997-06-06 21:14:14 +00008if oct(-100) not in ('037777777634', '01777777777777777777634'):
Fred Drake004d5e62000-10-23 17:22:08 +00009 raise TestFailed, 'oct(-100)'
Guido van Rossum3bead091992-01-27 17:00:37 +000010if oct(-100L) != '-0144L': raise TestFailed, 'oct(-100L)'
11
12print 'open'
13# NB the first 4 lines are also used to test input and raw_input, below
14fp = open(TESTFN, 'w')
15try:
Fred Drake004d5e62000-10-23 17:22:08 +000016 fp.write('1+1\n')
17 fp.write('1+1\n')
18 fp.write('The quick brown fox jumps over the lazy dog')
19 fp.write('.\n')
20 fp.write('Dear John\n')
21 fp.write('XXX'*100)
22 fp.write('YYY'*100)
Guido van Rossum3bead091992-01-27 17:00:37 +000023finally:
Fred Drake004d5e62000-10-23 17:22:08 +000024 fp.close()
Guido van Rossum3bead091992-01-27 17:00:37 +000025#
26fp = open(TESTFN, 'r')
27try:
Fred Drake132dce22000-12-12 23:11:42 +000028 if fp.readline(4) != '1+1\n': raise TestFailed, 'readline(4) # exact'
29 if fp.readline(4) != '1+1\n': raise TestFailed, 'readline(4) # exact'
30 if fp.readline() != 'The quick brown fox jumps over the lazy dog.\n':
Fred Drake004d5e62000-10-23 17:22:08 +000031 raise TestFailed, 'readline() # default'
Fred Drake132dce22000-12-12 23:11:42 +000032 if fp.readline(4) != 'Dear': raise TestFailed, 'readline(4) # short'
33 if fp.readline(100) != ' John\n': raise TestFailed, 'readline(100)'
34 if fp.read(300) != 'XXX'*100: raise TestFailed, 'read(300)'
35 if fp.read(1000) != 'YYY'*100: raise TestFailed, 'read(1000) # truncate'
Guido van Rossum3bead091992-01-27 17:00:37 +000036finally:
Fred Drake004d5e62000-10-23 17:22:08 +000037 fp.close()
Guido van Rossum3bead091992-01-27 17:00:37 +000038
39print 'ord'
Fred Drake132dce22000-12-12 23:11:42 +000040if ord(' ') != 32: raise TestFailed, 'ord(\' \')'
41if ord('A') != 65: raise TestFailed, 'ord(\'A\')'
42if ord('a') != 97: raise TestFailed, 'ord(\'a\')'
Guido van Rossum3bead091992-01-27 17:00:37 +000043
44print 'pow'
Fred Drake132dce22000-12-12 23:11:42 +000045if pow(0,0) != 1: raise TestFailed, 'pow(0,0)'
46if pow(0,1) != 0: raise TestFailed, 'pow(0,1)'
47if pow(1,0) != 1: raise TestFailed, 'pow(1,0)'
48if pow(1,1) != 1: raise TestFailed, 'pow(1,1)'
Guido van Rossum3bead091992-01-27 17:00:37 +000049#
Fred Drake132dce22000-12-12 23:11:42 +000050if pow(2,0) != 1: raise TestFailed, 'pow(2,0)'
51if pow(2,10) != 1024: raise TestFailed, 'pow(2,10)'
52if pow(2,20) != 1024*1024: raise TestFailed, 'pow(2,20)'
53if pow(2,30) != 1024*1024*1024: raise TestFailed, 'pow(2,30)'
Guido van Rossum3bead091992-01-27 17:00:37 +000054#
Fred Drake132dce22000-12-12 23:11:42 +000055if pow(-2,0) != 1: raise TestFailed, 'pow(-2,0)'
56if pow(-2,1) != -2: raise TestFailed, 'pow(-2,1)'
57if pow(-2,2) != 4: raise TestFailed, 'pow(-2,2)'
58if pow(-2,3) != -8: raise TestFailed, 'pow(-2,3)'
Guido van Rossum3bead091992-01-27 17:00:37 +000059#
Fred Drake132dce22000-12-12 23:11:42 +000060if pow(0L,0) != 1: raise TestFailed, 'pow(0L,0)'
61if pow(0L,1) != 0: raise TestFailed, 'pow(0L,1)'
62if pow(1L,0) != 1: raise TestFailed, 'pow(1L,0)'
63if pow(1L,1) != 1: raise TestFailed, 'pow(1L,1)'
Guido van Rossum3bead091992-01-27 17:00:37 +000064#
Fred Drake132dce22000-12-12 23:11:42 +000065if pow(2L,0) != 1: raise TestFailed, 'pow(2L,0)'
66if pow(2L,10) != 1024: raise TestFailed, 'pow(2L,10)'
67if pow(2L,20) != 1024*1024: raise TestFailed, 'pow(2L,20)'
68if pow(2L,30) != 1024*1024*1024: raise TestFailed, 'pow(2L,30)'
Guido van Rossum3bead091992-01-27 17:00:37 +000069#
Fred Drake132dce22000-12-12 23:11:42 +000070if pow(-2L,0) != 1: raise TestFailed, 'pow(-2L,0)'
71if pow(-2L,1) != -2: raise TestFailed, 'pow(-2L,1)'
72if pow(-2L,2) != 4: raise TestFailed, 'pow(-2L,2)'
73if pow(-2L,3) != -8: raise TestFailed, 'pow(-2L,3)'
Guido van Rossum3bead091992-01-27 17:00:37 +000074#
Guido van Rossum35fb82a1993-01-26 13:04:43 +000075if fcmp(pow(0.,0), 1.): raise TestFailed, 'pow(0.,0)'
76if fcmp(pow(0.,1), 0.): raise TestFailed, 'pow(0.,1)'
77if fcmp(pow(1.,0), 1.): raise TestFailed, 'pow(1.,0)'
78if fcmp(pow(1.,1), 1.): raise TestFailed, 'pow(1.,1)'
Guido van Rossum3bead091992-01-27 17:00:37 +000079#
Guido van Rossum35fb82a1993-01-26 13:04:43 +000080if fcmp(pow(2.,0), 1.): raise TestFailed, 'pow(2.,0)'
81if fcmp(pow(2.,10), 1024.): raise TestFailed, 'pow(2.,10)'
82if fcmp(pow(2.,20), 1024.*1024.): raise TestFailed, 'pow(2.,20)'
83if fcmp(pow(2.,30), 1024.*1024.*1024.): raise TestFailed, 'pow(2.,30)'
Guido van Rossum3bead091992-01-27 17:00:37 +000084#
Tim Peters32f453e2001-09-03 08:35:41 +000085if fcmp(pow(-2.,0), 1.): raise TestFailed, 'pow(-2.,0)'
86if fcmp(pow(-2.,1), -2.): raise TestFailed, 'pow(-2.,1)'
87if fcmp(pow(-2.,2), 4.): raise TestFailed, 'pow(-2.,2)'
88if fcmp(pow(-2.,3), -8.): raise TestFailed, 'pow(-2.,3)'
89
90from types import FloatType
Guido van Rossume23b62f1994-11-10 22:25:26 +000091for x in 2, 2L, 2.0:
Fred Drake004d5e62000-10-23 17:22:08 +000092 for y in 10, 10L, 10.0:
93 for z in 1000, 1000L, 1000.0:
Tim Peters32f453e2001-09-03 08:35:41 +000094 if isinstance(x, FloatType) or \
95 isinstance(y, FloatType) or \
96 isinstance(z, FloatType):
97 try:
98 pow(x, y, z)
99 except TypeError:
100 pass
101 else:
Finn Bock71be9842001-12-07 18:21:56 +0000102 raise TestFailed("3-arg float pow(%s, %s, %s) should "
103 "have raised TypeError" % (x, y, z))
Tim Peters32f453e2001-09-03 08:35:41 +0000104 else:
105 if fcmp(pow(x, y, z), 24.0):
106 raise TestFailed, 'pow(%s, %s, %s)' % (x, y, z)
Guido van Rossum3bead091992-01-27 17:00:37 +0000107
Neal Norwitz26e53412001-12-29 00:16:09 +0000108try: pow(-1, -2, 3)
109except TypeError: pass
110else: raise TestFailed, 'pow(1, -2, 3) should raise TypeError'
111
112try: pow(1, 2, 0)
113except ValueError: pass
114else: raise TestFailed, 'pow(1, 2, 0) should raise ValueError'
115
116try: pow(-1L, -2L, 3L)
117except TypeError: pass
118else: raise TestFailed, 'pow(1L, -2L, 3L) should raise TypeError'
119
120try: pow(1L, 2L, 0L)
121except ValueError: pass
122else: raise TestFailed, 'pow(1L, 2L, 0L) should raise ValueError'
123
124try: pow(-342.43, 0.234)
125except ValueError: pass
126else: raise TestFailed, 'pow(-342.43, 0.234) should raise ValueError'
127
Guido van Rossum3bead091992-01-27 17:00:37 +0000128print 'range'
Fred Drake132dce22000-12-12 23:11:42 +0000129if range(3) != [0, 1, 2]: raise TestFailed, 'range(3)'
130if range(1, 5) != [1, 2, 3, 4]: raise TestFailed, 'range(1, 5)'
131if range(0) != []: raise TestFailed, 'range(0)'
132if range(-3) != []: raise TestFailed, 'range(-3)'
133if range(1, 10, 3) != [1, 4, 7]: raise TestFailed, 'range(1, 10, 3)'
134if range(5, -5, -3) != [5, 2, -1, -4]: raise TestFailed, 'range(5, -5, -3)'
Guido van Rossum3bead091992-01-27 17:00:37 +0000135
136print 'input and raw_input'
137import sys
138fp = open(TESTFN, 'r')
139savestdin = sys.stdin
140try:
Fred Drake004d5e62000-10-23 17:22:08 +0000141 sys.stdin = fp
Fred Drake132dce22000-12-12 23:11:42 +0000142 if input() != 2: raise TestFailed, 'input()'
143 if input('testing\n') != 2: raise TestFailed, 'input()'
144 if raw_input() != 'The quick brown fox jumps over the lazy dog.':
Fred Drake004d5e62000-10-23 17:22:08 +0000145 raise TestFailed, 'raw_input()'
Fred Drake132dce22000-12-12 23:11:42 +0000146 if raw_input('testing\n') != 'Dear John':
Fred Drake004d5e62000-10-23 17:22:08 +0000147 raise TestFailed, 'raw_input(\'testing\\n\')'
Guido van Rossum3bead091992-01-27 17:00:37 +0000148finally:
Fred Drake004d5e62000-10-23 17:22:08 +0000149 sys.stdin = savestdin
150 fp.close()
Guido van Rossum3bead091992-01-27 17:00:37 +0000151
Guido van Rossume65cce51993-11-08 15:05:21 +0000152print 'reduce'
Fred Drake132dce22000-12-12 23:11:42 +0000153if reduce(lambda x, y: x+y, ['a', 'b', 'c'], '') != 'abc':
Fred Drake004d5e62000-10-23 17:22:08 +0000154 raise TestFailed, 'reduce(): implode a string'
Guido van Rossumae3b3a31993-11-30 13:43:54 +0000155if reduce(lambda x, y: x+y,
Fred Drake132dce22000-12-12 23:11:42 +0000156 [['a', 'c'], [], ['d', 'w']], []) != ['a','c','d','w']:
Fred Drake004d5e62000-10-23 17:22:08 +0000157 raise TestFailed, 'reduce(): append'
Fred Drake132dce22000-12-12 23:11:42 +0000158if reduce(lambda x, y: x*y, range(2,8), 1) != 5040:
Fred Drake004d5e62000-10-23 17:22:08 +0000159 raise TestFailed, 'reduce(): compute 7!'
Fred Drake132dce22000-12-12 23:11:42 +0000160if reduce(lambda x, y: x*y, range(2,21), 1L) != 2432902008176640000L:
Fred Drake004d5e62000-10-23 17:22:08 +0000161 raise TestFailed, 'reduce(): compute 20!, use long'
Guido van Rossumb6775db1994-08-01 11:34:53 +0000162class Squares:
Fred Drake004d5e62000-10-23 17:22:08 +0000163 def __init__(self, max):
164 self.max = max
165 self.sofar = []
166 def __len__(self): return len(self.sofar)
167 def __getitem__(self, i):
168 if not 0 <= i < self.max: raise IndexError
169 n = len(self.sofar)
170 while n <= i:
171 self.sofar.append(n*n)
172 n = n+1
173 return self.sofar[i]
Guido van Rossumb6775db1994-08-01 11:34:53 +0000174if reduce(lambda x, y: x+y, Squares(10)) != 285:
Fred Drake004d5e62000-10-23 17:22:08 +0000175 raise TestFailed, 'reduce(<+>, Squares(10))'
Guido van Rossumb6775db1994-08-01 11:34:53 +0000176if reduce(lambda x, y: x+y, Squares(10), 0) != 285:
Fred Drake004d5e62000-10-23 17:22:08 +0000177 raise TestFailed, 'reduce(<+>, Squares(10), 0)'
Guido van Rossumb6775db1994-08-01 11:34:53 +0000178if reduce(lambda x, y: x+y, Squares(0), 0) != 0:
Fred Drake004d5e62000-10-23 17:22:08 +0000179 raise TestFailed, 'reduce(<+>, Squares(0), 0)'
Guido van Rossumb6775db1994-08-01 11:34:53 +0000180
Guido van Rossume65cce51993-11-08 15:05:21 +0000181
Guido van Rossum3bead091992-01-27 17:00:37 +0000182print 'reload'
Guido van Rossumeecf0351994-12-30 17:17:46 +0000183import marshal
184reload(marshal)
Guido van Rossum3bead091992-01-27 17:00:37 +0000185import string
186reload(string)
Guido van Rossum7995ed21997-08-02 03:19:26 +0000187## import sys
188## try: reload(sys)
189## except ImportError: pass
190## else: raise TestFailed, 'reload(sys) should fail'
Guido van Rossum3bead091992-01-27 17:00:37 +0000191
Guido van Rossum85f18201992-11-27 22:53:50 +0000192print 'repr'
Fred Drake132dce22000-12-12 23:11:42 +0000193if repr('') != '\'\'': raise TestFailed, 'repr(\'\')'
194if repr(0) != '0': raise TestFailed, 'repr(0)'
195if repr(0L) != '0L': raise TestFailed, 'repr(0L)'
196if repr(()) != '()': raise TestFailed, 'repr(())'
197if repr([]) != '[]': raise TestFailed, 'repr([])'
198if repr({}) != '{}': raise TestFailed, 'repr({})'
Guido van Rossum85f18201992-11-27 22:53:50 +0000199
Guido van Rossume7113b61993-03-29 11:30:50 +0000200print 'round'
Fred Drake132dce22000-12-12 23:11:42 +0000201if round(0.0) != 0.0: raise TestFailed, 'round(0.0)'
202if round(1.0) != 1.0: raise TestFailed, 'round(1.0)'
203if round(10.0) != 10.0: raise TestFailed, 'round(10.0)'
204if round(1000000000.0) != 1000000000.0:
Fred Drake004d5e62000-10-23 17:22:08 +0000205 raise TestFailed, 'round(1000000000.0)'
Fred Drake132dce22000-12-12 23:11:42 +0000206if round(1e20) != 1e20: raise TestFailed, 'round(1e20)'
Guido van Rossume7113b61993-03-29 11:30:50 +0000207
Fred Drake132dce22000-12-12 23:11:42 +0000208if round(-1.0) != -1.0: raise TestFailed, 'round(-1.0)'
209if round(-10.0) != -10.0: raise TestFailed, 'round(-10.0)'
210if round(-1000000000.0) != -1000000000.0:
Fred Drake004d5e62000-10-23 17:22:08 +0000211 raise TestFailed, 'round(-1000000000.0)'
Fred Drake132dce22000-12-12 23:11:42 +0000212if round(-1e20) != -1e20: raise TestFailed, 'round(-1e20)'
Guido van Rossume7113b61993-03-29 11:30:50 +0000213
Fred Drake132dce22000-12-12 23:11:42 +0000214if round(0.1) != 0.0: raise TestFailed, 'round(0.0)'
215if round(1.1) != 1.0: raise TestFailed, 'round(1.0)'
216if round(10.1) != 10.0: raise TestFailed, 'round(10.0)'
217if round(1000000000.1) != 1000000000.0:
Fred Drake004d5e62000-10-23 17:22:08 +0000218 raise TestFailed, 'round(1000000000.0)'
Guido van Rossume7113b61993-03-29 11:30:50 +0000219
Fred Drake132dce22000-12-12 23:11:42 +0000220if round(-1.1) != -1.0: raise TestFailed, 'round(-1.0)'
221if round(-10.1) != -10.0: raise TestFailed, 'round(-10.0)'
222if round(-1000000000.1) != -1000000000.0:
Fred Drake004d5e62000-10-23 17:22:08 +0000223 raise TestFailed, 'round(-1000000000.0)'
Guido van Rossume7113b61993-03-29 11:30:50 +0000224
Fred Drake132dce22000-12-12 23:11:42 +0000225if round(0.9) != 1.0: raise TestFailed, 'round(0.9)'
226if round(9.9) != 10.0: raise TestFailed, 'round(9.9)'
227if round(999999999.9) != 1000000000.0:
Fred Drake004d5e62000-10-23 17:22:08 +0000228 raise TestFailed, 'round(999999999.9)'
Guido van Rossume7113b61993-03-29 11:30:50 +0000229
Fred Drake132dce22000-12-12 23:11:42 +0000230if round(-0.9) != -1.0: raise TestFailed, 'round(-0.9)'
231if round(-9.9) != -10.0: raise TestFailed, 'round(-9.9)'
232if round(-999999999.9) != -1000000000.0:
Fred Drake004d5e62000-10-23 17:22:08 +0000233 raise TestFailed, 'round(-999999999.9)'
Guido van Rossume7113b61993-03-29 11:30:50 +0000234
Guido van Rossum3bead091992-01-27 17:00:37 +0000235print 'setattr'
236import sys
Guido van Rossume23b62f1994-11-10 22:25:26 +0000237setattr(sys, 'spam', 1)
238if sys.spam != 1: raise TestFailed, 'setattr(sys, \'spam\', 1)'
Jeremy Hyltonb7a77312001-07-30 22:49:11 +0000239try:
240 setattr(sys, 1, 'spam')
241except TypeError:
242 pass
243else:
244 raise TestFailed, "setattr(sys, 1, 'spam') should raise exception"
Guido van Rossum3bead091992-01-27 17:00:37 +0000245
Guido van Rossum85f18201992-11-27 22:53:50 +0000246print 'str'
Fred Drake132dce22000-12-12 23:11:42 +0000247if str('') != '': raise TestFailed, 'str(\'\')'
248if str(0) != '0': raise TestFailed, 'str(0)'
249if str(0L) != '0': raise TestFailed, 'str(0L)'
250if str(()) != '()': raise TestFailed, 'str(())'
251if str([]) != '[]': raise TestFailed, 'str([])'
252if str({}) != '{}': raise TestFailed, 'str({})'
Guido van Rossum85f18201992-11-27 22:53:50 +0000253
Guido van Rossume23b62f1994-11-10 22:25:26 +0000254print 'tuple'
Fred Drake132dce22000-12-12 23:11:42 +0000255if tuple(()) != (): raise TestFailed, 'tuple(())'
Guido van Rossum06ee2532002-02-26 22:39:23 +0000256t0_3 = (0, 1, 2, 3)
257t0_3_bis = tuple(t0_3)
258if t0_3 is not t0_3_bis: raise TestFailed, 'tuple((0, 1, 2, 3))'
Fred Drake132dce22000-12-12 23:11:42 +0000259if tuple([]) != (): raise TestFailed, 'tuple([])'
260if tuple([0, 1, 2, 3]) != (0, 1, 2, 3): raise TestFailed, 'tuple([0, 1, 2, 3])'
261if tuple('') != (): raise TestFailed, 'tuple('')'
262if tuple('spam') != ('s', 'p', 'a', 'm'): raise TestFailed, "tuple('spam')"
Guido van Rossume23b62f1994-11-10 22:25:26 +0000263
Guido van Rossum3bead091992-01-27 17:00:37 +0000264print 'type'
Fred Drake132dce22000-12-12 23:11:42 +0000265if type('') != type('123') or type('') == type(()):
Fred Drake004d5e62000-10-23 17:22:08 +0000266 raise TestFailed, 'type()'
Guido van Rossum3bead091992-01-27 17:00:37 +0000267
Guido van Rossume23b62f1994-11-10 22:25:26 +0000268print 'vars'
269a = b = None
270a = vars().keys()
271b = dir()
272a.sort()
273b.sort()
Fred Drake132dce22000-12-12 23:11:42 +0000274if a != b: raise TestFailed, 'vars()'
Guido van Rossume23b62f1994-11-10 22:25:26 +0000275import sys
276a = vars(sys).keys()
277b = dir(sys)
278a.sort()
279b.sort()
Fred Drake132dce22000-12-12 23:11:42 +0000280if a != b: raise TestFailed, 'vars(sys)'
Guido van Rossuma49d94a1995-08-11 14:24:35 +0000281def f0():
Fred Drake004d5e62000-10-23 17:22:08 +0000282 if vars() != {}: raise TestFailed, 'vars() in f0()'
Guido van Rossuma49d94a1995-08-11 14:24:35 +0000283f0()
284def f2():
Fred Drake004d5e62000-10-23 17:22:08 +0000285 f0()
286 a = 1
287 b = 2
288 if vars() != {'a': a, 'b': b}: raise TestFailed, 'vars() in f2()'
Guido van Rossuma49d94a1995-08-11 14:24:35 +0000289f2()
Guido van Rossume23b62f1994-11-10 22:25:26 +0000290
291print 'xrange'
Fred Drakee0e890a2002-05-02 16:07:44 +0000292import warnings
293warnings.filterwarnings('ignore', r".*xrange", DeprecationWarning)
Fred Drake132dce22000-12-12 23:11:42 +0000294if tuple(xrange(10)) != tuple(range(10)): raise TestFailed, 'xrange(10)'
295if tuple(xrange(5,10)) != tuple(range(5,10)): raise TestFailed, 'xrange(5,10)'
296if tuple(xrange(0,10,2)) != tuple(range(0,10,2)):
Fred Drake004d5e62000-10-23 17:22:08 +0000297 raise TestFailed, 'xrange(0,10,2)'
Raymond Hettingerc4c453f2002-06-05 23:12:45 +0000298x = xrange(10); a = iter(x); b = iter(a) # test clearing of SF bug 564601
299if id(x) == id(a): raise TestFailed, "xrange doesn't have a separate iterator"
300if id(a) != id(b): raise TestFailed, "xrange iterator not behaving like range"
301if type(x) != xrange: raise TestFailed, "xrange type not exposed" # SF 559833
302if list(x) != list(x): raise TestFailed, "xrange should be restartable"
Guido van Rossume23b62f1994-11-10 22:25:26 +0000303
Barry Warsaw7bfc1a12000-08-03 15:48:07 +0000304print 'zip'
305a = (1, 2, 3)
306b = (4, 5, 6)
307t = [(1, 4), (2, 5), (3, 6)]
Fred Drake132dce22000-12-12 23:11:42 +0000308if zip(a, b) != t: raise TestFailed, 'zip(a, b) - same size, both tuples'
Barry Warsaw7bfc1a12000-08-03 15:48:07 +0000309b = [4, 5, 6]
Fred Drake132dce22000-12-12 23:11:42 +0000310if zip(a, b) != t: raise TestFailed, 'zip(a, b) - same size, tuple/list'
Barry Warsaw7bfc1a12000-08-03 15:48:07 +0000311b = (4, 5, 6, 7)
Fred Drake132dce22000-12-12 23:11:42 +0000312if zip(a, b) != t: raise TestFailed, 'zip(a, b) - b is longer'
Barry Warsaw7bfc1a12000-08-03 15:48:07 +0000313class I:
Fred Drake004d5e62000-10-23 17:22:08 +0000314 def __getitem__(self, i):
315 if i < 0 or i > 2: raise IndexError
316 return i + 4
Fred Drake132dce22000-12-12 23:11:42 +0000317if zip(a, I()) != t: raise TestFailed, 'zip(a, b) - b is instance'
Barry Warsaw7bfc1a12000-08-03 15:48:07 +0000318exc = 0
319try:
Fred Drake004d5e62000-10-23 17:22:08 +0000320 zip()
Barry Warsaw7bfc1a12000-08-03 15:48:07 +0000321except TypeError:
Fred Drake004d5e62000-10-23 17:22:08 +0000322 exc = 1
Barry Warsaw7bfc1a12000-08-03 15:48:07 +0000323except:
Fred Drake004d5e62000-10-23 17:22:08 +0000324 e = sys.exc_info()[0]
325 raise TestFailed, 'zip() - no args, expected TypeError, got %s' % e
Barry Warsaw7bfc1a12000-08-03 15:48:07 +0000326if not exc:
Fred Drake004d5e62000-10-23 17:22:08 +0000327 raise TestFailed, 'zip() - no args, missing expected TypeError'
Barry Warsaw7bfc1a12000-08-03 15:48:07 +0000328
329exc = 0
330try:
Fred Drake004d5e62000-10-23 17:22:08 +0000331 zip(None)
Barry Warsaw7bfc1a12000-08-03 15:48:07 +0000332except TypeError:
Fred Drake004d5e62000-10-23 17:22:08 +0000333 exc = 1
Barry Warsaw7bfc1a12000-08-03 15:48:07 +0000334except:
Fred Drake004d5e62000-10-23 17:22:08 +0000335 e = sys.exc_info()[0]
336 raise TestFailed, 'zip(None) - expected TypeError, got %s' % e
Barry Warsaw7bfc1a12000-08-03 15:48:07 +0000337if not exc:
Fred Drake004d5e62000-10-23 17:22:08 +0000338 raise TestFailed, 'zip(None) - missing expected TypeError'
Barry Warsaw370a29f2000-10-01 04:28:43 +0000339class G:
Fred Drake004d5e62000-10-23 17:22:08 +0000340 pass
Barry Warsaw370a29f2000-10-01 04:28:43 +0000341exc = 0
342try:
Fred Drake004d5e62000-10-23 17:22:08 +0000343 zip(a, G())
Tim Peters8572b4f2001-05-06 01:05:02 +0000344except TypeError:
Fred Drake004d5e62000-10-23 17:22:08 +0000345 exc = 1
Barry Warsaw370a29f2000-10-01 04:28:43 +0000346except:
Fred Drake004d5e62000-10-23 17:22:08 +0000347 e = sys.exc_info()[0]
348 raise TestFailed, 'zip(a, b) - b instance w/o __getitem__'
Barry Warsaw370a29f2000-10-01 04:28:43 +0000349if not exc:
Tim Peters8572b4f2001-05-06 01:05:02 +0000350 raise TestFailed, 'zip(a, b) - missing expected TypeError'
Barry Warsaw7bfc1a12000-08-03 15:48:07 +0000351
Tim Peters39a86c22002-05-12 07:19:38 +0000352# Make sure zip doesn't try to allocate a billion elements for the
353# result list when one of its arguments doesn't say how long it is.
354# A MemoryError is the most likely failure mode.
355class SequenceWithoutALength:
356 def __getitem__(self, i):
357 if i == 5:
358 raise IndexError
359 else:
360 return i
361vereq(zip(SequenceWithoutALength(), xrange(2**30)),
362 list(enumerate(range(5))))
Guido van Rossum3bead091992-01-27 17:00:37 +0000363
364# Epilogue -- unlink the temp file
Guido van Rossum3bead091992-01-27 17:00:37 +0000365unlink(TESTFN)