Skip to content
Permalink
Browse files
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.
  • Loading branch information
zevdg committed May 14, 2020
1 parent 8ed1dcd commit 09e644719166aecb21a01b6d5ee9898843e7cd58
Showing with 22 additions and 12 deletions.
  1. +14 鈭12 googleapiclient/discovery_cache/__init__.py
  2. +8 鈭0 tests/test_discovery.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
@@ -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__

0 comments on commit 09e6447

Please sign in to comment.