Fun with 'str' and 'bytes'

F

Frank Millman

Hi all

I want to create a cookie containing a session id. In python 2.6 I had the
following -

from __future__ import unicode_literals
session_id = b64encode(urandom(20))
response_headers.append(
(b'Set-Cookie', b'sid="{0}"'.format(session_id)))

After upgrading to 3.2, the above lines generate this traceback -
AttributeError: 'bytes' object has no attribute 'format'

The best workaround I can come up with is the following -

session_id = b64encode(urandom(20))
response_headers.append(
(b'Set-Cookie', b'sid="' + session_id + b'"'))

It works, but it is not pretty. Is there a more elegant solution?

Thanks

Frank Millman
 
N

nn

Hi all

I want to create a cookie containing a session id. In python 2.6 I had the
following -

from __future__ import unicode_literals
session_id = b64encode(urandom(20))
response_headers.append(
    (b'Set-Cookie', b'sid="{0}"'.format(session_id)))

After upgrading to 3.2, the above lines generate this traceback -
    AttributeError: 'bytes' object has no attribute 'format'

The best workaround I can come up with is the following -

session_id = b64encode(urandom(20))
response_headers.append(
    (b'Set-Cookie', b'sid="' + session_id + b'"'))

It works, but it is not pretty. Is there a more elegant solution?

Thanks

Frank Millman

As far as I know, that is pretty much it. Also see:

http://bugs.python.org/issue3982
http://mail.python.org/pipermail/python-dev/2010-July/102252.html
http://lucumr.pocoo.org/2010/5/25/wsgi-on-python-3/
 
T

Terry Reedy

That is a depressing bug report, and really comes across as people who
don't use networking commenting on the requirements of people who write
networking code.

It's good to see that the idea was getting a bit more treatment last yeat.

I added the following note to that issue.
"
struct.pack, not mentioned here, is a binary bytes formatting function.
It can do ascii bytes mixed with binary octets. It works the same in
Python 2 and 3.

Str.bytes does two things: convert objects to strings according to the
contents of field specifiers; interpolate the resulting strings into a
template string according to the locations of the field specifiers. If
desired bytes represent encoded text, then encoding computed text is the
obvious Py3 solution.

For some mixed ascii-binary uses, struct.pack is not as elegant as a
bytes.format might be. But I think such a method should use struct
format codes within field specifiers to convert objects into binary
bytes rather than text.
"
Note that struct codes include s = C char[] = Py bytes of possibly
unspecified length copied unchanged.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top