~ubuntu-branches/debian/experimental/nuitka/experimental

« back to all changes in this revision

Viewing changes to tests/basics/Referencing.py

  • Committer: Package Import Robot
  • Author(s): Kay Hayen
  • Date: 2012-01-10 22:21:56 UTC
  • Revision ID: package-import@ubuntu.com-20120110222156-fuyjhnhomqm6609e
Tags: upstream-0.3.18~pre2+ds
ImportĀ upstreamĀ versionĀ 0.3.18~pre2+ds

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#     Copyright 2012, Kay Hayen, mailto:kayhayen@gmx.de
 
2
#
 
3
#     Python tests originally created or extracted from other peoples work. The
 
4
#     parts were too small to be protected.
 
5
#
 
6
#     Licensed under the Apache License, Version 2.0 (the "License");
 
7
#     you may not use this file except in compliance with the License.
 
8
#     You may obtain a copy of the License at
 
9
#
 
10
#        http://www.apache.org/licenses/LICENSE-2.0
 
11
#
 
12
#     Unless required by applicable law or agreed to in writing, software
 
13
#     distributed under the License is distributed on an "AS IS" BASIS,
 
14
#     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
15
#     See the License for the specific language governing permissions and
 
16
#     limitations under the License.
 
17
#
 
18
import sys, gc
 
19
 
 
20
gc.disable()
 
21
 
 
22
def simpleFunction1():
 
23
   return 1
 
24
 
 
25
def simpleFunction2():
 
26
    y = 3 * x
 
27
    y = 3
 
28
    y = 2
 
29
 
 
30
    return x*2
 
31
 
 
32
def simpleFunction3():
 
33
    def contained():
 
34
        return x
 
35
 
 
36
    return contained
 
37
 
 
38
def simpleFunction4():
 
39
    y = 1
 
40
 
 
41
    def contained():
 
42
        return y
 
43
 
 
44
    return contained
 
45
 
 
46
def simpleFunction5( a = 1*2 ):
 
47
    c = 1
 
48
    f = [ a, a + c ]
 
49
 
 
50
def simpleFunction6():
 
51
    for b in range(6):
 
52
        pass
 
53
 
 
54
    for c in (1, 2, 3, 4, 5, 6):
 
55
        pass
 
56
 
 
57
 
 
58
def simpleFunction7( b = 1 ):
 
59
    for b in range(6):
 
60
        pass
 
61
 
 
62
def simpleFunction8():
 
63
    c = []
 
64
    c.append( x )
 
65
 
 
66
def simpleFunction9( a = 1*2 ):
 
67
    if a == a:
 
68
        pass
 
69
 
 
70
u = None
 
71
 
 
72
def simpleFunction10( a = 1*2 ):
 
73
    x = [u for u in range(8)]
 
74
 
 
75
def simpleFunction11():
 
76
    f = 1
 
77
 
 
78
    while f < 8:
 
79
        f += 1
 
80
 
 
81
v = None
 
82
 
 
83
def simpleFunction12():
 
84
    a = [ (u,v) for (u,v) in zip(range(8),range(8)) ]
 
85
 
 
86
def cond():
 
87
    return 1
 
88
 
 
89
def simpleFunction13( a = 1*2 ):
 
90
    pass
 
91
 
 
92
def simpleFunction14p(x):
 
93
    try:
 
94
        simpleFunction14p(1,1)
 
95
    except TypeError, e:
 
96
        pass
 
97
 
 
98
    try:
 
99
        simpleFunction14p(1,1)
 
100
    except TypeError:
 
101
        pass
 
102
 
 
103
def simpleFunction14():
 
104
    simpleFunction14p( 3 )
 
105
 
 
106
def simpleFunction15p(x):
 
107
    try:
 
108
        try:
 
109
            x += 1
 
110
        finally:
 
111
            try:
 
112
                x *= 1
 
113
            finally:
 
114
                z = 1
 
115
    except:
 
116
        pass
 
117
 
 
118
def simpleFunction15():
 
119
    simpleFunction15p( [ 1 ] )
 
120
 
 
121
def simpleFunction16():
 
122
    class EmptyClass:
 
123
        pass
 
124
 
 
125
    return EmptyClass
 
126
 
 
127
def simpleFunction17():
 
128
    class EmptyObjectClass:
 
129
        pass
 
130
 
 
131
    return EmptyObjectClass()
 
132
 
 
133
def simpleFunction18():
 
134
    closured = 1
 
135
 
 
136
    class NonEmptyClass:
 
137
        def __init__( self, a, b ):
 
138
            self.a = a
 
139
            self.b = b
 
140
 
 
141
        inside = closured
 
142
 
 
143
    return NonEmptyClass( 133, 135 )
 
144
 
 
145
def simpleFunction19():
 
146
    lam = lambda l : l+1
 
147
 
 
148
    return lam( 9 ), lam
 
149
 
 
150
 
 
151
def simpleFunction20():
 
152
    try:
 
153
        a = []
 
154
        a[1]
 
155
    except IndexError, e:
 
156
        pass
 
157
 
 
158
 
 
159
def simpleFunction21():
 
160
    class EmptyBaseClass:
 
161
        def base( self ):
 
162
            return 3
 
163
 
 
164
    class EmptyObjectClass( EmptyBaseClass ):
 
165
        pass
 
166
 
 
167
    result = EmptyObjectClass()
 
168
 
 
169
    c = result.base()
 
170
 
 
171
    return result
 
172
 
 
173
def simpleFunction22():
 
174
    return True is False and False is not None
 
175
 
 
176
def simpleFunction23():
 
177
    not 2
 
178
 
 
179
def simpleFunction24p( x ):
 
180
    pass
 
181
 
 
182
def simpleFunction24():
 
183
    simpleFunction24p( x = 3 )
 
184
 
 
185
 
 
186
def simpleFunction25():
 
187
    class X:
 
188
        f = 1
 
189
 
 
190
    def inplace_adder( b ):
 
191
        X.f += b
 
192
 
 
193
    return inplace_adder( 6**8 )
 
194
 
 
195
 
 
196
def simpleFunction26():
 
197
    class X:
 
198
        f = [ 5 ]
 
199
 
 
200
    def inplace_adder( b ):
 
201
        X.f += b
 
202
 
 
203
    return inplace_adder( [ 1, 2 ] )
 
204
 
 
205
def simpleFunction27():
 
206
    a = { "g": 8 }
 
207
 
 
208
    def inplace_adder( b ):
 
209
        a[ "g" ] += b
 
210
 
 
211
    return inplace_adder( 3 )
 
212
 
 
213
def simpleFunction28():
 
214
    a = { "g": [ 8 ], "h": 2 }
 
215
 
 
216
    def inplace_adder( b ):
 
217
        a[ "g" ] += b
 
218
 
 
219
    return inplace_adder( [ 3, 5 ] )
 
220
 
 
221
 
 
222
def simpleFunction29():
 
223
    return "3" in "7"
 
224
 
 
225
def simpleFunction30():
 
226
    def generatorFunction():
 
227
        yield 1
 
228
        yield 2
 
229
        yield 3
 
230
 
 
231
def simpleFunction31():
 
232
   def generatorFunction():
 
233
      yield 1
 
234
      yield 2
 
235
      yield 3
 
236
 
 
237
   a = []
 
238
 
 
239
   for y in generatorFunction():
 
240
      a.append( y )
 
241
 
 
242
   for z in generatorFunction():
 
243
      a.append( z )
 
244
 
 
245
 
 
246
def simpleFunction32():
 
247
   def generatorFunction():
 
248
      yield 1
 
249
 
 
250
   gen = generatorFunction()
 
251
   gen.next()
 
252
 
 
253
def simpleFunction33():
 
254
   def generatorFunction():
 
255
      a = 1
 
256
 
 
257
      yield a
 
258
 
 
259
   a = []
 
260
 
 
261
   for y in generatorFunction():
 
262
      a.append( y )
 
263
 
 
264
 
 
265
def simpleFunction34():
 
266
   try:
 
267
      raise ValueError
 
268
   except:
 
269
      pass
 
270
 
 
271
def simpleFunction35():
 
272
   try:
 
273
      raise ValueError(1,2,3)
 
274
   except:
 
275
      pass
 
276
 
 
277
 
 
278
def simpleFunction36():
 
279
   try:
 
280
      raise TypeError, (3,x,x,x)
 
281
   except TypeError:
 
282
      pass
 
283
 
 
284
def simpleFunction37():
 
285
   l = [ 1, 2, 3 ]
 
286
 
 
287
   try:
 
288
      a, b = l
 
289
   except ValueError:
 
290
      pass
 
291
 
 
292
 
 
293
def simpleFunction38():
 
294
   class Base:
 
295
      pass
 
296
 
 
297
   class Parent( Base ):
 
298
      pass
 
299
 
 
300
def simpleFunction39():
 
301
   class Parent( object ):
 
302
      pass
 
303
 
 
304
 
 
305
def simpleFunction40():
 
306
   def myGenerator():
 
307
      yield 1
 
308
 
 
309
   myGenerator()
 
310
 
 
311
def simpleFunction41():
 
312
   a = b = 2
 
313
 
 
314
 
 
315
def simpleFunction42():
 
316
   a = b = 2 * x
 
317
 
 
318
 
 
319
def simpleFunction43():
 
320
   class D:
 
321
      pass
 
322
 
 
323
   a = D()
 
324
 
 
325
   a.b = 1
 
326
 
 
327
def simpleFunction44():
 
328
   def nested_args_function( (a,b), c ):
 
329
      return a, b, c
 
330
 
 
331
   nested_args_function( ( 1, 2 ), 3 )
 
332
 
 
333
def simpleFunction45():
 
334
   def nested_args_function( (a,b), c ):
 
335
      return a, b, c
 
336
 
 
337
   try:
 
338
      nested_args_function( ( 1, ), 3 )
 
339
   except ValueError:
 
340
      pass
 
341
 
 
342
def simpleFunction46():
 
343
   def nested_args_function( (a,b), c ):
 
344
      return a, b, c
 
345
 
 
346
   try:
 
347
      nested_args_function( ( 1, 2, 3 ), 3 )
 
348
   except ValueError:
 
349
      pass
 
350
 
 
351
def simpleFunction47():
 
352
   def reraisy():
 
353
      def raisingFunction():
 
354
         raise ValueError(3)
 
355
 
 
356
      def reraiser():
 
357
         raise
 
358
 
 
359
      try:
 
360
         raisingFunction()
 
361
      except:
 
362
         reraiser()
 
363
 
 
364
   try:
 
365
      reraisy()
 
366
   except:
 
367
      pass
 
368
 
 
369
def simpleFunction48():
 
370
   class BlockExceptions:
 
371
      def __enter__( self ):
 
372
         pass
 
373
      def __exit__( self, exc, val, tb):
 
374
         return True
 
375
 
 
376
   with BlockExceptions():
 
377
      raise ValueError()
 
378
 
 
379
template = "lala %s lala"
 
380
 
 
381
def simpleFunction49():
 
382
   c = 3
 
383
   d = 4
 
384
 
 
385
   a = x, y = b,e = (c,d)
 
386
 
 
387
b = range(10)
 
388
 
 
389
def simpleFunction50():
 
390
   def getF():
 
391
      def f():
 
392
         for i in b:
 
393
            yield i
 
394
 
 
395
      return f
 
396
 
 
397
   f = getF()
 
398
 
 
399
   for x in range( 2 ):
 
400
      r = list( f() )
 
401
 
 
402
 
 
403
x = 17
 
404
 
 
405
def checkReferenceCount( checked_function, max_rounds = 4 ):
 
406
   sys.exc_clear()
 
407
   print checked_function.func_name + ":",
 
408
 
 
409
   ref_count1 = 17
 
410
   ref_count2 = 17
 
411
 
 
412
   for count in range( max_rounds ):
 
413
      gc.collect()
 
414
      ref_count1 = sys.gettotalrefcount()
 
415
 
 
416
      checked_function()
 
417
 
 
418
      sys.exc_clear()
 
419
      gc.collect()
 
420
 
 
421
      ref_count2 = sys.gettotalrefcount()
 
422
 
 
423
      if ref_count1 == ref_count2:
 
424
         print "PASSED"
 
425
         break
 
426
   else:
 
427
      print "FAILED", ref_count1, ref_count2
 
428
 
 
429
   sys.exc_clear()
 
430
   gc.collect()
 
431
 
 
432
 
 
433
checkReferenceCount( simpleFunction1 )
 
434
checkReferenceCount( simpleFunction2 )
 
435
checkReferenceCount( simpleFunction3 )
 
436
checkReferenceCount( simpleFunction4 )
 
437
checkReferenceCount( simpleFunction5 )
 
438
checkReferenceCount( simpleFunction6 )
 
439
checkReferenceCount( simpleFunction7 )
 
440
checkReferenceCount( simpleFunction8 )
 
441
checkReferenceCount( simpleFunction9 )
 
442
checkReferenceCount( simpleFunction10 )
 
443
checkReferenceCount( simpleFunction11 )
 
444
checkReferenceCount( simpleFunction12 )
 
445
checkReferenceCount( simpleFunction13 )
 
446
checkReferenceCount( simpleFunction14 )
 
447
checkReferenceCount( simpleFunction15 )
 
448
checkReferenceCount( simpleFunction16 )
 
449
checkReferenceCount( simpleFunction17 )
 
450
checkReferenceCount( simpleFunction18 )
 
451
checkReferenceCount( simpleFunction19 )
 
452
checkReferenceCount( simpleFunction20 )
 
453
checkReferenceCount( simpleFunction21 )
 
454
checkReferenceCount( simpleFunction22 )
 
455
checkReferenceCount( simpleFunction23 )
 
456
checkReferenceCount( simpleFunction24 )
 
457
checkReferenceCount( simpleFunction25 )
 
458
checkReferenceCount( simpleFunction26 )
 
459
checkReferenceCount( simpleFunction27 )
 
460
checkReferenceCount( simpleFunction28 )
 
461
checkReferenceCount( simpleFunction29 )
 
462
checkReferenceCount( simpleFunction30 )
 
463
checkReferenceCount( simpleFunction31 )
 
464
checkReferenceCount( simpleFunction32 )
 
465
checkReferenceCount( simpleFunction33 )
 
466
checkReferenceCount( simpleFunction34 )
 
467
checkReferenceCount( simpleFunction35 )
 
468
checkReferenceCount( simpleFunction36 )
 
469
checkReferenceCount( simpleFunction37 )
 
470
checkReferenceCount( simpleFunction38 )
 
471
checkReferenceCount( simpleFunction39 )
 
472
checkReferenceCount( simpleFunction40 )
 
473
checkReferenceCount( simpleFunction41 )
 
474
checkReferenceCount( simpleFunction42 )
 
475
checkReferenceCount( simpleFunction43 )
 
476
checkReferenceCount( simpleFunction44 )
 
477
checkReferenceCount( simpleFunction45 )
 
478
checkReferenceCount( simpleFunction46 )
 
479
checkReferenceCount( simpleFunction47 )
 
480
checkReferenceCount( simpleFunction48 )
 
481
checkReferenceCount( simpleFunction49 )
 
482
checkReferenceCount( simpleFunction50 )