Skip to content

Commit f9409ad

Browse files
author
Sylvain MARIE
committed
2 parents bb11762 + f44bc10 commit f9409ad

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

docs/changelog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
### 1.15.2 - bugfix
4+
5+
- Fixed `SyntaxError` happening when the name of a native coroutine function to create contains `'return'`.
6+
Fixes [#96](https://github.com/smarie/python-makefun/issues/96).
7+
38
### 1.15.1 - bugfixes
49

510
- Fixed `ValueError: Invalid co_name` happening on python 2 when the name of a function to create starts or ends with

src/makefun/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ def create_function(func_signature, # type: Union[str, Signature]
312312
body = "def %s\n return _func_impl_(%s)\n" % (func_signature_str, params_str)
313313

314314
if iscoroutinefunction(func_impl):
315-
body = ("async " + body).replace('return', 'return await')
315+
body = ("async " + body).replace('return _func_impl_', 'return await _func_impl_')
316316

317317
# create the function by compiling code, mapping the `_func_impl_` symbol to `func_impl`
318318
protect_eval_dict(evaldict, func_name, params_names)

tests/test_generators_coroutines.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,25 @@ def test_native_coroutine():
9595
from asyncio import get_event_loop
9696
out = get_event_loop().run_until_complete(dynamic_fun(0.1))
9797
assert out == 0.1
98+
99+
100+
@pytest.mark.skipif(sys.version_info < (3, 5), reason="native coroutines with async/await require python3.6 or higher")
101+
def test_issue_96():
102+
"""Same as `test_native_coroutine` but tests that we can use 'return' in the coroutine name"""
103+
104+
# define the handler that should be called
105+
from tests._test_py35 import make_native_coroutine_handler
106+
my_native_coroutine_handler = make_native_coroutine_handler()
107+
108+
# create the dynamic function
109+
dynamic_fun = create_function("foo_returns_bar(sleep_time=2)", my_native_coroutine_handler)
110+
111+
# check that this is a coroutine for inspect and for asyncio
112+
assert iscoroutinefunction(dynamic_fun)
113+
from asyncio import iscoroutinefunction as is_native_co
114+
assert is_native_co(dynamic_fun)
115+
116+
# verify that the new function is a native coroutine and behaves correctly
117+
from asyncio import get_event_loop
118+
out = get_event_loop().run_until_complete(dynamic_fun(0.1))
119+
assert out == 0.1

0 commit comments

Comments
 (0)