|
2 | 2 | import toolz |
3 | 3 | import toolz.curried.exceptions |
4 | 4 | import pickle |
| 5 | +from toolz.compatibility import PY3 |
5 | 6 |
|
6 | 7 |
|
7 | 8 | def test_compose(): |
@@ -62,3 +63,117 @@ def test_curried_exceptions(): |
62 | 63 | # This tests a global curried object that isn't defined in toolz.functoolz |
63 | 64 | merge = pickle.loads(pickle.dumps(toolz.curried.exceptions.merge)) |
64 | 65 | assert merge is toolz.curried.exceptions.merge |
| 66 | + |
| 67 | + |
| 68 | +@toolz.curry |
| 69 | +class GlobalCurried(object): |
| 70 | + def __init__(self, x, y): |
| 71 | + self.x = x |
| 72 | + self.y = y |
| 73 | + |
| 74 | + @toolz.curry |
| 75 | + def f1(self, a, b): |
| 76 | + return self.x + self.y + a + b |
| 77 | + |
| 78 | + def g1(self): |
| 79 | + pass |
| 80 | + |
| 81 | + def __reduce__(self): |
| 82 | + """Allow us to serialize instances of GlobalCurried""" |
| 83 | + return (GlobalCurried, (self.x, self.y)) |
| 84 | + |
| 85 | + @toolz.curry |
| 86 | + class NestedCurried(object): |
| 87 | + def __init__(self, x, y): |
| 88 | + self.x = x |
| 89 | + self.y = y |
| 90 | + |
| 91 | + @toolz.curry |
| 92 | + def f2(self, a, b): |
| 93 | + return self.x + self.y + a + b |
| 94 | + |
| 95 | + def g2(self): |
| 96 | + pass |
| 97 | + |
| 98 | + def __reduce__(self): |
| 99 | + """Allow us to serialize instances of NestedCurried""" |
| 100 | + return (GlobalCurried.NestedCurried, (self.x, self.y)) |
| 101 | + |
| 102 | + class Nested(object): |
| 103 | + def __init__(self, x, y): |
| 104 | + self.x = x |
| 105 | + self.y = y |
| 106 | + |
| 107 | + @toolz.curry |
| 108 | + def f3(self, a, b): |
| 109 | + return self.x + self.y + a + b |
| 110 | + |
| 111 | + def g3(self): |
| 112 | + pass |
| 113 | + |
| 114 | + |
| 115 | +def test_curried_qualname(): |
| 116 | + if not PY3: |
| 117 | + return |
| 118 | + def preserves_identity(obj): |
| 119 | + return pickle.loads(pickle.dumps(obj)) is obj |
| 120 | + |
| 121 | + assert preserves_identity(GlobalCurried) |
| 122 | + assert preserves_identity(GlobalCurried.func.f1) |
| 123 | + assert preserves_identity(GlobalCurried.func.g1) |
| 124 | + assert preserves_identity(GlobalCurried.func.NestedCurried) |
| 125 | + assert preserves_identity(GlobalCurried.func.NestedCurried.func.f2) |
| 126 | + assert preserves_identity(GlobalCurried.func.NestedCurried.func.g2) |
| 127 | + assert preserves_identity(GlobalCurried.func.Nested) |
| 128 | + assert preserves_identity(GlobalCurried.func.Nested.f3) |
| 129 | + assert preserves_identity(GlobalCurried.func.Nested.g3) |
| 130 | + |
| 131 | + # Rely on curry.__getattr__ |
| 132 | + assert preserves_identity(GlobalCurried.f1) |
| 133 | + assert preserves_identity(GlobalCurried.g1) |
| 134 | + assert preserves_identity(GlobalCurried.NestedCurried) |
| 135 | + assert preserves_identity(GlobalCurried.NestedCurried.f2) |
| 136 | + assert preserves_identity(GlobalCurried.NestedCurried.g2) |
| 137 | + assert preserves_identity(GlobalCurried.Nested) |
| 138 | + assert preserves_identity(GlobalCurried.Nested.f3) |
| 139 | + assert preserves_identity(GlobalCurried.Nested.g3) |
| 140 | + |
| 141 | + global_curried1 = GlobalCurried(1) |
| 142 | + global_curried2 = pickle.loads(pickle.dumps(global_curried1)) |
| 143 | + assert global_curried1 is not global_curried2 |
| 144 | + assert global_curried1(2).f1(3, 4) == global_curried2(2).f1(3, 4) == 10 |
| 145 | + |
| 146 | + global_curried3 = global_curried1(2) |
| 147 | + global_curried4 = pickle.loads(pickle.dumps(global_curried3)) |
| 148 | + assert global_curried3 is not global_curried4 |
| 149 | + assert global_curried3.f1(3, 4) == global_curried4.f1(3, 4) == 10 |
| 150 | + |
| 151 | + func1 = global_curried1(2).f1(3) |
| 152 | + func2 = pickle.loads(pickle.dumps(func1)) |
| 153 | + assert func1 is not func2 |
| 154 | + assert func1(4) == func2(4) == 10 |
| 155 | + |
| 156 | + nested_curried1 = GlobalCurried.NestedCurried(1) |
| 157 | + nested_curried2 = pickle.loads(pickle.dumps(nested_curried1)) |
| 158 | + assert nested_curried1 is not nested_curried2 |
| 159 | + assert nested_curried1(2).f2(3, 4) == nested_curried2(2).f2(3, 4) == 10 |
| 160 | + |
| 161 | + nested_curried3 = nested_curried1(2) |
| 162 | + nested_curried4 = pickle.loads(pickle.dumps(nested_curried3)) |
| 163 | + assert nested_curried3 is not nested_curried4 |
| 164 | + assert nested_curried3.f2(3, 4) == nested_curried4.f2(3, 4) == 10 |
| 165 | + |
| 166 | + func1 = nested_curried1(2).f2(3) |
| 167 | + func2 = pickle.loads(pickle.dumps(func1)) |
| 168 | + assert func1 is not func2 |
| 169 | + assert func1(4) == func2(4) == 10 |
| 170 | + |
| 171 | + nested3 = GlobalCurried.Nested(1, 2) |
| 172 | + nested4 = pickle.loads(pickle.dumps(nested3)) |
| 173 | + assert nested3 is not nested4 |
| 174 | + assert nested3.f3(3, 4) == nested4.f3(3, 4) == 10 |
| 175 | + |
| 176 | + func1 = nested3.f3(3) |
| 177 | + func2 = pickle.loads(pickle.dumps(func1)) |
| 178 | + assert func1 is not func2 |
| 179 | + assert func1(4) == func2(4) == 10 |
0 commit comments