| 1 |
"""Snippets for translation |
| 2 |
|
| 3 |
This module holds various snippets, to be used by translator |
| 4 |
unittests. |
| 5 |
|
| 6 |
We define argument types as default arguments to the snippet |
| 7 |
functions. |
| 8 |
""" |
| 9 |
|
| 10 |
numtype = (int, float) |
| 11 |
anytype = (int, float, str) |
| 12 |
seqtype = (list, tuple) |
| 13 |
|
| 14 |
def if_then_else(cond=anytype, x=anytype, y=anytype): |
| 15 |
if cond: |
| 16 |
return x |
| 17 |
else: |
| 18 |
return y |
| 19 |
|
| 20 |
def my_gcd(a=numtype, b=numtype): |
| 21 |
r = a % b |
| 22 |
while r: |
| 23 |
a = b |
| 24 |
b = r |
| 25 |
r = a % b |
| 26 |
return b |
| 27 |
|
| 28 |
def is_perfect_number(n=int): |
| 29 |
div = 1 |
| 30 |
sum = 0 |
| 31 |
while div < n: |
| 32 |
if n % div == 0: |
| 33 |
sum += div |
| 34 |
div += 1 |
| 35 |
return n == sum |
| 36 |
|
| 37 |
def my_bool(x=int): |
| 38 |
return not not x |
| 39 |
|
| 40 |
def my_contains(seq=seqtype, elem=anytype): |
| 41 |
return elem in seq |
| 42 |
|
| 43 |
def is_one_or_two(n=int): |
| 44 |
return n in [1, 2] |
| 45 |
|
| 46 |
def two_plus_two(): |
| 47 |
"""Array test""" |
| 48 |
array = [0] * 3 |
| 49 |
array[0] = 2 |
| 50 |
array[1] = 2 |
| 51 |
array[2] = array[0] + array[1] |
| 52 |
return array[2] |
| 53 |
|
| 54 |
def get_set_del_slice(l=list): |
| 55 |
del l[:1] |
| 56 |
del l[-1:] |
| 57 |
del l[2:4] |
| 58 |
l[:1] = [3] |
| 59 |
l[-1:] = [9] |
| 60 |
l[2:4] = [8,11] |
| 61 |
return l[:2], l[5:], l[3:5] |
| 62 |
|
| 63 |
def sieve_of_eratosthenes(): |
| 64 |
"""Sieve of Eratosthenes |
| 65 |
|
| 66 |
This one is from an infamous benchmark, "The Great Computer |
| 67 |
Language Shootout". |
| 68 |
|
| 69 |
URL is: http://www.bagley.org/~doug/shootout/ |
| 70 |
""" |
| 71 |
flags = [True] * (8192+1) |
| 72 |
count = 0 |
| 73 |
i = 2 |
| 74 |
while i <= 8192: |
| 75 |
if flags[i]: |
| 76 |
k = i + i |
| 77 |
while k <= 8192: |
| 78 |
flags[k] = False |
| 79 |
k = k + i |
| 80 |
count = count + 1 |
| 81 |
i = i + 1 |
| 82 |
return count |
| 83 |
|
| 84 |
def simple_func(i=numtype): |
| 85 |
return i + 1 |
| 86 |
|
| 87 |
def while_func(i=numtype): |
| 88 |
total = 0 |
| 89 |
while i > 0: |
| 90 |
total = total + i |
| 91 |
i = i - 1 |
| 92 |
return total |
| 93 |
|
| 94 |
def nested_whiles(i=int, j=int): |
| 95 |
s = '' |
| 96 |
z = 5 |
| 97 |
while z > 0: |
| 98 |
z = z - 1 |
| 99 |
u = i |
| 100 |
while u < j: |
| 101 |
u = u + 1 |
| 102 |
s = s + '.' |
| 103 |
s = s + '!' |
| 104 |
return s |
| 105 |
|
| 106 |
def poor_man_range(i=int): |
| 107 |
lst = [] |
| 108 |
while i > 0: |
| 109 |
i = i - 1 |
| 110 |
lst.append(i) |
| 111 |
lst.reverse() |
| 112 |
return lst |
| 113 |
|
| 114 |
def poor_man_rev_range(i=int): |
| 115 |
lst = [] |
| 116 |
while i > 0: |
| 117 |
i = i - 1 |
| 118 |
lst += [i] |
| 119 |
return lst |
| 120 |
|
| 121 |
def simple_id(x=anytype): |
| 122 |
return x |
| 123 |
|
| 124 |
def branch_id(cond=anytype, a=anytype, b=anytype): |
| 125 |
while 1: |
| 126 |
if cond: |
| 127 |
return a |
| 128 |
else: |
| 129 |
return b |
| 130 |
|
| 131 |
def builtinusage(): |
| 132 |
return pow(2, 2) |
| 133 |
|
| 134 |
def yast(lst=seqtype): |
| 135 |
total = 0 |
| 136 |
for z in lst: |
| 137 |
total = total + z |
| 138 |
return total |
| 139 |
|
| 140 |
def time_waster(n=int): |
| 141 |
"""Arbitrary test function""" |
| 142 |
i = 0 |
| 143 |
x = 1 |
| 144 |
while i < n: |
| 145 |
j = 0 |
| 146 |
while j <= i: |
| 147 |
j = j + 1 |
| 148 |
x = x + (i & j) |
| 149 |
i = i + 1 |
| 150 |
return x |
| 151 |
|
| 152 |
def half_of_n(n=int): |
| 153 |
"""Slice test""" |
| 154 |
i = 0 |
| 155 |
lst = range(n) |
| 156 |
while lst: |
| 157 |
lst = lst[1:-1] |
| 158 |
i = i + 1 |
| 159 |
return i |
| 160 |
|
| 161 |
def int_id(x=int): |
| 162 |
i = 0 |
| 163 |
while i < x: |
| 164 |
i = i + 1 |
| 165 |
return i |
| 166 |
|
| 167 |
def greet(target=str): |
| 168 |
"""String test""" |
| 169 |
hello = "hello" |
| 170 |
return hello + target |
| 171 |
|
| 172 |
def choose_last(): |
| 173 |
"""For loop test""" |
| 174 |
set = ["foo", "bar", "spam", "egg", "python"] |
| 175 |
choice = "" |
| 176 |
for choice in set: |
| 177 |
pass |
| 178 |
return choice |
| 179 |
|
| 180 |
def poly_branch(x=int): |
| 181 |
if x: |
| 182 |
y = [1,2,3] |
| 183 |
else: |
| 184 |
y = ['a','b','c'] |
| 185 |
|
| 186 |
z = y |
| 187 |
return z*2 |
| 188 |
|
| 189 |
def s_and(x=anytype, y=anytype): |
| 190 |
if x and y: |
| 191 |
return 'yes' |
| 192 |
else: |
| 193 |
return 'no' |
| 194 |
|
| 195 |
def break_continue(x=numtype): |
| 196 |
result = [] |
| 197 |
i = 0 |
| 198 |
while 1: |
| 199 |
i = i + 1 |
| 200 |
try: |
| 201 |
if i&1: |
| 202 |
continue |
| 203 |
if i >= x: |
| 204 |
break |
| 205 |
finally: |
| 206 |
result.append(i) |
| 207 |
i = i + 1 |
| 208 |
return result |
| 209 |
|
| 210 |
def reverse_3(lst=seqtype): |
| 211 |
try: |
| 212 |
a, b, c = lst |
| 213 |
except: |
| 214 |
return 0, 0, 0 |
| 215 |
return c, b, a |
| 216 |
|
| 217 |
def finallys(lst=seqtype): |
| 218 |
x = 1 |
| 219 |
try: |
| 220 |
x = 2 |
| 221 |
try: |
| 222 |
x = 3 |
| 223 |
a, = lst |
| 224 |
x = 4 |
| 225 |
except KeyError: |
| 226 |
return 5 |
| 227 |
except ValueError: |
| 228 |
return 6 |
| 229 |
b, = lst |
| 230 |
x = 7 |
| 231 |
finally: |
| 232 |
x = 8 |
| 233 |
return x |
| 234 |
|
| 235 |
def finally2(o, k): |
| 236 |
try: |
| 237 |
o[k] += 1 |
| 238 |
finally: |
| 239 |
o[-1] = 'done' |
| 240 |
|
| 241 |
def bare_raise(o, ignore): |
| 242 |
try: |
| 243 |
return o[5] |
| 244 |
except: |
| 245 |
if not ignore: |
| 246 |
raise |
| 247 |
|
| 248 |
def factorial(n=int): |
| 249 |
if n <= 1: |
| 250 |
return 1 |
| 251 |
else: |
| 252 |
return n * factorial(n-1) |
| 253 |
|
| 254 |
def factorial2(n=int): |
| 255 |
if n > 1: |
| 256 |
return n * factorial2(n-1) |
| 257 |
else: |
| 258 |
return 1 |
| 259 |
|
| 260 |
def _append_five(lst): |
| 261 |
lst += [5] |
| 262 |
|
| 263 |
def call_five(): |
| 264 |
a = [] |
| 265 |
_append_five(a) |
| 266 |
return a |
| 267 |
|
| 268 |
def _append_six(lst): |
| 269 |
lst += [6] |
| 270 |
|
| 271 |
def call_five_six(): |
| 272 |
a = [] |
| 273 |
_append_five(a) |
| 274 |
_append_six(a) |
| 275 |
return a |
| 276 |
|
| 277 |
def call_unpack_56(): |
| 278 |
a = call_five_six() |
| 279 |
return len(a), a[0], a[1] |
| 280 |
|
| 281 |
def forty_two(): |
| 282 |
return 42 |
| 283 |
|
| 284 |
def never_called(): |
| 285 |
return "booo" |
| 286 |
|
| 287 |
def constant_result(): |
| 288 |
if forty_two(): |
| 289 |
return "yadda" |
| 290 |
else: |
| 291 |
return never_called() |
| 292 |
|
| 293 |
class CallablePrebuiltConstant(object): |
| 294 |
def __call__(self): |
| 295 |
return 42 |
| 296 |
|
| 297 |
callable_prebuilt_constant = CallablePrebuiltConstant() |
| 298 |
|
| 299 |
def call_cpbc(): |
| 300 |
return callable_prebuilt_constant() |
| 301 |
|
| 302 |
|
| 303 |
class E1(Exception): |
| 304 |
pass |
| 305 |
|
| 306 |
class E2(Exception): |
| 307 |
pass |
| 308 |
|
| 309 |
def raise_choose(n): |
| 310 |
if n == 1: |
| 311 |
raise E1 |
| 312 |
elif n == 2: |
| 313 |
raise E2 |
| 314 |
elif n == -1: |
| 315 |
raise Exception |
| 316 |
return 0 |
| 317 |
|
| 318 |
def try_raise_choose(n=int): |
| 319 |
try: |
| 320 |
raise_choose(n) |
| 321 |
except E1: |
| 322 |
return 1 |
| 323 |
except E2: |
| 324 |
return 2 |
| 325 |
except Exception: |
| 326 |
return -1 |
| 327 |
return 0 |
| 328 |
|
| 329 |
def do_try_raise_choose(): |
| 330 |
r = [] |
| 331 |
for n in [-1,0,1,2]: |
| 332 |
r.append(try_raise_choose(n)) |
| 333 |
return r |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
class C(object): pass |
| 338 |
|
| 339 |
def build_instance(): |
| 340 |
c = C() |
| 341 |
return c |
| 342 |
|
| 343 |
def set_attr(): |
| 344 |
c = C() |
| 345 |
c.a = 1 |
| 346 |
c.a = 2 |
| 347 |
return c.a |
| 348 |
|
| 349 |
def merge_setattr(x): |
| 350 |
if x: |
| 351 |
c = C() |
| 352 |
c.a = 1 |
| 353 |
else: |
| 354 |
c = C() |
| 355 |
return c.a |
| 356 |
|
| 357 |
class D(C): pass |
| 358 |
class E(C): pass |
| 359 |
|
| 360 |
def inheritance1(): |
| 361 |
d = D() |
| 362 |
d.stuff = () |
| 363 |
e = E() |
| 364 |
e.stuff = -12 |
| 365 |
e.stuff = 3 |
| 366 |
lst = [d, e] |
| 367 |
return d.stuff, e.stuff |
| 368 |
|
| 369 |
|
| 370 |
def inheritance2(): |
| 371 |
d = D() |
| 372 |
d.stuff = (-12, -12) |
| 373 |
e = E() |
| 374 |
e.stuff = (3, 12.3) |
| 375 |
return _getstuff(d), _getstuff(e) |
| 376 |
|
| 377 |
class F: |
| 378 |
pass |
| 379 |
class G(F): |
| 380 |
def m(self, x): |
| 381 |
return self.m2(x) |
| 382 |
def m2(self, x): |
| 383 |
return D(), x |
| 384 |
class H(F): |
| 385 |
def m(self, y): |
| 386 |
self.attr = 1 |
| 387 |
return E(), y |
| 388 |
|
| 389 |
def knownkeysdict(b=anytype): |
| 390 |
if b: |
| 391 |
d = {'a': 0} |
| 392 |
d['b'] = b |
| 393 |
d['c'] = 'world' |
| 394 |
else: |
| 395 |
d = {'b': -123} |
| 396 |
return d['b'] |
| 397 |
|
| 398 |
def generaldict(key=str, value=int, key2=str, value2=int): |
| 399 |
d = {key: value} |
| 400 |
d[key2] = value2 |
| 401 |
return d[key or key2] |
| 402 |
|
| 403 |
def prime(n=int): |
| 404 |
return len([i for i in range(1,n+1) if n%i==0]) == 2 |
| 405 |
|
| 406 |
class A0: |
| 407 |
pass |
| 408 |
class A1(A0): |
| 409 |
clsattr = 123 |
| 410 |
class A2(A1): |
| 411 |
clsattr = 456 |
| 412 |
class A3(A2): |
| 413 |
clsattr = 789 |
| 414 |
class A4(A3): |
| 415 |
pass |
| 416 |
class A5(A0): |
| 417 |
clsattr = 101112 |
| 418 |
|
| 419 |
def classattribute(flag=int): |
| 420 |
if flag == 1: |
| 421 |
x = A1() |
| 422 |
elif flag == 2: |
| 423 |
x = A2() |
| 424 |
elif flag == 3: |
| 425 |
x = A3() |
| 426 |
elif flag == 4: |
| 427 |
x = A4() |
| 428 |
else: |
| 429 |
x = A5() |
| 430 |
return x.clsattr |
| 431 |
|
| 432 |
|
| 433 |
class Z: |
| 434 |
def my_method(self): |
| 435 |
return self.my_attribute |
| 436 |
|
| 437 |
class WithInit: |
| 438 |
def __init__(self, n): |
| 439 |
self.a = n |
| 440 |
|
| 441 |
class WithMoreInit(WithInit): |
| 442 |
def __init__(self, n, m): |
| 443 |
WithInit.__init__(self, n) |
| 444 |
self.b = m |
| 445 |
|
| 446 |
def simple_method(v=anytype): |
| 447 |
z = Z() |
| 448 |
z.my_attribute = v |
| 449 |
return z.my_method() |
| 450 |
|
| 451 |
def with_init(v=int): |
| 452 |
z = WithInit(v) |
| 453 |
return z.a |
| 454 |
|
| 455 |
def with_more_init(v=int, w=bool): |
| 456 |
z = WithMoreInit(v, w) |
| 457 |
if z.b: |
| 458 |
return z.a |
| 459 |
else: |
| 460 |
return -z.a |
| 461 |
|
| 462 |
global_z = Z() |
| 463 |
global_z.my_attribute = 42 |
| 464 |
|
| 465 |
def global_instance(): |
| 466 |
return global_z.my_method() |
| 467 |
|
| 468 |
def call_Z_my_method(z): |
| 469 |
return z.my_method |
| 470 |
|
| 471 |
def somepbc_simplify(): |
| 472 |
z = Z() |
| 473 |
call_Z_my_method(global_z) |
| 474 |
call_Z_my_method(z) |
| 475 |
|
| 476 |
class ClassWithMethods: |
| 477 |
def cm(cls, x): |
| 478 |
return x |
| 479 |
cm = classmethod(cm) |
| 480 |
|
| 481 |
def sm(x): |
| 482 |
return x |
| 483 |
sm = staticmethod(sm) |
| 484 |
|
| 485 |
|
| 486 |
global_c = C() |
| 487 |
global_c.a = 1 |
| 488 |
|
| 489 |
def global_newstyle_instance(): |
| 490 |
return global_c |
| 491 |
|
| 492 |
global_rl = [] |
| 493 |
global_rl.append(global_rl) |
| 494 |
|
| 495 |
def global_recursive_list(): |
| 496 |
return global_rl |
| 497 |
|
| 498 |
class MI_A(object): |
| 499 |
a = 1 |
| 500 |
class MI_B(MI_A): |
| 501 |
b = 2 |
| 502 |
class MI_C(MI_A): |
| 503 |
c = 3 |
| 504 |
class MI_D(MI_B, MI_C): |
| 505 |
d = 4 |
| 506 |
|
| 507 |
def multiple_inheritance(): |
| 508 |
i = MI_D() |
| 509 |
return i.a + i.b + i.c + i.d |
| 510 |
|
| 511 |
class CBase(object): |
| 512 |
pass |
| 513 |
class CSub1(CBase): |
| 514 |
def m(self): |
| 515 |
self.x = 42 |
| 516 |
return self.x |
| 517 |
class CSub2(CBase): |
| 518 |
def m(self): |
| 519 |
self.x = 'world' |
| 520 |
return self.x |
| 521 |
|
| 522 |
def methodcall_is_precise(cond): |
| 523 |
if cond: |
| 524 |
x = CSub1() |
| 525 |
x.m() |
| 526 |
else: |
| 527 |
x = CSub2() |
| 528 |
x.m() |
| 529 |
return CSub1().m() |
| 530 |
|
| 531 |
|
| 532 |
def flow_type_info(i): |
| 533 |
if isinstance(i, int): |
| 534 |
a = i + 1 |
| 535 |
else: |
| 536 |
a = len(str(i)) |
| 537 |
return a |
| 538 |
|
| 539 |
def flow_usertype_info(ob): |
| 540 |
if isinstance(ob, WithInit): |
| 541 |
return ob |
| 542 |
else: |
| 543 |
return WithMoreInit(1, 2) |
| 544 |
|
| 545 |
def star_args0(*args): |
| 546 |
return args[0] / 2 |
| 547 |
|
| 548 |
def call_star_args0(z): |
| 549 |
return star_args0(z) |
| 550 |
|
| 551 |
def star_args1(a, *args): |
| 552 |
return a + args[0] / 2 |
| 553 |
|
| 554 |
def call_star_args1(z): |
| 555 |
return star_args1(z, 20) |
| 556 |
|
| 557 |
def star_args1def(a=4, *args): |
| 558 |
if args: |
| 559 |
return a + args[0] / 2 |
| 560 |
else: |
| 561 |
return a*3 |
| 562 |
|
| 563 |
def call_star_args1def(z): |
| 564 |
a = star_args1def(z, 22) |
| 565 |
b = star_args1def(5) |
| 566 |
c = star_args1def() |
| 567 |
return a+b+c |
| 568 |
|
| 569 |
def star_args(x, y, *args): |
| 570 |
return x + args[0] |
| 571 |
|
| 572 |
def call_star_args(z): |
| 573 |
return star_args(z, 5, 10, 15, 20) |
| 574 |
|
| 575 |
def call_star_args_multiple(z): |
| 576 |
a = star_args(z, 5, 10) |
| 577 |
b = star_args(z, 5, 10, 15) |
| 578 |
c = star_args(z, 5, 10, 15, 20) |
| 579 |
return a+b+c |
| 580 |
|
| 581 |
def default_args(x, y=2, z=3L): |
| 582 |
return x+y+z |
| 583 |
|
| 584 |
def call_default_args(u): |
| 585 |
return default_args(111, u) |
| 586 |
|
| 587 |
def default_and_star_args(x, y=2, z=3, *more): |
| 588 |
return x+y+z+len(more) |
| 589 |
|
| 590 |
def call_default_and_star_args(u): |
| 591 |
return (default_and_star_args(111, u), |
| 592 |
default_and_star_args(-1000, -2000, -3000, -4000, -5000)) |
| 593 |
|
| 594 |
def call_with_star(z): |
| 595 |
return default_args(-20, *z) |
| 596 |
|
| 597 |
def call_with_keyword(z): |
| 598 |
return default_args(-20, z=z) |
| 599 |
|
| 600 |
def call_very_complex(z, args, kwds): |
| 601 |
return default_args(-20, z=z, *args, **kwds) |
| 602 |
|
| 603 |
def powerset(setsize=int): |
| 604 |
"""Powerset |
| 605 |
|
| 606 |
This one is from a Philippine Pythonista Hangout, an modified |
| 607 |
version of Andy Sy's code. |
| 608 |
|
| 609 |
list.append is modified to list concatenation, and powerset |
| 610 |
is pre-allocated and stored, instead of printed. |
| 611 |
|
| 612 |
URL is: http://lists.free.net.ph/pipermail/python/2002-November/ |
| 613 |
""" |
| 614 |
set = range(setsize) |
| 615 |
maxcardinality = pow(2, setsize) |
| 616 |
bitmask = 0L |
| 617 |
powerset = [None] * maxcardinality |
| 618 |
ptr = 0 |
| 619 |
while bitmask < maxcardinality: |
| 620 |
bitpos = 1L |
| 621 |
index = 0 |
| 622 |
subset = [] |
| 623 |
while bitpos < maxcardinality: |
| 624 |
if bitpos & bitmask: |
| 625 |
subset = subset + [set[index]] |
| 626 |
index += 1 |
| 627 |
bitpos <<= 1 |
| 628 |
powerset[ptr] = subset |
| 629 |
ptr += 1 |
| 630 |
bitmask += 1 |
| 631 |
return powerset |
| 632 |
|
| 633 |
def harmonic(n): |
| 634 |
result = 0.0 |
| 635 |
for i in range(n, 0, -1): |
| 636 |
result += 1.0 / n |
| 637 |
return result |
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
def _somebug1(n=int): |
| 643 |
l = [] |
| 644 |
v = l.append |
| 645 |
while n: |
| 646 |
l[7] = 5 |
| 647 |
break |
| 648 |
return v |
| 649 |
|
| 650 |
def _getstuff(x): |
| 651 |
return x.stuff |
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
class BadInit(object): |
| 656 |
def update(self, k): |
| 657 |
self.k = 1 |
| 658 |
def __init__(self, v): |
| 659 |
return |
| 660 |
self.update(**{'k':v}) |
| 661 |
def read(self): |
| 662 |
return self.k |
| 663 |
|
| 664 |
global_bi = BadInit(1) |
| 665 |
|
| 666 |
def global_badinit(): |
| 667 |
return global_bi.read() |
| 668 |
|
| 669 |
def _attrs(): |
| 670 |
def b(): pass |
| 671 |
b.f = 4 |
| 672 |
b.g = 5 |
| 673 |
return b.f + b.g |
| 674 |
|
| 675 |
def _methodcall1(cond): |
| 676 |
if cond: |
| 677 |
x = G() |
| 678 |
else: |
| 679 |
x = H() |
| 680 |
return x.m(42) |
| 681 |
|
| 682 |
def func1(): |
| 683 |
pass |
| 684 |
|
| 685 |
def func2(): |
| 686 |
pass |
| 687 |
|
| 688 |
def mergefunctions(cond): |
| 689 |
if cond: |
| 690 |
x = func1 |
| 691 |
else: |
| 692 |
x = func2 |
| 693 |
return x |
| 694 |
|
| 695 |
def func_producing_exception(): |
| 696 |
raise ValueError("this might e.g. block the caller") |
| 697 |
|
| 698 |
def funccallsex(): |
| 699 |
return func_producing_exception() |
| 700 |
|
| 701 |
|
| 702 |
def func_arg_unpack(): |
| 703 |
a,b = 3, "hello" |
| 704 |
return a |
| 705 |
|
| 706 |
class APBC: |
| 707 |
def __init__(self): |
| 708 |
self.answer = 42 |
| 709 |
|
| 710 |
apbc = APBC() |
| 711 |
apbc.answer = 7 |
| 712 |
|
| 713 |
def preserve_pbc_attr_on_instance(cond): |
| 714 |
if cond: |
| 715 |
x = APBC() |
| 716 |
else: |
| 717 |
x = apbc |
| 718 |
return x.answer |
| 719 |
|
| 720 |
|
| 721 |
class APBCS(object): |
| 722 |
__slots__ = ['answer'] |
| 723 |
def __init__(self): |
| 724 |
self.answer = 42 |
| 725 |
|
| 726 |
apbcs = APBCS() |
| 727 |
apbcs.answer = 7 |
| 728 |
|
| 729 |
def preserve_pbc_attr_on_instance_with_slots(cond): |
| 730 |
if cond: |
| 731 |
x = APBCS() |
| 732 |
else: |
| 733 |
x = apbcs |
| 734 |
return x.answer |
| 735 |
|
| 736 |
|
| 737 |
def is_and_knowntype(x): |
| 738 |
if x is None: |
| 739 |
return x |
| 740 |
else: |
| 741 |
return None |
| 742 |
|
| 743 |
def isinstance_and_knowntype(x): |
| 744 |
if isinstance(x, APBC): |
| 745 |
return x |
| 746 |
else: |
| 747 |
return apbc |
| 748 |
|
| 749 |
def simple_slice(x): |
| 750 |
return x[:10] |
| 751 |
|
| 752 |
def simple_iter(x): |
| 753 |
return iter(x) |
| 754 |
|
| 755 |
def simple_zip(x,y): |
| 756 |
return zip(x,y) |
| 757 |
|
| 758 |
def dict_copy(d): |
| 759 |
return d.copy() |
| 760 |
|
| 761 |
def dict_update(x): |
| 762 |
d = {x:x} |
| 763 |
d.update({1:2}) |
| 764 |
return d |
| 765 |
|
| 766 |
def dict_keys(): |
| 767 |
d = {"a" : 1} |
| 768 |
return d.keys() |
| 769 |
|
| 770 |
def dict_keys2(): |
| 771 |
d = {"a" : 1} |
| 772 |
keys = d.keys() |
| 773 |
d["123"] = 12 |
| 774 |
return keys |
| 775 |
|
| 776 |
def dict_values(): |
| 777 |
d = {"a" : "a"} |
| 778 |
return d.values() |
| 779 |
|
| 780 |
def dict_values2(): |
| 781 |
d = {54312 : "a"} |
| 782 |
values = d.values() |
| 783 |
d[1] = "12" |
| 784 |
return values |
| 785 |
|
| 786 |
def dict_items(): |
| 787 |
d = {'a' : 1} |
| 788 |
return d.items() |
| 789 |
|
| 790 |
class Exc(Exception): |
| 791 |
pass |
| 792 |
|
| 793 |
def exception_deduction0(x): |
| 794 |
pass |
| 795 |
|
| 796 |
def exception_deduction(): |
| 797 |
try: |
| 798 |
exception_deduction0(2) |
| 799 |
except Exc as e: |
| 800 |
return e |
| 801 |
return Exc() |
| 802 |
|
| 803 |
|
| 804 |
def always_raising(x): |
| 805 |
raise ValueError |
| 806 |
|
| 807 |
def witness(x): |
| 808 |
pass |
| 809 |
|
| 810 |
def exception_deduction_with_raise1(x): |
| 811 |
try: |
| 812 |
exception_deduction0(2) |
| 813 |
if x: |
| 814 |
raise Exc() |
| 815 |
except Exc as e: |
| 816 |
witness(e) |
| 817 |
return e |
| 818 |
return Exc() |
| 819 |
|
| 820 |
def exception_deduction_with_raise2(x): |
| 821 |
try: |
| 822 |
exception_deduction0(2) |
| 823 |
if x: |
| 824 |
raise Exc |
| 825 |
except Exc as e: |
| 826 |
witness(e) |
| 827 |
return e |
| 828 |
return Exc() |
| 829 |
|
| 830 |
def exception_deduction_with_raise3(x): |
| 831 |
try: |
| 832 |
exception_deduction0(2) |
| 833 |
if x: |
| 834 |
raise Exc, Exc() |
| 835 |
except Exc as e: |
| 836 |
witness(e) |
| 837 |
return e |
| 838 |
return Exc() |
| 839 |
|
| 840 |
def slice_union(x): |
| 841 |
if x: |
| 842 |
return slice(1) |
| 843 |
else: |
| 844 |
return slice(0, 10, 2) |
| 845 |
|
| 846 |
def exception_deduction_we_are_dumb(): |
| 847 |
a = 1 |
| 848 |
try: |
| 849 |
exception_deduction0(2) |
| 850 |
except Exc as e: |
| 851 |
a += 1 |
| 852 |
return e |
| 853 |
return Exc() |
| 854 |
|
| 855 |
class Exc2(Exception): |
| 856 |
pass |
| 857 |
|
| 858 |
def nested_exception_deduction(): |
| 859 |
try: |
| 860 |
exception_deduction0(1) |
| 861 |
except Exc as e: |
| 862 |
try: |
| 863 |
exception_deduction0(2) |
| 864 |
except Exc2 as f: |
| 865 |
return (e, f) |
| 866 |
return (e, Exc2()) |
| 867 |
return (Exc(), Exc2()) |
| 868 |
|
| 869 |
class Exc3(Exception): |
| 870 |
def m(self): |
| 871 |
return 1 |
| 872 |
|
| 873 |
class Exc4(Exc3): |
| 874 |
def m(self): |
| 875 |
return 1 |
| 876 |
|
| 877 |
class Sp: |
| 878 |
def o(self): |
| 879 |
raise Exc3 |
| 880 |
|
| 881 |
class Mod: |
| 882 |
def __init__(self, s): |
| 883 |
self.s = s |
| 884 |
|
| 885 |
def p(self): |
| 886 |
s = self.s |
| 887 |
try: |
| 888 |
s.o() |
| 889 |
except Exc3 as e: |
| 890 |
return e.m() |
| 891 |
return 0 |
| 892 |
|
| 893 |
class Mod3: |
| 894 |
def __init__(self, s): |
| 895 |
self.s = s |
| 896 |
|
| 897 |
def p(self): |
| 898 |
s = self.s |
| 899 |
try: |
| 900 |
s.o() |
| 901 |
except Exc4 as e1: |
| 902 |
return e1.m() |
| 903 |
except Exc3 as e2: |
| 904 |
try: |
| 905 |
return e2.m() |
| 906 |
except Exc4 as e3: |
| 907 |
return e3.m() |
| 908 |
return 0 |
| 909 |
|
| 910 |
|
| 911 |
mod = Mod(Sp()) |
| 912 |
mod3 = Mod3(Sp()) |
| 913 |
|
| 914 |
def exc_deduction_our_exc_plus_others(): |
| 915 |
return mod.p() |
| 916 |
|
| 917 |
def exc_deduction_our_excs_plus_others(): |
| 918 |
return mod3.p() |
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
def call_two_funcs_but_one_can_only_raise(n): |
| 923 |
fn = [witness, always_raising][n] |
| 924 |
return fn(n) |
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
class Thing1: |
| 930 |
|
| 931 |
def __init__(self): |
| 932 |
self.thingness = 1 |
| 933 |
|
| 934 |
thing1 = Thing1() |
| 935 |
|
| 936 |
def one_thing1(): |
| 937 |
return thing1 |
| 938 |
|
| 939 |
|
| 940 |
class Thing2(long): |
| 941 |
def __new__(t, v): |
| 942 |
return long.__new__(t, v * 2) |
| 943 |
|
| 944 |
thing2 = Thing2(2) |
| 945 |
|
| 946 |
def one_thing2(): |
| 947 |
return thing2 |
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
class Stk: |
| 952 |
def __init__(self): |
| 953 |
self.itms = [] |
| 954 |
|
| 955 |
def push(self, v): |
| 956 |
self.itms.append(v) |
| 957 |
|
| 958 |
class EC: |
| 959 |
|
| 960 |
def __init__(self): |
| 961 |
self.stk = Stk() |
| 962 |
|
| 963 |
def enter(self, f): |
| 964 |
self.stk.push(f) |
| 965 |
|
| 966 |
def propagation_of_fresh_instances_through_attrs(x): |
| 967 |
e = EC() |
| 968 |
e.enter(x) |
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
class R: |
| 974 |
def __init__(self, n): |
| 975 |
if n > 0: |
| 976 |
self.r = R(n-1) |
| 977 |
else: |
| 978 |
self.r = None |
| 979 |
self.n = n |
| 980 |
if self.r: |
| 981 |
self.m = self.r.n |
| 982 |
else: |
| 983 |
self.m = -1 |
| 984 |
|
| 985 |
def make_r(n): |
| 986 |
return R(n) |
| 987 |
|
| 988 |
class B: |
| 989 |
pass |
| 990 |
|
| 991 |
class Even(B): |
| 992 |
def __init__(self, n): |
| 993 |
if n > 0: |
| 994 |
self.x = [Odd(n-1)] |
| 995 |
self.y = self.x[0].x |
| 996 |
else: |
| 997 |
self.x = [] |
| 998 |
self.y = [] |
| 999 |
|
| 1000 |
class Odd(B): |
| 1001 |
def __init__(self, n): |
| 1002 |
self.x = [Even(n-1)] |
| 1003 |
self.y = self.x[0].x |
| 1004 |
|
| 1005 |
def make_eo(n): |
| 1006 |
if n % 2 == 0: |
| 1007 |
return Even(n) |
| 1008 |
else: |
| 1009 |
return Odd(n) |
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
class Box: |
| 1016 |
pass |
| 1017 |
|
| 1018 |
class Box2: |
| 1019 |
pass |
| 1020 |
|
| 1021 |
class Box3(Box2): |
| 1022 |
pass |
| 1023 |
|
| 1024 |
def flow_rev_numbers(n): |
| 1025 |
bx3 = Box3() |
| 1026 |
bx3.x = 1 |
| 1027 |
bx = Box() |
| 1028 |
bx.bx3 = bx3 |
| 1029 |
if n > 0: |
| 1030 |
z = bx.bx3.x |
| 1031 |
if n > 0: |
| 1032 |
bx2 = Box2() |
| 1033 |
bx2.x = 3 |
| 1034 |
return z |
| 1035 |
raise Exception |
| 1036 |
|
| 1037 |
|
| 1038 |
from rpython.rlib.rarithmetic import ovfcheck |
| 1039 |
|
| 1040 |
def add_func(i=numtype): |
| 1041 |
try: |
| 1042 |
return ovfcheck(i + 1) |
| 1043 |
except OverflowError: |
| 1044 |
raise |
| 1045 |
|
| 1046 |
from sys import maxint |
| 1047 |
|
| 1048 |
def div_func(i=numtype): |
| 1049 |
try: |
| 1050 |
return ovfcheck((-maxint-1) // i) |
| 1051 |
except (OverflowError, ZeroDivisionError): |
| 1052 |
raise |
| 1053 |
|
| 1054 |
def mul_func(x=numtype, y=numtype): |
| 1055 |
try: |
| 1056 |
return ovfcheck(x * y) |
| 1057 |
except OverflowError: |
| 1058 |
raise |
| 1059 |
|
| 1060 |
def mod_func(i=numtype): |
| 1061 |
try: |
| 1062 |
return ovfcheck((-maxint-1) % i) |
| 1063 |
except OverflowError: |
| 1064 |
raise |
| 1065 |
except ZeroDivisionError: |
| 1066 |
raise |
| 1067 |
|
| 1068 |
def rshift_func(i=numtype): |
| 1069 |
try: |
| 1070 |
return (-maxint-1) >> i |
| 1071 |
except ValueError: |
| 1072 |
raise |
| 1073 |
|
| 1074 |
class hugelmugel(OverflowError): |
| 1075 |
pass |
| 1076 |
|
| 1077 |
def hugo(a, b, c):pass |
| 1078 |
|
| 1079 |
def lshift_func(i=numtype): |
| 1080 |
try: |
| 1081 |
hugo(2, 3, 5) |
| 1082 |
return ovfcheck((-maxint-1) << i) |
| 1083 |
except (hugelmugel, OverflowError, StandardError, ValueError): |
| 1084 |
raise |
| 1085 |
|
| 1086 |
def unary_func(i=numtype): |
| 1087 |
try: |
| 1088 |
return ovfcheck(-i), ovfcheck(abs(i-1)) |
| 1089 |
except: |
| 1090 |
raise |
| 1091 |
|
| 1092 |
|