forked from doctaphred/phrecipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.py
654 lines (529 loc) Β· 16.5 KB
/
proxy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
from functools import partialmethod
special_method_names = (
# Basics
'__repr__',
'__str__',
'__bytes__',
'__format__',
'__lt__',
'__le__',
'__eq__',
'__ne__',
'__gt__',
'__ge__',
'__hash__',
'__bool__',
# Attribute access
# '__getattr__',
# '__getattribute__',
'__setattr__',
'__delattr__',
'__dir__',
# Descriptors
'__get__',
'__set__',
'__delete__',
# Callable objects
'__call__',
# Container types
'__len__', # Must return an int
'__length_hint__',
'__getitem__',
'__missing__',
'__setitem__',
'__delitem__',
'__iter__',
'__reversed__',
'__contains__', # `in` casts the return value to bool
'__neg__',
'__pos__',
'__abs__',
'__invert__',
# Must return values of the appropriate types
'__complex__',
'__int__',
'__float__',
'__round__',
'__index__', # Must return an int
'__enter__',
'__exit__',
)
binary_method_names = (
'__add__',
'__sub__',
'__mul__',
'__matmul__',
'__truediv__',
'__floordiv__',
'__divmod__',
'__mod__',
'__pow__',
'__lshift__',
'__rshift__',
'__and__',
'__or__',
'__xor__',
'__radd__',
'__rsub__',
'__rmul__',
'__rmatmul__',
'__rtruediv__',
'__rfloordiv__',
'__rdivmod__',
'__rmod__',
'__rpow__',
'__rlshift__',
'__rrshift__',
'__rand__',
'__ror__',
'__rxor__',
)
inplace_method_names = (
'__iadd__',
'__isub__',
'__imul__',
'__imatmul__',
'__itruediv__',
'__ifloordiv__',
'__imod__',
'__ipow__',
'__ilshift__',
'__irshift__',
'__iand__',
'__ior__',
'__ixor__',
)
class Proxy:
"""Transparently wrap an object with another object.
--------------------------------------------------------------------
Proxies behave just like the objects they wrap:
>>> x = Proxy({})
>>> x
{}
>>> x['a'] = 1
>>> x
{'a': 1}
>>> x.__str__()
"{'a': 1}"
>>> str(x)
"{'a': 1}"
>>> x['a'] = 2
>>> x
{'a': 2}
>>> del x['a']; x['b'] = 3; x
{'b': 3}
--------------------------------------------------------------------
Only the `type` function gives the Proxy away:
>>> Proxy({}).__class__
<class 'dict'>
>>> x.__doc__[:4]
'dict'
>>> type(Proxy({}))
<class 'proxy.Proxy'>
--------------------------------------------------------------------
Attribute assignment occurs on the wrapped object:
>>> f = lambda: None
>>> g = f
>>> g.attr = 2
>>> g.attr
2
>>> f.attr
2
>>> f = lambda: None
>>> p = Proxy(f)
>>> p.attr = 2
>>> p.attr
2
>>> f.attr
2
>>> f == p
True
>>> {}.attr = 2
Traceback (most recent call last):
...
AttributeError: 'dict' object has no attribute 'attr'
>>> Proxy({}).attr = 2
Traceback (most recent call last):
...
AttributeError: 'dict' object has no attribute 'attr'
--------------------------------------------------------------------
The Proxy class' own attributes are not directly accessible:
>>> Proxy({}).delegate_special
Traceback (most recent call last):
...
AttributeError: 'dict' object has no attribute 'delegate_special'
>>> Proxy({}).delegate_inplace
Traceback (most recent call last):
...
AttributeError: 'dict' object has no attribute 'delegate_inplace'
>>> type(Proxy({})).delegate_special # doctest: +ELLIPSIS
<function Proxy.delegate_special at ...>
>>> object.__getattribute__(Proxy({}), 'delegate_special')
<bound method Proxy.delegate_special of {}>
>>> object.__getattribute__(Proxy({}), 'ayyy_lmao')
Traceback (most recent call last):
...
AttributeError: 'Proxy' object has no attribute 'ayyy_lmao'
--------------------------------------------------------------------
If the wrapped object does not define an in-place operation (e.g.,
immutable types like int and str), the proxy substitutes the
non-in-place version and updates its wrapped object to the result:
>>> p = Proxy(1)
>>> type(p)
<class 'proxy.Proxy'>
>>> p.__class__
<class 'int'>
>>> p
1
>>> p += 1
>>> type(p)
<class 'proxy.Proxy'>
>>> p.__class__
<class 'int'>
>>> p
2
>>> p = Proxy({1})
>>> type(p)
<class 'proxy.Proxy'>
>>> p -= {1}
>>> type(p)
<class 'proxy.Proxy'>
>>> p.__class__
<class 'set'>
>>> p
set()
--------------------------------------------------------------------
Some exception messages may be altered, but the exception types
remain the same:
>>> set() + set()
Traceback (most recent call last):
...
TypeError: unsupported operand type(s) for +: 'set' and 'set'
>>> Proxy(set()) + set()
Traceback (most recent call last):
...
TypeError: unsupported operand type(s) for +: 'Proxy' and 'set'
>>> set() + Proxy(set)
Traceback (most recent call last):
...
TypeError: unsupported operand type(s) for +: 'set' and 'Proxy'
>>> s = set()
>>> s += set()
Traceback (most recent call last):
...
TypeError: unsupported operand type(s) for +=: 'set' and 'set'
>>> s += Proxy(set)
Traceback (most recent call last):
...
TypeError: unsupported operand type(s) for +=: 'set' and 'Proxy'
>>> p = Proxy(set())
>>> p += set()
Traceback (most recent call last):
...
TypeError: unsupported operand type(s) for +=: 'Proxy' and 'set'
>>> p += Proxy(set)
Traceback (most recent call last):
...
TypeError: unsupported operand type(s) for +=: 'Proxy' and 'Proxy'
--------------------------------------------------------------------
Special methods are looked up via `getattr(obj.__class__, name)`,
matching the behavior of the interpreter:
>>> class C: pass
>>> c = C()
>>> c.__len__ = lambda: 5
>>> c.__len__()
5
>>> len(c)
Traceback (most recent call last):
...
TypeError: object of type 'C' has no len()
>>> p = Proxy(c)
>>> p.__len__()
5
>>> len(p)
Traceback (most recent call last):
...
TypeError: type object 'C' has no attribute '__len__'
>>> f = lambda: None
>>> f.__eq__ = lambda self, other: True
>>> f == 2
False
"""
def __init__(self, obj):
super().__setattr__('wrapped_obj', obj)
def __getattribute__(self, name):
wrapped_obj = super().__getattribute__('wrapped_obj')
getattr_wrapper = super().__getattribute__('getattr_wrapper')
return getattr_wrapper(wrapped_obj, name)
def getattr_wrapper(self, wrapped_obj, name):
"""Override this method in subclasses to alter the proxy's behavior."""
return getattr(wrapped_obj, name)
# Certain implicit invocations of special methods may bypass
# __getattribute__: for example, `len(Proxy([]))` invokes
# Proxy.__len__ directly.
# Solution: make sure all accesses are properly forwarded by
# defining each special method on the Proxy class and invoking
# Proxy.__getattribute__ from within.
def delegate_special(self, name, *args, **kwargs):
"""Delegate the named special method to the wrapped object.
----------------------------------------------------------------
Binary special methods are looked up on the class of the object,
rather than the object itself:
>>> f = lambda: None
>>> f.__len__ = lambda: print('calling __len__')
>>> f.__len__()
calling __len__
>>> len(f)
Traceback (most recent call last):
...
TypeError: object of type 'function' has no len()
>>> p = Proxy(lambda: None)
>>> p.__len__ = lambda: print('calling __len__')
>>> p.__len__()
calling __len__
>>> len(p)
Traceback (most recent call last):
...
TypeError: type object 'function' has no attribute '__len__'
"""
wrapped_obj = super().__getattribute__('wrapped_obj')
getattr_wrapper = super().__getattribute__('getattr_wrapper')
try:
wrapped_method = getattr_wrapper(wrapped_obj.__class__, name)
except AttributeError as e:
raise TypeError(e)
else:
return wrapped_method(wrapped_obj, *args, **kwargs)
for name in special_method_names:
locals()[name] = partialmethod(delegate_special, name)
# Clean up temporary variable
# (NameError if done outside an empty loop)
del name
def delegate_binary(self, name, *args, **kwargs):
"""Delegate the named binary method, or return NotImplemented.
>>> p = Proxy([1])
>>> Proxy.delegate_binary(p, '__add__', [2])
[1, 2]
>>> Proxy.delegate_binary(p, '__sub__', [2])
NotImplemented
----------------------------------------------------------------
Binary special methods are looked up on the class of the object,
rather than the object itself:
>>> f = lambda: None
>>> f.__add__ = lambda value: print('adding', value)
>>> f.__add__(1)
adding 1
>>> f + 1
Traceback (most recent call last):
...
TypeError: unsupported operand type(s) for +: 'function' and 'int'
>>> p = Proxy(lambda: None)
>>> p.__add__ = lambda value: print('adding', value)
>>> p.__add__(1)
adding 1
>>> p + 1
Traceback (most recent call last):
...
TypeError: unsupported operand type(s) for +: 'Proxy' and 'int'
"""
wrapped_obj = super().__getattribute__('wrapped_obj')
getattr_wrapper = super().__getattribute__('getattr_wrapper')
try:
wrapped_method = getattr_wrapper(wrapped_obj.__class__, name)
except AttributeError:
return NotImplemented
else:
return wrapped_method(wrapped_obj, *args, **kwargs)
for name in binary_method_names:
locals()[name] = partialmethod(delegate_binary, name)
del name
def delegate_inplace(self, name, *args, **kwargs):
"""Delegate the named in-place method, or return NotImplemented.
If the wrapped object does not define an in-place version of the
method, fall back to the regular version and return a new Proxy
object wrapping the result, mimicking the behavior of ints and
other immutable objects.
>>> p = Proxy([])
>>> q = p
>>> p += [1]
>>> p
[1]
>>> q
[1]
>>> p is q
True
>>> p = Proxy(1)
>>> q = p
>>> p += 1
>>> p
2
>>> q
1
>>> p is q
False
----------------------------------------------------------------
In-place special methods are looked up on the class of the
object, rather than the object itself:
>>> f = lambda: None
>>> f.__iadd__ = lambda value: print('inplace-adding', value)
>>> f.__iadd__(1)
inplace-adding 1
>>> f += 1
Traceback (most recent call last):
...
TypeError: unsupported operand type(s) for +=: 'function' and 'int'
>>> p = Proxy(lambda: None)
>>> p.__iadd__ = lambda value: print('inplace-adding', value)
>>> p.__iadd__(1)
inplace-adding 1
>>> p += 1
Traceback (most recent call last):
...
TypeError: unsupported operand type(s) for +=: 'Proxy' and 'int'
----------------------------------------------------------------
Note that the in-place methods may return a value, even though
they may not be used as an expression unless invoked directly:
>>> x = [1].__iadd__([2])
>>> x
[1, 2]
>>> x = ([1] += [2])
Traceback (most recent call last):
...
x = ([1] += [2])
^
SyntaxError: invalid syntax
>>> p = Proxy([1])
>>> x = Proxy.delegate_inplace(p, '__iadd__', [2])
>>> x
[1, 2]
>>> p
[1, 2]
>>> x = (p += [2])
Traceback (most recent call last):
...
x = (p += [2])
^
SyntaxError: invalid syntax
"""
wrapped_obj = super().__getattribute__('wrapped_obj')
getattr_wrapper = super().__getattribute__('getattr_wrapper')
try:
wrapped_method = getattr_wrapper(wrapped_obj.__class__, name)
except AttributeError:
# If the in-place version was not found, fall back to the
# regular version and return a new Proxy object.
try:
fallback_name = name.replace('i', '', 1) # ZOMG HAX
wrapped_fallback_method = getattr_wrapper(
wrapped_obj.__class__, fallback_name)
except AttributeError:
# If the regular version was not found, return
# NotImplemented and let the interpreter sort it out.
return NotImplemented
else:
new_obj = wrapped_fallback_method(wrapped_obj, *args, **kwargs)
return type(self)(new_obj)
else:
# Otherwise, update this Proxy object's wrapped value.
# (Most objects will probably just return `self` from the
# in-place operation, but that is not a guarantee.)
new_obj = wrapped_method(wrapped_obj, *args, **kwargs)
super().__setattr__('wrapped_obj', new_obj)
return self
for name in inplace_method_names:
locals()[name] = partialmethod(delegate_inplace, name)
del name
class PrintingProxy(Proxy):
"""Test and demonstration class.
--------------------------------------------------------------------
>>> x = PrintingProxy({'a': 1})
>>> type(x)
<class 'proxy.PrintingProxy'>
>>> x.__class__
looking for __class__ on {'a': 1}
<class 'dict'>
>>> x.__doc__[:4]
looking for __doc__ on {'a': 1}
'dict'
>>> x
looking for __repr__ on <class 'dict'>
{'a': 1}
>>> x == 2
looking for __eq__ on <class 'dict'>
False
>>> 2 == x
looking for __eq__ on <class 'dict'>
False
>>> str(x)
looking for __str__ on <class 'dict'>
"{'a': 1}"
>>> x.__str__()
looking for __str__ on {'a': 1}
"{'a': 1}"
--------------------------------------------------------------------
>>> x['a'] = 2
looking for __setitem__ on <class 'dict'>
>>> x
looking for __repr__ on <class 'dict'>
{'a': 2}
>>> del x['a']
looking for __delitem__ on <class 'dict'>
>>> x['b'] = 3
looking for __setitem__ on <class 'dict'>
>>> x
looking for __repr__ on <class 'dict'>
{'b': 3}
>>> len(PrintingProxy([]))
looking for __len__ on <class 'list'>
0
>>> p1 = PrintingProxy(1)
>>> p1
looking for __repr__ on <class 'int'>
1
>>> p1 + 1
looking for __add__ on <class 'int'>
2
>>> 1 + p1
looking for __radd__ on <class 'int'>
2
>>> sum([p1, 1])
looking for __radd__ on <class 'int'>
2
>>> sum([1, p1])
looking for __radd__ on <class 'int'>
2
"""
def getattr_wrapper(self, wrapped_obj, name):
print('looking for', name, 'on', wrapped_obj)
return getattr(wrapped_obj, name)
class CallbackProxy:
"""Invoke a callback every time the proxy's value is accessed.
TODO: replace regular Proxy
LOL WUT:
>>> x = CallbackProxy(iter(range(10)).__next__)
>>> x
0
>>> x
1
>>> x
2
"""
def __init__(self, callback):
super().__setattr__('callback', callback)
def __getattribute__(self, name):
callback_result = super().__getattribute__('callback')()
return getattr(callback_result, name)
# Certain implicit invocations of special methods may bypass
# __getattribute__: for example, `len(Proxy([]))` invokes
# Proxy.__len__ directly.
# Solution: make sure all accesses are properly forwarded by
# defining each special method on the Proxy class and invoking
# Proxy.__getattribute__ from within.
def delegate(self, name, *args, **kwargs):
# I literally have no idea how this works anymore.
return getattr(self, name)(*args, **kwargs)
for name in special_method_names + inplace_method_names:
locals()[name] = partialmethod(delegate, name)
# Clean up temporary variable
# (NameError if done outside an empty loop)
del name