summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>2018-02-21 10:26:51 +0000
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>2018-02-21 10:26:51 +0000
commit806cc5e610e3558d7c7f976d94d5f2709a4b69e7 (patch)
tree81dc43061f211658f3875b762a5ccdf26ffe1443
parent19c74cc6546c81d064d99e5c81d170e1c536425d (diff)
parent214600fcc037e0871146290797c91a169c6ee664 (diff)
downloadpsycopg2-806cc5e610e3558d7c7f976d94d5f2709a4b69e7.tar.gz
Merge branch 'fix-679' into maint_2_7
-rw-r--r--NEWS1
-rw-r--r--psycopg/utils.c4
-rwxr-xr-xtests/test_connection.py14
3 files changed, 17 insertions, 2 deletions
diff --git a/NEWS b/NEWS
index ef12613..2a58b9d 100644
--- a/NEWS
+++ b/NEWS
@@ -7,6 +7,7 @@ What's new in psycopg 2.7.5
- Fixed building on Solaris 11 and derivatives such as SmartOS and illumos
(:ticket:`#677`).
- Maybe fixed building on MSYS2 (as reported in :ticket:`#658`).
+- Allow string subclasses in connection and other places (:ticket:`#679`).
What's new in psycopg 2.7.4
diff --git a/psycopg/utils.c b/psycopg/utils.c
index 7073504..9aa2d46 100644
--- a/psycopg/utils.c
+++ b/psycopg/utils.c
@@ -168,11 +168,11 @@ psycopg_ensure_bytes(PyObject *obj)
PyObject *rv = NULL;
if (!obj) { return NULL; }
- if (PyUnicode_CheckExact(obj)) {
+ if (PyUnicode_Check(obj)) {
rv = PyUnicode_AsUTF8String(obj);
Py_DECREF(obj);
}
- else if (Bytes_CheckExact(obj)) {
+ else if (Bytes_Check(obj)) {
rv = obj;
}
else {
diff --git a/tests/test_connection.py b/tests/test_connection.py
index e42aa1d..d8b86d7 100755
--- a/tests/test_connection.py
+++ b/tests/test_connection.py
@@ -246,6 +246,13 @@ class ConnectionTests(ConnectingTestCase):
else:
del os.environ['PGCLIENTENCODING']
+ def test_connect_no_string(self):
+ class MyString(str):
+ pass
+
+ conn = psycopg2.connect(MyString(dsn))
+ conn.close()
+
def test_weakref(self):
from weakref import ref
import gc
@@ -401,6 +408,13 @@ class ParseDsnTestCase(ConnectingTestCase):
self.assertRaises(TypeError, ext.parse_dsn, None)
self.assertRaises(TypeError, ext.parse_dsn, 42)
+ def test_str_subclass(self):
+ class MyString(str):
+ pass
+
+ res = ext.parse_dsn(MyString("dbname=test"))
+ self.assertEqual(res, {'dbname': 'test'})
+
class MakeDsnTestCase(ConnectingTestCase):
def test_empty_arguments(self):