mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-25 11:04:01 +03:00
move back decorator into .py file to carry signature
This commit is contained in:
parent
cc2304e46e
commit
4af4685918
File diff suppressed because it is too large
Load Diff
|
@ -12,31 +12,28 @@ from .wiring import _Marker, PatchedCallable
|
|||
from .providers cimport Provider
|
||||
|
||||
|
||||
def _get_sync_patched(fn, patched: PatchedCallable):
|
||||
@functools.wraps(fn)
|
||||
def _patched(*args, **kwargs):
|
||||
cdef object result
|
||||
cdef dict to_inject
|
||||
cdef object arg_key
|
||||
cdef Provider provider
|
||||
def _sync_inject(object fn, tuple args, dict kwargs, dict injections, dict closings):
|
||||
cdef object result
|
||||
cdef dict to_inject
|
||||
cdef object arg_key
|
||||
cdef Provider provider
|
||||
|
||||
to_inject = kwargs.copy()
|
||||
for arg_key, provider in patched.injections.items():
|
||||
if arg_key not in kwargs or isinstance(kwargs[arg_key], _Marker):
|
||||
to_inject[arg_key] = provider()
|
||||
to_inject = kwargs.copy()
|
||||
for arg_key, provider in injections.items():
|
||||
if arg_key not in kwargs or isinstance(kwargs[arg_key], _Marker):
|
||||
to_inject[arg_key] = provider()
|
||||
|
||||
result = fn(*args, **to_inject)
|
||||
result = fn(*args, **to_inject)
|
||||
|
||||
if patched.closing:
|
||||
for arg_key, provider in patched.closing.items():
|
||||
if arg_key in kwargs and not isinstance(kwargs[arg_key], _Marker):
|
||||
continue
|
||||
if not isinstance(provider, providers.Resource):
|
||||
continue
|
||||
provider.shutdown()
|
||||
if closings:
|
||||
for arg_key, provider in closings.items():
|
||||
if arg_key in kwargs and not isinstance(kwargs[arg_key], _Marker):
|
||||
continue
|
||||
if not isinstance(provider, providers.Resource):
|
||||
continue
|
||||
provider.shutdown()
|
||||
|
||||
return result
|
||||
return _patched
|
||||
return result
|
||||
|
||||
|
||||
async def _async_inject(object fn, tuple args, dict kwargs, dict injections, dict closings):
|
||||
|
|
|
@ -982,7 +982,7 @@ _inspect_filter = InspectFilter()
|
|||
_loader = AutoLoader()
|
||||
|
||||
# Optimizations
|
||||
from ._cwiring import _get_sync_patched # noqa
|
||||
from ._cwiring import _sync_inject # noqa
|
||||
from ._cwiring import _async_inject # noqa
|
||||
|
||||
|
||||
|
@ -999,3 +999,15 @@ def _get_async_patched(fn: F, patched: PatchedCallable) -> F:
|
|||
patched.closing,
|
||||
)
|
||||
return _patched
|
||||
|
||||
def _get_sync_patched(fn: F, patched: PatchedCallable) -> F:
|
||||
@functools.wraps(fn)
|
||||
def _patched(*args, **kwargs):
|
||||
return _sync_inject(
|
||||
fn,
|
||||
args,
|
||||
kwargs,
|
||||
patched.injections,
|
||||
patched.closing,
|
||||
)
|
||||
return _patched
|
||||
|
|
Loading…
Reference in New Issue
Block a user