Create an appropriate wrapper for the given function, `func`. The wrapper handles both sync and async functions, and respects whether the function expects to receive event arguments.
(func)
| 196 | |
| 197 | |
| 198 | def _create_wrapper(func): |
| 199 | """ |
| 200 | Create an appropriate wrapper for the given function, `func`. |
| 201 | |
| 202 | The wrapper handles both sync and async functions, and respects whether |
| 203 | the function expects to receive event arguments. |
| 204 | """ |
| 205 | # Get the original function if it's been wrapped. This avoids wrapper |
| 206 | # loops when stacking decorators. |
| 207 | original_func = func |
| 208 | while hasattr(original_func, "__wrapped__"): |
| 209 | original_func = original_func.__wrapped__ |
| 210 | # Inspect the original function signature. |
| 211 | sig = inspect.signature(original_func) |
| 212 | accepts_args = bool(sig.parameters) |
| 213 | if is_awaitable(func): |
| 214 | if accepts_args: |
| 215 | |
| 216 | async def wrapper(event): |
| 217 | return await func(event) |
| 218 | |
| 219 | else: |
| 220 | |
| 221 | async def wrapper(*args, **kwargs): |
| 222 | return await func() |
| 223 | |
| 224 | else: |
| 225 | if accepts_args: |
| 226 | # Always create a new wrapper function to avoid issues with |
| 227 | # stacked decorators getting into an infinite loop. |
| 228 | |
| 229 | def wrapper(event): |
| 230 | return func(event) |
| 231 | |
| 232 | else: |
| 233 | |
| 234 | def wrapper(*args, **kwargs): |
| 235 | return func() |
| 236 | |
| 237 | return wraps(func)(wrapper) |
no test coverage detected