Skip to content

Commit

Permalink
Made support for both parameters: async & async_ (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yaroslav unimariJo committed Nov 20, 2017
1 parent 73300ea commit e943454
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
13 changes: 10 additions & 3 deletions psycopg2cffi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def _param_escape(s,

def connect(dsn=None,
database=None, user=None, password=None, host=None, port=None,
connection_factory=None, cursor_factory=None, async_=False, **kwargs):
connection_factory=None, cursor_factory=None, **kwargs):
"""
Create a new database connection.
Expand Down Expand Up @@ -69,7 +69,8 @@ def connect(dsn=None,
Using the *cursor_factory* parameter, a new default cursor factory will be
used by cursor().
Using *async_*=True an asynchronous connection will be created.
Using *async_*=True an asynchronous connection will be created. *async_* is
a valid alias (for Python versions where *async* is a keyword).
Any other keyword parameter will be passed to the underlying client
library: the list of supported parameters depends on the library version.
Expand All @@ -87,6 +88,12 @@ def connect(dsn=None,
if port is not None:
items.append(('port', port))

kwasync = {}
if 'async' in kwargs:
kwasync['async'] = kwargs.pop('async')
if 'async_' in kwargs:
kwasync['async_'] = kwargs.pop('async_')

items.extend([(k, v) for (k, v) in kwargs.items() if v is not None])

if dsn is not None and items:
Expand All @@ -101,7 +108,7 @@ def connect(dsn=None,
dsn = " ".join(["%s=%s" % (k, _param_escape(str(v)))
for (k, v) in items])

conn = _connect(dsn, connection_factory=connection_factory, async_=async_)
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
if cursor_factory is not None:
conn.cursor_factory = cursor_factory

Expand Down
8 changes: 6 additions & 2 deletions psycopg2cffi/_impl/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class Connection(object):
ProgrammingError = exceptions.ProgrammingError
Warning = exceptions.Warning

def __init__(self, dsn, async_=False):
def __init__(self, dsn, **kwargs):

self.dsn = dsn
self.status = consts.STATUS_SETUP
Expand All @@ -121,7 +121,11 @@ def __init__(self, dsn, async_=False):
# The number of commits/rollbacks done so far
self._mark = 0

self._async = async_
if 'async' in kwargs:
self._async = kwargs.pop('async')
if 'async_' in kwargs:
self._async = kwargs.pop('async_')

self._async_status = consts.ASYNC_DONE
self._async_cursor = None

Expand Down

0 comments on commit e943454

Please sign in to comment.