diff --git a/googleapiclient/discovery_cache/__init__.py b/googleapiclient/discovery_cache/__init__.py index 3e4e9a597ad..455ff6224f2 100644 --- a/googleapiclient/discovery_cache/__init__.py +++ b/googleapiclient/discovery_cache/__init__.py @@ -18,7 +18,7 @@ import logging import datetime - +import os LOGGER = logging.getLogger(__name__) @@ -32,16 +32,18 @@ def autodetect(): googleapiclient.discovery_cache.base.Cache, a cache object which is auto detected, or None if no cache object is available. """ - try: - from google.appengine.api import memcache - from . import appengine_memcache - - return appengine_memcache.cache - except Exception: + if 'APPENGINE_RUNTIME' in os.environ: try: - from . import file_cache + from google.appengine.api import memcache + from . import appengine_memcache + + return appengine_memcache.cache + except Exception: + pass + try: + from . import file_cache - return file_cache.cache - except Exception as e: - LOGGER.warning(e, exc_info=True) - return None + return file_cache.cache + except Exception as e: + LOGGER.warning(e, exc_info=True) + return None diff --git a/tests/test_discovery.py b/tests/test_discovery.py index 217f69d08c5..a07e861222a 100644 --- a/tests/test_discovery.py +++ b/tests/test_discovery.py @@ -648,6 +648,14 @@ def test_api_endpoint_override_from_client_options_dict(self): class DiscoveryFromAppEngineCache(unittest.TestCase): + + def setUp(self): + self.old_environ = os.environ.copy() + os.environ["APPENGINE_RUNTIME"] = "python27" + + def tearDown(self): + os.environ = self.old_environ + def test_appengine_memcache(self): # Hack module import self.orig_import = __import__