Source code for uw.sql.escape

"""Temporary module for Python 2 bytea support.

Contains a routine for formatting a byte array as a valid PostgreSQL string
literal.
"""

[docs]def escape_bytea (bytes): """Escape a (byte) string so that it can be interpreted as a bytea. :param bytes: the byte string to escape. :return: the byte string as a valid PostgreSQL bytea literal. This is needed because in Python 2 psycopg2 interprets str as a string and does encoding on it for sending to the database. There is no separate bytes type. In Python 3 we would use bytes for bytea input to the database and this function would not be needed. """ return '\\x' + ''.join ('%02X' % ord (byte) for byte in bytes)