Don't hope to encode stuff in an arbitrary encoding

libpq's PQescapeString will use the same encoding it has seen before in
a connection (static_client_encoding).

So I think I'll leave this feature here for people who know what is
doing, but won't really document it as a feature: it can't really work
in a generic way (unless adding some disgusting hack like creating a
fake connection with the encoding we want to call PQescapeStringConn
instead of PQescapeString).
This commit is contained in:
Daniele Varrazzo 2016-07-01 17:33:12 +01:00
parent e062404d5c
commit f335c46a54
2 changed files with 10 additions and 5 deletions

1
NEWS
View File

@ -7,6 +7,7 @@ What's new in psycopg 2.6.2
- Report the server response status on errors (such as :ticket:`#281`).
- Raise `!NotSupportedError` on unhandled server response status
(:ticket:`#352`).
- Allow overriding string adapter encoding with no connection (:ticket:`#331`).
- The `~psycopg2.extras.wait_select` callback allows interrupting a
long-running query in an interactive shell using :kbd:`Ctrl-C`
(:ticket:`#333`).

View File

@ -157,7 +157,7 @@ class QuotingTestCase(ConnectingTestCase):
class TestQuotedString(ConnectingTestCase):
def test_encoding(self):
def test_encoding_from_conn(self):
q = psycopg2.extensions.QuotedString('hi')
self.assertEqual(q.encoding, 'latin1')
@ -165,16 +165,17 @@ class TestQuotedString(ConnectingTestCase):
q.prepare(self.conn)
self.assertEqual(q.encoding, 'utf_8')
class TestStringAdapter(ConnectingTestCase):
def test_encoding_default(self):
from psycopg2.extensions import adapt
a = adapt("hello")
self.assertEqual(a.encoding, 'latin1')
self.assertEqual(a.getquoted(), "'hello'")
egrave = u'\xe8'
self.assertEqual(adapt(egrave).getquoted(), "'\xe8'")
# NOTE: we can't really test an encoding different from utf8, because
# when encoding without connection the libpq will use parameters from
# a previous one, so what would happens depends jn the tests run order.
# egrave = u'\xe8'
# self.assertEqual(adapt(egrave).getquoted(), "'\xe8'")
def test_encoding_error(self):
from psycopg2.extensions import adapt
@ -183,6 +184,9 @@ class TestStringAdapter(ConnectingTestCase):
self.assertRaises(UnicodeEncodeError, a.getquoted)
def test_set_encoding(self):
# Note: this works-ish mostly in case when the standard db connection
# we test with is utf8, otherwise the encoding chosen by PQescapeString
# may give bad results.
from psycopg2.extensions import adapt
snowman = u"\u2603"
a = adapt(snowman)