py2http 0.1.62__tar.gz → 0.1.63__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of py2http might be problematic. Click here for more details.
- {py2http-0.1.62 → py2http-0.1.63}/PKG-INFO +1 -1
- {py2http-0.1.62 → py2http-0.1.63}/py2http/service.py +153 -8
- py2http-0.1.63/py2http/tests/test_attr_dispatch.py +284 -0
- {py2http-0.1.62 → py2http-0.1.63}/py2http.egg-info/PKG-INFO +1 -1
- {py2http-0.1.62 → py2http-0.1.63}/py2http.egg-info/SOURCES.txt +1 -0
- {py2http-0.1.62 → py2http-0.1.63}/setup.cfg +1 -1
- {py2http-0.1.62 → py2http-0.1.63}/LICENSE +0 -0
- {py2http-0.1.62 → py2http-0.1.63}/README.md +0 -0
- {py2http-0.1.62 → py2http-0.1.63}/py2http/__init__.py +0 -0
- {py2http-0.1.62 → py2http-0.1.63}/py2http/bottle_plugins.py +0 -0
- {py2http-0.1.62 → py2http-0.1.63}/py2http/config.py +0 -0
- {py2http-0.1.62 → py2http-0.1.63}/py2http/constants.py +0 -0
- {py2http-0.1.62 → py2http-0.1.63}/py2http/decorators.py +0 -0
- {py2http-0.1.62 → py2http-0.1.63}/py2http/default_configs.py +0 -0
- {py2http-0.1.62 → py2http-0.1.63}/py2http/diagnosis.py +0 -0
- {py2http-0.1.62 → py2http-0.1.63}/py2http/examples/__init__.py +0 -0
- {py2http-0.1.62 → py2http-0.1.63}/py2http/examples/custom_input_mapper.py +0 -0
- {py2http-0.1.62 → py2http-0.1.63}/py2http/examples/custom_input_mappers.py +0 -0
- {py2http-0.1.62 → py2http-0.1.63}/py2http/examples/example_service.py +0 -0
- {py2http-0.1.62 → py2http-0.1.63}/py2http/examples/example_service_bottle.py +0 -0
- {py2http-0.1.62 → py2http-0.1.63}/py2http/examples/example_service_flask.py +0 -0
- {py2http-0.1.62 → py2http-0.1.63}/py2http/examples/object_dispatch.py +0 -0
- {py2http-0.1.62 → py2http-0.1.63}/py2http/middleware.py +0 -0
- {py2http-0.1.62 → py2http-0.1.63}/py2http/openapi_utils.py +0 -0
- {py2http-0.1.62 → py2http-0.1.63}/py2http/route_utils.py +0 -0
- {py2http-0.1.62 → py2http-0.1.63}/py2http/schema_tools.py +0 -0
- {py2http-0.1.62 → py2http-0.1.63}/py2http/tests/__init__.py +0 -0
- {py2http-0.1.62 → py2http-0.1.63}/py2http/tests/example_service_test.py +0 -0
- {py2http-0.1.62 → py2http-0.1.63}/py2http/tests/objects_for_testing.py +0 -0
- {py2http-0.1.62 → py2http-0.1.63}/py2http/tests/simple_run_process_test.py +0 -0
- {py2http-0.1.62 → py2http-0.1.63}/py2http/tests/test_binary_req.py +0 -0
- {py2http-0.1.62 → py2http-0.1.63}/py2http/tests/test_decorators.py +0 -0
- {py2http-0.1.62 → py2http-0.1.63}/py2http/tests/test_flask.py +0 -0
- {py2http-0.1.62 → py2http-0.1.63}/py2http/tests/test_not_set_defaults.py +0 -0
- {py2http-0.1.62 → py2http-0.1.63}/py2http/tests/test_p2h2p.py +0 -0
- {py2http-0.1.62 → py2http-0.1.63}/py2http/tests/utils_for_testing.py +0 -0
- {py2http-0.1.62 → py2http-0.1.63}/py2http/types.py +0 -0
- {py2http-0.1.62 → py2http-0.1.63}/py2http/util.py +0 -0
- {py2http-0.1.62 → py2http-0.1.63}/py2http.egg-info/dependency_links.txt +0 -0
- {py2http-0.1.62 → py2http-0.1.63}/py2http.egg-info/not-zip-safe +0 -0
- {py2http-0.1.62 → py2http-0.1.63}/py2http.egg-info/requires.txt +0 -0
- {py2http-0.1.62 → py2http-0.1.63}/py2http.egg-info/top_level.txt +0 -0
- {py2http-0.1.62 → py2http-0.1.63}/setup.py +0 -0
|
@@ -16,6 +16,9 @@ Python functions, enabling developers to easily expose their functions as remote
|
|
|
16
16
|
with minimal setup."""
|
|
17
17
|
|
|
18
18
|
import inspect
|
|
19
|
+
from collections import OrderedDict
|
|
20
|
+
from collections.abc import MutableMapping
|
|
21
|
+
from types import ModuleType
|
|
19
22
|
from uuid import uuid4
|
|
20
23
|
from aiohttp import web
|
|
21
24
|
import asyncio
|
|
@@ -53,7 +56,146 @@ from py2http.schema_tools import (
|
|
|
53
56
|
from py2http.util import TypeAsserter
|
|
54
57
|
from py2http.constants import JSON_CONTENT_TYPE
|
|
55
58
|
|
|
56
|
-
|
|
59
|
+
# Maximum number of objects a class endpoint keeps alive; the least recently used
|
|
60
|
+
# one is evicted beyond that. Pass ``obj_store=...`` in a handler to change it.
|
|
61
|
+
# Note: this bounds the number of objects, not their size, and object ids are the
|
|
62
|
+
# only access control (any client holding an id can use the object).
|
|
63
|
+
DFLT_OBJ_STORE_MAX_SIZE = 1000
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
class LRUObjStore(MutableMapping):
|
|
67
|
+
"""A mapping that keeps at most ``max_size`` items, evicting the least recently
|
|
68
|
+
used one when a new key would exceed it.
|
|
69
|
+
|
|
70
|
+
Used as the default store of objects created through a class endpoint, so that
|
|
71
|
+
clients cannot grow server memory without bound by creating objects.
|
|
72
|
+
|
|
73
|
+
>>> s = LRUObjStore(max_size=2)
|
|
74
|
+
>>> s['a'], s['b'] = 1, 2
|
|
75
|
+
>>> _ = s['a'] # 'a' is now the most recently used
|
|
76
|
+
>>> s['c'] = 3
|
|
77
|
+
>>> sorted(s)
|
|
78
|
+
['a', 'c']
|
|
79
|
+
"""
|
|
80
|
+
|
|
81
|
+
def __init__(self, *, max_size: int = DFLT_OBJ_STORE_MAX_SIZE):
|
|
82
|
+
if max_size < 1:
|
|
83
|
+
raise ValueError(f"max_size must be at least 1, got {max_size}")
|
|
84
|
+
self.max_size = max_size
|
|
85
|
+
self._data = OrderedDict()
|
|
86
|
+
|
|
87
|
+
def __getitem__(self, k):
|
|
88
|
+
v = self._data[k]
|
|
89
|
+
self._data.move_to_end(k)
|
|
90
|
+
return v
|
|
91
|
+
|
|
92
|
+
def __setitem__(self, k, v):
|
|
93
|
+
self._data[k] = v
|
|
94
|
+
self._data.move_to_end(k)
|
|
95
|
+
while len(self._data) > self.max_size:
|
|
96
|
+
self._data.popitem(last=False)
|
|
97
|
+
|
|
98
|
+
def __delitem__(self, k):
|
|
99
|
+
del self._data[k]
|
|
100
|
+
|
|
101
|
+
def __iter__(self):
|
|
102
|
+
return iter(self._data)
|
|
103
|
+
|
|
104
|
+
def __len__(self):
|
|
105
|
+
return len(self._data)
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def public_attr(obj, attr_name: str) -> bool:
|
|
109
|
+
"""The attribute policy ``attr_names='*'`` stands for: only public methods.
|
|
110
|
+
|
|
111
|
+
A name is dispatchable if it does not start with ``_`` and
|
|
112
|
+
|
|
113
|
+
- for a module: it is a function that the module defines (or lists in its
|
|
114
|
+
``__all__``), never something it merely imports, nor plain data;
|
|
115
|
+
- otherwise: it is a plain Python function, ``staticmethod``,
|
|
116
|
+
``classmethod`` or ``property`` defined in the object's class (or a base
|
|
117
|
+
class), and the instance does not shadow it.
|
|
118
|
+
|
|
119
|
+
This keeps private helpers, dunders (``__dict__``, ``__init__``, ...),
|
|
120
|
+
per-instance state (``__dict__`` entries, ``__slots__``, dataclass and
|
|
121
|
+
namedtuple fields) and callables merely assigned as class attributes
|
|
122
|
+
(``opener = open``) out of reach of HTTP clients.
|
|
123
|
+
|
|
124
|
+
Note that every public method is exposed, inherited ones included (e.g. a
|
|
125
|
+
pydantic model's ``model_dump``). For anything holding secrets, or a mapping
|
|
126
|
+
whose mutators should stay private (``MutableMapping`` mixins such as
|
|
127
|
+
``clear`` and ``pop`` are plain functions), list the names explicitly instead.
|
|
128
|
+
|
|
129
|
+
>>> class A:
|
|
130
|
+
... data = 0
|
|
131
|
+
... opener = open
|
|
132
|
+
... def f(self): ...
|
|
133
|
+
... def _g(self): ...
|
|
134
|
+
... def __init__(self): self.secret = 1
|
|
135
|
+
>>> [public_attr(A(), name)
|
|
136
|
+
... for name in ('f', '_g', '__init__', '__dict__', 'secret', 'data', 'opener')]
|
|
137
|
+
[True, False, False, False, False, False, False]
|
|
138
|
+
"""
|
|
139
|
+
if not isinstance(attr_name, str) or attr_name.startswith("_"):
|
|
140
|
+
return False
|
|
141
|
+
if isinstance(obj, ModuleType):
|
|
142
|
+
return _is_public_module_function(obj, attr_name)
|
|
143
|
+
try: # (bypassing any class-level ``__dict__`` attribute)
|
|
144
|
+
instance_dict = object.__getattribute__(obj, "__dict__")
|
|
145
|
+
except AttributeError:
|
|
146
|
+
instance_dict = {}
|
|
147
|
+
if attr_name in instance_dict:
|
|
148
|
+
return False # instance state, or an instance attribute shadowing a method
|
|
149
|
+
# Look in the class's MRO dicts only (not the metaclass: ``mro``, ...), and at
|
|
150
|
+
# the raw class attribute, not what the instance would return for it
|
|
151
|
+
for klass in type(obj).__mro__:
|
|
152
|
+
if attr_name in vars(klass):
|
|
153
|
+
return _is_method_like(vars(klass)[attr_name])
|
|
154
|
+
return False
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def _is_method_like(class_attr) -> bool:
|
|
158
|
+
if isinstance(class_attr, (staticmethod, classmethod)):
|
|
159
|
+
class_attr = class_attr.__func__
|
|
160
|
+
return isinstance(class_attr, (FunctionType, property))
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def _is_public_module_function(module: ModuleType, attr_name: str) -> bool:
|
|
164
|
+
value = vars(module).get(attr_name)
|
|
165
|
+
if not inspect.isfunction(value):
|
|
166
|
+
return False
|
|
167
|
+
exported = getattr(module, "__all__", None)
|
|
168
|
+
if exported is not None:
|
|
169
|
+
return attr_name in exported
|
|
170
|
+
return value.__module__ == module.__name__
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
def unsafe_any_attr(obj, attr_name: str) -> bool:
|
|
174
|
+
"""Dispatch ANY attribute of the object, including private ones and dunders.
|
|
175
|
+
|
|
176
|
+
This was the behaviour of ``attr_names='*'`` before ``public_attr``: it lets a
|
|
177
|
+
client read ``__dict__``, call ``__init__`` with arguments of its choosing, and so
|
|
178
|
+
on. Use ``attr_names=unsafe_any_attr`` only when every client is trusted.
|
|
179
|
+
"""
|
|
180
|
+
return isinstance(attr_name, str) and hasattr(obj, attr_name)
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def _attr_name_policy(attr_names) -> Callable[[Any, str], bool]:
|
|
184
|
+
"""Normalize a handler's ``attr_names`` into a ``(obj, attr_name) -> bool``
|
|
185
|
+
predicate: ``'*'`` means ``public_attr``, a callable is used as is, and an
|
|
186
|
+
iterable of names is an explicit allow-list (dunders included, if listed).
|
|
187
|
+
"""
|
|
188
|
+
if attr_names == "*":
|
|
189
|
+
return public_attr
|
|
190
|
+
if callable(attr_names):
|
|
191
|
+
return attr_names
|
|
192
|
+
if isinstance(attr_names, str):
|
|
193
|
+
allowed = {attr_names}
|
|
194
|
+
else:
|
|
195
|
+
allowed = set(attr_names)
|
|
196
|
+
return lambda obj, attr_name: (
|
|
197
|
+
isinstance(attr_name, str) and attr_name in allowed and hasattr(obj, attr_name)
|
|
198
|
+
)
|
|
57
199
|
|
|
58
200
|
|
|
59
201
|
def method_not_found(method_name):
|
|
@@ -617,8 +759,12 @@ def _get_func_to_dispatch_handler(handler):
|
|
|
617
759
|
return endpoint
|
|
618
760
|
# was return endpoint, but then the output didn't get the __name__ = name
|
|
619
761
|
# func = endpoint
|
|
762
|
+
is_dispatchable = _attr_name_policy(attr_names)
|
|
620
763
|
if inspect.isclass(endpoint):
|
|
621
764
|
cls = endpoint
|
|
765
|
+
obj_store = handler.get("obj_store")
|
|
766
|
+
if obj_store is None:
|
|
767
|
+
obj_store = LRUObjStore()
|
|
622
768
|
|
|
623
769
|
def func(_obj_id=None, _attr_name=None, **kwargs):
|
|
624
770
|
if _obj_id is None:
|
|
@@ -628,25 +774,24 @@ def _get_func_to_dispatch_handler(handler):
|
|
|
628
774
|
new_id = str(uuid4())
|
|
629
775
|
obj_store[new_id] = new_obj
|
|
630
776
|
return new_id
|
|
631
|
-
obj = obj_store.get(_obj_id)
|
|
632
|
-
if
|
|
777
|
+
obj = obj_store.get(_obj_id) if isinstance(_obj_id, str) else None
|
|
778
|
+
if obj is None:
|
|
633
779
|
raise InputError(f"No object found with id {_obj_id}")
|
|
634
780
|
if _attr_name is None:
|
|
635
781
|
raise InputError("_attr_name must be provided when _obj_id is not None")
|
|
636
|
-
return _get_attr_value(obj, _attr_name,
|
|
782
|
+
return _get_attr_value(obj, _attr_name, is_dispatchable, **kwargs)
|
|
637
783
|
|
|
638
784
|
else:
|
|
639
785
|
|
|
640
786
|
def func(_attr_name=None, **kwargs):
|
|
641
|
-
return _get_attr_value(endpoint, _attr_name,
|
|
787
|
+
return _get_attr_value(endpoint, _attr_name, is_dispatchable, **kwargs)
|
|
642
788
|
|
|
643
789
|
func.__name__ = name
|
|
644
790
|
return func
|
|
645
791
|
|
|
646
792
|
|
|
647
|
-
def _get_attr_value(obj, attr_name,
|
|
648
|
-
|
|
649
|
-
if attr_name not in valid_attr_names or not hasattr(obj, attr_name):
|
|
793
|
+
def _get_attr_value(obj, attr_name, is_dispatchable, /, **kwargs):
|
|
794
|
+
if not is_dispatchable(obj, attr_name):
|
|
650
795
|
raise InputError(
|
|
651
796
|
f"No attribute found with name {attr_name} or it is not dispatched"
|
|
652
797
|
)
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
"""Object/attribute dispatch (``attr_names``) only exposes what was meant to be exposed."""
|
|
2
|
+
|
|
3
|
+
import pytest
|
|
4
|
+
from i2.errors import InputError
|
|
5
|
+
|
|
6
|
+
from py2http.service import (
|
|
7
|
+
LRUObjStore,
|
|
8
|
+
_get_func_to_dispatch_handler,
|
|
9
|
+
public_attr,
|
|
10
|
+
unsafe_any_attr,
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class Counter:
|
|
15
|
+
def __init__(self, start: int = 0):
|
|
16
|
+
self.count = start
|
|
17
|
+
self._secret = "s3cr3t"
|
|
18
|
+
|
|
19
|
+
def incr(self, by: int = 1):
|
|
20
|
+
self.count += by
|
|
21
|
+
return self.count
|
|
22
|
+
|
|
23
|
+
def _reset(self):
|
|
24
|
+
self.count = 0
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def _dispatcher(endpoint, attr_names, **handler_extras):
|
|
28
|
+
return _get_func_to_dispatch_handler(
|
|
29
|
+
dict(endpoint=endpoint, name="ep", attr_names=attr_names, **handler_extras)
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
@pytest.mark.parametrize(
|
|
34
|
+
"attr_name", ["_reset", "_secret", "__dict__", "__init__", "__class__", "count"]
|
|
35
|
+
)
|
|
36
|
+
def test_star_refuses_private_dunder_and_instance_attrs(attr_name):
|
|
37
|
+
obj = Counter(5)
|
|
38
|
+
func = _dispatcher(obj, "*")
|
|
39
|
+
with pytest.raises(InputError):
|
|
40
|
+
func(_attr_name=attr_name)
|
|
41
|
+
assert obj.count == 5
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def test_star_allows_public_methods():
|
|
45
|
+
func = _dispatcher(Counter(5), "*")
|
|
46
|
+
assert func(_attr_name="incr", by=2) == 7
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def test_star_on_class_endpoint():
|
|
50
|
+
func = _dispatcher(Counter, "*")
|
|
51
|
+
obj_id = func(start=3)
|
|
52
|
+
assert func(_obj_id=obj_id, _attr_name="incr") == 4
|
|
53
|
+
for attr_name in ("__init__", "__dict__", "_secret", "mro"):
|
|
54
|
+
with pytest.raises(InputError):
|
|
55
|
+
func(_obj_id=obj_id, _attr_name=attr_name)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def test_explicit_list_unaffected():
|
|
59
|
+
"""An explicit allow-list may name dunders on purpose (see examples/object_dispatch)."""
|
|
60
|
+
|
|
61
|
+
class Store(dict):
|
|
62
|
+
def __getitem__(self, key):
|
|
63
|
+
return super().__getitem__(key)
|
|
64
|
+
|
|
65
|
+
d = Store(a=1)
|
|
66
|
+
func = _dispatcher(d, ["__getitem__", "__len__"])
|
|
67
|
+
assert func(_attr_name="__getitem__", key="a") == 1
|
|
68
|
+
assert func(_attr_name="__len__") == 1
|
|
69
|
+
with pytest.raises(InputError):
|
|
70
|
+
func(_attr_name="clear")
|
|
71
|
+
with pytest.raises(InputError):
|
|
72
|
+
func(_attr_name="__dict__")
|
|
73
|
+
assert d == {"a": 1}
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def test_unsafe_any_attr_opt_in_restores_old_behaviour():
|
|
77
|
+
obj = Counter(5)
|
|
78
|
+
func = _dispatcher(obj, unsafe_any_attr)
|
|
79
|
+
func(_attr_name="_reset")
|
|
80
|
+
assert obj.count == 0
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def test_custom_predicate():
|
|
84
|
+
func = _dispatcher(Counter(1), lambda obj, name: name == "incr")
|
|
85
|
+
assert func(_attr_name="incr") == 2
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def test_public_attr_on_module():
|
|
89
|
+
import json
|
|
90
|
+
|
|
91
|
+
assert public_attr(json, "dumps")
|
|
92
|
+
assert not public_attr(json, "_default_encoder")
|
|
93
|
+
assert not public_attr(json, "__loader__")
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def test_obj_store_is_bounded():
|
|
97
|
+
store = LRUObjStore(max_size=2)
|
|
98
|
+
func = _dispatcher(Counter, "*", obj_store=store)
|
|
99
|
+
ids = [func(start=i) for i in range(3)]
|
|
100
|
+
assert len(store) == 2
|
|
101
|
+
with pytest.raises(InputError, match="No object found"):
|
|
102
|
+
func(_obj_id=ids[0], _attr_name="incr")
|
|
103
|
+
assert func(_obj_id=ids[2], _attr_name="incr") == 3
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def test_obj_store_default_is_bounded_lru():
|
|
107
|
+
func = _dispatcher(Counter, "*")
|
|
108
|
+
obj_id = func()
|
|
109
|
+
assert func(_obj_id=obj_id, _attr_name="incr") == 1
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def test_falsy_object_is_still_found():
|
|
113
|
+
func = _dispatcher(list, ["append", "__len__"])
|
|
114
|
+
obj_id = func() # an empty list is falsy
|
|
115
|
+
assert func(_obj_id=obj_id, _attr_name="__len__") == 0
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def test_lru_obj_store_rejects_bad_size():
|
|
119
|
+
with pytest.raises(ValueError):
|
|
120
|
+
LRUObjStore(max_size=0)
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
# --- shapes found by the adversarial review: instance state must stay private ---
|
|
124
|
+
|
|
125
|
+
from dataclasses import dataclass
|
|
126
|
+
from types import ModuleType
|
|
127
|
+
from typing import NamedTuple
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
class Slotted:
|
|
131
|
+
__slots__ = ("token",)
|
|
132
|
+
|
|
133
|
+
def __init__(self):
|
|
134
|
+
self.token = "TOK"
|
|
135
|
+
|
|
136
|
+
def ping(self):
|
|
137
|
+
return "pong"
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
@dataclass
|
|
141
|
+
class DC:
|
|
142
|
+
token: str = "default"
|
|
143
|
+
|
|
144
|
+
def ping(self):
|
|
145
|
+
return "pong"
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
class NT(NamedTuple):
|
|
149
|
+
token: str
|
|
150
|
+
|
|
151
|
+
def ping(self):
|
|
152
|
+
return "pong"
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
class Odd:
|
|
156
|
+
count = 0
|
|
157
|
+
opener = open
|
|
158
|
+
Nested = dict
|
|
159
|
+
|
|
160
|
+
def __init__(self):
|
|
161
|
+
self.count = 42
|
|
162
|
+
self.incr = lambda: "shadowed"
|
|
163
|
+
|
|
164
|
+
def incr(self):
|
|
165
|
+
return "real"
|
|
166
|
+
|
|
167
|
+
@staticmethod
|
|
168
|
+
def s():
|
|
169
|
+
return "static"
|
|
170
|
+
|
|
171
|
+
@classmethod
|
|
172
|
+
def c(cls):
|
|
173
|
+
return "class"
|
|
174
|
+
|
|
175
|
+
@property
|
|
176
|
+
def p(self):
|
|
177
|
+
return "prop"
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
@pytest.mark.parametrize(
|
|
181
|
+
"obj", [Slotted(), DC(token="TOK"), NT(token="TOK")], ids=["slots", "dc", "nt"]
|
|
182
|
+
)
|
|
183
|
+
def test_star_refuses_fields(obj):
|
|
184
|
+
func = _dispatcher(obj, "*")
|
|
185
|
+
with pytest.raises(InputError):
|
|
186
|
+
func(_attr_name="token")
|
|
187
|
+
assert func(_attr_name="ping") == "pong"
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
@pytest.mark.parametrize("attr_name", ["count", "opener", "Nested", "incr"])
|
|
191
|
+
def test_star_refuses_data_shadowed_and_assigned_callables(attr_name):
|
|
192
|
+
func = _dispatcher(Odd(), "*")
|
|
193
|
+
with pytest.raises(InputError):
|
|
194
|
+
func(_attr_name=attr_name)
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
def test_star_allows_static_class_methods_and_properties():
|
|
198
|
+
func = _dispatcher(Odd(), "*")
|
|
199
|
+
assert [func(_attr_name=n) for n in ("s", "c", "p")] == ["static", "class", "prop"]
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def test_star_refuses_builtin_mapping_mutators():
|
|
203
|
+
class Store(dict):
|
|
204
|
+
def keys_list(self):
|
|
205
|
+
return list(self)
|
|
206
|
+
|
|
207
|
+
d = Store(a=1)
|
|
208
|
+
func = _dispatcher(d, "*")
|
|
209
|
+
with pytest.raises(InputError):
|
|
210
|
+
func(_attr_name="clear")
|
|
211
|
+
assert func(_attr_name="keys_list") == ["a"] and d == {"a": 1}
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
def _mk_module(name, *, all_=None):
|
|
215
|
+
mod = ModuleType(name)
|
|
216
|
+
exec(
|
|
217
|
+
"from os import listdir\nfrom json import dumps\nimport os\nAPI_KEY = 'k-123'\n"
|
|
218
|
+
"def hello():\n return 'hi'\n",
|
|
219
|
+
vars(mod),
|
|
220
|
+
)
|
|
221
|
+
if all_ is not None:
|
|
222
|
+
mod.__all__ = all_
|
|
223
|
+
return mod
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
@pytest.mark.parametrize("attr_name", ["listdir", "dumps", "os", "API_KEY"])
|
|
227
|
+
def test_star_on_module_refuses_imports_and_data(attr_name):
|
|
228
|
+
func = _dispatcher(_mk_module("some_service"), "*")
|
|
229
|
+
with pytest.raises(InputError):
|
|
230
|
+
func(_attr_name=attr_name)
|
|
231
|
+
assert func(_attr_name="hello") == "hi"
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
def test_star_on_module_honours_all():
|
|
235
|
+
mod = _mk_module("some_service", all_=["dumps", "listdir"])
|
|
236
|
+
assert public_attr(mod, "dumps") # a re-export listed in __all__
|
|
237
|
+
assert not public_attr(mod, "listdir") # listed, but not a Python function
|
|
238
|
+
assert not public_attr(mod, "hello") # not listed
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
@pytest.mark.parametrize("bad", [["x"], {"a": 1}, 3, None])
|
|
242
|
+
def test_non_str_names_and_ids_are_input_errors(bad):
|
|
243
|
+
func = _dispatcher({"x": 1}, ["x", "__len__"])
|
|
244
|
+
if bad is not None:
|
|
245
|
+
with pytest.raises(InputError):
|
|
246
|
+
func(_attr_name=bad)
|
|
247
|
+
cls_func = _dispatcher(Counter, "*")
|
|
248
|
+
with pytest.raises(InputError):
|
|
249
|
+
cls_func(_obj_id=bad if bad is not None else [], _attr_name="incr")
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
def test_method_params_named_like_dispatch_internals():
|
|
253
|
+
class A:
|
|
254
|
+
def f(self, obj, attr_name, is_dispatchable):
|
|
255
|
+
return [obj, attr_name, is_dispatchable]
|
|
256
|
+
|
|
257
|
+
func = _dispatcher(A(), "*")
|
|
258
|
+
assert func(_attr_name="f", obj=1, attr_name=2, is_dispatchable=3) == [1, 2, 3]
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
def test_star_exposes_mutable_mapping_mixins_as_documented():
|
|
262
|
+
"""Inherited public Python methods ARE dispatched: use an explicit list for stores."""
|
|
263
|
+
from collections.abc import MutableMapping
|
|
264
|
+
|
|
265
|
+
class MM(MutableMapping):
|
|
266
|
+
def __init__(self):
|
|
267
|
+
self._d = {"a": 1}
|
|
268
|
+
|
|
269
|
+
def __getitem__(self, k):
|
|
270
|
+
return self._d[k]
|
|
271
|
+
|
|
272
|
+
def __setitem__(self, k, v):
|
|
273
|
+
self._d[k] = v
|
|
274
|
+
|
|
275
|
+
def __delitem__(self, k):
|
|
276
|
+
del self._d[k]
|
|
277
|
+
|
|
278
|
+
def __iter__(self):
|
|
279
|
+
return iter(self._d)
|
|
280
|
+
|
|
281
|
+
def __len__(self):
|
|
282
|
+
return len(self._d)
|
|
283
|
+
|
|
284
|
+
assert public_attr(MM(), "clear")
|
|
@@ -33,6 +33,7 @@ py2http/tests/__init__.py
|
|
|
33
33
|
py2http/tests/example_service_test.py
|
|
34
34
|
py2http/tests/objects_for_testing.py
|
|
35
35
|
py2http/tests/simple_run_process_test.py
|
|
36
|
+
py2http/tests/test_attr_dispatch.py
|
|
36
37
|
py2http/tests/test_binary_req.py
|
|
37
38
|
py2http/tests/test_decorators.py
|
|
38
39
|
py2http/tests/test_flask.py
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|