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