|
47 | 47 | from .testutils import assert_run_python_script
|
48 | 48 |
|
49 | 49 |
|
| 50 | +_TEST_GLOBAL_VARIABLE = "default_value" |
| 51 | + |
| 52 | + |
50 | 53 | class RaiserOnPickle(object):
|
51 | 54 |
|
52 | 55 | def __init__(self, exc):
|
@@ -887,6 +890,39 @@ def f1():
|
887 | 890 | clone_func=clone_func)
|
888 | 891 | assert_run_python_script(textwrap.dedent(code))
|
889 | 892 |
|
| 893 | + def test_closure_interacting_with_a_global_variable(self): |
| 894 | + global _TEST_GLOBAL_VARIABLE |
| 895 | + orig_value = _TEST_GLOBAL_VARIABLE |
| 896 | + try: |
| 897 | + def f0(): |
| 898 | + global _TEST_GLOBAL_VARIABLE |
| 899 | + _TEST_GLOBAL_VARIABLE = "changed_by_f0" |
| 900 | + |
| 901 | + def f1(): |
| 902 | + return _TEST_GLOBAL_VARIABLE |
| 903 | + |
| 904 | + cloned_f0 = cloudpickle.loads(cloudpickle.dumps( |
| 905 | + f0, protocol=self.protocol)) |
| 906 | + cloned_f1 = cloudpickle.loads(cloudpickle.dumps( |
| 907 | + f1, protocol=self.protocol)) |
| 908 | + pickled_f1 = cloudpickle.dumps(f1, protocol=self.protocol) |
| 909 | + |
| 910 | + # Change the value of the global variable |
| 911 | + cloned_f0() |
| 912 | + |
| 913 | + # Ensure that the global variable is the same for another function |
| 914 | + result_f1 = cloned_f1() |
| 915 | + assert result_f1 == "changed_by_f0", result_f1 |
| 916 | + assert f1() == result_f1 |
| 917 | + |
| 918 | + # Ensure that unpickling the global variable does not change its |
| 919 | + # value |
| 920 | + result_pickled_f1 = cloudpickle.loads(pickled_f1)() |
| 921 | + assert result_pickled_f1 == "changed_by_f0", result_pickled_f1 |
| 922 | + finally: |
| 923 | + _TEST_GLOBAL_VARIABLE = orig_value |
| 924 | + |
| 925 | + |
890 | 926 | @pytest.mark.skipif(sys.version_info >= (3, 0),
|
891 | 927 | reason="hardcoded pickle bytes for 2.7")
|
892 | 928 | def test_function_pickle_compat_0_4_0(self):
|
|
0 commit comments