PageRenderTime 44ms CodeModel.GetById 15ms app.highlight 25ms RepoModel.GetById 1ms app.codeStats 0ms

/IronPython_Main/Languages/IronPython/Tests/compat/sbs_exceptions/shared.py

#
Python | 372 lines | 355 code | 3 blank | 14 comment | 1 complexity | a9d12ec6b11f69da4e2aee7d63447099 MD5 | raw file
  1#####################################################################################
  2#
  3#  Copyright (c) Microsoft Corporation. All rights reserved.
  4#
  5# This source code is subject to terms and conditions of the Apache License, Version 2.0. A 
  6# copy of the license can be found in the License.html file at the root of this distribution. If 
  7# you cannot locate the  Apache License, Version 2.0, please send an email to 
  8# ironpy@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
  9# by the terms of the Apache License, Version 2.0.
 10#
 11# You must not remove this notice, or any other, from this software.
 12#
 13#
 14#####################################################################################
 15
 16from common import runtests, printwith
 17
 18import sys
 19import nt
 20
 21class CodeHolder(object):
 22    def __init__(self):
 23        self.text = ''
 24        self.depth = 0
 25    def Add(self, text):
 26        self.text += text
 27       
 28class try_finally_generator(object):
 29    def __init__(self, codeHolder, tryBody, finallyBody):
 30        self.code = codeHolder
 31        self.tryBody = tryBody
 32        self.finallyBody = finallyBody
 33    def generate(self, indent=1):
 34        self.code.Add('    '*indent); self.code.Add('try:\n')
 35        self.code.Add('    '*(indent+1));self.code.Add('log+="try"\n')
 36        self.tryBody.generate(indent+1)
 37        self.code.Add('    '*indent); self.code.Add('finally:\n')
 38        self.code.Add('    '*(indent+1));self.code.Add('log+="finally"\n')
 39        self.code.Add('    '*(indent+1));self.code.Add('log+=dump_exc_info()\n')
 40        self.finallyBody.generate(indent+1)
 41    
 42class try_except_generator(object):
 43    def __init__(self, codeHolder, tryBody, exceptBody):
 44        self.code = codeHolder
 45        self.tryBody = tryBody
 46        self.exceptBody = exceptBody
 47    def generate(self, indent=1):
 48        self.code.Add('    '*indent); self.code.Add('try:\n')
 49        self.code.Add('    '*(indent+1));self.code.Add('log+="try"\n')
 50        self.tryBody.generate(indent+1)
 51        self.code.Add('    '*indent); self.code.Add('except:\n')
 52        self.code.Add('    '*(indent+1));self.code.Add('log+="except"\n')
 53        self.code.Add('    '*(indent+1));self.code.Add('log+=dump_exc_info()\n')
 54        self.exceptBody.generate(indent+1)
 55    
 56class try_except_else_generator(object):        
 57    def __init__(self, codeHolder, tryBody, exceptBody, elseBody):
 58        self.code = codeHolder
 59        self.tryBody = tryBody
 60        self.exceptBody = exceptBody
 61        self.elseBody = elseBody
 62    def generate(self, indent=1):
 63        self.code.Add('    '*indent); self.code.Add('try:\n')
 64        self.code.Add('    '*(indent+1));self.code.Add('log+="try"\n')
 65        self.tryBody.generate(indent+1)
 66        self.code.Add('    '*indent); self.code.Add('except:\n')
 67        self.code.Add('    '*(indent+1));self.code.Add('log+="except"\n')
 68        self.code.Add('    '*(indent+1));self.code.Add('log+=dump_exc_info()\n')
 69        self.exceptBody.generate(indent+1)
 70        self.code.Add('    '*indent); self.code.Add('else:\n')
 71        self.code.Add('    '*(indent+1));self.code.Add('log+="else"\n')
 72        self.code.Add('    '*(indent+1));self.code.Add('log+=dump_exc_info()\n')
 73        self.elseBody.generate(indent+1)    
 74        
 75class for_loop_generator(object):
 76    def __init__(self, codeHolder, var, items, body):
 77        self.code = codeHolder
 78        self.var = var
 79        self.items = items
 80        self.body = body
 81    def generate(self, indent=1):
 82        self.code.Add('    '*indent);self.code.Add('log+="preloop"\n')
 83        self.code.Add('    '*indent);self.code.Add('for %s in %s:\n' % (self.var+str(indent), self.items))
 84        self.code.Add('    '*(indent+1));self.code.Add('log+="inloop"\n')
 85        self.body.generate(indent+1)
 86
 87class while_loop_generator(object):
 88    def __init__(self, codeHolder, body):
 89        self.code = codeHolder
 90        self.body = body
 91    def generate(self, indent=1):
 92        global uniqueCount
 93        self.code.Add('    '*indent);self.code.Add('log+="preloop"\n')
 94        self.code.Add('    '*indent);self.code.Add('whilevar%d_%d = 0\n' % (indent, uniqueCount))
 95        self.code.Add('    '*indent);self.code.Add('while whilevar%d_%d < 3:\n' % (indent, uniqueCount))
 96        self.code.Add('    '*(indent+1));self.code.Add('whilevar%d_%d += 1\n' % (indent, uniqueCount))
 97        self.code.Add('    '*(indent+1));self.code.Add('log+="inloop"\n')
 98        uniqueCount += 1
 99        self.body.generate(indent+1)
100
101class pass_generator(object):
102    def __init__(self, codeHolder):
103        self.code = codeHolder
104    def generate(self, indent=1):
105        self.code.Add('    '*indent);self.code.Add('log+="pass"\n')
106        self.code.Add('    '*indent);self.code.Add('pass\n')
107        
108class break_generator(object):
109    def __init__(self, codeHolder):
110        self.code = codeHolder
111    def generate(self, indent=1):
112        self.code.Add('    '*indent);self.code.Add('log+="break"\n')
113        self.code.Add('    '*indent);self.code.Add('break\n')
114
115class continue_generator(object):
116    def __init__(self, codeHolder):
117        self.code = codeHolder
118    def generate(self, indent=1):
119        self.code.Add('    '*indent);self.code.Add('log+="continue"\n')
120        self.code.Add('    '*indent);self.code.Add('continue\n')
121
122class return_generator(object):
123    def __init__(self, codeHolder, state):
124        self.code = codeHolder
125        self.state = state
126    def generate(self, indent=1):
127        self.code.Add('    '*indent);self.code.Add('log+="return"\n')
128        self.code.Add('    '*indent);self.code.Add('return %s\n' % self.state)
129
130class if_false_generator(object):
131    def __init__(self, codeHolder, body):
132        self.code = codeHolder
133        self.body = body
134    def generate(self, indent=1):
135        self.code.Add('    '*indent);self.code.Add('log+="preif"\n')
136        self.code.Add('    '*indent);self.code.Add('if False:\n')
137        self.code.Add('    '*(indent+1));self.code.Add('log+="huh?"\n')
138        self.body.generate(indent+1)
139
140class if_true_generator(object):
141    def __init__(self, codeHolder, body):
142        self.code = codeHolder
143        self.body = body
144    def generate(self, indent=1):
145        self.code.Add('    '*indent);self.code.Add('log+="preif"\n')
146        self.code.Add('    '*indent);self.code.Add('if True:\n')
147        self.code.Add('    '*(indent+1));self.code.Add('log+="true!"\n')
148        self.body.generate(indent+1)
149
150class yield_generator(object):
151    def __init__(self, codeHolder, state):
152        self.code = codeHolder
153        self.state = state
154    def generate(self, indent=1):
155        self.code.Add('    '*indent);self.code.Add('log+="yield"\n')
156        self.code.Add('    '*indent);self.code.Add('yield %s\n' % self.state)
157        
158class raise_generator(object):
159    def __init__(self, codeHolder, state):
160        self.code = codeHolder
161        self.state = state
162    def generate(self, indent=1):
163        self.code.Add('    '*indent);self.code.Add('raise Exception("%s")\n' % self.state)
164
165class define_generator(object):
166    def __init__(self, codeHolder, body):
167        self.code = codeHolder
168        self.body = body
169    def generate(self, indent=1):
170        global uniqueCount
171        saved = uniqueCount
172        uniqueCount += 1
173        self.code.Add('    '*indent);self.code.Add('log+="predefine"\n')
174        self.code.Add('    '*indent);self.code.Add('def func%d_%d():\n' % (indent, saved))
175        self.code.Add('    '*(indent+1));self.code.Add('global log\n')
176        self.body.generate(indent+1)
177        self.code.Add('    '*indent);self.code.Add('func%d_%d()\n' % (indent, saved))
178
179ch = CodeHolder()
180
181def for_loop_maker(ch, body):
182    return for_loop_generator(ch, 'x', 'range(3)', body)
183    
184def while_loop_maker(ch, body):
185    return while_loop_generator(ch, body)
186    
187def try_except_maker1(ch, body):
188    return try_except_generator(ch, pass_generator(ch), body)
189
190def try_except_maker2(ch, body):
191    return try_except_generator(ch, body, pass_generator(ch))
192
193def try_except_maker3(ch, body):
194    return try_except_generator(ch, body, body)
195
196def try_finally_maker1(ch, body):
197    return try_finally_generator(ch, pass_generator(ch), body)
198
199def try_finally_maker2(ch, body):
200    return try_finally_generator(ch, body, pass_generator(ch))
201
202def try_finally_maker3(ch, body):
203    return try_finally_generator(ch, body, body)
204
205def try_else_maker1(ch, body):
206    return try_except_else_generator(ch, pass_generator(ch), body, body)
207
208def pass_maker(ch, body):
209    return pass_generator(ch)
210
211def break_maker(ch, body):
212    return break_generator(ch)
213    
214def continue_maker(ch, body):
215    return continue_generator(ch)
216
217def define_maker(ch, body):
218    return define_generator(ch, body)
219
220def generator(): yield 2
221generator_type = type(generator())
222
223loopCnt = 0
224generatorDepth = 0
225yieldState = 0
226finallyCnt = 0
227tryOrCatchCount = 0
228
229uniqueCount = 0
230
231def dump_exc_info():
232    tb = sys.exc_info()[2]
233    tb_list = []
234    while tb is not None :
235        f = tb.tb_frame
236        co = f.f_code
237        filename = co.co_filename
238        #Shrink the filename a bit
239        if filename.count("\\"):
240            filename = filename.rsplit("\\", 1)[1]
241        name = co.co_name
242        tb_list.append((tb.tb_lineno, filename, name))
243        tb = tb.tb_next
244    return str(tb_list)
245
246
247#------------------------------------------------------------------------------
248allGenerators = []
249    
250def setGenerator(generator):
251    global allGenerators
252    allGenerators = [ generator ]
253
254
255knownFailures = []
256
257def setKnownFailures(failures):
258    global knownFailures
259    knownFailures = failures
260    
261
262class test_exceptions(object):
263    
264    def test_exceptions(self):    
265        if len(allGenerators)==0:
266            raise Exception("Need at least one generator from test_exceptions")
267    
268        stateful = [raise_generator, return_generator]
269        
270        ch = CodeHolder()
271        
272        curTest = 0 # a counter so that trace backs have unique method names and we check left over info properly
273        for depth in xrange(3):
274            def do_generate(test):
275                global loopCnt, yieldState, finallyCnt, tryOrCatchCount, uniqueCount
276                yieldState += 1
277                
278                if test in (for_loop_maker,  while_loop_maker):
279                    loopCnt += 1
280                if test in (try_finally_maker1, try_finally_maker2, try_finally_maker3):
281                    finallyCnt += 1
282                if test in (try_except_maker1, try_except_maker2, try_except_maker3, try_else_maker1):
283                    tryOrCatchCount += 1
284                 
285
286                genSet = [  for_loop_maker, while_loop_maker,
287                            try_except_maker1, try_except_maker2, try_except_maker3, 
288                            try_finally_maker1, try_finally_maker2, try_finally_maker3, 
289                            try_else_maker1, 
290                            if_false_generator, if_true_generator, 
291                            define_maker,
292                            ]    
293
294                if loopCnt > 0: 
295                    if finallyCnt > 0: genSet = genSet + [break_maker]        
296                    else: genSet = genSet + [break_maker, continue_maker]        
297                
298                if ch.depth > depth:
299                    yield test(ch, pass_generator(ch))
300                else:
301                    for testCase in genSet:
302                        ch.depth += 1
303        
304                        x = do_generate(testCase)
305                        for body in x:                            
306                            yield test(ch, body)
307        
308                        ch.depth -= 1
309                
310                for statefulGuy in stateful:
311                    yield test(ch, statefulGuy(ch, yieldState))
312                
313                if finallyCnt == 0:
314                    yield test(ch, yield_generator(ch, yieldState))
315                
316                if test in (for_loop_maker,  while_loop_maker):
317                    loopCnt -= 1        
318                if test in (try_finally_maker1, try_finally_maker2, try_finally_maker3):
319                    finallyCnt -= 1
320                if test in (try_except_maker1, try_except_maker2, try_except_maker3, try_else_maker1):
321                    tryOrCatchCount -= 1
322                        
323            for testCase in allGenerators:
324                x = do_generate(testCase)
325                        
326                for y in x:
327                    curTest += 1
328                    
329                    if curTest in knownFailures:
330                        continue
331                    
332                    if 'IRONPYTHON_RUNSLOWTESTS' in nt.environ:
333                        uniqueCount = 0
334    
335                        # run without a function                    
336                        y.code.text = ''
337                        y.generate(0)
338                        y.code.text += 'print log'
339                        d = {'log': '', 'dump_exc_info': dump_exc_info}
340                        try: 
341                            #printwith(y.code.text)
342                            exec y.code.text in d, d
343                        except Exception, e:
344                            printwith('same', sys.exc_type)
345
346                    uniqueCount = 0
347                    
348                    # run within a function
349                    y.code.text = 'def test' + str(curTest) + '():\n'
350                    y.code.text += '    global log\n'
351                    y.generate()
352                    d = {'log' : '', 'dump_exc_info': dump_exc_info}
353                    
354                    try:
355                        printwith(y.code.text)
356                        exec y.code.text in d, d
357                    except SyntaxError:
358                        printwith("same", sys.exc_type)
359                        continue
360                    
361                    retval = None
362                    try:
363                        retval = d['test' + str(curTest)]()
364                        if isinstance(retval, generator_type):
365                            for it in retval: printwith('same', it)
366                        else:
367                            printwith('same', retval)
368                    except: 
369                        printwith("same", sys.exc_type)
370                    if isinstance(retval, generator_type):
371                        retval.close()
372                    printwith('same', d['log'])