Last Updated: February 25, 2016
·
629
· exallium

Fixing "getheader() only accepts 2 arguments, 3 given" on Google App Engine

Recently, I've been doing code maintenance for a product that runs on Google App Engine. Part of this was figuring out why a task was repeatedly failing. Looking at the logs, I found the error mentioned above... Something along the lines of:

getheader() only accepts 2 arguments, 3 given.

Looking deeper, it seems that this was triggered in atom.http_core. The instructions in the function triggering the error (get_headers()), were fairly explicit stating that as long as you were running Python2.7, it shouldn't even be getting to the part of the code I had failures at... so what gives?

Turns out GData has it's own HTTPResponse class. Why they used a class from scratch is beyond me, as I didn't dig that deep. However, this class is missing a few things. The aforementioned get_headers() function in atom.http_core works by first checking if the passed object has a getheaders method, and, if it does not, it grabs headers according to a prepopulated list. The output format is simply a list of tuples, pairs of keys and values.

GData's own HTTPResponse object does have a getheader() function, which is what the code was trying to originally use, but it does not accept a default value. In fact, if you pass it a key that doesn't exist, it will trigger a KeyError. Not wanting to modify that original function, I opted for the second option, and that was to implement the missing class method, getheaders(). Simple enough:

def getheaders(self):
  return [(k, v) for k, v in self.headers.iteritems()]

After this, that particular error went away, and I was able to continue on with my debugging and testing.