You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
python: use consistent key type for opt dict manipulations - fixesunbit#1374
PyString_FromString is defined as PyBytes_FromString in uwsgi_python.h
What happens in Python 3 during the population of the opt_dict is that
we first check if a byte object, representing the key is in the dict.
If there is none, we use PyDict_SetItemString to set it. However, as
the docs say, PyDict_SetItemString will convert the key using
PyUnicode_FromString and we actually put a unicode key in the dict[1].
Therefore, when we check the "same" key again, we check again for the
"same" key as bytes object we don't find it and end up overwriting it
instead of doing the list promotion dance.
Attached patch fixes this by using PyDict_SetItem and PyDict_GetItem with
a consistent key type. For Python 3, a unicode object is used as key as
this is the backwards compatible thing to do.
Mini tester:
import uwsgi
def application(env, start_response):
start_response('200 OK', [('Content-Type', 'text/plain')])
for k, v in uwsgi.opt.items():
yield "{} {!r} ({})\n".format(k, v, type(v)).encode("utf-8")
yield "{} {}\n".format(uwsgi.version, type(uwsgi.version)).encode("utf-8")
yield "{} {}\n".format(uwsgi.hostname, type(uwsgi.hostname)).encode("utf-8")
yield b"END"
What is a bit rough is that in Python 3 the actual values for uwsgi.opt
entries end-up being all bytes, but that has always been like this...
[1] https://docs.python.org/3.5/c-api/dict.html#c.PyDict_SetItemString
0 commit comments