joerussbowman / gaeutilities

gaeutilities - A collection of utilities to help with application development on Google Appengine
http://gaeutilities.appspot.com
BSD 3-Clause "New" or "Revised" License
78 stars 4 forks source link

Session's HTTP_COOKIE, Python2.6 Cookie.py, crash looking for rawdata.items #24

Open lahosken opened 14 years ago

lahosken commented 14 years ago

I was getting a crash when I ran my app on dev_appserver. Alas, my knowledge of cookies is puny, and once I found a workaround, I couldn't then figure out a way to repro this. And, of course, I didn't save a strack trace along the way. But I can point out something that sure looks fishy.

The crash hit in Python2.6/Cookie.py:

    if type(rawdata) == type(""):
        self.__ParseString(rawdata)
    else:
        # self.update() wouldn't call our custom __setitem__
        for k, v in rawdata.items():
            self[k] = v

The reason for the crash: Python was trying to get .items() of a Unicode string, which doesn't have an items function.

Next level up in the stack frame was...

    string_cookie = os.environ.get(u"HTTP_COOKIE", u"")

    self.cookie = Cookie.SimpleCookie()

    self.output_cookie = Cookie.SimpleCookie()
    self.cookie.load(string_cookie)

Something to notice: that default value for HTTP_COOKIE is unicode, not plain ol' ASCII. When I changed that u"" to a "", that workaround worked. (And when I changed it back to a u"", it still worked. Maybe that means that some HTTP_COOKIE environment variable got set somewhere. Or maybe it means I've totally failed to spot a real error. But maybe...) Maybe this is because the Python2.6 Cookie.py code is doing...

type(u"") == type("") False

If I knew more about Cookies, I'd probably know whether

Cookie.py should accept Unicode?

sessions.py should use ASCII?

lahosken commented 14 years ago

OK, I was able to repro this by clearing cookies and got a stack trace:

File "/home/lahosken/lab/google_appengine/google/appengine/ext/webapp/init.py", line 500, in call handler = handler_class() File "/home/lahosken/lab/gpbang/main.py", line 139, in init self.session = gaeu.sessions.Session(session_expire_time=345600) # 4 days File "/home/lahosken/lab/gpbang/gaeu/sessions.py", line 567, in init self.cookie.load(string_cookie) File "/usr/lib/python2.6/Cookie.py", line 628, in load for k, v in rawdata.items(): AttributeError: 'unicode' object has no attribute 'items'

joerussbowman commented 14 years ago

I'd be willing to add a community fix for this problem if it can be tested. Unfortunately I'm not working actively with appengine any more and have an infant at home so am really time constrained for working on this project any further.

My hunch is Python doesn't like working with cookies an unicode, I vaguely recall running into similar issues when I first moved all string handling to unicode. I could be wrong though so would need some formal validation of that assumption before I removed the unicode declarations.