1.1 --- a/tests/nested_mixed_names.py Mon Nov 28 00:59:41 2016 +0100
1.2 +++ b/tests/nested_mixed_names.py Mon Nov 28 01:00:10 2016 +0100
1.3 @@ -2,22 +2,66 @@
1.4 def c(self):
1.5 return 1
1.6
1.7 -class D:
1.8 +class D(C):
1.9 def d(self):
1.10 return 3
1.11
1.12 a = 4
1.13 +l = []
1.14
1.15 def f(x):
1.16 - x.c()
1.17 +
1.18 + # Test global mutation.
1.19 +
1.20 + l.append(x.c())
1.21 +
1.22 + # Test function initialisation.
1.23 +
1.24 def g(y, x=x): # x must be introduced as default here
1.25 if y:
1.26 x = D()
1.27 return x.d(), y, a # UnboundLocalError in Python (if y is a false value)
1.28 +
1.29 return g
1.30
1.31 -result = f(C())(2)
1.32 -print result[0]
1.33 -print result[1]
1.34 -print result[2]
1.35 -assert result == (3, 2, 4)
1.36 +# Provide a default instance for a function to be obtained.
1.37 +
1.38 +fn = f(C())
1.39 +print l # [1]
1.40 +print fn # __main__.f.$l0
1.41 +try:
1.42 + print fn(2) # should fail due to g requiring an object providing d
1.43 +except TypeError:
1.44 + print "fn(2): fn initialised with an inappropriate object"
1.45 +
1.46 +try:
1.47 + print fn(0) # should fail due to g requiring an object providing d
1.48 +except TypeError:
1.49 + print "fn(0): fn initialised with an inappropriate object"
1.50 +
1.51 +# Override the default when calling the function.
1.52 +
1.53 +print fn(2, D()) # (3, 2, 4)
1.54 +print fn(0, D()) # (3, 0, 4)
1.55 +
1.56 +# Provide a more suitable default instance for the function.
1.57 +
1.58 +fn = f(D())
1.59 +print l # [1, 1]
1.60 +print fn(2) # (3, 2, 4)
1.61 +print fn(0) # (3, 0, 4)
1.62 +print fn(0, D()) # (3, 0, 4)
1.63 +
1.64 +# Override with an unsuitable object even though it would be ignored.
1.65 +
1.66 +try:
1.67 + print fn(1, C())
1.68 +except TypeError:
1.69 + print "fn(1, C()): an unsuitable argument was given."
1.70 +
1.71 +# Override with an unsuitable object.
1.72 +
1.73 +try:
1.74 + print fn(0, C())
1.75 +except TypeError:
1.76 + print "fn(0, C()): an unsuitable argument was given."