Define a dummy httplib_max_headers context manager if a httplib module without the _MAXHEADERS attribute is used

This commit is contained in:
cryzed 2017-04-18 22:50:11 +02:00
parent 387fbd2276
commit 9a6ad62771

View file

@ -33,14 +33,21 @@ def getClass():
return RoyalRoadAdapter
# Using a context manager for this guarantees that the original max headers value is restored, even when an uncaught
# exception is raised
@contextlib.contextmanager
def httplib_max_headers(number):
original_max_headers = httplib._MAXHEADERS
httplib._MAXHEADERS = number
yield
httplib._MAXHEADERS = original_max_headers
# Work around "http.client.HTTPException: got more than 100 headers" issue. Using a context manager for this guarantees
# that the original max headers value is restored, even when an uncaught exception is raised.
if hasattr(httplib, '_MAXHEADERS'):
@contextlib.contextmanager
def httplib_max_headers(number):
original_max_headers = httplib._MAXHEADERS
httplib._MAXHEADERS = number
yield
httplib._MAXHEADERS = original_max_headers
# Google App Engine seems to vendor a modified version of httplib in which the _MAXHEADERS attribute is missing (and
# also avoids this issue entirely) -- in this case we define a dummy version of the context manager
else:
@contextlib.contextmanager
def httplib_max_headers(number):
yield
# Class name has to be unique. Our convention is camel case the