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.
diff --git a/googleapiclient/discovery_cache/__init__.py b/googleapiclient/discovery_cache/__init__.py
index 3e4e9a5..455ff62 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 @@
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 file_cache.cache
- except Exception as e:
- LOGGER.warning(e, exc_info=True)
- return None
+ 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
diff --git a/tests/test_discovery.py b/tests/test_discovery.py
index 217f69d..a07e861 100644
--- a/tests/test_discovery.py
+++ b/tests/test_discovery.py
@@ -648,6 +648,14 @@
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__