|
1
|
|
|
from __future__ import print_function |
|
2
|
|
|
|
|
3
|
|
|
import gc |
|
4
|
|
|
import imp |
|
5
|
|
|
import pickle |
|
6
|
|
|
import platform |
|
7
|
|
|
import sys |
|
8
|
|
|
import weakref |
|
9
|
|
|
from datetime import date |
|
10
|
|
|
from datetime import datetime |
|
11
|
|
|
from decimal import Decimal |
|
12
|
|
|
from functools import partial |
|
13
|
|
|
|
|
14
|
|
|
import pytest |
|
15
|
|
|
|
|
16
|
|
|
from compat import PY2 |
|
17
|
|
|
from compat import PY3 |
|
18
|
|
|
from compat import exec_ |
|
19
|
|
|
|
|
20
|
|
|
PYPY = '__pypy__' in sys.builtin_module_names |
|
21
|
|
|
|
|
22
|
|
|
OBJECTS_CODE = """ |
|
23
|
|
|
class TargetBaseClass(object): |
|
24
|
|
|
"documentation" |
|
25
|
|
|
|
|
26
|
|
|
class Target(TargetBaseClass): |
|
27
|
|
|
"documentation" |
|
28
|
|
|
|
|
29
|
|
|
def target(): |
|
30
|
|
|
"documentation" |
|
31
|
|
|
pass |
|
32
|
|
|
""" |
|
33
|
|
|
|
|
34
|
|
|
objects = imp.new_module('objects') |
|
35
|
|
|
exec_(OBJECTS_CODE, objects.__dict__, objects.__dict__) |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
def load_implementation(name): |
|
39
|
|
|
class FakeModule: |
|
40
|
|
|
subclass = False |
|
41
|
|
|
kind = name |
|
42
|
|
|
if name == "slots": |
|
43
|
|
|
from lazy_object_proxy.slots import Proxy |
|
44
|
|
|
elif name == "simple": |
|
45
|
|
|
from lazy_object_proxy.simple import Proxy |
|
46
|
|
|
elif name == "cext": |
|
47
|
|
|
try: |
|
48
|
|
|
from lazy_object_proxy.cext import Proxy |
|
49
|
|
|
except ImportError: |
|
50
|
|
|
if PYPY: |
|
51
|
|
|
pytest.skip(msg="C Extension not available.") |
|
52
|
|
|
else: |
|
53
|
|
|
raise |
|
54
|
|
|
elif name == "objproxies": |
|
55
|
|
|
Proxy = pytest.importorskip("objproxies").LazyProxy |
|
56
|
|
|
elif name == "django": |
|
57
|
|
|
Proxy = pytest.importorskip("django.utils.functional").SimpleLazyObject |
|
58
|
|
|
else: |
|
59
|
|
|
raise RuntimeError("Unsupported param: %r." % name) |
|
60
|
|
|
|
|
61
|
|
|
Proxy |
|
62
|
|
|
|
|
63
|
|
|
return FakeModule |
|
64
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
@pytest.fixture(scope="module", params=[ |
|
67
|
|
|
"slots", "cext", |
|
68
|
|
|
"simple", |
|
69
|
|
|
# "external-django", "external-objproxies" |
|
70
|
|
|
]) |
|
71
|
|
|
def lop_implementation(request): |
|
72
|
|
|
return load_implementation(request.param) |
|
73
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
@pytest.fixture(scope="module", params=[True, False], ids=['subclassed', 'normal']) |
|
76
|
|
|
def lop_subclass(request, lop_implementation): |
|
77
|
|
|
if request.param: |
|
78
|
|
|
class submod(lop_implementation): |
|
79
|
|
|
subclass = True |
|
80
|
|
|
Proxy = type("SubclassOf_" + lop_implementation.Proxy.__name__, |
|
81
|
|
|
(lop_implementation.Proxy,), {}) |
|
82
|
|
|
|
|
83
|
|
|
return submod |
|
84
|
|
|
else: |
|
85
|
|
|
return lop_implementation |
|
86
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
@pytest.fixture(scope="function") |
|
89
|
|
|
def lazy_object_proxy(request, lop_subclass): |
|
90
|
|
|
if request.node.get_marker('xfail_subclass'): |
|
91
|
|
|
request.applymarker(pytest.mark.xfail( |
|
92
|
|
|
reason="This test can't work because subclassing disables certain " |
|
93
|
|
|
"features like __doc__ and __module__ proxying." |
|
94
|
|
|
)) |
|
95
|
|
|
if request.node.get_marker('xfail_simple'): |
|
96
|
|
|
request.applymarker(pytest.mark.xfail( |
|
97
|
|
|
reason="The lazy_object_proxy.simple.Proxy has some limitations." |
|
98
|
|
|
)) |
|
99
|
|
|
|
|
100
|
|
|
return lop_subclass |
|
101
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
def test_round(lazy_object_proxy): |
|
104
|
|
|
proxy = lazy_object_proxy.Proxy(lambda: 1.2) |
|
105
|
|
|
assert round(proxy) == 1 |
|
106
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
def test_attributes(lazy_object_proxy): |
|
109
|
|
|
def function1(*args, **kwargs): |
|
110
|
|
|
return args, kwargs |
|
111
|
|
|
|
|
112
|
|
|
function2 = lazy_object_proxy.Proxy(lambda: function1) |
|
113
|
|
|
|
|
114
|
|
|
assert function2.__wrapped__ == function1 |
|
115
|
|
|
|
|
116
|
|
|
|
|
117
|
|
|
def test_get_wrapped(lazy_object_proxy): |
|
118
|
|
|
def function1(*args, **kwargs): |
|
119
|
|
|
return args, kwargs |
|
120
|
|
|
|
|
121
|
|
|
function2 = lazy_object_proxy.Proxy(lambda: function1) |
|
122
|
|
|
|
|
123
|
|
|
assert function2.__wrapped__ == function1 |
|
124
|
|
|
|
|
125
|
|
|
function3 = lazy_object_proxy.Proxy(lambda: function2) |
|
126
|
|
|
|
|
127
|
|
|
assert function3.__wrapped__ == function1 |
|
128
|
|
|
|
|
129
|
|
|
|
|
130
|
|
|
def test_set_wrapped(lazy_object_proxy): |
|
131
|
|
|
def function1(*args, **kwargs): |
|
132
|
|
|
return args, kwargs |
|
133
|
|
|
|
|
134
|
|
|
function2 = lazy_object_proxy.Proxy(lambda: function1) |
|
135
|
|
|
|
|
136
|
|
|
assert function2 == function1 |
|
137
|
|
|
assert function2.__wrapped__ is function1 |
|
138
|
|
|
assert function2.__name__ == function1.__name__ |
|
139
|
|
|
|
|
140
|
|
|
if PY3: |
|
141
|
|
|
assert function2.__qualname__ == function1.__qualname__ |
|
142
|
|
|
|
|
143
|
|
|
function2.__wrapped__ = None |
|
144
|
|
|
|
|
145
|
|
|
assert not hasattr(function1, '__wrapped__') |
|
146
|
|
|
|
|
147
|
|
|
assert function2 == None |
|
148
|
|
|
assert function2.__wrapped__ is None |
|
149
|
|
|
assert not hasattr(function2, '__name__') |
|
150
|
|
|
|
|
151
|
|
|
if PY3: |
|
152
|
|
|
assert not hasattr(function2, '__qualname__') |
|
153
|
|
|
|
|
154
|
|
|
def function3(*args, **kwargs): |
|
155
|
|
|
return args, kwargs |
|
156
|
|
|
|
|
157
|
|
|
function2.__wrapped__ = function3 |
|
158
|
|
|
|
|
159
|
|
|
assert function2 == function3 |
|
160
|
|
|
assert function2.__wrapped__ == function3 |
|
161
|
|
|
assert function2.__name__ == function3.__name__ |
|
162
|
|
|
|
|
163
|
|
|
if PY3: |
|
164
|
|
|
assert function2.__qualname__ == function3.__qualname__ |
|
165
|
|
|
|
|
166
|
|
|
|
|
167
|
|
|
def test_wrapped_attribute(lazy_object_proxy): |
|
168
|
|
|
def function1(*args, **kwargs): |
|
169
|
|
|
return args, kwargs |
|
170
|
|
|
|
|
171
|
|
|
function2 = lazy_object_proxy.Proxy(lambda: function1) |
|
172
|
|
|
|
|
173
|
|
|
function2.variable = True |
|
174
|
|
|
|
|
175
|
|
|
assert hasattr(function1, 'variable') |
|
176
|
|
|
assert hasattr(function2, 'variable') |
|
177
|
|
|
|
|
178
|
|
|
assert function2.variable == True |
|
179
|
|
|
|
|
180
|
|
|
del function2.variable |
|
181
|
|
|
|
|
182
|
|
|
assert not hasattr(function1, 'variable') |
|
183
|
|
|
assert not hasattr(function2, 'variable') |
|
184
|
|
|
|
|
185
|
|
|
assert getattr(function2, 'variable', None) == None |
|
186
|
|
|
|
|
187
|
|
|
|
|
188
|
|
|
def test_class_object_name(lazy_object_proxy): |
|
189
|
|
|
# Test preservation of class __name__ attribute. |
|
190
|
|
|
|
|
191
|
|
|
target = objects.Target |
|
192
|
|
|
wrapper = lazy_object_proxy.Proxy(lambda: target) |
|
193
|
|
|
|
|
194
|
|
|
assert wrapper.__name__ == target.__name__ |
|
195
|
|
|
|
|
196
|
|
|
|
|
197
|
|
|
def test_class_object_qualname(lazy_object_proxy): |
|
198
|
|
|
# Test preservation of class __qualname__ attribute. |
|
199
|
|
|
|
|
200
|
|
|
target = objects.Target |
|
201
|
|
|
wrapper = lazy_object_proxy.Proxy(lambda: target) |
|
202
|
|
|
|
|
203
|
|
|
try: |
|
204
|
|
|
__qualname__ = target.__qualname__ |
|
205
|
|
|
except AttributeError: |
|
206
|
|
|
pass |
|
207
|
|
|
else: |
|
208
|
|
|
assert wrapper.__qualname__ == __qualname__ |
|
209
|
|
|
|
|
210
|
|
|
|
|
211
|
|
|
@pytest.mark.xfail_subclass |
|
212
|
|
|
def test_class_module_name(lazy_object_proxy): |
|
213
|
|
|
# Test preservation of class __module__ attribute. |
|
214
|
|
|
|
|
215
|
|
|
target = objects.Target |
|
216
|
|
|
wrapper = lazy_object_proxy.Proxy(lambda: target) |
|
217
|
|
|
|
|
218
|
|
|
assert wrapper.__module__ == target.__module__ |
|
219
|
|
|
|
|
220
|
|
|
|
|
221
|
|
|
@pytest.mark.xfail_subclass |
|
222
|
|
|
def test_class_doc_string(lazy_object_proxy): |
|
223
|
|
|
# Test preservation of class __doc__ attribute. |
|
224
|
|
|
|
|
225
|
|
|
target = objects.Target |
|
226
|
|
|
wrapper = lazy_object_proxy.Proxy(lambda: target) |
|
227
|
|
|
|
|
228
|
|
|
assert wrapper.__doc__ == target.__doc__ |
|
229
|
|
|
|
|
230
|
|
|
|
|
231
|
|
|
@pytest.mark.xfail_subclass |
|
232
|
|
|
def test_instance_module_name(lazy_object_proxy): |
|
233
|
|
|
# Test preservation of instance __module__ attribute. |
|
234
|
|
|
|
|
235
|
|
|
target = objects.Target() |
|
236
|
|
|
wrapper = lazy_object_proxy.Proxy(lambda: target) |
|
237
|
|
|
|
|
238
|
|
|
assert wrapper.__module__ == target.__module__ |
|
239
|
|
|
|
|
240
|
|
|
|
|
241
|
|
|
@pytest.mark.xfail_subclass |
|
242
|
|
|
def test_instance_doc_string(lazy_object_proxy): |
|
243
|
|
|
# Test preservation of instance __doc__ attribute. |
|
244
|
|
|
|
|
245
|
|
|
target = objects.Target() |
|
246
|
|
|
wrapper = lazy_object_proxy.Proxy(lambda: target) |
|
247
|
|
|
|
|
248
|
|
|
assert wrapper.__doc__ == target.__doc__ |
|
249
|
|
|
|
|
250
|
|
|
|
|
251
|
|
|
def test_function_object_name(lazy_object_proxy): |
|
252
|
|
|
# Test preservation of function __name__ attribute. |
|
253
|
|
|
|
|
254
|
|
|
target = objects.target |
|
255
|
|
|
wrapper = lazy_object_proxy.Proxy(lambda: target) |
|
256
|
|
|
|
|
257
|
|
|
assert wrapper.__name__ == target.__name__ |
|
258
|
|
|
|
|
259
|
|
|
|
|
260
|
|
|
def test_function_object_qualname(lazy_object_proxy): |
|
261
|
|
|
# Test preservation of function __qualname__ attribute. |
|
262
|
|
|
|
|
263
|
|
|
target = objects.target |
|
264
|
|
|
wrapper = lazy_object_proxy.Proxy(lambda: target) |
|
265
|
|
|
|
|
266
|
|
|
try: |
|
267
|
|
|
__qualname__ = target.__qualname__ |
|
268
|
|
|
except AttributeError: |
|
269
|
|
|
pass |
|
270
|
|
|
else: |
|
271
|
|
|
assert wrapper.__qualname__ == __qualname__ |
|
272
|
|
|
|
|
273
|
|
|
|
|
274
|
|
|
@pytest.mark.xfail_subclass |
|
275
|
|
|
def test_function_module_name(lazy_object_proxy): |
|
276
|
|
|
# Test preservation of function __module__ attribute. |
|
277
|
|
|
|
|
278
|
|
|
target = objects.target |
|
279
|
|
|
wrapper = lazy_object_proxy.Proxy(lambda: target) |
|
280
|
|
|
|
|
281
|
|
|
assert wrapper.__module__ == target.__module__ |
|
282
|
|
|
|
|
283
|
|
|
|
|
284
|
|
|
@pytest.mark.xfail_subclass |
|
285
|
|
|
def test_function_doc_string(lazy_object_proxy): |
|
286
|
|
|
# Test preservation of function __doc__ attribute. |
|
287
|
|
|
|
|
288
|
|
|
target = objects.target |
|
289
|
|
|
wrapper = lazy_object_proxy.Proxy(lambda: target) |
|
290
|
|
|
|
|
291
|
|
|
assert wrapper.__doc__ == target.__doc__ |
|
292
|
|
|
|
|
293
|
|
|
|
|
294
|
|
|
def test_class_of_class(lazy_object_proxy): |
|
295
|
|
|
# Test preservation of class __class__ attribute. |
|
296
|
|
|
|
|
297
|
|
|
target = objects.Target |
|
298
|
|
|
wrapper = lazy_object_proxy.Proxy(lambda: target) |
|
299
|
|
|
|
|
300
|
|
|
assert wrapper.__class__ is target.__class__ |
|
301
|
|
|
|
|
302
|
|
|
assert isinstance(wrapper, type(target)) |
|
303
|
|
|
|
|
304
|
|
|
|
|
305
|
|
|
def test_revert_class_proxying(lazy_object_proxy): |
|
306
|
|
|
class ProxyWithOldStyleIsInstance(lazy_object_proxy.Proxy): |
|
307
|
|
|
__class__ = object.__dict__['__class__'] |
|
308
|
|
|
|
|
309
|
|
|
target = objects.Target() |
|
310
|
|
|
wrapper = ProxyWithOldStyleIsInstance(lambda: target) |
|
311
|
|
|
|
|
312
|
|
|
assert wrapper.__class__ is ProxyWithOldStyleIsInstance |
|
313
|
|
|
|
|
314
|
|
|
assert isinstance(wrapper, ProxyWithOldStyleIsInstance) |
|
315
|
|
|
assert not isinstance(wrapper, objects.Target) |
|
316
|
|
|
assert not isinstance(wrapper, objects.TargetBaseClass) |
|
317
|
|
|
|
|
318
|
|
|
class ProxyWithOldStyleIsInstance2(ProxyWithOldStyleIsInstance): |
|
319
|
|
|
pass |
|
320
|
|
|
|
|
321
|
|
|
wrapper = ProxyWithOldStyleIsInstance2(lambda: target) |
|
322
|
|
|
|
|
323
|
|
|
assert wrapper.__class__ is ProxyWithOldStyleIsInstance2 |
|
324
|
|
|
|
|
325
|
|
|
assert isinstance(wrapper, ProxyWithOldStyleIsInstance2) |
|
326
|
|
|
assert not isinstance(wrapper, objects.Target) |
|
327
|
|
|
assert not isinstance(wrapper, objects.TargetBaseClass) |
|
328
|
|
|
|
|
329
|
|
|
|
|
330
|
|
|
def test_class_of_instance(lazy_object_proxy): |
|
331
|
|
|
# Test preservation of instance __class__ attribute. |
|
332
|
|
|
|
|
333
|
|
|
target = objects.Target() |
|
334
|
|
|
wrapper = lazy_object_proxy.Proxy(lambda: target) |
|
335
|
|
|
|
|
336
|
|
|
assert wrapper.__class__ is target.__class__ |
|
337
|
|
|
|
|
338
|
|
|
assert isinstance(wrapper, objects.Target) |
|
339
|
|
|
assert isinstance(wrapper, objects.TargetBaseClass) |
|
340
|
|
|
|
|
341
|
|
|
|
|
342
|
|
|
def test_class_of_function(lazy_object_proxy): |
|
343
|
|
|
# Test preservation of function __class__ attribute. |
|
344
|
|
|
|
|
345
|
|
|
target = objects.target |
|
346
|
|
|
wrapper = lazy_object_proxy.Proxy(lambda: target) |
|
347
|
|
|
|
|
348
|
|
|
assert wrapper.__class__ is target.__class__ |
|
349
|
|
|
|
|
350
|
|
|
assert isinstance(wrapper, type(target)) |
|
351
|
|
|
|
|
352
|
|
|
|
|
353
|
|
|
def test_dir_of_class(lazy_object_proxy): |
|
354
|
|
|
# Test preservation of class __dir__ attribute. |
|
355
|
|
|
|
|
356
|
|
|
target = objects.Target |
|
357
|
|
|
wrapper = lazy_object_proxy.Proxy(lambda: target) |
|
358
|
|
|
|
|
359
|
|
|
assert dir(wrapper) == dir(target) |
|
360
|
|
|
|
|
361
|
|
|
|
|
362
|
|
|
@pytest.mark.xfail_simple |
|
363
|
|
|
def test_vars_of_class(lazy_object_proxy): |
|
364
|
|
|
# Test preservation of class __dir__ attribute. |
|
365
|
|
|
|
|
366
|
|
|
target = objects.Target |
|
367
|
|
|
wrapper = lazy_object_proxy.Proxy(lambda: target) |
|
368
|
|
|
|
|
369
|
|
|
assert vars(wrapper) == vars(target) |
|
370
|
|
|
|
|
371
|
|
|
|
|
372
|
|
|
def test_dir_of_instance(lazy_object_proxy): |
|
373
|
|
|
# Test preservation of instance __dir__ attribute. |
|
374
|
|
|
|
|
375
|
|
|
target = objects.Target() |
|
376
|
|
|
wrapper = lazy_object_proxy.Proxy(lambda: target) |
|
377
|
|
|
|
|
378
|
|
|
assert dir(wrapper) == dir(target) |
|
379
|
|
|
|
|
380
|
|
|
|
|
381
|
|
|
@pytest.mark.xfail_simple |
|
382
|
|
|
def test_vars_of_instance(lazy_object_proxy): |
|
383
|
|
|
# Test preservation of instance __dir__ attribute. |
|
384
|
|
|
|
|
385
|
|
|
target = objects.Target() |
|
386
|
|
|
wrapper = lazy_object_proxy.Proxy(lambda: target) |
|
387
|
|
|
|
|
388
|
|
|
assert vars(wrapper) == vars(target) |
|
389
|
|
|
|
|
390
|
|
|
|
|
391
|
|
|
def test_dir_of_function(lazy_object_proxy): |
|
392
|
|
|
# Test preservation of function __dir__ attribute. |
|
393
|
|
|
|
|
394
|
|
|
target = objects.target |
|
395
|
|
|
wrapper = lazy_object_proxy.Proxy(lambda: target) |
|
396
|
|
|
|
|
397
|
|
|
assert dir(wrapper) == dir(target) |
|
398
|
|
|
|
|
399
|
|
|
|
|
400
|
|
|
@pytest.mark.xfail_simple |
|
401
|
|
|
def test_vars_of_function(lazy_object_proxy): |
|
402
|
|
|
# Test preservation of function __dir__ attribute. |
|
403
|
|
|
|
|
404
|
|
|
target = objects.target |
|
405
|
|
|
wrapper = lazy_object_proxy.Proxy(lambda: target) |
|
406
|
|
|
|
|
407
|
|
|
assert vars(wrapper) == vars(target) |
|
408
|
|
|
|
|
409
|
|
|
|
|
410
|
|
|
def test_function_no_args(lazy_object_proxy): |
|
411
|
|
|
_args = () |
|
412
|
|
|
_kwargs = {} |
|
413
|
|
|
|
|
414
|
|
|
def function(*args, **kwargs): |
|
415
|
|
|
return args, kwargs |
|
416
|
|
|
|
|
417
|
|
|
wrapper = lazy_object_proxy.Proxy(lambda: function) |
|
418
|
|
|
|
|
419
|
|
|
result = wrapper() |
|
420
|
|
|
|
|
421
|
|
|
assert result == (_args, _kwargs) |
|
422
|
|
|
|
|
423
|
|
|
|
|
424
|
|
|
def test_function_args(lazy_object_proxy): |
|
425
|
|
|
_args = (1, 2) |
|
426
|
|
|
_kwargs = {} |
|
427
|
|
|
|
|
428
|
|
|
def function(*args, **kwargs): |
|
429
|
|
|
return args, kwargs |
|
430
|
|
|
|
|
431
|
|
|
wrapper = lazy_object_proxy.Proxy(lambda: function) |
|
432
|
|
|
|
|
433
|
|
|
result = wrapper(*_args) |
|
434
|
|
|
|
|
435
|
|
|
assert result == (_args, _kwargs) |
|
436
|
|
|
|
|
437
|
|
|
|
|
438
|
|
|
def test_function_kwargs(lazy_object_proxy): |
|
439
|
|
|
_args = () |
|
440
|
|
|
_kwargs = {"one": 1, "two": 2} |
|
441
|
|
|
|
|
442
|
|
|
def function(*args, **kwargs): |
|
443
|
|
|
return args, kwargs |
|
444
|
|
|
|
|
445
|
|
|
wrapper = lazy_object_proxy.Proxy(lambda: function) |
|
446
|
|
|
|
|
447
|
|
|
result = wrapper(**_kwargs) |
|
448
|
|
|
|
|
449
|
|
|
assert result == (_args, _kwargs) |
|
450
|
|
|
|
|
451
|
|
|
|
|
452
|
|
|
def test_function_args_plus_kwargs(lazy_object_proxy): |
|
453
|
|
|
_args = (1, 2) |
|
454
|
|
|
_kwargs = {"one": 1, "two": 2} |
|
455
|
|
|
|
|
456
|
|
|
def function(*args, **kwargs): |
|
457
|
|
|
return args, kwargs |
|
458
|
|
|
|
|
459
|
|
|
wrapper = lazy_object_proxy.Proxy(lambda: function) |
|
460
|
|
|
|
|
461
|
|
|
result = wrapper(*_args, **_kwargs) |
|
462
|
|
|
|
|
463
|
|
|
assert result == (_args, _kwargs) |
|
464
|
|
|
|
|
465
|
|
|
|
|
466
|
|
|
def test_instancemethod_no_args(lazy_object_proxy): |
|
467
|
|
|
_args = () |
|
468
|
|
|
_kwargs = {} |
|
469
|
|
|
|
|
470
|
|
|
class Class(object): |
|
471
|
|
|
def function(self, *args, **kwargs): |
|
472
|
|
|
return args, kwargs |
|
473
|
|
|
|
|
474
|
|
|
wrapper = lazy_object_proxy.Proxy(lambda: Class().function) |
|
475
|
|
|
|
|
476
|
|
|
result = wrapper() |
|
477
|
|
|
|
|
478
|
|
|
assert result == (_args, _kwargs) |
|
479
|
|
|
|
|
480
|
|
|
|
|
481
|
|
|
def test_instancemethod_args(lazy_object_proxy): |
|
482
|
|
|
_args = (1, 2) |
|
483
|
|
|
_kwargs = {} |
|
484
|
|
|
|
|
485
|
|
|
class Class(object): |
|
486
|
|
|
def function(self, *args, **kwargs): |
|
487
|
|
|
return args, kwargs |
|
488
|
|
|
|
|
489
|
|
|
wrapper = lazy_object_proxy.Proxy(lambda: Class().function) |
|
490
|
|
|
|
|
491
|
|
|
result = wrapper(*_args) |
|
492
|
|
|
|
|
493
|
|
|
assert result == (_args, _kwargs) |
|
494
|
|
|
|
|
495
|
|
|
|
|
496
|
|
|
def test_instancemethod_kwargs(lazy_object_proxy): |
|
497
|
|
|
_args = () |
|
498
|
|
|
_kwargs = {"one": 1, "two": 2} |
|
499
|
|
|
|
|
500
|
|
|
class Class(object): |
|
501
|
|
|
def function(self, *args, **kwargs): |
|
502
|
|
|
return args, kwargs |
|
503
|
|
|
|
|
504
|
|
|
wrapper = lazy_object_proxy.Proxy(lambda: Class().function) |
|
505
|
|
|
|
|
506
|
|
|
result = wrapper(**_kwargs) |
|
507
|
|
|
|
|
508
|
|
|
assert result == (_args, _kwargs) |
|
509
|
|
|
|
|
510
|
|
|
|
|
511
|
|
|
def test_instancemethod_args_plus_kwargs(lazy_object_proxy): |
|
512
|
|
|
_args = (1, 2) |
|
513
|
|
|
_kwargs = {"one": 1, "two": 2} |
|
514
|
|
|
|
|
515
|
|
|
class Class(object): |
|
516
|
|
|
def function(self, *args, **kwargs): |
|
517
|
|
|
return args, kwargs |
|
518
|
|
|
|
|
519
|
|
|
wrapper = lazy_object_proxy.Proxy(lambda: Class().function) |
|
520
|
|
|
|
|
521
|
|
|
result = wrapper(*_args, **_kwargs) |
|
522
|
|
|
|
|
523
|
|
|
assert result == (_args, _kwargs) |
|
524
|
|
|
|
|
525
|
|
|
|
|
526
|
|
|
def test_instancemethod_via_class_no_args(lazy_object_proxy): |
|
527
|
|
|
_args = () |
|
528
|
|
|
_kwargs = {} |
|
529
|
|
|
|
|
530
|
|
|
class Class(object): |
|
531
|
|
|
def function(self, *args, **kwargs): |
|
532
|
|
|
return args, kwargs |
|
533
|
|
|
|
|
534
|
|
|
wrapper = lazy_object_proxy.Proxy(lambda: Class.function) |
|
535
|
|
|
|
|
536
|
|
|
result = wrapper(Class()) |
|
537
|
|
|
|
|
538
|
|
|
assert result == (_args, _kwargs) |
|
539
|
|
|
|
|
540
|
|
|
|
|
541
|
|
|
def test_instancemethod_via_class_args(lazy_object_proxy): |
|
542
|
|
|
_args = (1, 2) |
|
543
|
|
|
_kwargs = {} |
|
544
|
|
|
|
|
545
|
|
|
class Class(object): |
|
546
|
|
|
def function(self, *args, **kwargs): |
|
547
|
|
|
return args, kwargs |
|
548
|
|
|
|
|
549
|
|
|
wrapper = lazy_object_proxy.Proxy(lambda: Class.function) |
|
550
|
|
|
|
|
551
|
|
|
result = wrapper(Class(), *_args) |
|
552
|
|
|
|
|
553
|
|
|
assert result == (_args, _kwargs) |
|
554
|
|
|
|
|
555
|
|
|
|
|
556
|
|
|
def test_instancemethod_via_class_kwargs(lazy_object_proxy): |
|
557
|
|
|
_args = () |
|
558
|
|
|
_kwargs = {"one": 1, "two": 2} |
|
559
|
|
|
|
|
560
|
|
|
class Class(object): |
|
561
|
|
|
def function(self, *args, **kwargs): |
|
562
|
|
|
return args, kwargs |
|
563
|
|
|
|
|
564
|
|
|
wrapper = lazy_object_proxy.Proxy(lambda: Class.function) |
|
565
|
|
|
|
|
566
|
|
|
result = wrapper(Class(), **_kwargs) |
|
567
|
|
|
|
|
568
|
|
|
assert result == (_args, _kwargs) |
|
569
|
|
|
|
|
570
|
|
|
|
|
571
|
|
|
def test_instancemethod_via_class_args_plus_kwargs(lazy_object_proxy): |
|
572
|
|
|
_args = (1, 2) |
|
573
|
|
|
_kwargs = {"one": 1, "two": 2} |
|
574
|
|
|
|
|
575
|
|
|
class Class(object): |
|
576
|
|
|
def function(self, *args, **kwargs): |
|
577
|
|
|
return args, kwargs |
|
578
|
|
|
|
|
579
|
|
|
wrapper = lazy_object_proxy.Proxy(lambda: Class.function) |
|
580
|
|
|
|
|
581
|
|
|
result = wrapper(Class(), *_args, **_kwargs) |
|
582
|
|
|
|
|
583
|
|
|
assert result == (_args, _kwargs) |
|
584
|
|
|
|
|
585
|
|
|
|
|
586
|
|
|
def test_classmethod_no_args(lazy_object_proxy): |
|
587
|
|
|
_args = () |
|
588
|
|
|
_kwargs = {} |
|
589
|
|
|
|
|
590
|
|
|
class Class(object): |
|
591
|
|
|
@classmethod |
|
592
|
|
|
def function(cls, *args, **kwargs): |
|
593
|
|
|
return args, kwargs |
|
594
|
|
|
|
|
595
|
|
|
wrapper = lazy_object_proxy.Proxy(lambda: Class().function) |
|
596
|
|
|
|
|
597
|
|
|
result = wrapper() |
|
598
|
|
|
|
|
599
|
|
|
assert result == (_args, _kwargs) |
|
600
|
|
|
|
|
601
|
|
|
|
|
602
|
|
|
def test_classmethod_args(lazy_object_proxy): |
|
603
|
|
|
_args = (1, 2) |
|
604
|
|
|
_kwargs = {} |
|
605
|
|
|
|
|
606
|
|
|
class Class(object): |
|
607
|
|
|
@classmethod |
|
608
|
|
|
def function(cls, *args, **kwargs): |
|
609
|
|
|
return args, kwargs |
|
610
|
|
|
|
|
611
|
|
|
wrapper = lazy_object_proxy.Proxy(lambda: Class().function) |
|
612
|
|
|
|
|
613
|
|
|
result = wrapper(*_args) |
|
614
|
|
|
|
|
615
|
|
|
assert result == (_args, _kwargs) |
|
616
|
|
|
|
|
617
|
|
|
|
|
618
|
|
|
def test_classmethod_kwargs(lazy_object_proxy): |
|
619
|
|
|
_args = () |
|
620
|
|
|
_kwargs = {"one": 1, "two": 2} |
|
621
|
|
|
|
|
622
|
|
|
class Class(object): |
|
623
|
|
|
@classmethod |
|
624
|
|
|
def function(cls, *args, **kwargs): |
|
625
|
|
|
return args, kwargs |
|
626
|
|
|
|
|
627
|
|
|
wrapper = lazy_object_proxy.Proxy(lambda: Class().function) |
|
628
|
|
|
|
|
629
|
|
|
result = wrapper(**_kwargs) |
|
630
|
|
|
|
|
631
|
|
|
assert result == (_args, _kwargs) |
|
632
|
|
|
|
|
633
|
|
|
|
|
634
|
|
|
def test_classmethod_args_plus_kwargs(lazy_object_proxy): |
|
635
|
|
|
_args = (1, 2) |
|
636
|
|
|
_kwargs = {"one": 1, "two": 2} |
|
637
|
|
|
|
|
638
|
|
|
class Class(object): |
|
639
|
|
|
@classmethod |
|
640
|
|
|
def function(cls, *args, **kwargs): |
|
641
|
|
|
return args, kwargs |
|
642
|
|
|
|
|
643
|
|
|
wrapper = lazy_object_proxy.Proxy(lambda: Class().function) |
|
644
|
|
|
|
|
645
|
|
|
result = wrapper(*_args, **_kwargs) |
|
646
|
|
|
|
|
647
|
|
|
assert result == (_args, _kwargs) |
|
648
|
|
|
|
|
649
|
|
|
|
|
650
|
|
|
def test_classmethod_via_class_no_args(lazy_object_proxy): |
|
651
|
|
|
_args = () |
|
652
|
|
|
_kwargs = {} |
|
653
|
|
|
|
|
654
|
|
|
class Class(object): |
|
655
|
|
|
@classmethod |
|
656
|
|
|
def function(cls, *args, **kwargs): |
|
657
|
|
|
return args, kwargs |
|
658
|
|
|
|
|
659
|
|
|
wrapper = lazy_object_proxy.Proxy(lambda: Class.function) |
|
660
|
|
|
|
|
661
|
|
|
result = wrapper() |
|
662
|
|
|
|
|
663
|
|
|
assert result == (_args, _kwargs) |
|
664
|
|
|
|
|
665
|
|
|
|
|
666
|
|
|
def test_classmethod_via_class_args(lazy_object_proxy): |
|
667
|
|
|
_args = (1, 2) |
|
668
|
|
|
_kwargs = {} |
|
669
|
|
|
|
|
670
|
|
|
class Class(object): |
|
671
|
|
|
@classmethod |
|
672
|
|
|
def function(cls, *args, **kwargs): |
|
673
|
|
|
return args, kwargs |
|
674
|
|
|
|
|
675
|
|
|
wrapper = lazy_object_proxy.Proxy(lambda: Class.function) |
|
676
|
|
|
|
|
677
|
|
|
result = wrapper(*_args) |
|
678
|
|
|
|
|
679
|
|
|
assert result == (_args, _kwargs) |
|
680
|
|
|
|
|
681
|
|
|
|
|
682
|
|
|
def test_classmethod_via_class_kwargs(lazy_object_proxy): |
|
683
|
|
|
_args = () |
|
684
|
|
|
_kwargs = {"one": 1, "two": 2} |
|
685
|
|
|
|
|
686
|
|
|
class Class(object): |
|
687
|
|
|
@classmethod |
|
688
|
|
|
def function(cls, *args, **kwargs): |
|
689
|
|
|
return args, kwargs |
|
690
|
|
|
|
|
691
|
|
|
wrapper = lazy_object_proxy.Proxy(lambda: Class.function) |
|
692
|
|
|
|
|
693
|
|
|
result = wrapper(**_kwargs) |
|
694
|
|
|
|
|
695
|
|
|
assert result == (_args, _kwargs) |
|
696
|
|
|
|
|
697
|
|
|
|
|
698
|
|
|
def test_classmethod_via_class_args_plus_kwargs(lazy_object_proxy): |
|
699
|
|
|
_args = (1, 2) |
|
700
|
|
|
_kwargs = {"one": 1, "two": 2} |
|
701
|
|
|
|
|
702
|
|
|
class Class(object): |
|
703
|
|
|
@classmethod |
|
704
|
|
|
def function(cls, *args, **kwargs): |
|
705
|
|
|
return args, kwargs |
|
706
|
|
|
|
|
707
|
|
|
wrapper = lazy_object_proxy.Proxy(lambda: Class.function) |
|
708
|
|
|
|
|
709
|
|
|
result = wrapper(*_args, **_kwargs) |
|
710
|
|
|
|
|
711
|
|
|
assert result == (_args, _kwargs) |
|
712
|
|
|
|
|
713
|
|
|
|
|
714
|
|
|
def test_staticmethod_no_args(lazy_object_proxy): |
|
715
|
|
|
_args = () |
|
716
|
|
|
_kwargs = {} |
|
717
|
|
|
|
|
718
|
|
|
class Class(object): |
|
719
|
|
|
@staticmethod |
|
720
|
|
|
def function(*args, **kwargs): |
|
721
|
|
|
return args, kwargs |
|
722
|
|
|
|
|
723
|
|
|
wrapper = lazy_object_proxy.Proxy(lambda: Class().function) |
|
724
|
|
|
|
|
725
|
|
|
result = wrapper() |
|
726
|
|
|
|
|
727
|
|
|
assert result == (_args, _kwargs) |
|
728
|
|
|
|
|
729
|
|
|
|
|
730
|
|
|
def test_staticmethod_args(lazy_object_proxy): |
|
731
|
|
|
_args = (1, 2) |
|
732
|
|
|
_kwargs = {} |
|
733
|
|
|
|
|
734
|
|
|
class Class(object): |
|
735
|
|
|
@staticmethod |
|
736
|
|
|
def function(*args, **kwargs): |
|
737
|
|
|
return args, kwargs |
|
738
|
|
|
|
|
739
|
|
|
wrapper = lazy_object_proxy.Proxy(lambda: Class().function) |
|
740
|
|
|
|
|
741
|
|
|
result = wrapper(*_args) |
|
742
|
|
|
|
|
743
|
|
|
assert result == (_args, _kwargs) |
|
744
|
|
|
|
|
745
|
|
|
|
|
746
|
|
|
def test_staticmethod_kwargs(lazy_object_proxy): |
|
747
|
|
|
_args = () |
|
748
|
|
|
_kwargs = {"one": 1, "two": 2} |
|
749
|
|
|
|
|
750
|
|
|
class Class(object): |
|
751
|
|
|
@staticmethod |
|
752
|
|
|
def function(*args, **kwargs): |
|
753
|
|
|
return args, kwargs |
|
754
|
|
|
|
|
755
|
|
|
wrapper = lazy_object_proxy.Proxy(lambda: Class().function) |
|
756
|
|
|
|
|
757
|
|
|
result = wrapper(**_kwargs) |
|
758
|
|
|
|
|
759
|
|
|
assert result == (_args, _kwargs) |
|
760
|
|
|
|
|
761
|
|
|
|
|
762
|
|
|
def test_staticmethod_args_plus_kwargs(lazy_object_proxy): |
|
763
|
|
|
_args = (1, 2) |
|
764
|
|
|
_kwargs = {"one": 1, "two": 2} |
|
765
|
|
|
|
|
766
|
|
|
class Class(object): |
|
767
|
|
|
@staticmethod |
|
768
|
|
|
def function(*args, **kwargs): |
|
769
|
|
|
return args, kwargs |
|
770
|
|
|
|
|
771
|
|
|
wrapper = lazy_object_proxy.Proxy(lambda: Class().function) |
|
772
|
|
|
|
|
773
|
|
|
result = wrapper(*_args, **_kwargs) |
|
774
|
|
|
|
|
775
|
|
|
assert result == (_args, _kwargs) |
|
776
|
|
|
|
|
777
|
|
|
|
|
778
|
|
|
def test_staticmethod_via_class_no_args(lazy_object_proxy): |
|
779
|
|
|
_args = () |
|
780
|
|
|
_kwargs = {} |
|
781
|
|
|
|
|
782
|
|
|
class Class(object): |
|
783
|
|
|
@staticmethod |
|
784
|
|
|
def function(*args, **kwargs): |
|
785
|
|
|
return args, kwargs |
|
786
|
|
|
|
|
787
|
|
|
wrapper = lazy_object_proxy.Proxy(lambda: Class.function) |
|
788
|
|
|
|
|
789
|
|
|
result = wrapper() |
|
790
|
|
|
|
|
791
|
|
|
assert result == (_args, _kwargs) |
|
792
|
|
|
|
|
793
|
|
|
|
|
794
|
|
|
def test_staticmethod_via_class_args(lazy_object_proxy): |
|
795
|
|
|
_args = (1, 2) |
|
796
|
|
|
_kwargs = {} |
|
797
|
|
|
|
|
798
|
|
|
class Class(object): |
|
799
|
|
|
@staticmethod |
|
800
|
|
|
def function(*args, **kwargs): |
|
801
|
|
|
return args, kwargs |
|
802
|
|
|
|
|
803
|
|
|
wrapper = lazy_object_proxy.Proxy(lambda: Class.function) |
|
804
|
|
|
|
|
805
|
|
|
result = wrapper(*_args) |
|
806
|
|
|
|
|
807
|
|
|
assert result == (_args, _kwargs) |
|
808
|
|
|
|
|
809
|
|
|
|
|
810
|
|
|
def test_staticmethod_via_class_kwargs(lazy_object_proxy): |
|
811
|
|
|
_args = () |
|
812
|
|
|
_kwargs = {"one": 1, "two": 2} |
|
813
|
|
|
|
|
814
|
|
|
class Class(object): |
|
815
|
|
|
@staticmethod |
|
816
|
|
|
def function(*args, **kwargs): |
|
817
|
|
|
return args, kwargs |
|
818
|
|
|
|
|
819
|
|
|
wrapper = lazy_object_proxy.Proxy(lambda: Class.function) |
|
820
|
|
|
|
|
821
|
|
|
result = wrapper(**_kwargs) |
|
822
|
|
|
|
|
823
|
|
|
assert result == (_args, _kwargs) |
|
824
|
|
|
|
|
825
|
|
|
|
|
826
|
|
|
def test_staticmethod_via_class_args_plus_kwargs(lazy_object_proxy): |
|
827
|
|
|
_args = (1, 2) |
|
828
|
|
|
_kwargs = {"one": 1, "two": 2} |
|
829
|
|
|
|
|
830
|
|
|
class Class(object): |
|
831
|
|
|
@staticmethod |
|
832
|
|
|
def function(*args, **kwargs): |
|
833
|
|
|
return args, kwargs |
|
834
|
|
|
|
|
835
|
|
|
wrapper = lazy_object_proxy.Proxy(lambda: Class.function) |
|
836
|
|
|
|
|
837
|
|
|
result = wrapper(*_args, **_kwargs) |
|
838
|
|
|
|
|
839
|
|
|
assert result == (_args, _kwargs) |
|
840
|
|
|
|
|
841
|
|
|
|
|
842
|
|
|
def test_iteration(lazy_object_proxy): |
|
843
|
|
|
items = [1, 2] |
|
844
|
|
|
|
|
845
|
|
|
wrapper = lazy_object_proxy.Proxy(lambda: items) |
|
846
|
|
|
|
|
847
|
|
|
result = [x for x in wrapper] |
|
848
|
|
|
|
|
849
|
|
|
assert result == items |
|
850
|
|
|
|
|
851
|
|
|
with pytest.raises(TypeError): |
|
852
|
|
|
for _ in lazy_object_proxy.Proxy(lambda: 1): |
|
853
|
|
|
pass |
|
854
|
|
|
|
|
855
|
|
|
|
|
856
|
|
|
def test_iter_builtin(lazy_object_proxy): |
|
857
|
|
|
iter(lazy_object_proxy.Proxy(lambda: [1, 2])) |
|
858
|
|
|
pytest.raises(TypeError, iter, lazy_object_proxy.Proxy(lambda: 1)) |
|
859
|
|
|
|
|
860
|
|
|
|
|
861
|
|
|
def test_context_manager(lazy_object_proxy): |
|
862
|
|
|
class Class(object): |
|
863
|
|
|
def __enter__(self): |
|
864
|
|
|
return self |
|
865
|
|
|
|
|
866
|
|
|
def __exit__(*args, **kwargs): |
|
867
|
|
|
return |
|
868
|
|
|
|
|
869
|
|
|
instance = Class() |
|
870
|
|
|
|
|
871
|
|
|
wrapper = lazy_object_proxy.Proxy(lambda: instance) |
|
872
|
|
|
|
|
873
|
|
|
with wrapper: |
|
874
|
|
|
pass |
|
875
|
|
|
|
|
876
|
|
|
|
|
877
|
|
|
def test_object_hash(lazy_object_proxy): |
|
878
|
|
|
def function1(*args, **kwargs): |
|
879
|
|
|
return args, kwargs |
|
880
|
|
|
|
|
881
|
|
|
function2 = lazy_object_proxy.Proxy(lambda: function1) |
|
882
|
|
|
|
|
883
|
|
|
assert hash(function2) == hash(function1) |
|
884
|
|
|
|
|
885
|
|
|
|
|
886
|
|
|
def test_mapping_key(lazy_object_proxy): |
|
887
|
|
|
def function1(*args, **kwargs): |
|
888
|
|
|
return args, kwargs |
|
889
|
|
|
|
|
890
|
|
|
function2 = lazy_object_proxy.Proxy(lambda: function1) |
|
891
|
|
|
|
|
892
|
|
|
table = dict() |
|
893
|
|
|
table[function1] = True |
|
894
|
|
|
|
|
895
|
|
|
assert table.get(function2) |
|
896
|
|
|
|
|
897
|
|
|
table = dict() |
|
898
|
|
|
table[function2] = True |
|
899
|
|
|
|
|
900
|
|
|
assert table.get(function1) |
|
901
|
|
|
|
|
902
|
|
|
|
|
903
|
|
|
def test_comparison(lazy_object_proxy): |
|
904
|
|
|
one = lazy_object_proxy.Proxy(lambda: 1) |
|
905
|
|
|
two = lazy_object_proxy.Proxy(lambda: 2) |
|
906
|
|
|
three = lazy_object_proxy.Proxy(lambda: 3) |
|
907
|
|
|
|
|
908
|
|
|
assert two > 1 |
|
909
|
|
|
assert two >= 1 |
|
910
|
|
|
assert two < 3 |
|
911
|
|
|
assert two <= 3 |
|
912
|
|
|
assert two != 1 |
|
913
|
|
|
assert two == 2 |
|
914
|
|
|
assert two != 3 |
|
915
|
|
|
|
|
916
|
|
|
assert 2 > one |
|
917
|
|
|
assert 2 >= one |
|
918
|
|
|
assert 2 < three |
|
919
|
|
|
assert 2 <= three |
|
920
|
|
|
assert 2 != one |
|
921
|
|
|
assert 2 == two |
|
922
|
|
|
assert 2 != three |
|
923
|
|
|
|
|
924
|
|
|
assert two > one |
|
925
|
|
|
assert two >= one |
|
926
|
|
|
assert two < three |
|
927
|
|
|
assert two <= three |
|
928
|
|
|
assert two != one |
|
929
|
|
|
assert two == two |
|
930
|
|
|
assert two != three |
|
931
|
|
|
|
|
932
|
|
|
|
|
933
|
|
|
def test_nonzero(lazy_object_proxy): |
|
934
|
|
|
true = lazy_object_proxy.Proxy(lambda: True) |
|
935
|
|
|
false = lazy_object_proxy.Proxy(lambda: False) |
|
936
|
|
|
|
|
937
|
|
|
assert true |
|
938
|
|
|
assert not false |
|
939
|
|
|
|
|
940
|
|
|
assert bool(true) |
|
941
|
|
|
assert not bool(false) |
|
942
|
|
|
|
|
943
|
|
|
assert not false |
|
944
|
|
|
assert not not true |
|
945
|
|
|
|
|
946
|
|
|
|
|
947
|
|
|
def test_int(lazy_object_proxy): |
|
948
|
|
|
one = lazy_object_proxy.Proxy(lambda: 1) |
|
949
|
|
|
|
|
950
|
|
|
assert int(one) == 1 |
|
951
|
|
|
|
|
952
|
|
|
if not PY3: |
|
953
|
|
|
assert long(one) == 1 |
|
954
|
|
|
|
|
955
|
|
|
|
|
956
|
|
|
def test_float(lazy_object_proxy): |
|
957
|
|
|
one = lazy_object_proxy.Proxy(lambda: 1) |
|
958
|
|
|
|
|
959
|
|
|
assert float(one) == 1.0 |
|
960
|
|
|
|
|
961
|
|
|
|
|
962
|
|
|
def test_add(lazy_object_proxy): |
|
963
|
|
|
one = lazy_object_proxy.Proxy(lambda: 1) |
|
964
|
|
|
two = lazy_object_proxy.Proxy(lambda: 2) |
|
965
|
|
|
|
|
966
|
|
|
assert one + two == 1 + 2 |
|
967
|
|
|
assert 1 + two == 1 + 2 |
|
968
|
|
|
assert one + 2 == 1 + 2 |
|
969
|
|
|
|
|
970
|
|
|
|
|
971
|
|
|
def test_sub(lazy_object_proxy): |
|
972
|
|
|
one = lazy_object_proxy.Proxy(lambda: 1) |
|
973
|
|
|
two = lazy_object_proxy.Proxy(lambda: 2) |
|
974
|
|
|
|
|
975
|
|
|
assert one - two == 1 - 2 |
|
976
|
|
|
assert 1 - two == 1 - 2 |
|
977
|
|
|
assert one - 2 == 1 - 2 |
|
978
|
|
|
|
|
979
|
|
|
|
|
980
|
|
|
def test_mul(lazy_object_proxy): |
|
981
|
|
|
two = lazy_object_proxy.Proxy(lambda: 2) |
|
982
|
|
|
three = lazy_object_proxy.Proxy(lambda: 3) |
|
983
|
|
|
|
|
984
|
|
|
assert two * three == 2 * 3 |
|
985
|
|
|
assert 2 * three == 2 * 3 |
|
986
|
|
|
assert two * 3 == 2 * 3 |
|
987
|
|
|
|
|
988
|
|
|
|
|
989
|
|
|
def test_div(lazy_object_proxy): |
|
990
|
|
|
# On Python 2 this will pick up div and on Python |
|
991
|
|
|
# 3 it will pick up truediv. |
|
992
|
|
|
|
|
993
|
|
|
two = lazy_object_proxy.Proxy(lambda: 2) |
|
994
|
|
|
three = lazy_object_proxy.Proxy(lambda: 3) |
|
995
|
|
|
|
|
996
|
|
|
assert two / three == 2 / 3 |
|
997
|
|
|
assert 2 / three == 2 / 3 |
|
998
|
|
|
assert two / 3 == 2 / 3 |
|
999
|
|
|
|
|
1000
|
|
|
|
|
1001
|
|
|
def test_mod(lazy_object_proxy): |
|
1002
|
|
|
two = lazy_object_proxy.Proxy(lambda: 2) |
|
1003
|
|
|
three = lazy_object_proxy.Proxy(lambda: 3) |
|
1004
|
|
|
|
|
1005
|
|
|
assert three // two == 3 // 2 |
|
1006
|
|
|
assert 3 // two == 3 // 2 |
|
1007
|
|
|
assert three // 2 == 3 // 2 |
|
1008
|
|
|
|
|
1009
|
|
|
|
|
1010
|
|
|
def test_mod(lazy_object_proxy): |
|
1011
|
|
|
two = lazy_object_proxy.Proxy(lambda: 2) |
|
1012
|
|
|
three = lazy_object_proxy.Proxy(lambda: 3) |
|
1013
|
|
|
|
|
1014
|
|
|
assert three % two == 3 % 2 |
|
1015
|
|
|
assert 3 % two == 3 % 2 |
|
1016
|
|
|
assert three % 2 == 3 % 2 |
|
1017
|
|
|
|
|
1018
|
|
|
|
|
1019
|
|
|
def test_divmod(lazy_object_proxy): |
|
1020
|
|
|
two = lazy_object_proxy.Proxy(lambda: 2) |
|
1021
|
|
|
three = lazy_object_proxy.Proxy(lambda: 3) |
|
1022
|
|
|
|
|
1023
|
|
|
assert divmod(three, two), divmod(3 == 2) |
|
1024
|
|
|
assert divmod(3, two), divmod(3 == 2) |
|
1025
|
|
|
assert divmod(three, 2), divmod(3 == 2) |
|
1026
|
|
|
|
|
1027
|
|
|
|
|
1028
|
|
|
def test_pow(lazy_object_proxy): |
|
1029
|
|
|
two = lazy_object_proxy.Proxy(lambda: 2) |
|
1030
|
|
|
three = lazy_object_proxy.Proxy(lambda: 3) |
|
1031
|
|
|
|
|
1032
|
|
|
assert three ** two == pow(3, 2) |
|
1033
|
|
|
assert 3 ** two == pow(3, 2) |
|
1034
|
|
|
assert three ** 2 == pow(3, 2) |
|
1035
|
|
|
|
|
1036
|
|
|
assert pow(three, two) == pow(3, 2) |
|
1037
|
|
|
assert pow(3, two) == pow(3, 2) |
|
1038
|
|
|
assert pow(three, 2) == pow(3, 2) |
|
1039
|
|
|
|
|
1040
|
|
|
# Only PyPy implements __rpow__ for ternary pow(). |
|
1041
|
|
|
|
|
1042
|
|
|
if PYPY: |
|
1043
|
|
|
assert pow(three, two, 2) == pow(3, 2, 2) |
|
1044
|
|
|
assert pow(3, two, 2) == pow(3, 2, 2) |
|
1045
|
|
|
|
|
1046
|
|
|
assert pow(three, 2, 2) == pow(3, 2, 2) |
|
1047
|
|
|
|
|
1048
|
|
|
|
|
1049
|
|
|
def test_lshift(lazy_object_proxy): |
|
1050
|
|
|
two = lazy_object_proxy.Proxy(lambda: 2) |
|
1051
|
|
|
three = lazy_object_proxy.Proxy(lambda: 3) |
|
1052
|
|
|
|
|
1053
|
|
|
assert three << two == 3 << 2 |
|
1054
|
|
|
assert 3 << two == 3 << 2 |
|
1055
|
|
|
assert three << 2 == 3 << 2 |
|
1056
|
|
|
|
|
1057
|
|
|
|
|
1058
|
|
|
def test_rshift(lazy_object_proxy): |
|
1059
|
|
|
two = lazy_object_proxy.Proxy(lambda: 2) |
|
1060
|
|
|
three = lazy_object_proxy.Proxy(lambda: 3) |
|
1061
|
|
|
|
|
1062
|
|
|
assert three >> two == 3 >> 2 |
|
1063
|
|
|
assert 3 >> two == 3 >> 2 |
|
1064
|
|
|
assert three >> 2 == 3 >> 2 |
|
1065
|
|
|
|
|
1066
|
|
|
|
|
1067
|
|
|
def test_and(lazy_object_proxy): |
|
1068
|
|
|
two = lazy_object_proxy.Proxy(lambda: 2) |
|
1069
|
|
|
three = lazy_object_proxy.Proxy(lambda: 3) |
|
1070
|
|
|
|
|
1071
|
|
|
assert three & two == 3 & 2 |
|
1072
|
|
|
assert 3 & two == 3 & 2 |
|
1073
|
|
|
assert three & 2 == 3 & 2 |
|
1074
|
|
|
|
|
1075
|
|
|
|
|
1076
|
|
|
def test_xor(lazy_object_proxy): |
|
1077
|
|
|
two = lazy_object_proxy.Proxy(lambda: 2) |
|
1078
|
|
|
three = lazy_object_proxy.Proxy(lambda: 3) |
|
1079
|
|
|
|
|
1080
|
|
|
assert three ^ two == 3 ^ 2 |
|
1081
|
|
|
assert 3 ^ two == 3 ^ 2 |
|
1082
|
|
|
assert three ^ 2 == 3 ^ 2 |
|
1083
|
|
|
|
|
1084
|
|
|
|
|
1085
|
|
|
def test_or(lazy_object_proxy): |
|
1086
|
|
|
two = lazy_object_proxy.Proxy(lambda: 2) |
|
1087
|
|
|
three = lazy_object_proxy.Proxy(lambda: 3) |
|
1088
|
|
|
|
|
1089
|
|
|
assert three | two == 3 | 2 |
|
1090
|
|
|
assert 3 | two == 3 | 2 |
|
1091
|
|
|
assert three | 2 == 3 | 2 |
|
1092
|
|
|
|
|
1093
|
|
|
|
|
1094
|
|
|
def test_iadd(lazy_object_proxy): |
|
1095
|
|
|
value = lazy_object_proxy.Proxy(lambda: 1) |
|
1096
|
|
|
one = lazy_object_proxy.Proxy(lambda: 1) |
|
1097
|
|
|
|
|
1098
|
|
|
value += 1 |
|
1099
|
|
|
assert value == 2 |
|
1100
|
|
|
|
|
1101
|
|
|
if lazy_object_proxy.kind != 'simple': |
|
1102
|
|
|
assert type(value) == lazy_object_proxy.Proxy |
|
1103
|
|
|
|
|
1104
|
|
|
value += one |
|
1105
|
|
|
assert value == 3 |
|
1106
|
|
|
|
|
1107
|
|
|
if lazy_object_proxy.kind != 'simple': |
|
1108
|
|
|
assert type(value) == lazy_object_proxy.Proxy |
|
1109
|
|
|
|
|
1110
|
|
|
|
|
1111
|
|
|
def test_isub(lazy_object_proxy): |
|
1112
|
|
|
value = lazy_object_proxy.Proxy(lambda: 1) |
|
1113
|
|
|
one = lazy_object_proxy.Proxy(lambda: 1) |
|
1114
|
|
|
|
|
1115
|
|
|
value -= 1 |
|
1116
|
|
|
assert value == 0 |
|
1117
|
|
|
if lazy_object_proxy.kind != 'simple': |
|
1118
|
|
|
assert type(value) == lazy_object_proxy.Proxy |
|
1119
|
|
|
|
|
1120
|
|
|
value -= one |
|
1121
|
|
|
assert value == -1 |
|
1122
|
|
|
|
|
1123
|
|
|
if lazy_object_proxy.kind != 'simple': |
|
1124
|
|
|
assert type(value) == lazy_object_proxy.Proxy |
|
1125
|
|
|
|
|
1126
|
|
|
|
|
1127
|
|
|
def test_imul(lazy_object_proxy): |
|
1128
|
|
|
value = lazy_object_proxy.Proxy(lambda: 2) |
|
1129
|
|
|
two = lazy_object_proxy.Proxy(lambda: 2) |
|
1130
|
|
|
|
|
1131
|
|
|
value *= 2 |
|
1132
|
|
|
assert value == 4 |
|
1133
|
|
|
|
|
1134
|
|
|
if lazy_object_proxy.kind != 'simple': |
|
1135
|
|
|
assert type(value) == lazy_object_proxy.Proxy |
|
1136
|
|
|
|
|
1137
|
|
|
value *= two |
|
1138
|
|
|
assert value == 8 |
|
1139
|
|
|
|
|
1140
|
|
|
if lazy_object_proxy.kind != 'simple': |
|
1141
|
|
|
assert type(value) == lazy_object_proxy.Proxy |
|
1142
|
|
|
|
|
1143
|
|
|
|
|
1144
|
|
|
def test_idiv(lazy_object_proxy): |
|
1145
|
|
|
# On Python 2 this will pick up div and on Python |
|
1146
|
|
|
# 3 it will pick up truediv. |
|
1147
|
|
|
|
|
1148
|
|
|
value = lazy_object_proxy.Proxy(lambda: 2) |
|
1149
|
|
|
two = lazy_object_proxy.Proxy(lambda: 2) |
|
1150
|
|
|
|
|
1151
|
|
|
value /= 2 |
|
1152
|
|
|
assert value == 2 / 2 |
|
1153
|
|
|
|
|
1154
|
|
|
if lazy_object_proxy.kind != 'simple': |
|
1155
|
|
|
assert type(value) == lazy_object_proxy.Proxy |
|
1156
|
|
|
|
|
1157
|
|
|
value /= two |
|
1158
|
|
|
assert value == 2 / 2 / 2 |
|
1159
|
|
|
|
|
1160
|
|
|
if lazy_object_proxy.kind != 'simple': |
|
1161
|
|
|
assert type(value) == lazy_object_proxy.Proxy |
|
1162
|
|
|
|
|
1163
|
|
|
|
|
1164
|
|
|
def test_ifloordiv(lazy_object_proxy): |
|
1165
|
|
|
value = lazy_object_proxy.Proxy(lambda: 2) |
|
1166
|
|
|
two = lazy_object_proxy.Proxy(lambda: 2) |
|
1167
|
|
|
|
|
1168
|
|
|
value //= 2 |
|
1169
|
|
|
assert value == 2 // 2 |
|
1170
|
|
|
|
|
1171
|
|
|
if lazy_object_proxy.kind != 'simple': |
|
1172
|
|
|
assert type(value) == lazy_object_proxy.Proxy |
|
1173
|
|
|
|
|
1174
|
|
|
value //= two |
|
1175
|
|
|
assert value == 2 // 2 // 2 |
|
1176
|
|
|
|
|
1177
|
|
|
if lazy_object_proxy.kind != 'simple': |
|
1178
|
|
|
assert type(value) == lazy_object_proxy.Proxy |
|
1179
|
|
|
|
|
1180
|
|
|
|
|
1181
|
|
|
def test_imod(lazy_object_proxy): |
|
1182
|
|
|
value = lazy_object_proxy.Proxy(lambda: 10) |
|
1183
|
|
|
two = lazy_object_proxy.Proxy(lambda: 2) |
|
1184
|
|
|
|
|
1185
|
|
|
value %= 2 |
|
1186
|
|
|
assert value == 10 % 2 |
|
1187
|
|
|
|
|
1188
|
|
|
if lazy_object_proxy.kind != 'simple': |
|
1189
|
|
|
assert type(value) == lazy_object_proxy.Proxy |
|
1190
|
|
|
|
|
1191
|
|
|
value %= two |
|
1192
|
|
|
assert value == 10 % 2 % 2 |
|
1193
|
|
|
|
|
1194
|
|
|
if lazy_object_proxy.kind != 'simple': |
|
1195
|
|
|
assert type(value) == lazy_object_proxy.Proxy |
|
1196
|
|
|
|
|
1197
|
|
|
|
|
1198
|
|
|
def test_ipow(lazy_object_proxy): |
|
1199
|
|
|
value = lazy_object_proxy.Proxy(lambda: 10) |
|
1200
|
|
|
two = lazy_object_proxy.Proxy(lambda: 2) |
|
1201
|
|
|
|
|
1202
|
|
|
value **= 2 |
|
1203
|
|
|
assert value == 10 ** 2 |
|
1204
|
|
|
|
|
1205
|
|
|
if lazy_object_proxy.kind != 'simple': |
|
1206
|
|
|
assert type(value) == lazy_object_proxy.Proxy |
|
1207
|
|
|
|
|
1208
|
|
|
value **= two |
|
1209
|
|
|
assert value == 10 ** 2 ** 2 |
|
1210
|
|
|
|
|
1211
|
|
|
if lazy_object_proxy.kind != 'simple': |
|
1212
|
|
|
assert type(value) == lazy_object_proxy.Proxy |
|
1213
|
|
|
|
|
1214
|
|
|
|
|
1215
|
|
|
def test_ilshift(lazy_object_proxy): |
|
1216
|
|
|
value = lazy_object_proxy.Proxy(lambda: 256) |
|
1217
|
|
|
two = lazy_object_proxy.Proxy(lambda: 2) |
|
1218
|
|
|
|
|
1219
|
|
|
value <<= 2 |
|
1220
|
|
|
assert value == 256 << 2 |
|
1221
|
|
|
|
|
1222
|
|
|
if lazy_object_proxy.kind != 'simple': |
|
1223
|
|
|
assert type(value) == lazy_object_proxy.Proxy |
|
1224
|
|
|
|
|
1225
|
|
|
value <<= two |
|
1226
|
|
|
assert value == 256 << 2 << 2 |
|
1227
|
|
|
|
|
1228
|
|
|
if lazy_object_proxy.kind != 'simple': |
|
1229
|
|
|
assert type(value) == lazy_object_proxy.Proxy |
|
1230
|
|
|
|
|
1231
|
|
|
|
|
1232
|
|
|
def test_irshift(lazy_object_proxy): |
|
1233
|
|
|
value = lazy_object_proxy.Proxy(lambda: 2) |
|
1234
|
|
|
two = lazy_object_proxy.Proxy(lambda: 2) |
|
1235
|
|
|
|
|
1236
|
|
|
value >>= 2 |
|
1237
|
|
|
assert value == 2 >> 2 |
|
1238
|
|
|
|
|
1239
|
|
|
if lazy_object_proxy.kind != 'simple': |
|
1240
|
|
|
assert type(value) == lazy_object_proxy.Proxy |
|
1241
|
|
|
|
|
1242
|
|
|
value >>= two |
|
1243
|
|
|
assert value == 2 >> 2 >> 2 |
|
1244
|
|
|
|
|
1245
|
|
|
if lazy_object_proxy.kind != 'simple': |
|
1246
|
|
|
assert type(value) == lazy_object_proxy.Proxy |
|
1247
|
|
|
|
|
1248
|
|
|
|
|
1249
|
|
|
def test_iand(lazy_object_proxy): |
|
1250
|
|
|
value = lazy_object_proxy.Proxy(lambda: 1) |
|
1251
|
|
|
two = lazy_object_proxy.Proxy(lambda: 2) |
|
1252
|
|
|
|
|
1253
|
|
|
value &= 2 |
|
1254
|
|
|
assert value == 1 & 2 |
|
1255
|
|
|
|
|
1256
|
|
|
if lazy_object_proxy.kind != 'simple': |
|
1257
|
|
|
assert type(value) == lazy_object_proxy.Proxy |
|
1258
|
|
|
|
|
1259
|
|
|
value &= two |
|
1260
|
|
|
assert value == 1 & 2 & 2 |
|
1261
|
|
|
|
|
1262
|
|
|
if lazy_object_proxy.kind != 'simple': |
|
1263
|
|
|
assert type(value) == lazy_object_proxy.Proxy |
|
1264
|
|
|
|
|
1265
|
|
|
|
|
1266
|
|
|
def test_ixor(lazy_object_proxy): |
|
1267
|
|
|
value = lazy_object_proxy.Proxy(lambda: 1) |
|
1268
|
|
|
two = lazy_object_proxy.Proxy(lambda: 2) |
|
1269
|
|
|
|
|
1270
|
|
|
value ^= 2 |
|
1271
|
|
|
assert value == 1 ^ 2 |
|
1272
|
|
|
|
|
1273
|
|
|
if lazy_object_proxy.kind != 'simple': |
|
1274
|
|
|
assert type(value) == lazy_object_proxy.Proxy |
|
1275
|
|
|
|
|
1276
|
|
|
value ^= two |
|
1277
|
|
|
assert value == 1 ^ 2 ^ 2 |
|
1278
|
|
|
|
|
1279
|
|
|
if lazy_object_proxy.kind != 'simple': |
|
1280
|
|
|
assert type(value) == lazy_object_proxy.Proxy |
|
1281
|
|
|
|
|
1282
|
|
|
|
|
1283
|
|
|
def test_ior(lazy_object_proxy): |
|
1284
|
|
|
value = lazy_object_proxy.Proxy(lambda: 1) |
|
1285
|
|
|
two = lazy_object_proxy.Proxy(lambda: 2) |
|
1286
|
|
|
|
|
1287
|
|
|
value |= 2 |
|
1288
|
|
|
assert value == 1 | 2 |
|
1289
|
|
|
|
|
1290
|
|
|
if lazy_object_proxy.kind != 'simple': |
|
1291
|
|
|
assert type(value) == lazy_object_proxy.Proxy |
|
1292
|
|
|
|
|
1293
|
|
|
value |= two |
|
1294
|
|
|
assert value == 1 | 2 | 2 |
|
1295
|
|
|
|
|
1296
|
|
|
if lazy_object_proxy.kind != 'simple': |
|
1297
|
|
|
assert type(value) == lazy_object_proxy.Proxy |
|
1298
|
|
|
|
|
1299
|
|
|
|
|
1300
|
|
|
def test_neg(lazy_object_proxy): |
|
1301
|
|
|
value = lazy_object_proxy.Proxy(lambda: 1) |
|
1302
|
|
|
|
|
1303
|
|
|
assert -value == -1 |
|
1304
|
|
|
|
|
1305
|
|
|
|
|
1306
|
|
|
def test_pos(lazy_object_proxy): |
|
1307
|
|
|
value = lazy_object_proxy.Proxy(lambda: 1) |
|
1308
|
|
|
|
|
1309
|
|
|
assert +value == 1 |
|
1310
|
|
|
|
|
1311
|
|
|
|
|
1312
|
|
|
def test_abs(lazy_object_proxy): |
|
1313
|
|
|
value = lazy_object_proxy.Proxy(lambda: -1) |
|
1314
|
|
|
|
|
1315
|
|
|
assert abs(value) == 1 |
|
1316
|
|
|
|
|
1317
|
|
|
|
|
1318
|
|
|
def test_invert(lazy_object_proxy): |
|
1319
|
|
|
value = lazy_object_proxy.Proxy(lambda: 1) |
|
1320
|
|
|
|
|
1321
|
|
|
assert ~value == ~1 |
|
1322
|
|
|
|
|
1323
|
|
|
|
|
1324
|
|
|
def test_oct(lazy_object_proxy): |
|
1325
|
|
|
value = lazy_object_proxy.Proxy(lambda: 20) |
|
1326
|
|
|
|
|
1327
|
|
|
assert oct(value) == oct(20) |
|
1328
|
|
|
|
|
1329
|
|
|
|
|
1330
|
|
|
def test_hex(lazy_object_proxy): |
|
1331
|
|
|
value = lazy_object_proxy.Proxy(lambda: 20) |
|
1332
|
|
|
|
|
1333
|
|
|
assert hex(value) == hex(20) |
|
1334
|
|
|
|
|
1335
|
|
|
|
|
1336
|
|
|
def test_index(lazy_object_proxy): |
|
1337
|
|
|
class Class(object): |
|
1338
|
|
|
def __index__(self): |
|
1339
|
|
|
return 1 |
|
1340
|
|
|
|
|
1341
|
|
|
value = lazy_object_proxy.Proxy(lambda: Class()) |
|
1342
|
|
|
items = [0, 1, 2] |
|
1343
|
|
|
|
|
1344
|
|
|
assert items[value] == items[1] |
|
1345
|
|
|
|
|
1346
|
|
|
|
|
1347
|
|
|
def test_length(lazy_object_proxy): |
|
1348
|
|
|
value = lazy_object_proxy.Proxy(lambda: list(range(3))) |
|
1349
|
|
|
|
|
1350
|
|
|
assert len(value) == 3 |
|
1351
|
|
|
|
|
1352
|
|
|
|
|
1353
|
|
|
def test_contains(lazy_object_proxy): |
|
1354
|
|
|
value = lazy_object_proxy.Proxy(lambda: list(range(3))) |
|
1355
|
|
|
|
|
1356
|
|
|
assert 2 in value |
|
1357
|
|
|
assert not -2 in value |
|
1358
|
|
|
|
|
1359
|
|
|
|
|
1360
|
|
|
def test_getitem(lazy_object_proxy): |
|
1361
|
|
|
value = lazy_object_proxy.Proxy(lambda: list(range(3))) |
|
1362
|
|
|
|
|
1363
|
|
|
assert value[1] == 1 |
|
1364
|
|
|
|
|
1365
|
|
|
|
|
1366
|
|
|
def test_setitem(lazy_object_proxy): |
|
1367
|
|
|
value = lazy_object_proxy.Proxy(lambda: list(range(3))) |
|
1368
|
|
|
value[1] = -1 |
|
1369
|
|
|
|
|
1370
|
|
|
assert value[1] == -1 |
|
1371
|
|
|
|
|
1372
|
|
|
|
|
1373
|
|
|
def test_delitem(lazy_object_proxy): |
|
1374
|
|
|
value = lazy_object_proxy.Proxy(lambda: list(range(3))) |
|
1375
|
|
|
|
|
1376
|
|
|
assert len(value) == 3 |
|
1377
|
|
|
|
|
1378
|
|
|
del value[1] |
|
1379
|
|
|
|
|
1380
|
|
|
assert len(value) == 2 |
|
1381
|
|
|
assert value[1] == 2 |
|
1382
|
|
|
|
|
1383
|
|
|
|
|
1384
|
|
|
def test_getslice(lazy_object_proxy): |
|
1385
|
|
|
value = lazy_object_proxy.Proxy(lambda: list(range(5))) |
|
1386
|
|
|
|
|
1387
|
|
|
assert value[1:4] == [1, 2, 3] |
|
1388
|
|
|
|
|
1389
|
|
|
|
|
1390
|
|
|
def test_setslice(lazy_object_proxy): |
|
1391
|
|
|
value = lazy_object_proxy.Proxy(lambda: list(range(5))) |
|
1392
|
|
|
|
|
1393
|
|
|
value[1:4] = reversed(value[1:4]) |
|
1394
|
|
|
|
|
1395
|
|
|
assert value[1:4] == [3, 2, 1] |
|
1396
|
|
|
|
|
1397
|
|
|
|
|
1398
|
|
|
def test_delslice(lazy_object_proxy): |
|
1399
|
|
|
value = lazy_object_proxy.Proxy(lambda: list(range(5))) |
|
1400
|
|
|
|
|
1401
|
|
|
del value[1:4] |
|
1402
|
|
|
|
|
1403
|
|
|
assert len(value) == 2 |
|
1404
|
|
|
assert value == [0, 4] |
|
1405
|
|
|
|
|
1406
|
|
|
|
|
1407
|
|
|
def test_length(lazy_object_proxy): |
|
1408
|
|
|
value = lazy_object_proxy.Proxy(lambda: dict.fromkeys(range(3), False)) |
|
1409
|
|
|
|
|
1410
|
|
|
assert len(value) == 3 |
|
1411
|
|
|
|
|
1412
|
|
|
|
|
1413
|
|
|
def test_contains(lazy_object_proxy): |
|
1414
|
|
|
value = lazy_object_proxy.Proxy(lambda: dict.fromkeys(range(3), False)) |
|
1415
|
|
|
|
|
1416
|
|
|
assert 2 in value |
|
1417
|
|
|
assert -2 not in value |
|
1418
|
|
|
|
|
1419
|
|
|
|
|
1420
|
|
|
def test_getitem(lazy_object_proxy): |
|
1421
|
|
|
value = lazy_object_proxy.Proxy(lambda: dict.fromkeys(range(3), False)) |
|
1422
|
|
|
|
|
1423
|
|
|
assert value[1] == False |
|
1424
|
|
|
|
|
1425
|
|
|
|
|
1426
|
|
|
def test_setitem(lazy_object_proxy): |
|
1427
|
|
|
value = lazy_object_proxy.Proxy(lambda: dict.fromkeys(range(3), False)) |
|
1428
|
|
|
value[1] = True |
|
1429
|
|
|
|
|
1430
|
|
|
assert value[1] == True |
|
1431
|
|
|
|
|
1432
|
|
|
|
|
1433
|
|
|
def test_delitem(lazy_object_proxy): |
|
1434
|
|
|
value = lazy_object_proxy.Proxy(lambda: dict.fromkeys(range(3), False)) |
|
1435
|
|
|
|
|
1436
|
|
|
assert len(value) == 3 |
|
1437
|
|
|
|
|
1438
|
|
|
del value[1] |
|
1439
|
|
|
|
|
1440
|
|
|
assert len(value) == 2 |
|
1441
|
|
|
|
|
1442
|
|
|
|
|
1443
|
|
|
def test_str(lazy_object_proxy): |
|
1444
|
|
|
value = lazy_object_proxy.Proxy(lambda: 10) |
|
1445
|
|
|
|
|
1446
|
|
|
assert str(value) == str(10) |
|
1447
|
|
|
|
|
1448
|
|
|
value = lazy_object_proxy.Proxy(lambda: (10,)) |
|
1449
|
|
|
|
|
1450
|
|
|
assert str(value) == str((10,)) |
|
1451
|
|
|
|
|
1452
|
|
|
value = lazy_object_proxy.Proxy(lambda: [10]) |
|
1453
|
|
|
|
|
1454
|
|
|
assert str(value) == str([10]) |
|
1455
|
|
|
|
|
1456
|
|
|
value = lazy_object_proxy.Proxy(lambda: {10: 10}) |
|
1457
|
|
|
|
|
1458
|
|
|
assert str(value) == str({10: 10}) |
|
1459
|
|
|
|
|
1460
|
|
|
|
|
1461
|
|
|
def test_repr(lazy_object_proxy): |
|
1462
|
|
|
class Foobar: |
|
1463
|
|
|
pass |
|
1464
|
|
|
|
|
1465
|
|
|
value = lazy_object_proxy.Proxy(lambda: Foobar()) |
|
1466
|
|
|
str(value) |
|
1467
|
|
|
representation = repr(value) |
|
1468
|
|
|
print(representation) |
|
1469
|
|
|
assert 'Proxy at' in representation |
|
1470
|
|
|
assert 'lambda' in representation |
|
1471
|
|
|
assert 'Foobar' in representation |
|
1472
|
|
|
|
|
1473
|
|
|
|
|
1474
|
|
|
def test_repr_doesnt_consume(lazy_object_proxy): |
|
1475
|
|
|
consumed = [] |
|
1476
|
|
|
value = lazy_object_proxy.Proxy(lambda: consumed.append(1)) |
|
1477
|
|
|
print(repr(value)) |
|
1478
|
|
|
assert not consumed |
|
1479
|
|
|
|
|
1480
|
|
|
|
|
1481
|
|
|
def test_derived_new(lazy_object_proxy): |
|
1482
|
|
|
class DerivedObjectProxy(lazy_object_proxy.Proxy): |
|
1483
|
|
|
def __new__(cls, wrapped): |
|
1484
|
|
|
instance = super(DerivedObjectProxy, cls).__new__(cls) |
|
1485
|
|
|
instance.__init__(wrapped) |
|
1486
|
|
|
|
|
1487
|
|
|
def __init__(self, wrapped): |
|
1488
|
|
|
super(DerivedObjectProxy, self).__init__(wrapped) |
|
1489
|
|
|
|
|
1490
|
|
|
def function(): |
|
1491
|
|
|
pass |
|
1492
|
|
|
|
|
1493
|
|
|
obj = DerivedObjectProxy(lambda: function) |
|
1494
|
|
|
|
|
1495
|
|
|
|
|
1496
|
|
|
def test_setup_class_attributes(lazy_object_proxy): |
|
1497
|
|
|
def function(): |
|
1498
|
|
|
pass |
|
1499
|
|
|
|
|
1500
|
|
|
class DerivedObjectProxy(lazy_object_proxy.Proxy): |
|
1501
|
|
|
pass |
|
1502
|
|
|
|
|
1503
|
|
|
obj = DerivedObjectProxy(lambda: function) |
|
1504
|
|
|
|
|
1505
|
|
|
DerivedObjectProxy.ATTRIBUTE = 1 |
|
1506
|
|
|
|
|
1507
|
|
|
assert obj.ATTRIBUTE == 1 |
|
1508
|
|
|
assert not hasattr(function, 'ATTRIBUTE') |
|
1509
|
|
|
|
|
1510
|
|
|
del DerivedObjectProxy.ATTRIBUTE |
|
1511
|
|
|
|
|
1512
|
|
|
assert not hasattr(DerivedObjectProxy, 'ATTRIBUTE') |
|
1513
|
|
|
assert not hasattr(obj, 'ATTRIBUTE') |
|
1514
|
|
|
assert not hasattr(function, 'ATTRIBUTE') |
|
1515
|
|
|
|
|
1516
|
|
|
|
|
1517
|
|
|
def test_override_class_attributes(lazy_object_proxy): |
|
1518
|
|
|
def function(): |
|
1519
|
|
|
pass |
|
1520
|
|
|
|
|
1521
|
|
|
class DerivedObjectProxy(lazy_object_proxy.Proxy): |
|
1522
|
|
|
ATTRIBUTE = 1 |
|
1523
|
|
|
|
|
1524
|
|
|
obj = DerivedObjectProxy(lambda: function) |
|
1525
|
|
|
|
|
1526
|
|
|
assert DerivedObjectProxy.ATTRIBUTE == 1 |
|
1527
|
|
|
assert obj.ATTRIBUTE == 1 |
|
1528
|
|
|
|
|
1529
|
|
|
obj.ATTRIBUTE = 2 |
|
1530
|
|
|
|
|
1531
|
|
|
assert DerivedObjectProxy.ATTRIBUTE == 1 |
|
1532
|
|
|
|
|
1533
|
|
|
assert obj.ATTRIBUTE == 2 |
|
1534
|
|
|
assert not hasattr(function, 'ATTRIBUTE') |
|
1535
|
|
|
|
|
1536
|
|
|
del DerivedObjectProxy.ATTRIBUTE |
|
1537
|
|
|
|
|
1538
|
|
|
assert not hasattr(DerivedObjectProxy, 'ATTRIBUTE') |
|
1539
|
|
|
assert obj.ATTRIBUTE == 2 |
|
1540
|
|
|
assert not hasattr(function, 'ATTRIBUTE') |
|
1541
|
|
|
|
|
1542
|
|
|
|
|
1543
|
|
|
def test_attr_functions(lazy_object_proxy): |
|
1544
|
|
|
def function(): |
|
1545
|
|
|
pass |
|
1546
|
|
|
|
|
1547
|
|
|
proxy = lazy_object_proxy.Proxy(lambda: function) |
|
1548
|
|
|
|
|
1549
|
|
|
assert hasattr(proxy, '__getattr__') |
|
1550
|
|
|
assert hasattr(proxy, '__setattr__') |
|
1551
|
|
|
assert hasattr(proxy, '__delattr__') |
|
1552
|
|
|
|
|
1553
|
|
|
|
|
1554
|
|
|
def test_override_getattr(lazy_object_proxy): |
|
1555
|
|
|
def function(): |
|
1556
|
|
|
pass |
|
1557
|
|
|
|
|
1558
|
|
|
accessed = [] |
|
1559
|
|
|
|
|
1560
|
|
|
class DerivedObjectProxy(lazy_object_proxy.Proxy): |
|
1561
|
|
|
def __getattr__(self, name): |
|
1562
|
|
|
accessed.append(name) |
|
1563
|
|
|
try: |
|
1564
|
|
|
__getattr__ = super(DerivedObjectProxy, self).__getattr__ |
|
1565
|
|
|
except AttributeError as e: |
|
1566
|
|
|
raise RuntimeError(str(e)) |
|
1567
|
|
|
return __getattr__(name) |
|
1568
|
|
|
|
|
1569
|
|
|
function.attribute = 1 |
|
1570
|
|
|
|
|
1571
|
|
|
proxy = DerivedObjectProxy(lambda: function) |
|
1572
|
|
|
|
|
1573
|
|
|
assert proxy.attribute == 1 |
|
1574
|
|
|
|
|
1575
|
|
|
assert 'attribute' in accessed |
|
1576
|
|
|
|
|
1577
|
|
|
|
|
1578
|
|
|
skipcallable = pytest.mark.xfail( |
|
1579
|
|
|
reason="Don't know how to make this work. This tests the existence of the __call__ method.") |
|
1580
|
|
|
|
|
1581
|
|
|
|
|
1582
|
|
|
@skipcallable |
|
1583
|
|
|
def test_proxy_hasattr_call(lazy_object_proxy): |
|
1584
|
|
|
proxy = lazy_object_proxy.Proxy(lambda: None) |
|
1585
|
|
|
|
|
1586
|
|
|
assert not hasattr(proxy, '__call__') |
|
1587
|
|
|
|
|
1588
|
|
|
|
|
1589
|
|
|
@skipcallable |
|
1590
|
|
|
def test_proxy_getattr_call(lazy_object_proxy): |
|
1591
|
|
|
proxy = lazy_object_proxy.Proxy(lambda: None) |
|
1592
|
|
|
|
|
1593
|
|
|
assert getattr(proxy, '__call__', None) == None |
|
1594
|
|
|
|
|
1595
|
|
|
|
|
1596
|
|
|
@skipcallable |
|
1597
|
|
|
def test_proxy_is_callable(lazy_object_proxy): |
|
1598
|
|
|
proxy = lazy_object_proxy.Proxy(lambda: None) |
|
1599
|
|
|
|
|
1600
|
|
|
assert not callable(proxy) |
|
1601
|
|
|
|
|
1602
|
|
|
|
|
1603
|
|
|
def test_callable_proxy_hasattr_call(lazy_object_proxy): |
|
1604
|
|
|
proxy = lazy_object_proxy.Proxy(lambda: None) |
|
1605
|
|
|
|
|
1606
|
|
|
assert hasattr(proxy, '__call__') |
|
1607
|
|
|
|
|
1608
|
|
|
|
|
1609
|
|
|
@skipcallable |
|
1610
|
|
|
def test_callable_proxy_getattr_call(lazy_object_proxy): |
|
1611
|
|
|
proxy = lazy_object_proxy.Proxy(lambda: None) |
|
1612
|
|
|
|
|
1613
|
|
|
assert getattr(proxy, '__call__', None) is None |
|
1614
|
|
|
|
|
1615
|
|
|
|
|
1616
|
|
|
def test_callable_proxy_is_callable(lazy_object_proxy): |
|
1617
|
|
|
proxy = lazy_object_proxy.Proxy(lambda: None) |
|
1618
|
|
|
|
|
1619
|
|
|
assert callable(proxy) |
|
1620
|
|
|
|
|
1621
|
|
|
|
|
1622
|
|
|
def test_class_bytes(lazy_object_proxy): |
|
1623
|
|
|
if PY3: |
|
1624
|
|
|
class Class(object): |
|
1625
|
|
|
def __bytes__(self): |
|
1626
|
|
|
return b'BYTES' |
|
1627
|
|
|
|
|
1628
|
|
|
instance = Class() |
|
1629
|
|
|
|
|
1630
|
|
|
proxy = lazy_object_proxy.Proxy(lambda: instance) |
|
1631
|
|
|
|
|
1632
|
|
|
assert bytes(instance) == bytes(proxy) |
|
1633
|
|
|
|
|
1634
|
|
|
|
|
1635
|
|
|
def test_str_format(lazy_object_proxy): |
|
1636
|
|
|
instance = 'abcd' |
|
1637
|
|
|
|
|
1638
|
|
|
proxy = lazy_object_proxy.Proxy(lambda: instance) |
|
1639
|
|
|
|
|
1640
|
|
|
assert format(instance, ''), format(proxy == '') |
|
1641
|
|
|
|
|
1642
|
|
|
|
|
1643
|
|
|
def test_list_reversed(lazy_object_proxy): |
|
1644
|
|
|
instance = [1, 2] |
|
1645
|
|
|
|
|
1646
|
|
|
proxy = lazy_object_proxy.Proxy(lambda: instance) |
|
1647
|
|
|
|
|
1648
|
|
|
assert list(reversed(instance)) == list(reversed(proxy)) |
|
1649
|
|
|
|
|
1650
|
|
|
|
|
1651
|
|
|
def test_decimal_complex(lazy_object_proxy): |
|
1652
|
|
|
import decimal |
|
1653
|
|
|
|
|
1654
|
|
|
instance = decimal.Decimal(123) |
|
1655
|
|
|
|
|
1656
|
|
|
proxy = lazy_object_proxy.Proxy(lambda: instance) |
|
1657
|
|
|
|
|
1658
|
|
|
assert complex(instance) == complex(proxy) |
|
1659
|
|
|
|
|
1660
|
|
|
|
|
1661
|
|
|
def test_fractions_round(lazy_object_proxy): |
|
1662
|
|
|
import fractions |
|
1663
|
|
|
|
|
1664
|
|
|
instance = fractions.Fraction('1/2') |
|
1665
|
|
|
|
|
1666
|
|
|
proxy = lazy_object_proxy.Proxy(lambda: instance) |
|
1667
|
|
|
|
|
1668
|
|
|
assert round(instance) == round(proxy) |
|
1669
|
|
|
|
|
1670
|
|
|
|
|
1671
|
|
|
def test_readonly(lazy_object_proxy): |
|
1672
|
|
|
class Foo(object): |
|
1673
|
|
|
if PY2: |
|
1674
|
|
|
@property |
|
1675
|
|
|
def __qualname__(self): |
|
1676
|
|
|
return 'object' |
|
1677
|
|
|
|
|
1678
|
|
|
proxy = lazy_object_proxy.Proxy(lambda: Foo() if PY2 else object) |
|
1679
|
|
|
assert proxy.__qualname__ == 'object' |
|
1680
|
|
|
|
|
1681
|
|
|
|
|
1682
|
|
|
def test_del_wrapped(lazy_object_proxy): |
|
1683
|
|
|
foo = object() |
|
1684
|
|
|
called = [] |
|
1685
|
|
|
|
|
1686
|
|
|
def make_foo(): |
|
1687
|
|
|
called.append(1) |
|
1688
|
|
|
return foo |
|
1689
|
|
|
|
|
1690
|
|
|
proxy = lazy_object_proxy.Proxy(make_foo) |
|
1691
|
|
|
str(proxy) |
|
1692
|
|
|
assert called == [1] |
|
1693
|
|
|
assert proxy.__wrapped__ is foo |
|
1694
|
|
|
# print(type(proxy), hasattr(type(proxy), '__wrapped__')) |
|
1695
|
|
|
del proxy.__wrapped__ |
|
1696
|
|
|
str(proxy) |
|
1697
|
|
|
assert called == [1, 1] |
|
1698
|
|
|
|
|
1699
|
|
|
|
|
1700
|
|
|
def test_raise_attribute_error(lazy_object_proxy): |
|
1701
|
|
|
def foo(): |
|
1702
|
|
|
raise AttributeError("boom!") |
|
1703
|
|
|
|
|
1704
|
|
|
proxy = lazy_object_proxy.Proxy(foo) |
|
1705
|
|
|
pytest.raises(AttributeError, str, proxy) |
|
1706
|
|
|
pytest.raises(AttributeError, lambda: proxy.__wrapped__) |
|
1707
|
|
|
assert proxy.__factory__ is foo |
|
1708
|
|
|
|
|
1709
|
|
|
|
|
1710
|
|
|
def test_patching_the_factory(lazy_object_proxy): |
|
1711
|
|
|
def foo(): |
|
1712
|
|
|
raise AttributeError("boom!") |
|
1713
|
|
|
|
|
1714
|
|
|
proxy = lazy_object_proxy.Proxy(foo) |
|
1715
|
|
|
pytest.raises(AttributeError, lambda: proxy.__wrapped__) |
|
1716
|
|
|
assert proxy.__factory__ is foo |
|
1717
|
|
|
|
|
1718
|
|
|
proxy.__factory__ = lambda: foo |
|
1719
|
|
|
pytest.raises(AttributeError, proxy) |
|
1720
|
|
|
assert proxy.__wrapped__ is foo |
|
1721
|
|
|
|
|
1722
|
|
|
|
|
1723
|
|
|
def test_deleting_the_factory(lazy_object_proxy): |
|
1724
|
|
|
proxy = lazy_object_proxy.Proxy(None) |
|
1725
|
|
|
assert proxy.__factory__ is None |
|
1726
|
|
|
proxy.__factory__ = None |
|
1727
|
|
|
assert proxy.__factory__ is None |
|
1728
|
|
|
|
|
1729
|
|
|
pytest.raises(TypeError, str, proxy) |
|
1730
|
|
|
del proxy.__factory__ |
|
1731
|
|
|
pytest.raises(ValueError, str, proxy) |
|
1732
|
|
|
|
|
1733
|
|
|
|
|
1734
|
|
|
def test_patching_the_factory_with_none(lazy_object_proxy): |
|
1735
|
|
|
proxy = lazy_object_proxy.Proxy(None) |
|
1736
|
|
|
assert proxy.__factory__ is None |
|
1737
|
|
|
proxy.__factory__ = None |
|
1738
|
|
|
assert proxy.__factory__ is None |
|
1739
|
|
|
proxy.__factory__ = None |
|
1740
|
|
|
assert proxy.__factory__ is None |
|
1741
|
|
|
|
|
1742
|
|
|
def foo(): |
|
1743
|
|
|
return 1 |
|
1744
|
|
|
|
|
1745
|
|
|
proxy.__factory__ = foo |
|
1746
|
|
|
assert proxy.__factory__ is foo |
|
1747
|
|
|
assert proxy.__wrapped__ == 1 |
|
1748
|
|
|
assert str(proxy) == '1' |
|
1749
|
|
|
|
|
1750
|
|
|
|
|
1751
|
|
|
def test_new(lazy_object_proxy): |
|
1752
|
|
|
a = lazy_object_proxy.Proxy.__new__(lazy_object_proxy.Proxy) |
|
1753
|
|
|
b = lazy_object_proxy.Proxy.__new__(lazy_object_proxy.Proxy) |
|
1754
|
|
|
# NOW KISS |
|
1755
|
|
|
pytest.raises(ValueError, lambda: a + b) |
|
1756
|
|
|
# no segfault, yay |
|
1757
|
|
|
pytest.raises(ValueError, lambda: a.__wrapped__) |
|
1758
|
|
|
|
|
1759
|
|
|
|
|
1760
|
|
|
def test_set_wrapped_via_new(lazy_object_proxy): |
|
1761
|
|
|
obj = lazy_object_proxy.Proxy.__new__(lazy_object_proxy.Proxy) |
|
1762
|
|
|
obj.__wrapped__ = 1 |
|
1763
|
|
|
assert str(obj) == '1' |
|
1764
|
|
|
assert obj + 1 == 2 |
|
1765
|
|
|
|
|
1766
|
|
|
|
|
1767
|
|
|
def test_set_wrapped(lazy_object_proxy): |
|
1768
|
|
|
obj = lazy_object_proxy.Proxy(None) |
|
1769
|
|
|
obj.__wrapped__ = 1 |
|
1770
|
|
|
assert str(obj) == '1' |
|
1771
|
|
|
assert obj + 1 == 2 |
|
1772
|
|
|
|
|
1773
|
|
|
|
|
1774
|
|
|
@pytest.fixture(params=["pickle", "cPickle"]) |
|
1775
|
|
|
def pickler(request): |
|
1776
|
|
|
return pytest.importorskip(request.param) |
|
1777
|
|
|
|
|
1778
|
|
|
|
|
1779
|
|
|
@pytest.mark.parametrize("obj", [ |
|
1780
|
|
|
1, |
|
1781
|
|
|
1.2, |
|
1782
|
|
|
"a", |
|
1783
|
|
|
["b", "c"], |
|
1784
|
|
|
{"d": "e"}, |
|
1785
|
|
|
date(2015, 5, 1), |
|
1786
|
|
|
datetime(2015, 5, 1), |
|
1787
|
|
|
Decimal("1.2") |
|
1788
|
|
|
]) |
|
1789
|
|
|
@pytest.mark.parametrize("level", range(pickle.HIGHEST_PROTOCOL + 1)) |
|
1790
|
|
|
def test_pickling(lazy_object_proxy, obj, pickler, level): |
|
1791
|
|
|
proxy = lazy_object_proxy.Proxy(lambda: obj) |
|
1792
|
|
|
dump = pickler.dumps(proxy, protocol=level) |
|
1793
|
|
|
result = pickler.loads(dump) |
|
1794
|
|
|
assert obj == result |
|
1795
|
|
|
|
|
1796
|
|
|
|
|
1797
|
|
|
@pytest.mark.parametrize("level", range(pickle.HIGHEST_PROTOCOL + 1)) |
|
1798
|
|
|
def test_pickling_exception(lazy_object_proxy, pickler, level): |
|
1799
|
|
|
class BadStuff(Exception): |
|
1800
|
|
|
pass |
|
1801
|
|
|
|
|
1802
|
|
|
def trouble_maker(): |
|
1803
|
|
|
raise BadStuff("foo") |
|
1804
|
|
|
|
|
1805
|
|
|
proxy = lazy_object_proxy.Proxy(trouble_maker) |
|
1806
|
|
|
pytest.raises(BadStuff, pickler.dumps, proxy, protocol=level) |
|
1807
|
|
|
|
|
1808
|
|
|
|
|
1809
|
|
|
@pytest.mark.skipif(platform.python_implementation() != 'CPython', |
|
1810
|
|
|
reason="Interpreter doesn't have reference counting") |
|
1811
|
|
|
def test_garbage_collection(lazy_object_proxy): |
|
1812
|
|
|
leaky = lambda: "foobar" |
|
1813
|
|
|
proxy = lazy_object_proxy.Proxy(leaky) |
|
1814
|
|
|
leaky.leak = proxy |
|
1815
|
|
|
ref = weakref.ref(leaky) |
|
1816
|
|
|
assert proxy == "foobar" |
|
1817
|
|
|
del leaky |
|
1818
|
|
|
del proxy |
|
1819
|
|
|
gc.collect() |
|
1820
|
|
|
assert ref() is None |
|
1821
|
|
|
|
|
1822
|
|
|
|
|
1823
|
|
|
@pytest.mark.skipif(platform.python_implementation() != 'CPython', |
|
1824
|
|
|
reason="Interpreter doesn't have reference counting") |
|
1825
|
|
|
def test_garbage_collection_count(lazy_object_proxy): |
|
1826
|
|
|
obj = object() |
|
1827
|
|
|
count = sys.getrefcount(obj) |
|
1828
|
|
|
for _ in range(100): |
|
1829
|
|
|
str(lazy_object_proxy.Proxy(lambda: obj)) |
|
1830
|
|
|
assert count == sys.getrefcount(obj) |
|
1831
|
|
|
|
|
1832
|
|
|
|
|
1833
|
|
|
@pytest.mark.parametrize("name", ["slots", "cext", "simple", "django", "objproxies"]) |
|
1834
|
|
|
def test_perf(benchmark, name): |
|
1835
|
|
|
implementation = load_implementation(name) |
|
1836
|
|
|
obj = "foobar" |
|
1837
|
|
|
proxied = implementation.Proxy(lambda: obj) |
|
1838
|
|
|
assert benchmark(partial(str, proxied)) == obj |
|
1839
|
|
|
|
|
1840
|
|
|
|
|
1841
|
|
|
empty = object() |
|
1842
|
|
|
|
|
1843
|
|
|
|
|
1844
|
|
|
@pytest.fixture(scope="module", params=["SimpleProxy", "LocalsSimpleProxy", "CachedPropertyProxy", |
|
1845
|
|
|
"LocalsCachedPropertyProxy"]) |
|
1846
|
|
|
def prototype(request): |
|
1847
|
|
|
from lazy_object_proxy.simple import cached_property |
|
1848
|
|
|
name = request.param |
|
1849
|
|
|
|
|
1850
|
|
|
if name == "SimpleProxy": |
|
1851
|
|
|
class SimpleProxy(object): |
|
1852
|
|
|
def __init__(self, factory): |
|
1853
|
|
|
self.factory = factory |
|
1854
|
|
|
self.object = empty |
|
1855
|
|
|
|
|
1856
|
|
|
def __str__(self): |
|
1857
|
|
|
if self.object is empty: |
|
1858
|
|
|
self.object = self.factory() |
|
1859
|
|
|
return str(self.object) |
|
1860
|
|
|
|
|
1861
|
|
|
return SimpleProxy |
|
1862
|
|
|
elif name == "CachedPropertyProxy": |
|
1863
|
|
|
class CachedPropertyProxy(object): |
|
1864
|
|
|
def __init__(self, factory): |
|
1865
|
|
|
self.factory = factory |
|
1866
|
|
|
|
|
1867
|
|
|
@cached_property |
|
1868
|
|
|
def object(self): |
|
1869
|
|
|
return self.factory() |
|
1870
|
|
|
|
|
1871
|
|
|
def __str__(self): |
|
1872
|
|
|
return str(self.object) |
|
1873
|
|
|
|
|
1874
|
|
|
return CachedPropertyProxy |
|
1875
|
|
|
elif name == "LocalsSimpleProxy": |
|
1876
|
|
|
class LocalsSimpleProxy(object): |
|
1877
|
|
|
def __init__(self, factory): |
|
1878
|
|
|
self.factory = factory |
|
1879
|
|
|
self.object = empty |
|
1880
|
|
|
|
|
1881
|
|
|
def __str__(self, func=str): |
|
1882
|
|
|
if self.object is empty: |
|
1883
|
|
|
self.object = self.factory() |
|
1884
|
|
|
return func(self.object) |
|
1885
|
|
|
|
|
1886
|
|
|
return LocalsSimpleProxy |
|
1887
|
|
|
elif name == "LocalsCachedPropertyProxy": |
|
1888
|
|
|
class LocalsCachedPropertyProxy(object): |
|
1889
|
|
|
def __init__(self, factory): |
|
1890
|
|
|
self.factory = factory |
|
1891
|
|
|
|
|
1892
|
|
|
@cached_property |
|
1893
|
|
|
def object(self): |
|
1894
|
|
|
return self.factory() |
|
1895
|
|
|
|
|
1896
|
|
|
def __str__(self, func=str): |
|
1897
|
|
|
return func(self.object) |
|
1898
|
|
|
|
|
1899
|
|
|
return LocalsCachedPropertyProxy |
|
1900
|
|
|
|
|
1901
|
|
|
|
|
1902
|
|
|
@pytest.mark.benchmark(group="prototypes") |
|
1903
|
|
|
def test_proto(benchmark, prototype): |
|
1904
|
|
|
obj = "foobar" |
|
1905
|
|
|
proxied = prototype(lambda: obj) |
|
1906
|
|
|
assert benchmark(partial(str, proxied)) == obj |
|
1907
|
|
|
|
|
1908
|
|
|
|
|
1909
|
|
View Code Duplication |
def test_subclassing_with_local_attr(lazy_object_proxy): |
|
|
|
|
|
|
1910
|
|
|
class Foo: |
|
1911
|
|
|
pass |
|
1912
|
|
|
called = [] |
|
1913
|
|
|
|
|
1914
|
|
|
class LazyProxy(lazy_object_proxy.Proxy): |
|
1915
|
|
|
name = None |
|
1916
|
|
|
|
|
1917
|
|
|
def __init__(self, func, **lazy_attr): |
|
1918
|
|
|
super(LazyProxy, self).__init__(func) |
|
1919
|
|
|
for attr, val in lazy_attr.items(): |
|
1920
|
|
|
setattr(self, attr, val) |
|
1921
|
|
|
|
|
1922
|
|
|
proxy = LazyProxy(lambda: called.append(1) or Foo(), name='bar') |
|
1923
|
|
|
assert proxy.name == 'bar' |
|
1924
|
|
|
assert not called |
|
1925
|
|
|
|
|
1926
|
|
|
|
|
1927
|
|
View Code Duplication |
def test_subclassing_dynamic_with_local_attr(lazy_object_proxy): |
|
|
|
|
|
|
1928
|
|
|
if lazy_object_proxy.kind == 'cext': |
|
1929
|
|
|
pytest.skip("Not possible.") |
|
1930
|
|
|
|
|
1931
|
|
|
class Foo: |
|
1932
|
|
|
pass |
|
1933
|
|
|
|
|
1934
|
|
|
called = [] |
|
1935
|
|
|
|
|
1936
|
|
|
class LazyProxy(lazy_object_proxy.Proxy): |
|
1937
|
|
|
def __init__(self, func, **lazy_attr): |
|
1938
|
|
|
super(LazyProxy, self).__init__(func) |
|
1939
|
|
|
for attr, val in lazy_attr.items(): |
|
1940
|
|
|
object.__setattr__(self, attr, val) |
|
1941
|
|
|
|
|
1942
|
|
|
proxy = LazyProxy(lambda: called.append(1) or Foo(), name='bar') |
|
1943
|
|
|
assert proxy.name == 'bar' |
|
1944
|
|
|
assert not called |
|
1945
|
|
|
|
|
1946
|
|
|
|