Closed serjflint closed 4 years ago
Hi @serjflint !
I guess the trouble come from the fact prepare()
returns a statement object (not sure precisely what kind of object is returned) this is not wrapped by trio.
See for instance what is done for the transaction()
method compared to the generic wrapper (that only wrap the execution of the method and return the value verbatim):
So I guess we should introduce a TrioStatementProxy
and mimic what is done for the transaction()
method
@touilleMan I am actually trying to do just that but I have a stack overflow on return from prepare()
class TrioStatementProxy:
def __init__(self, asyncpg_statement):
self._asyncpg_statement = asyncpg_statement
def __getattr__(self, attr):
target = getattr(self._asyncpg_statement, attr)
if callable(target):
@wraps(target)
@trio_asyncio.aio_as_trio
async def wrapper(*args, **kwargs):
return await target(*args, **kwargs)
# Only generate the function wrapper once per instance
setattr(self, attr, wrapper)
return wrapper
return target
@trio_asyncio.aio_as_trio
async def __aenter__(self, *args):
return await self._asyncpg_statement(*args)
@_shielded
@trio_asyncio.aio_as_trio
async def __aexit__(self, *args):
return await self._asyncpg_statement.__aexit__(*args)
class TrioConnectionProxy:
def __init__(self, *args, **kwargs):
self._asyncpg_create_connection = partial(
asyncpg.connect, *args, **kwargs
)
self._asyncpg_conn = None
async def prepare(self, *args, **kwargs):
asyncpg_statement = await trio_asyncio.aio_as_trio(self._asyncpg_conn.prepare(*args, **kwargs))
return TrioStatementProxy(asyncpg_statement)
I removed aenter and aexit and it seems to be working.
fixed by #9
@serjflint aaaaand version 0.50.0 is released ! 😄
Code below works normally
And the prepared statement throws an Exception