Skip to content

Commit

Permalink
Fix tests and pyawaitable_await_function implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeroIntensity committed Jun 22, 2024
1 parent 99e822a commit 7190b3f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
15 changes: 10 additions & 5 deletions src/_pyawaitable/awaitable.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,17 +213,22 @@ pyawaitable_await_function_impl(
)
{
size_t len = strlen(fmt);
char *tup_format = PyMem_Malloc(len + 3);
size_t size = len + 3;
char *tup_format = PyMem_Malloc(size);
if (!tup_format)
{
PyErr_NoMemory();
return -1;
}

tup_format[0] = '(';
strcpy(tup_format + 1, fmt);
tup_format[len - 2] = ')';
tup_format[len - 1] = '\0';
for (size_t i = 0; i < len; ++i)
{
tup_format[i + 1] = fmt[i];
}

tup_format[size - 2] = ')';
tup_format[size - 1] = '\0';

va_list vargs;
va_start(vargs, err);
Expand All @@ -233,8 +238,8 @@ pyawaitable_await_function_impl(

if (!args)
return -1;

PyObject *coro = PyObject_Call(func, args, NULL);
Py_DECREF(args);

if (!coro)
return -1;
Expand Down
8 changes: 7 additions & 1 deletion tests/test_awaitable.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class AwaitableABI(PyABI):
),
("unpack", ctypes.PYFUNCTYPE(ctypes.c_int, ctypes.py_object)),
("unpack_arb", ctypes.PYFUNCTYPE(ctypes.c_int, ctypes.py_object)),
("PyAwaitableType", ctypes.py_object),
("await_function", ctypes.PYFUNCTYPE(ctypes.c_int, ctypes.py_object, ctypes.py_object, ctypes.c_char_p, awaitcallback, awaitcallback_err,)),
]

Expand Down Expand Up @@ -440,14 +441,19 @@ def cb(awaitable_inner: pyawaitable.PyAwaitable, result: str):
@limit_leaks(LEAK_LIMIT)
async def test_await_function():
awaitable = abi.new()
called: bool = False

async def coro(value: int, suffix: str) -> str:
await asyncio.sleep(0)
return str(value * 2) + suffix

@awaitcallback
def cb(awaitable_inner: pyawaitable.PyAwaitable, result: str):
nonlocal called
called = True
assert result == "42hello"
return 0

abi.await_function(awaitable, coro, b"is", cb, None, 21, b"hello")
abi.await_function(awaitable, coro, b"is", cb, awaitcallback_err(0), 21, b"hello")
await awaitable
assert called is True

0 comments on commit 7190b3f

Please sign in to comment.