Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow setting the encoding on QuotedString #127

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions psycopg2cffi/_impl/adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

adapters = {}

DEFAULT_ENCODING = 'latin1'

# Adapters assept python objects and always return bytes, as described in
# http://initd.org/psycopg/articles/2011/01/24/psycopg2-porting-python-3-report/

Expand Down Expand Up @@ -224,7 +226,7 @@ def TimestampFromTicks(ticks):
class QuotedString(_BaseAdapter):
def __init__(self, obj):
super(QuotedString, self).__init__(obj)
self._default_encoding = "latin1"
self._encoding = None

def prepare(self, conn):
self._conn = conn
Expand All @@ -234,7 +236,11 @@ def encoding(self):
if self._conn:
return self._conn._py_enc
else:
return self._default_encoding
return self._encoding or DEFAULT_ENCODING

@encoding.setter
def encoding(self, value):
self._encoding = value

def getquoted(self):
obj = self._wrapped
Expand Down
3 changes: 3 additions & 0 deletions psycopg2cffi/tests/psycopg2_tests/test_quote.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ def test_encoding(self):
q = psycopg2.extensions.QuotedString('hi')
self.assertEqual(q.encoding, 'latin1')

q.encoding = 'ascii'
self.assertEqual(q.encoding, 'ascii')

self.conn.set_client_encoding('utf_8')
q.prepare(self.conn)
self.assertEqual(q.encoding, 'utf_8')
Expand Down