Discussion:
porting %s for bytes
Buck Golemon
2014-05-01 21:32:52 UTC
Permalink
My below message didn't seem to get through.
Is there a moderation issue?
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.
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
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?
Buck Golemon
2014-04-29 20:31:53 UTC
Permalink
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?
Ethan Furman
2014-05-10 15:14:46 UTC
Permalink
B) Implement modulo substitution for bytes in python3. I see that
this operation is making a reappearance in python3.5, so this
http://legacy.python.org/dev/peps/pep-0461/
There will be a backport (eventually ;), but as long as your implementation conforms to the PEP you should be fine.
I'm using the `future` package 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?
As I recall there was a functional function implementation somewhere in the PEP460-461 threads that may work well
enough. I /think/ it was Terry Reedy that offered it (but I could easily be wrong).

--
~Ethan~

Loading...