mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-22 09:36:48 +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
|
from .providers cimport Provider
|
||||||
|
|
||||||
|
|
||||||
def _get_sync_patched(fn, patched: PatchedCallable):
|
def _sync_inject(object fn, tuple args, dict kwargs, dict injections, dict closings):
|
||||||
@functools.wraps(fn)
|
cdef object result
|
||||||
def _patched(*args, **kwargs):
|
cdef dict to_inject
|
||||||
cdef object result
|
cdef object arg_key
|
||||||
cdef dict to_inject
|
cdef Provider provider
|
||||||
cdef object arg_key
|
|
||||||
cdef Provider provider
|
|
||||||
|
|
||||||
to_inject = kwargs.copy()
|
to_inject = kwargs.copy()
|
||||||
for arg_key, provider in patched.injections.items():
|
for arg_key, provider in injections.items():
|
||||||
if arg_key not in kwargs or isinstance(kwargs[arg_key], _Marker):
|
if arg_key not in kwargs or isinstance(kwargs[arg_key], _Marker):
|
||||||
to_inject[arg_key] = provider()
|
to_inject[arg_key] = provider()
|
||||||
|
|
||||||
result = fn(*args, **to_inject)
|
result = fn(*args, **to_inject)
|
||||||
|
|
||||||
if patched.closing:
|
if closings:
|
||||||
for arg_key, provider in patched.closing.items():
|
for arg_key, provider in closings.items():
|
||||||
if arg_key in kwargs and not isinstance(kwargs[arg_key], _Marker):
|
if arg_key in kwargs and not isinstance(kwargs[arg_key], _Marker):
|
||||||
continue
|
continue
|
||||||
if not isinstance(provider, providers.Resource):
|
if not isinstance(provider, providers.Resource):
|
||||||
continue
|
continue
|
||||||
provider.shutdown()
|
provider.shutdown()
|
||||||
|
|
||||||
return result
|
return result
|
||||||
return _patched
|
|
||||||
|
|
||||||
|
|
||||||
async def _async_inject(object fn, tuple args, dict kwargs, dict injections, dict closings):
|
async def _async_inject(object fn, tuple args, dict kwargs, dict injections, dict closings):
|
||||||
|
|
|
@ -982,7 +982,7 @@ _inspect_filter = InspectFilter()
|
||||||
_loader = AutoLoader()
|
_loader = AutoLoader()
|
||||||
|
|
||||||
# Optimizations
|
# Optimizations
|
||||||
from ._cwiring import _get_sync_patched # noqa
|
from ._cwiring import _sync_inject # noqa
|
||||||
from ._cwiring import _async_inject # noqa
|
from ._cwiring import _async_inject # noqa
|
||||||
|
|
||||||
|
|
||||||
|
@ -999,3 +999,15 @@ def _get_async_patched(fn: F, patched: PatchedCallable) -> F:
|
||||||
patched.closing,
|
patched.closing,
|
||||||
)
|
)
|
||||||
return _patched
|
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