Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _create_waiter(self, func_name):
if self._waiter is not None:
if self._cancelling:
if not self._waiter.done():
raise RuntimeError('%s() called while connection is '
'being cancelled' % func_name)
else:
raise RuntimeError('%s() called while another coroutine is '
'already waiting for incoming '
'data' % func_name)
self._waiter = create_future(self._loop)
return self._waiter
def release(self, conn):
"""Release free connection back to the connection pool.
"""
fut = create_future(self._loop)
fut.set_result(None)
if conn in self._terminated:
assert conn.closed, conn
self._terminated.remove(conn)
return fut
assert conn in self._used, (conn, self._used)
self._used.remove(conn)
if not conn.closed:
tran_status = conn._conn.get_transaction_status()
if tran_status != TRANSACTION_STATUS_IDLE:
warnings.warn(
("Invalid transaction status on "
"released connection: {}").format(tran_status),
ResourceWarning
)
conn.close()
def close(self):
self._close()
ret = create_future(self._loop)
ret.set_result(None)
return ret
async def _connect(dsn=None, *, timeout=TIMEOUT, loop=None, enable_json=True,
enable_hstore=True, enable_uuid=True, echo=False, **kwargs):
if loop is None:
loop = asyncio.get_event_loop()
waiter = create_future(loop)
conn = Connection(dsn, loop, timeout, waiter, bool(echo), **kwargs)
try:
await conn._poll(waiter, timeout)
except Exception:
conn.close()
raise
if enable_json:
extras.register_default_json(conn._conn)
if enable_uuid:
extras.register_uuid(conn_or_curs=conn._conn)
if enable_hstore:
oids = await _enable_hstore(conn)
if oids is not None:
oid, array_oid = oids
extras.register_hstore(conn._conn, oid=oid, array_oid=array_oid)
return conn
async def cancel():
self._waiter = create_future(self._loop)
self._cancelling = True
self._cancellation_waiter = self._waiter
self._conn.cancel()
if not self._conn.isexecuting():
return
try:
await asyncio.wait_for(self._waiter, timeout,
loop=self._loop)
except psycopg2.extensions.QueryCanceledError:
pass
except asyncio.TimeoutError:
self._close()