t443.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. '''
  2. Adapted from http://hg.python.org/cpython/file/936621d33c38/Lib/test/test_scope.py
  3. '''
  4. # testSimpleNesting
  5. print "\ntestSimpleNesting"
  6. def make_adder(x):
  7. def adder(y):
  8. return x + y
  9. return adder
  10. inc = make_adder(1)
  11. plus10 = make_adder(10)
  12. print inc(1), 2, inc(1)==2
  13. print inc(-4), -3, inc(-4)==-3
  14. print plus10(8), 18, plus10(8)==18
  15. print plus10(-2), 8, plus10(-2)==8
  16. # testSimpleAndRebinding
  17. print "\ntestSimpleAndRebinding"
  18. def make_adder3(x):
  19. def adder(y):
  20. return x + y
  21. x = x+1 # check tracking of assignment to x in defining scope
  22. return adder
  23. inc = make_adder3(0)
  24. plus10 = make_adder3(9)
  25. print inc(1), 2, inc(1)==2
  26. print inc(-4), -3, inc(-4)==-3
  27. print plus10(8), 18, plus10(8)==18
  28. print plus10(-2), 8, plus10(-2)==8
  29. # testNestingGlobalNoFree
  30. print "\ntestNestingGlobalNoFree"
  31. def make_adder4(): #XXX add extra level of indrection
  32. def nest():
  33. def nest():
  34. def adder(y):
  35. return global_x + y #check that globals work
  36. return adder
  37. return nest()
  38. return nest()
  39. global_x = 1
  40. adder = make_adder4()
  41. x = adder(1)
  42. print x, 2, x == 2
  43. global_x = 10
  44. x = adder(-2)
  45. print x, 8, x == 8
  46. # testNestingPlusFreeRefToGlobal
  47. print "\ntestNestingPlusFreeRefToGlobal"
  48. def make_adder6(x):
  49. global global_nest_x
  50. def adder(y):
  51. return global_nest_x + y
  52. global_nest_x = x
  53. return adder
  54. inc = make_adder6(1)
  55. print inc(1), 2, inc(1)==2
  56. print inc(-4), -3, inc(-4)==-3
  57. plus10 = make_adder6(10)
  58. print plus10(8), 18, plus10(8)==18
  59. print plus10(-2), 8, plus10(-2)==8
  60. # testNearestEnclosingScope
  61. print "\ntestNearestEnclosingScope"
  62. def f(x):
  63. def g(y):
  64. x = 42 # check that this masks binding in f()
  65. def h(z):
  66. return x + z
  67. return h
  68. return g(2)
  69. test_func = f(10)
  70. print test_func(5), 47, test_func(5)==47
  71. # testMixedFreevarsAndCellvars
  72. print "\ntestMixedFreevarsAndCellvars"
  73. def identity(x):
  74. return x
  75. def f(x,y,z):
  76. def g(a,b,c):
  77. a = a + x # 3
  78. def h():
  79. #z * (4+9)
  80. #3 * 13
  81. return identity(z*(b+y))
  82. y = c + z #9
  83. return h
  84. return g
  85. g = f(1,2,3)
  86. h = g(2,4,6)
  87. print h(), 39, h() == 39
  88. #testFreeVarInMethod
  89. print "\ntestFreeVarInMethod"
  90. method_and_var = "var"
  91. class Test:
  92. # this class is not nested, so the rules are different
  93. def method_and_var(self):
  94. return "method"
  95. def test(self):
  96. return method_and_var
  97. def actual_global(self):
  98. return str("global")
  99. def str(self):
  100. return str(self)
  101. t = Test()
  102. print t.test(), "var", t.test() == "var"
  103. print t.method_and_var(), "method", t.method_and_var() == "method"
  104. print t.actual_global(), "global", t.actual_global() == "global"
  105. # testRecursion
  106. print "\ntestRecursion"
  107. def f(x):
  108. def fact(n):
  109. if n == 0:
  110. return 1
  111. else:
  112. return n * fact(n-1)
  113. if x>=0:
  114. return fact(x)
  115. else:
  116. raise ValueError, "x must be >=0"
  117. print f(6), 720, f(6)==720
  118. # testLambdas
  119. print "\ntestLambdas"
  120. f1 = lambda x: lambda y: x + y
  121. inc = f1(1)
  122. plus10 = f1(10)
  123. print inc(1), 2, inc(1)==2
  124. print inc(-4), -3, inc(-4)==-3
  125. print plus10(8), 18, plus10(8)==18
  126. print plus10(-2), 8, plus10(-2)==8
  127. f3 = lambda x: lambda y: global_x + y
  128. global_x = 1
  129. inc = f3(None)
  130. print inc(2), 3, inc(2) == 3