diff --git a/lib/__init__.py b/lib/__init__.py index 0a8ed0f..9a275e5 100644 --- a/lib/__init__.py +++ b/lib/__init__.py @@ -149,34 +149,34 @@ def connect(dsn=None, library: the list of supported parameter 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()) diff --git a/setup.cfg b/setup.cfg index 637ed41..0f19572 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,5 +1,5 @@ [build_ext] -define=PSYCOPG_EXTENSIONS,PSYCOPG_NEW_BOOLEAN,HAVE_PQFREEMEM +define=PSYCOPG_EXTENSIONS,PSYCOPG_NEW_BOOLEAN,HAVE_PQFREEMEM,PSYCOPG_DEBUG # PSYCOPG_EXTENSIONS enables extensions to PEP-249 (you really want this) # PSYCOPG_DISPLAY_SIZE enable display size calculation (a little slower)