diff options
author | Daniele Varrazzo <daniele.varrazzo@gmail.com> | 2016-07-01 19:11:04 +0100 |
---|---|---|
committer | Daniele Varrazzo <daniele.varrazzo@gmail.com> | 2016-07-01 19:14:00 +0100 |
commit | 5bcaf11f9db43f15d52943916647a0cc0dc6ffca (patch) | |
tree | 17b5cb3c3ada947a52eff05b681c5e812e48c504 | |
parent | 70af49c0a2a59c516fe89b30019430b8db551833 (diff) | |
download | psycopg2-5bcaf11f9db43f15d52943916647a0cc0dc6ffca.tar.gz |
Allow adapting bytes using QuotedString on Python 3 too
Close #365.
-rw-r--r-- | NEWS | 1 | ||||
-rw-r--r-- | psycopg/adapter_qstring.c | 6 | ||||
-rwxr-xr-x | tests/test_quote.py | 15 |
3 files changed, 15 insertions, 7 deletions
@@ -32,6 +32,7 @@ What's new in psycopg 2.6.2 (:ticket:`#333`). - Fixed `!PersistentConnectionPool` on Python 3 (:ticket:`#348`). - Fixed segfault on `repr()` of an unitialized connection (:ticket:`#361`). +- Allow adapting bytes using QuotedString on Python 3 too (:ticket:`#365`). - Added support for setuptools/wheel (:ticket:`#370`). - Fix build on Windows with Python 3.5, VS 2015 (:ticket:`#380`). - Fixed `!errorcodes.lookup` initialization thread-safety (:ticket:`#382`). diff --git a/psycopg/adapter_qstring.c b/psycopg/adapter_qstring.c index 110093e..8c5a8f1 100644 --- a/psycopg/adapter_qstring.c +++ b/psycopg/adapter_qstring.c @@ -75,15 +75,13 @@ qstring_quote(qstringObject *self) } } -#if PY_MAJOR_VERSION < 3 - /* if the wrapped object is a simple string, we don't know how to + /* if the wrapped object is a binary string, we don't know how to (re)encode it, so we pass it as-is */ - else if (PyString_Check(self->wrapped)) { + else if (Bytes_Check(self->wrapped)) { str = self->wrapped; /* INCREF to make it ref-wise identical to unicode one */ Py_INCREF(str); } -#endif /* if the wrapped object is not a string, this is an error */ else { diff --git a/tests/test_quote.py b/tests/test_quote.py index 74b366c..25d1d31 100755 --- a/tests/test_quote.py +++ b/tests/test_quote.py @@ -23,7 +23,8 @@ # License for more details. import sys -from testutils import unittest, ConnectingTestCase, skip_before_libpq +import testutils +from testutils import unittest, ConnectingTestCase import psycopg2 import psycopg2.extensions @@ -167,13 +168,13 @@ class TestQuotedString(ConnectingTestCase): class TestQuotedIdentifier(ConnectingTestCase): - @skip_before_libpq(9, 0) + @testutils.skip_before_libpq(9, 0) def test_identifier(self): from psycopg2.extensions import quote_ident self.assertEqual(quote_ident('blah-blah', self.conn), '"blah-blah"') self.assertEqual(quote_ident('quote"inside', self.conn), '"quote""inside"') - @skip_before_libpq(9, 0) + @testutils.skip_before_libpq(9, 0) def test_unicode_ident(self): from psycopg2.extensions import quote_ident snowman = u"\u2603" @@ -226,6 +227,14 @@ class TestStringAdapter(ConnectingTestCase): self.assertEqual(a.encoding, 'utf_8') self.assertEqual(a.getquoted(), b("'\xe2\x98\x83'")) + @testutils.skip_before_python(3) + def test_adapt_bytes(self): + snowman = u"\u2603" + self.conn.set_client_encoding('utf8') + a = psycopg2.extensions.QuotedString(snowman.encode('utf8')) + a.prepare(self.conn) + self.assertEqual(a.getquoted(), b("'\xe2\x98\x83'")) + def test_suite(): return unittest.TestLoader().loadTestsFromName(__name__) |