Skip to content

fix: don't try to import GAE API in other environments #903

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions googleapiclient/discovery_cache/__init__.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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