Source

pypy-postgresql / pypy / translator / test / snippet.py

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
"""Snippets for translation

This module holds various snippets, to be used by translator
unittests.

We define argument types as default arguments to the snippet
functions.
"""

numtype = (int, float)
anytype = (int, float, str)
seqtype = (list, tuple)

def if_then_else(cond=anytype, x=anytype, y=anytype):
    if cond:
        return x
    else:
        return y

def my_gcd(a=numtype, b=numtype):
    r = a % b
    while r:
        a = b
        b = r
        r = a % b
    return b

def is_perfect_number(n=int):
    div = 1
    sum = 0
    while div < n:
        if n % div == 0:
            sum += div
        div += 1
    return n == sum

def my_bool(x=int):
    return not not x

def my_contains(seq=seqtype, elem=anytype):
    return elem in seq

def is_one_or_two(n=int):
    return n in [1, 2]

def two_plus_two():
    """Array test"""
    array = [0] * 3
    array[0] = 2
    array[1] = 2
    array[2] = array[0] + array[1]
    return array[2]

def get_set_del_slice(l=list):
    del l[:1]
    del l[-1:]
    del l[2:4]
    l[:1] = [3]
    l[-1:] = [9]
    l[2:4] = [8,11]
    return l[:2], l[5:], l[3:5]

def sieve_of_eratosthenes():
    """Sieve of Eratosthenes
    
    This one is from an infamous benchmark, "The Great Computer
    Language Shootout".

    URL is: http://www.bagley.org/~doug/shootout/
    """
    flags = [True] * (8192+1)
    count = 0
    i = 2
    while i <= 8192:
        if flags[i]:
            k = i + i
            while k <= 8192:
                flags[k] = False
                k = k + i
            count = count + 1
        i = i + 1
    return count

def simple_func(i=numtype):
    return i + 1

def while_func(i=numtype):
    total = 0
    while i > 0:
        total = total + i
        i = i - 1
    return total

def nested_whiles(i=int, j=int):
    s = ''
    z = 5
    while z > 0:
        z = z - 1
        u = i
        while u < j:
            u = u + 1
            s = s + '.'
        s = s + '!'
    return s

def poor_man_range(i=int): 
    lst = []
    while i > 0:
        i = i - 1
        lst.append(i)
    lst.reverse()
    return lst

def poor_man_rev_range(i=int): 
    lst = []
    while i > 0:
        i = i - 1
        lst += [i]
    return lst

def simple_id(x=anytype): 
    return x

def branch_id(cond=anytype, a=anytype, b=anytype):
    while 1:
        if cond:
            return a
        else:
            return b

def builtinusage():
    return pow(2, 2)

def yast(lst=seqtype):
    total = 0
    for z in lst:
        total = total + z
    return total

def time_waster(n=int):
    """Arbitrary test function"""
    i = 0
    x = 1
    while i < n:
        j = 0
        while j <= i:
            j = j + 1
            x = x + (i & j)
        i = i + 1
    return x

def half_of_n(n=int):
    """Slice test"""
    i = 0
    lst = range(n)
    while lst:
        lst = lst[1:-1]
        i = i + 1
    return i

def int_id(x=int):
    i = 0
    while i < x:
        i = i + 1
    return i

def greet(target=str):
    """String test"""
    hello = "hello"
    return hello + target

def choose_last():
    """For loop test"""
    set = ["foo", "bar", "spam", "egg", "python"]
    choice = ""
    for choice in set:
        pass
    return choice

def poly_branch(x=int):
    if x:
        y = [1,2,3]
    else:
        y = ['a','b','c']

    z = y
    return z*2

def s_and(x=anytype, y=anytype):
    if x and y:
        return 'yes'
    else:
        return 'no'

def break_continue(x=numtype):
    result = []
    i = 0
    while 1:
        i = i + 1
        try:
            if i&1:
                continue
            if i >= x:
                break
        finally:
            result.append(i)
        i = i + 1
    return result

def reverse_3(lst=seqtype):
    try:
        a, b, c = lst
    except:
        return 0, 0, 0
    return c, b, a

def finallys(lst=seqtype):
    x = 1
    try:
        x = 2
        try:
            x = 3
            a, = lst
            x = 4
        except KeyError:
            return 5
        except ValueError:
            return 6
        b, = lst
        x = 7
    finally:
        x = 8
    return x

def finally2(o, k):
    try:
        o[k] += 1
    finally:
        o[-1] = 'done'

def bare_raise(o, ignore):
    try:
        return o[5]
    except:
        if not ignore:
            raise

def factorial(n=int):
    if n <= 1:
        return 1
    else:
        return n * factorial(n-1)

def factorial2(n=int):   # analysed in a different order
    if n > 1:
        return n * factorial2(n-1)
    else:
        return 1

def _append_five(lst): 
    lst += [5]

def call_five():
    a = []
    _append_five(a)
    return a

def _append_six(lst): 
    lst += [6]

def call_five_six():
    a = []
    _append_five(a)
    _append_six(a)
    return a

def call_unpack_56():
    a = call_five_six()
    return len(a), a[0], a[1]

def forty_two():
    return 42

def never_called():
    return "booo"

def constant_result():
    if forty_two():
        return "yadda"
    else:
        return never_called()

class CallablePrebuiltConstant(object):
    def __call__(self):
        return 42

callable_prebuilt_constant = CallablePrebuiltConstant()

def call_cpbc():
    return callable_prebuilt_constant()


class E1(Exception):
    pass

class E2(Exception):
    pass

def raise_choose(n):
    if n == 1:
        raise E1
    elif n == 2:
        raise E2
    elif n == -1:
        raise Exception
    return 0

def try_raise_choose(n=int):
    try:
        raise_choose(n)
    except E1:
        return 1
    except E2:
        return 2
    except Exception:
        return -1
    return 0

def do_try_raise_choose():
    r = []
    for n in [-1,0,1,2]:
        r.append(try_raise_choose(n))
    return r


# INHERITANCE / CLASS TESTS  
class C(object): pass

def build_instance():
    c = C()
    return c

def set_attr():
    c = C()
    c.a = 1
    c.a = 2
    return c.a

def merge_setattr(x):
    if x:
        c = C()
        c.a = 1
    else:
        c = C()
    return c.a

class D(C): pass
class E(C): pass

def inheritance1():
    d = D()
    d.stuff = ()
    e = E()
    e.stuff = -12
    e.stuff = 3
    lst = [d, e]
    return d.stuff, e.stuff


def inheritance2():
    d = D()
    d.stuff = (-12, -12)
    e = E()
    e.stuff = (3, 12.3)
    return _getstuff(d), _getstuff(e)

class F:
    pass
class G(F):
    def m(self, x):
        return self.m2(x)
    def m2(self, x):
        return D(), x
class H(F):
    def m(self, y):
        self.attr = 1
        return E(), y

def knownkeysdict(b=anytype):
    if b:
        d = {'a': 0}
        d['b'] = b
        d['c'] = 'world'
    else:
        d = {'b': -123}
    return d['b']

def generaldict(key=str, value=int, key2=str, value2=int):
    d = {key: value}
    d[key2] = value2
    return d[key or key2]

def prime(n=int):
    return len([i for i in range(1,n+1) if n%i==0]) == 2

class A0:
    pass
class A1(A0):
    clsattr = 123
class A2(A1):
    clsattr = 456
class A3(A2):
    clsattr = 789
class A4(A3):
    pass
class A5(A0):
    clsattr = 101112

def classattribute(flag=int):
    if flag == 1:
        x = A1()
    elif flag == 2:
        x = A2()
    elif flag == 3:
        x = A3()
    elif flag == 4:
        x = A4()
    else:
        x = A5()
    return x.clsattr


class Z:
    def my_method(self):
        return self.my_attribute

class WithInit:
    def __init__(self, n):
        self.a = n

class WithMoreInit(WithInit):
    def __init__(self, n, m):
        WithInit.__init__(self, n)
        self.b = m

def simple_method(v=anytype):
    z = Z()
    z.my_attribute = v
    return z.my_method()

def with_init(v=int):
    z = WithInit(v)
    return z.a

def with_more_init(v=int, w=bool):
    z = WithMoreInit(v, w)
    if z.b:
        return z.a
    else:
        return -z.a

global_z = Z()
global_z.my_attribute = 42

def global_instance():
    return global_z.my_method()

def call_Z_my_method(z):
    return z.my_method

def somepbc_simplify():
    z = Z()
    call_Z_my_method(global_z)
    call_Z_my_method(z)

class ClassWithMethods:
    def cm(cls, x):
        return x
    cm = classmethod(cm)

    def sm(x):
        return x
    sm = staticmethod(sm)


global_c = C()
global_c.a = 1

def global_newstyle_instance():
    return global_c

global_rl = []
global_rl.append(global_rl)

def global_recursive_list():
    return global_rl

class MI_A(object):
    a = 1
class MI_B(MI_A):
    b = 2
class MI_C(MI_A):
    c = 3
class MI_D(MI_B, MI_C):
    d = 4

def multiple_inheritance():
    i = MI_D()
    return i.a + i.b + i.c + i.d

class CBase(object):
    pass
class CSub1(CBase):
    def m(self):
        self.x = 42
        return self.x
class CSub2(CBase):
    def m(self):
        self.x = 'world'
        return self.x

def methodcall_is_precise(cond):
    if cond:
        x = CSub1()
    else:
        x = CSub2()
    x.m()
    return CSub1().m()


def flow_type_info(i):
    if isinstance(i, int):
        a = i + 1
    else:
        a = len(str(i))
    return a

def flow_usertype_info(ob):
    if isinstance(ob, WithInit):
        return ob
    else:
        return WithMoreInit(1, 2)

def flow_identity_info(x=object, y=object):
    if x is None:
        if y is None:
            return (x, y)
        else:
            return (x, None)
    else:
        if y is None:
            return (None, y)
        else:
            return (None, None)

def star_args0(*args):
    return args[0] / 2

def call_star_args0(z):
    return star_args0(z)

def star_args1(a, *args):
    return a + args[0] / 2

def call_star_args1(z):
    return star_args1(z, 20)

def star_args1def(a=4, *args):
    if args:
        return a + args[0] / 2
    else:
        return a*3

def call_star_args1def(z):
    a = star_args1def(z, 22)
    b = star_args1def(5)
    c = star_args1def()
    return a+b+c
    
def star_args(x, y, *args):
    return x + args[0]

def call_star_args(z):
    return star_args(z, 5, 10, 15, 20)

def call_star_args_multiple(z):
    a = star_args(z, 5, 10)
    b = star_args(z, 5, 10, 15)
    c = star_args(z, 5, 10, 15, 20)
    return a+b+c

def default_args(x, y=2, z=3L):
    return x+y+z

def call_default_args(u):
    return default_args(111, u)

def default_and_star_args(x, y=2, z=3, *more):
    return x+y+z+len(more)

def call_default_and_star_args(u):
    return (default_and_star_args(111, u),
            default_and_star_args(-1000, -2000, -3000, -4000, -5000))

def call_with_star(z):
    return default_args(-20, *z)

def call_with_keyword(z):
    return default_args(-20, z=z)

def call_very_complex(z, args, kwds):
    return default_args(-20, z=z, *args, **kwds)

def powerset(setsize=int):
    """Powerset

    This one is from a Philippine Pythonista Hangout, an modified
    version of Andy Sy's code.
    
    list.append is modified to list concatenation, and powerset
    is pre-allocated and stored, instead of printed.
    
    URL is: http://lists.free.net.ph/pipermail/python/2002-November/
    """
    set = range(setsize)
    maxcardinality = pow(2, setsize)
    bitmask = 0L
    powerset = [None] * maxcardinality
    ptr = 0
    while bitmask < maxcardinality:
        bitpos = 1L
        index = 0
        subset = []
        while bitpos < maxcardinality:
            if bitpos & bitmask:
                subset = subset + [set[index]]
            index += 1
            bitpos <<= 1
        powerset[ptr] = subset
        ptr += 1
        bitmask += 1
    return powerset

def harmonic(n):
    result = 0.0
    for i in range(n, 0, -1):
        result += 1.0 / n
    return result


# --------------------(Currently) Non runnable Functions ---------------------

def _somebug1(n=int):
    l = []
    v = l.append
    while n:
        l[7] = 5 # raises an exception 
        break 
    return v

def _inheritance_nonrunnable():
    d = D()
    d.stuff = (-12, -12)
    e = E()
    e.stuff = (3, "world")
    return C().stuff

def _getstuff(x):
    return x.stuff

# --------------------(Currently) Non compilable Functions ---------------------

class BadInit(object):
    def update(self, k):
        self.k = 1
    def __init__(self, v):
        return
        self.update(**{'k':v})
    def read(self):
        return self.k

global_bi = BadInit(1)

def global_badinit():
    return global_bi.read()

def _attrs():
    def b(): pass
    b.f = 4
    b.g = 5
    return b.f + b.g

def _methodcall1(cond):
    if cond:
        x = G()
    else:
        x = H()
    return x.m(42)

def func1():
    pass

def func2():
    pass

def mergefunctions(cond):
    if cond: 
        x = func1
    else:
        x = func2
    return x

def func_producing_exception():
    raise ValueError, "this might e.g. block the caller"

def funccallsex():
    return func_producing_exception()


def func_arg_unpack():
    a,b = 3, "hello"
    return a

class APBC: 
    def __init__(self): 
        self.answer = 42

apbc = APBC()
apbc.answer = 7

def preserve_pbc_attr_on_instance(cond):
    if cond: 
        x = APBC()
    else: 
        x = apbc 
    return x.answer 


class APBCS(object):
    __slots__ = ['answer']
    def __init__(self): 
        self.answer = 42

apbcs = APBCS()
apbcs.answer = 7

def preserve_pbc_attr_on_instance_with_slots(cond):
    if cond: 
        x = APBCS()
    else: 
        x = apbcs
    return x.answer 


def is_and_knowntype(x): 
    if x is None: 
        return x 
    else: 
        return None 

def isinstance_and_knowntype(x): 
    if isinstance(x, APBC): 
        return x
    else: 
        return apbc 

def simple_slice(x):
    return x[:10]

def simple_iter(x):
    return iter(x)

def simple_zip(x,y):
    return zip(x,y)

def dict_copy(d):
    return d.copy()

def dict_update(x):
    d = {x:x}
    d.update({1:2})
    return d

def dict_keys():
    d = {"a" : 1}
    return d.keys()

def dict_keys2():
    d = {"a" : 1}
    keys = d.keys()
    d[1] = 12
    return keys

def dict_values():
    d = {"a" : "a"}
    return d.values()

def dict_values2():
    d = {"a" : "a"}
    values = d.values()
    d[1] = 12
    return values

def dict_items():
    d = {'a' : 1}
    return d.items()

class Exc(Exception):
    pass

def exception_deduction0(x):
    pass

def exception_deduction():
    try:
        exception_deduction0(2)
    except Exc, e:
        return e
    return Exc()


def always_raising(x):
    raise ValueError

def witness(x):
    pass

def exception_deduction_with_raise1(x):
    try:
        exception_deduction0(2)
        if x:
            raise Exc()
    except Exc, e:
        witness(e)
        return e
    return Exc()

def exception_deduction_with_raise2(x):
    try:
        exception_deduction0(2)
        if x:
            raise Exc
    except Exc, e:
        witness(e)
        return e
    return Exc()

def exception_deduction_with_raise3(x):
    try:
        exception_deduction0(2)
        if x:
            raise Exc, Exc()
    except Exc, e:
        witness(e)
        return e
    return Exc()
    
def slice_union(x):
    if x:
        return slice(1)
    else:
        return slice(0, 10, 2)

def exception_deduction_we_are_dumb():
    a = 1
    try:
        exception_deduction0(2)
    except Exc, e:
        a += 1
        return e
    return Exc()

class Exc2(Exception):
    pass

def nested_exception_deduction():
    try:
        exception_deduction0(1)
    except Exc, e:
        try:
            exception_deduction0(2)
        except Exc2, f:
            return (e, f)
        return (e, Exc2())
    return (Exc(), Exc2())

class Exc3(Exception):
    def m(self):
        return 1

class Exc4(Exc3):
    def m(self):
        return 1

class Sp:
    def o(self):
        raise Exc3

class Mod:
    def __init__(self, s):
        self.s = s

    def p(self):
        s = self.s
        try:
            s.o()
        except Exc3, e:
            return e.m()
        return 0

class Mod3:
    def __init__(self, s):
        self.s = s

    def p(self):
        s = self.s
        try:
            s.o()
        except Exc4, e1:
            return e1.m()
        except Exc3, e2:
            try:
                return e2.m()
            except Exc4, e3:
                return e3.m()
        return 0


mod = Mod(Sp())
mod3 = Mod3(Sp())

def exc_deduction_our_exc_plus_others():
    return mod.p()

def exc_deduction_our_excs_plus_others():
    return mod3.p()



def call_two_funcs_but_one_can_only_raise(n):
    fn = [witness, always_raising][n]
    return fn(n)


class BltinCode:
  def __init__(self, func, framecls):
    self.func = func
    self.framecls = framecls

  def call(self, x):
     return self.framecls(self).run(x)

class BltinFrame:
  def __init__(self, code):
    self.code = code

def bltin_code_frame_f(x):
  return x

def bltin_code_frame_g(x):
  return x

class FBltinFrame(BltinFrame):

  def run(self, x):
     return self.code.func(x)

class GBltinFrame(BltinFrame):

  def run(self, x):
     return self.code.func(x)

bltin_code_for_f = BltinCode(bltin_code_frame_f, FBltinFrame)
bltin_code_for_g = BltinCode(bltin_code_frame_g, GBltinFrame)

def bltin_code_frame_confusion():
  a = bltin_code_for_f.call(0)
  a1 = bltin_code_for_f.call(1)
  b = bltin_code_for_g.call("a")
  b1 = bltin_code_for_g.call("b")
  return (a,a1,b,b1)


# reorg

class BltinCodeReorg:
  def __init__(self, framecls):
    self.framecls = framecls

  def call(self, x):
     frame = self.framecls()
     frame.set(x)
     return frame.run()

class BltinFrameReorg:
  def __init__(self):
      pass

  def set(self,x):
      pass

  def run(self):
      pass

class FBltinFrameReorg(BltinFrameReorg):

  def set(self, x):
      self.arg = int(x)

  def run(self):
     return bltin_code_frame_f(self.arg)

class GBltinFrameReorg(BltinFrameReorg):

  def set(self, x):
      self.arg = str(x)

  def run(self):
     return bltin_code_frame_g(self.arg)


bltin_code_for_f_reorg = BltinCodeReorg(FBltinFrameReorg)
bltin_code_for_g_reorg = BltinCodeReorg(GBltinFrameReorg)

def bltin_code_frame_reorg():
  a = bltin_code_for_f_reorg.call(0)
  a1 = bltin_code_for_f_reorg.call(1)
  b = bltin_code_for_g_reorg.call("a")
  b1 = bltin_code_for_g_reorg.call("b")
  return (a,a1,b,b1)


# constant instances with __init__ vs. __new__

class Thing1:

    def __init__(self):
        self.thingness = 1

thing1 = Thing1()

def one_thing1():
    return thing1


class Thing2(long):
    def __new__(t,v):
       return  long.__new__(t,v*2)

thing2 = Thing2(2)

def one_thing2():
    return thing2

# propagation of fresh instances through attributes

class Stk:
    def __init__(self):
        self.itms = []

    def push(self, v):
        self.itms.append(v)

class EC:

    def __init__(self):
        self.stk = Stk()

    def enter(self, f):
        self.stk.push(f)

def propagation_of_fresh_instances_through_attrs(x):
    e = EC()
    e.enter(x)

# same involving recursion


class R:
  def __init__(self, n):
      if n>0:
          self.r = R(n-1)
      else:
          self.r = None
      self.n = n
      if self.r:
          self.m = self.r.n
      else:
          self.m = -1

def make_r(n):
    return R(n)

class B:
    pass

class Even(B):
    def __init__(self,n):
        if n > 0:
            self.x = [Odd(n-1)]
            self.y = self.x[0].x
        else:
            self.x = []
            self.y = []

class Odd(B):
    def __init__(self,n):
        self.x = [Even(n-1)]
        self.y = self.x[0].x

def make_eo(n):
    if n%2 == 0:
        return Even(n)
    else:
        return Odd(n)


# shows that we care about the expanded structure in front of changes to attributes involving only
# instances rev numbers

class Box:
 pass

class Box2:
  pass

class Box3(Box2):
  pass

def flow_rev_numbers(n):
  bx3 = Box3()
  bx3.x = 1
  bx = Box()
  bx.bx3 = bx3
  if n >0:
     z =  bx.bx3.x
     if n >0:
        bx2 = Box2()
        bx2.x = 3
     return z
  raise Exception


# class specialization

class PolyStk:
    _annspecialcase_ = "specialize:ctr_location"

    def __init__(self):
        self.itms = []

    def push(self, v):
        self.itms.append(v)

    def top(self):
        return self.itms[-1]


def class_spec():
    istk = PolyStk()
    istk.push(1)
    sstk = PolyStk()
    sstk.push("a")
    istk.push(2)
    sstk.push("b")
    #if not isinstance(istk, PolyStk):
    #    return "confused"
    return istk.top(), sstk.top()


from pypy.rlib.rarithmetic import ovfcheck

def add_func(i=numtype):
    try:
        return ovfcheck(i + 1)
    except OverflowError:
        raise

from sys import maxint

def div_func(i=numtype):
    try:
        return ovfcheck((-maxint-1) // i)
    except (OverflowError, ZeroDivisionError):
        raise
    
def mul_func(x=numtype, y=numtype):
    try:
        return ovfcheck(x * y)
    except OverflowError:
        raise
    
def mod_func(i=numtype):
    try:
        return ovfcheck((-maxint-1) % i)
    except OverflowError:
        raise
    except ZeroDivisionError:
        raise

def rshift_func(i=numtype):
    try:
        return (-maxint-1) >> i
    except ValueError:
        raise

class hugelmugel(OverflowError):pass

def hugo(a, b, c):pass

def lshift_func(i=numtype):
    try:
        hugo(2, 3, 5)
        return ovfcheck((-maxint-1) << i)
    except (hugelmugel, OverflowError, StandardError, ValueError):
        raise

def unary_func(i=numtype):
    try:
        return ovfcheck(-i), ovfcheck(abs(i-1))
    except: raise
    # XXX it would be nice to get it right without an exception
    # handler at all, but then we need to do much harder parsing