summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>2016-03-03 16:58:24 +0000
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>2016-03-03 17:09:15 +0000
commitc9fd828f8adeeed108a326892394c47e3747b558 (patch)
tree95342271c049b03bea72ccaac7f0a2ffea2a6f47
parent7aab934ae5950c6fa1bcd25ed053857538624310 (diff)
downloadpsycopg2-c9fd828f8adeeed108a326892394c47e3747b558.tar.gz
Allow make_dsn to take no parameter
The behaviour of connect() is unchanged: either dsn or params must be specified.
-rw-r--r--doc/src/extensions.rst7
-rw-r--r--lib/__init__.py3
-rw-r--r--lib/extensions.py2
-rwxr-xr-xtests/test_connection.py4
4 files changed, 8 insertions, 8 deletions
diff --git a/doc/src/extensions.rst b/doc/src/extensions.rst
index fa69d62..b661895 100644
--- a/doc/src/extensions.rst
+++ b/doc/src/extensions.rst
@@ -497,11 +497,8 @@ Other functions
Put together the arguments in *kwargs* into a connection string. If *dsn*
is specified too, merge the arguments coming from both the sources. If the
- same argument is specified in both the sources, the *kwargs* version
- overrides the *dsn* version.
-
- At least one parameter is required (either *dsn* or any keyword). Note
- that the empty string is a valid connection string.
+ same argument name is specified in both the sources, the *kwargs* value
+ overrides the *dsn* value.
The input arguments are validated: the output should always be a valid
connection string (as far as `parse_dsn()` is concerned). If not raise
diff --git a/lib/__init__.py b/lib/__init__.py
index 698f50d..829e29e 100644
--- a/lib/__init__.py
+++ b/lib/__init__.py
@@ -116,6 +116,9 @@ def connect(dsn=None, connection_factory=None, cursor_factory=None,
library: the list of supported parameters depends on the library version.
"""
+ if dsn is None and not kwargs:
+ raise TypeError('missing dsn and no parameters')
+
dsn = _ext.make_dsn(dsn, **kwargs)
conn = _connect(dsn, connection_factory=connection_factory, async=async)
if cursor_factory is not None:
diff --git a/lib/extensions.py b/lib/extensions.py
index 39cc4de..2130098 100644
--- a/lib/extensions.py
+++ b/lib/extensions.py
@@ -158,7 +158,7 @@ class NoneAdapter(object):
def make_dsn(dsn=None, **kwargs):
"""Convert a set of keywords into a connection strings."""
if dsn is None and not kwargs:
- raise TypeError('missing dsn and no parameters')
+ return ''
# If no kwarg is specified don't mung the dsn, but verify it
if not kwargs:
diff --git a/tests/test_connection.py b/tests/test_connection.py
index 0158f5c..ddec8cf 100755
--- a/tests/test_connection.py
+++ b/tests/test_connection.py
@@ -381,8 +381,8 @@ class MakeDsnTestCase(ConnectingTestCase):
def assertDsnEqual(self, dsn1, dsn2):
self.assertEqual(set(dsn1.split()), set(dsn2.split()))
- def test_there_has_to_be_something(self):
- self.assertRaises(TypeError, ext.make_dsn)
+ def test_empty_arguments(self):
+ self.assertEqual(ext.make_dsn(), '')
def test_empty_string(self):
dsn = ext.make_dsn('')