Skip to content

Commit

Permalink
Don't try to import GAE API in other environments
Browse files Browse the repository at this point in the history
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 65e5a7b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
26 changes: 14 additions & 12 deletions googleapiclient/discovery_cache/__init__.py
Expand Up @@ -18,7 +18,7 @@

import logging
import datetime

import os

LOGGER = logging.getLogger(__name__)

Expand All @@ -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
8 changes: 8 additions & 0 deletions tests/test_discovery.py
Expand Up @@ -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__
Expand Down

0 comments on commit 65e5a7b

Please sign in to comment.