1
0
Fork 0

Cast bytea fields to text for output.

This commit is contained in:
Daniel Rocco 2015-01-21 22:50:24 -05:00
parent 244c356116
commit 52b684574e
2 changed files with 15 additions and 1 deletions

View File

@ -13,6 +13,11 @@ _logger = logging.getLogger(__name__)
psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
psycopg2.extensions.register_type(psycopg2.extensions.UNICODEARRAY)
# Cast bytea fields to text. By default, this will render as hex strings with
# Postgres 9+ and as escaped binary in earlier versions.
psycopg2.extensions.register_type(
psycopg2.extensions.new_type((17,), 'BYTEA_TEXT', psycopg2.STRING))
# When running a query, make pressing CTRL+C raise a KeyboardInterrupt
# See http://initd.org/psycopg/articles/2014/07/20/cancelling-postgresql-statements-python/
psycopg2.extensions.set_wait_callback(psycopg2.extras.wait_select)

View File

@ -112,4 +112,13 @@ def test_multiple_queries_same_line_syntaxerror(executor):
@dbtest
def test_special_command(executor):
run(executor, '\\?')
run(executor, '\\?')
@dbtest
def test_bytea_field_support_in_output(executor):
run(executor, "create table binarydata(c bytea)")
run(executor,
"insert into binarydata (c) values (decode('DEADBEEF', 'hex'))")
assert u'\\xdeadbeef' in run(executor, "select * from binarydata", join=True)