Skip to content

Commit

Permalink
CoroWrapper AsyncPostgresqlConnection.release_cursor ... was never yi…
Browse files Browse the repository at this point in the history
…elded from (#86)

* scalar function: cursor.release in finally clause
* peewee <= 2.10.2
* release cursor after COMMIT, BEGIN, ROLLBACK and other
* remove duplicate import of logging
* aiopg is not compatible now with python 3.7 (syntax error)
  • Loading branch information
smagafurov authored and rudyryk committed Feb 14, 2018
1 parent fcbb866 commit f10fd77
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ python:
- "3.4"
- "3.5"
- "3.6"
- "nightly" # currently points to 3.7-dev
# TODO: aiopg is not compatible now with python 3.7 (syntax error)
# Uncomment line bellow when issue fixed: https://github.com/aio-libs/aiopg/issues/436
# - "nightly" # currently points to 3.7-dev
addons:
postgresql: "9.3"
services:
Expand Down
31 changes: 19 additions & 12 deletions peewee_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"""
import asyncio
import logging
import uuid
import contextlib
import peewee
Expand Down Expand Up @@ -585,7 +584,6 @@ def select(query):
finally:
yield from cursor.release

yield from cursor.release
return result


Expand Down Expand Up @@ -674,9 +672,12 @@ def scalar(query, as_tuple=False):
:return: result is the same as after sync ``query.scalar()`` call
"""
cursor = yield from _execute_query_async(query)
row = yield from cursor.fetchone()

yield from cursor.release
try:
row = yield from cursor.fetchone()
finally:
yield from cursor.release

if row and not as_tuple:
return row[0]
else:
Expand Down Expand Up @@ -1419,23 +1420,23 @@ def __init__(self, db):

@asyncio.coroutine
def commit(self, begin=True):
yield from _run_sql(self.db, 'COMMIT')
yield from _run_no_result_sql(self.db, 'COMMIT')
if begin:
yield from _run_sql(self.db, 'BEGIN')
yield from _run_no_result_sql(self.db, 'BEGIN')

@asyncio.coroutine
def rollback(self, begin=True):
yield from _run_sql(self.db, 'ROLLBACK')
yield from _run_no_result_sql(self.db, 'ROLLBACK')
if begin:
yield from _run_sql(self.db, 'BEGIN')
yield from _run_no_result_sql(self.db, 'BEGIN')

@asyncio.coroutine
def __aenter__(self):
if not asyncio.Task.current_task(loop=self.loop):
raise RuntimeError("The transaction must run within a task")
yield from self.db.push_transaction_async()
if self.db.transaction_depth_async() == 1:
yield from _run_sql(self.db, 'BEGIN')
yield from _run_no_result_sql(self.db, 'BEGIN')
return self

@asyncio.coroutine
Expand Down Expand Up @@ -1464,15 +1465,15 @@ def __init__(self, db, sid=None):

@asyncio.coroutine
def commit(self):
yield from _run_sql(self.db, 'RELEASE SAVEPOINT %s;' % self.quoted_sid)
yield from _run_no_result_sql(self.db, 'RELEASE SAVEPOINT %s;' % self.quoted_sid)

@asyncio.coroutine
def rollback(self):
yield from _run_sql(self.db, 'ROLLBACK TO SAVEPOINT %s;' % self.quoted_sid)
yield from _run_no_result_sql(self.db, 'ROLLBACK TO SAVEPOINT %s;' % self.quoted_sid)

@asyncio.coroutine
def __aenter__(self):
yield from _run_sql(self.db, 'SAVEPOINT %s;' % self.quoted_sid)
yield from _run_no_result_sql(self.db, 'SAVEPOINT %s;' % self.quoted_sid)
return self

@asyncio.coroutine
Expand Down Expand Up @@ -1542,6 +1543,12 @@ def _run_sql(database, operation, *args, **kwargs):
return cursor


@asyncio.coroutine
def _run_no_result_sql(database, operation, *args, **kwargs):
cursor = yield from _run_sql(database, operation, *args, **kwargs)
yield from cursor.release


@asyncio.coroutine
def _execute_query_async(query):
"""Execute query and return cursor object.
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
license='MIT',
zip_safe=False,
install_requires=(
'peewee >= 2.8.0',
'peewee>=2.8.0,<=2.10.2',
),
py_modules=[
'peewee_async',
Expand Down

0 comments on commit f10fd77

Please sign in to comment.