import os, sys

def delete_file(name):
    try:
        import os
        os.remove(name)
    except:
        pass

def generate_file(ret):
    with open("reload_test_target.py", "w") as f:
        f.write("# skip-if: True\n")
        f.write("# warning this file is generated by %s\n" % (__file__))
        f.write("def foo(): return %s\n" % (ret))
    # delete the pyc because cpython uses a timestamp for detecting if the pyc needs an update and we modify the file too fast
    delete_file("reload_test_target.pyc")

os.chdir(os.path.dirname(os.path.realpath(__file__)))

generate_file(1)
import reload_test_target
print reload_test_target.foo()

generate_file(42)
reload(reload_test_target)
print reload_test_target.foo()

delete_file("reload_test_target.py")
delete_file("reload_test_target.pyc")
