|
1 | 1 | import _pickle |
2 | 2 | import io |
3 | 3 | import sys |
| 4 | +from types import MappingProxyType, MethodType |
4 | 5 |
|
5 | 6 | from testutils import assert_raises |
6 | 7 |
|
|
35 | 36 | else: |
36 | 37 | expected = f"{name} exceeds system's maximum size of {sys.maxsize} bytes" |
37 | 38 | assert str(caught.exception) == expected |
| 39 | + |
| 40 | + |
| 41 | +# Reduction returns getattr from the active frame's builtins without calling it. |
| 42 | +class ReductionOwner: |
| 43 | + __slots__ = ("slot",) |
| 44 | + |
| 45 | + def method(self): |
| 46 | + pass |
| 47 | + |
| 48 | + |
| 49 | +owner = ReductionOwner() |
| 50 | +items = [] |
| 51 | +replacement_getattr = object() |
| 52 | +for descriptor, receiver, name in ( |
| 53 | + (object.__str__, object, "__str__"), |
| 54 | + (str.join, str, "join"), |
| 55 | + (io.BytesIO.getvalue, io.BytesIO, "getvalue"), |
| 56 | + (ReductionOwner.slot, ReductionOwner, "slot"), |
| 57 | + (owner.__str__, owner, "__str__"), |
| 58 | + (items.append, items, "append"), |
| 59 | + (dict.fromkeys, dict, "fromkeys"), |
| 60 | + (owner.method, owner, "method"), |
| 61 | +): |
| 62 | + for builtins_type in (dict, MappingProxyType): |
| 63 | + namespace = { |
| 64 | + "__builtins__": builtins_type({"getattr": replacement_getattr}), |
| 65 | + "descriptor": descriptor, |
| 66 | + } |
| 67 | + exec("reduced = descriptor.__reduce__()", namespace) |
| 68 | + assert namespace["reduced"] == (replacement_getattr, (receiver, name)) |
| 69 | + |
| 70 | + namespace["__builtins__"] = builtins_type({}) |
| 71 | + with assert_raises(AttributeError) as caught: |
| 72 | + exec("descriptor.__reduce__()", namespace) |
| 73 | + assert caught.exception.args == ("getattr",) |
| 74 | + |
| 75 | +# Module-bound functions reduce to a name without looking up getattr. |
| 76 | +namespace = {"__builtins__": {}, "function": len} |
| 77 | +exec("reduced = function.__reduce__()", namespace) |
| 78 | +assert namespace["reduced"] == "len" |
| 79 | + |
| 80 | + |
| 81 | +class MissingBuiltin(dict): |
| 82 | + def __getitem__(self, key): |
| 83 | + raise KeyError(key) |
| 84 | + |
| 85 | + def __missing__(self, key): |
| 86 | + raise AssertionError("must not retry __missing__ after custom __getitem__") |
| 87 | + |
| 88 | + |
| 89 | +for descriptor, name in ( |
| 90 | + (object.__str__, "getattr"), |
| 91 | + (iter([1]), "iter"), |
| 92 | + (reversed([1]), "reversed"), |
| 93 | +): |
| 94 | + namespace = {"__builtins__": MissingBuiltin(), "descriptor": descriptor} |
| 95 | + with assert_raises(AttributeError) as caught: |
| 96 | + exec("descriptor.__reduce__()", namespace) |
| 97 | + assert caught.exception.args == (name,) |
| 98 | + |
| 99 | + |
| 100 | +name_error = RuntimeError("function name first") |
| 101 | + |
| 102 | + |
| 103 | +class NamedCallable: |
| 104 | + def __call__(self, *args): |
| 105 | + pass |
| 106 | + |
| 107 | + @property |
| 108 | + def __name__(self): |
| 109 | + raise name_error |
| 110 | + |
| 111 | + |
| 112 | +namespace = { |
| 113 | + "__builtins__": {}, |
| 114 | + "method": MethodType(NamedCallable(), owner), |
| 115 | +} |
| 116 | +with assert_raises(RuntimeError) as caught: |
| 117 | + exec("method.__reduce__()", namespace) |
| 118 | +assert caught.exception is name_error |
0 commit comments