Skip to content

Commit 5bcaf11

Browse files
committed
Allow adapting bytes using QuotedString on Python 3 too
Close psycopg#365.
1 parent 70af49c commit 5bcaf11

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ What's new in psycopg 2.6.2
3232
(:ticket:`#333`).
3333
- Fixed `!PersistentConnectionPool` on Python 3 (:ticket:`#348`).
3434
- Fixed segfault on `repr()` of an unitialized connection (:ticket:`#361`).
35+
- Allow adapting bytes using QuotedString on Python 3 too (:ticket:`#365`).
3536
- Added support for setuptools/wheel (:ticket:`#370`).
3637
- Fix build on Windows with Python 3.5, VS 2015 (:ticket:`#380`).
3738
- Fixed `!errorcodes.lookup` initialization thread-safety (:ticket:`#382`).

psycopg/adapter_qstring.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,13 @@ qstring_quote(qstringObject *self)
7575
}
7676
}
7777

78-
#if PY_MAJOR_VERSION < 3
79-
/* if the wrapped object is a simple string, we don't know how to
78+
/* if the wrapped object is a binary string, we don't know how to
8079
(re)encode it, so we pass it as-is */
81-
else if (PyString_Check(self->wrapped)) {
80+
else if (Bytes_Check(self->wrapped)) {
8281
str = self->wrapped;
8382
/* INCREF to make it ref-wise identical to unicode one */
8483
Py_INCREF(str);
8584
}
86-
#endif
8785

8886
/* if the wrapped object is not a string, this is an error */
8987
else {

tests/test_quote.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
# License for more details.
2424

2525
import sys
26-
from testutils import unittest, ConnectingTestCase, skip_before_libpq
26+
import testutils
27+
from testutils import unittest, ConnectingTestCase
2728

2829
import psycopg2
2930
import psycopg2.extensions
@@ -167,13 +168,13 @@ def test_encoding_from_conn(self):
167168

168169

169170
class TestQuotedIdentifier(ConnectingTestCase):
170-
@skip_before_libpq(9, 0)
171+
@testutils.skip_before_libpq(9, 0)
171172
def test_identifier(self):
172173
from psycopg2.extensions import quote_ident
173174
self.assertEqual(quote_ident('blah-blah', self.conn), '"blah-blah"')
174175
self.assertEqual(quote_ident('quote"inside', self.conn), '"quote""inside"')
175176

176-
@skip_before_libpq(9, 0)
177+
@testutils.skip_before_libpq(9, 0)
177178
def test_unicode_ident(self):
178179
from psycopg2.extensions import quote_ident
179180
snowman = u"\u2603"
@@ -226,6 +227,14 @@ def test_connection_wins_anyway(self):
226227
self.assertEqual(a.encoding, 'utf_8')
227228
self.assertEqual(a.getquoted(), b("'\xe2\x98\x83'"))
228229

230+
@testutils.skip_before_python(3)
231+
def test_adapt_bytes(self):
232+
snowman = u"\u2603"
233+
self.conn.set_client_encoding('utf8')
234+
a = psycopg2.extensions.QuotedString(snowman.encode('utf8'))
235+
a.prepare(self.conn)
236+
self.assertEqual(a.getquoted(), b("'\xe2\x98\x83'"))
237+
229238

230239
def test_suite():
231240
return unittest.TestLoader().loadTestsFromName(__name__)

0 commit comments

Comments
 (0)