diff options
| author | Daniele Varrazzo <daniele.varrazzo@gmail.com> | 2012-09-25 23:46:46 +0100 |
|---|---|---|
| committer | Daniele Varrazzo <daniele.varrazzo@gmail.com> | 2012-09-25 23:46:46 +0100 |
| commit | cd316a94f1f410fa87664db6705bef3ccbe1f4e9 (patch) | |
| tree | 198ead4e92302a36cf806c9f94324f17a3756024 /lib | |
| parent | 59151886a02db66b91ed251a937d583a4c6a77b5 (diff) | |
| download | psycopg2-cd316a94f1f410fa87664db6705bef3ccbe1f4e9.tar.gz | |
Dropped quirks in connection arguments handling
Now connect() raises an exception instead of swallowing keyword arguments
when a connection string is specified as well
Closes ticket #131.
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/__init__.py | 52 |
1 files changed, 26 insertions, 26 deletions
diff --git a/lib/__init__.py b/lib/__init__.py index 0a8ed0f..b84236c 100644 --- a/lib/__init__.py +++ b/lib/__init__.py @@ -146,37 +146,37 @@ def connect(dsn=None, Using *async*=True an asynchronous connection will be created. Any other keyword parameter will be passed to the underlying client - library: the list of supported parameter depends on the library version. + library: the list of supported parameters depends on the library version. """ + items = [] + if database is not None: + items.append(('dbname', database)) + if user is not None: + items.append(('user', user)) + if password is not None: + items.append(('password', password)) + if host is not None: + items.append(('host', host)) + if port is not None: + items.append(('port', port)) + + items.extend([(k, v) for (k, v) in kwargs.iteritems() if v is not None]) + + if dsn is not None and items: + raise InterfaceError( + "you cannot specify keyword arguments (%s)" + " together with a connection string" + % ", ".join([k for k, v in items])) + if dsn is None: - # Note: reproducing the behaviour of the previous C implementation: - # keyword are silently swallowed if a DSN is specified. I would have - # raised an exception. File under "histerical raisins". - items = [] - if database is not None: - items.append(('dbname', database)) - if user is not None: - items.append(('user', user)) - if password is not None: - items.append(('password', password)) - if host is not None: - items.append(('host', host)) - # Reproducing the previous C implementation behaviour: swallow a - # negative port. The libpq would raise an exception for it. - if port is not None and int(port) > 0: - items.append(('port', port)) - - items.extend( - [(k, v) for (k, v) in kwargs.iteritems() if v is not None]) - dsn = " ".join(["%s=%s" % (k, _param_escape(str(v))) - for (k, v) in items]) - - if not dsn: + if not items: raise InterfaceError('missing dsn and no parameters') + else: + dsn = " ".join(["%s=%s" % (k, _param_escape(str(v))) + for (k, v) in items]) - return _connect(dsn, - connection_factory=connection_factory, async=async) + return _connect(dsn, connection_factory=connection_factory, async=async) __all__ = filter(lambda k: not k.startswith('_'), locals().keys()) |
