[Python-ideas] RFC: bytestring as a str representation [was: a new bytestring type?]

Antoine Pitrou solipsis at pitrou.net
Wed Jan 8 11:34:08 CET 2014


On Wed, 8 Jan 2014 09:50:30 +0900
INADA Naoki <songofacandy at gmail.com>
wrote:
> 
> textdata = b"hello"

textdata shouldn't be a bytes object! If it's text it's a str.

> bindata = b"abc\xff\x00"
> query = "UPDATE table SET textcol=%s bincol=%s"
> 
> print build_query(query, textdata, bindata)
> 
> 
> I can't port this to Python 3.

I'm sure you can port it. Just decode your bindata using
surrogateescape:

  bindata = bindata.decode('utf8', 'surrogateescape')

and then encode the query at the end:

  query = query.encode('utf8', 'surrogateescape')

It will be a little slower, though. 

Regards

Antoine




More information about the Python-ideas mailing list