From 9a6ad627713aa12112275acf8c13bfd6b29c0748 Mon Sep 17 00:00:00 2001 From: cryzed Date: Tue, 18 Apr 2017 22:50:11 +0200 Subject: [PATCH] Define a dummy httplib_max_headers context manager if a httplib module without the _MAXHEADERS attribute is used --- fanficfare/adapters/adapter_royalroadl.py | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/fanficfare/adapters/adapter_royalroadl.py b/fanficfare/adapters/adapter_royalroadl.py index 247d8702..95be7f91 100644 --- a/fanficfare/adapters/adapter_royalroadl.py +++ b/fanficfare/adapters/adapter_royalroadl.py @@ -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