Created
November 30, 2016 10:32
-
-
Save Leechael/5b0aeacdd3f4f25666f637b18a99b9a7 to your computer and use it in GitHub Desktop.
Buggy: inspect.iscoroutinefunction and inspect.isfunction :(
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import inspect | |
def iscoroutinefunction(fn): | |
if inspect.iscoroutinefunction(fn): | |
return True | |
if inspect.iscoroutinefunction(getattr(fn, "__call__", None)): | |
return True | |
return False | |
async def execute(fn, *args, **kwargs): | |
if callable(fn): | |
if iscoroutinefunction(fn): | |
result = await fn(*args, **kwargs) | |
else: | |
result = fn(*args, **kwargs) | |
else: | |
raise RuntimeError( | |
"Expected argument `fn` is callable." | |
) | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment