[Python-porting] porting %s for bytes

Buck Golemon buck.2019 at gmail.com
Tue Apr 29 22:31:53 CEST 2014


We're working on porting MySQLdb, and run up against a hard wall in %s in
bytestrings.
SQL queries are truly bytes (they can contain arbitrary binary blobs for
insertion), but also need substition operations. The substition scheme in
MySQLdb is %s. This was handy when we could do query_bytesring % params,
but makes things very tough in python3.

My options seem to be:

A) Somehow decode arbitrary bytes to str, do the substitution, and encode
back to bytes. I can use the 'sorrogateescape' erorr handler to do this in
python3, but since this doesn't existing in 2.6 or 2.7, it doesn't jive
with my goals of 2+3 support.

B) Implement modulo substitution for bytes in python3. I see that this
operation is making a reappearance in python3.5, so this seems fairly
reasonable:
http://legacy.python.org/dev/peps/pep-0461/


I'm using the `future` package <http://python-future.org/> to do the port,
and am very pleased with the result, but it doesn't handle this one rough
spot. Can I request a 2+3 implementation of bytes.__mod__?  Or, has anyone
seen a currently-existing implementation of this function?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-porting/attachments/20140429/11e960a3/attachment.html>


More information about the Python-porting mailing list