#     Copyright 2015, Kay Hayen, mailto:kay.hayen@gmail.com
#
#     Python tests originally created or extracted from other peoples work. The
#     parts were too small to be protected.
#
#     Licensed under the Apache License, Version 2.0 (the "License");
#     you may not use this file except in compliance with the License.
#     You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
#     Unless required by applicable law or agreed to in writing, software
#     distributed under the License is distributed on an "AS IS" BASIS,
#     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#     See the License for the specific language governing permissions and
#     limitations under the License.
#

def closureTest1():
    # Assign, but the value is not supposed to be used by the function, instead the later
    # update is effective.
    d = 1

    def subby():
        return d

    d = 22222*2222

    return subby()


def closureTest2():
    # Using a closure variable that is not initialized at the time it is closured should
    # work as well.

    def subby():
        return d

    d = 2222*2222

    return subby()

def closureTest3():
    def subby():
        return undefined_global  # @UndefinedVariable

    try:
        return subby()
    except NameError:
        return 88

d = 1

def scopeTest4():
    try:
        return d

        d = 1
    except UnboundLocalError, e:
        return repr(e)


print "Test closure where value is overwritten:", closureTest1()
print "Test closure where value is assigned only late:", closureTest2()

print "Test function where closured value is never assigned:", closureTest3()

print "Scope test where UnboundLocalError is expected:", scopeTest4()


def function():
    pass

class ClosureLocalizerClass:
    print "Function before assigned in a class", function

    function = 1

    print "Function after it was assigned in class", function

ClosureLocalizerClass()

def ClosureLocalizerFunction():
    try:
        function = function

        print "Function didn't give unbound local error"
    except UnboundLocalError, e:
        print "Function gave unbound local error when accessing function before assignment.", repr(e)

ClosureLocalizerFunction()

class X:
    def __init__(self, x):
        self.x = x

def changingClosure():
    a = 1

    def closureTaker():
        return X(a)

    x = closureTaker()
    a=2
    print x.x
    x = closureTaker()
    print x.x

changingClosure()

@@ -761,16 +761,54 @@
 cense.%0A#
+%0Afrom __future__ import print_function
 %0A%0Adef cl
@@ -1480,33 +1480,35 @@
 nboundLocalError
-,
+ as
  e:%0A        retu
@@ -1521,25 +1521,25 @@
 r(e)%0A%0A%0Aprint
- 
+(
 %22Test closur
@@ -1584,23 +1584,24 @@
 eTest1()
+)
 %0Aprint
- 
+(
 %22Test cl
@@ -1657,24 +1657,25 @@
 eTest2()
+)
 %0A%0Aprint
- 
+(
 %22Test fu
@@ -1741,16 +1741,17 @@
 t3()
+)
 %0A%0Aprint
- 
+(
 %22Sco
@@ -1809,16 +1809,17 @@
 eTest4()
+)
 %0A%0A%0Adef f
@@ -1869,33 +1869,33 @@
 Class:%0A    print
- 
+(
 %22Function before
@@ -1910,24 +1910,25 @@
 d in a class
+:
 %22, function%0A
@@ -1922,24 +1922,25 @@
 :%22, function
+)
 %0A%0A    functi
@@ -1948,33 +1948,33 @@
 n = 1%0A%0A    print
- 
+(
 %22Function after 
@@ -1997,16 +1997,17 @@
 in class
+:
 %22, funct
@@ -2009,16 +2009,17 @@
 function
+)
 %0A%0AClosur
@@ -2112,33 +2112,33 @@
 n%0A%0A        print
- 
+(
 %22Function didn't
@@ -2163,16 +2163,17 @@
 l error%22
+)
 %0A    exc
@@ -2193,17 +2193,19 @@
 calError
-,
+ as
  e:%0A    
@@ -2213,17 +2213,17 @@
    print
- 
+(
 %22Functio
@@ -2290,17 +2290,17 @@
 signment
-.
+:
 %22, repr(
@@ -2301,16 +2301,17 @@
  repr(e)
+)
 %0A%0AClosur
@@ -2407,24 +2407,89 @@
 gClosure():%0A
+    print(%22Changing a closure taken value after it was taken.%22)%0A%0A
     a = 1%0A%0A 
@@ -2572,20 +2572,50 @@
    print
+(%22Closure value first time:%22,
  x.x
+)
 %0A    x =
@@ -2639,20 +2639,51 @@
    print
+(%22Closure value second time:%22,
  x.x
+)
 %0A%0Achangi

Make another basic test directly execuable with Python.