Skip to content

Commit

Permalink
Encode/decode binary values for PyGreSQL driver
Browse files Browse the repository at this point in the history
  • Loading branch information
phdru committed May 16, 2017
1 parent 7570514 commit 4c812a1
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/News.rst
Expand Up @@ -13,6 +13,8 @@ SQLObject 3.4.0 (master)
* Python 2.6 is no longer supported. The minimal supported version is
Python 2.7.

* Encode/decode binary values for PyGreSQL driver.

SQLObject 3.3.0
===============

Expand Down
3 changes: 3 additions & 0 deletions sqlobject/col.py
Expand Up @@ -1770,6 +1770,9 @@ def to_python(self, value, state):
if isinstance(value, str):
if not PY2 and dbName == "mysql":
value = value.encode('ascii', errors='surrogateescape')
if dbName == "postgres" and connection.driver == 'pygresql':
from pg import unescape_bytea
value = unescape_bytea(value)
if dbName == "sqlite":
if not PY2:
value = bytes(value, 'ascii')
Expand Down
4 changes: 4 additions & 0 deletions sqlobject/postgres/pgconnection.py
Expand Up @@ -85,6 +85,10 @@ def __init__(self, dsn=None, host=None, port=None, db=None,
# Register a converter for psycopg Binary type.
registerConverter(type(self.module.Binary('')),
PsycoBinaryConverter)
elif driver == 'pygresql':
from pg import escape_bytea
self.createBinary = \
lambda value, escape_bytea=escape_bytea: escape_bytea(value)
elif type(self.module.Binary) in (
type, type(PostgresBinaryConverter)) and \
type(self.module.Binary(b'')) not in (bytes, unicode_type):
Expand Down

0 comments on commit 4c812a1

Please sign in to comment.