From 09e644719166aecb21a01b6d5ee9898843e7cd58 Mon Sep 17 00:00:00 2001 From: Zev Goldstein Date: Thu, 14 May 2020 16:29:20 -0400 Subject: [PATCH] fix: don't try to import GAE API in other environments (#903) The GAE memcache API isn't supported in python3. This change gives callers a new way to ensure that googleapiclient doesn't even try to import and use this legacy API. --- googleapiclient/discovery_cache/__init__.py | 26 +++++++++++---------- tests/test_discovery.py | 8 +++++++ 2 files changed, 22 insertions(+), 12 deletions(-) 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__