aboutsummaryrefslogtreecommitdiff
path: root/oauth2client/crypt.py
diff options
context:
space:
mode:
authorCraig Citro <craigcitro@google.com>2015-05-19 10:10:25 -0700
committerCraig Citro <craigcitro@google.com>2015-05-19 14:41:14 -0700
commitb726ce8aa2c07626159fff3e428979a74538f150 (patch)
treeecefad139e556b5dccd90cdc1223745af0c7151a /oauth2client/crypt.py
parent97fa11ed4f48a99cf9e7df57fd6dfe9d51d9f601 (diff)
downloadoauth2client-b726ce8aa2c07626159fff3e428979a74538f150.tar.gz
Fall back to importing for OpenSSL detection.
Previously, we'd attempted to detect OpenSSL without actually importing, in order to avoid a runtime penalty for importing `oauth2client`. However, in some exotic situations, `imp.find_module` can fail even though the import would be successful. We tweak to fall back to importing, and add some gross to the corresponding test.
Diffstat (limited to 'oauth2client/crypt.py')
-rw-r--r--oauth2client/crypt.py27
1 files changed, 26 insertions, 1 deletions
diff --git a/oauth2client/crypt.py b/oauth2client/crypt.py
index ccdda8c..19f9d9f 100644
--- a/oauth2client/crypt.py
+++ b/oauth2client/crypt.py
@@ -38,8 +38,33 @@ class AppIdentityError(Exception):
pass
+def _TryOpenSslImport():
+ """Import OpenSSL, avoiding the explicit import where possible.
+
+ Importing OpenSSL 0.14 can take up to 0.5s, which is a large price
+ to pay at module import time. However, it's also possible for
+ ``imp.find_module`` to fail to find the module, even when it's
+ installed. (This is the case in various exotic environments,
+ including some relevant for Google.) So we first try a fast-path,
+ and fall back to the slow import as needed.
+
+ Args:
+ None
+ Returns:
+ None
+ Raises:
+ ImportError if OpenSSL is unavailable.
+
+ """
+ try:
+ _ = imp.find_module('OpenSSL')
+ return
+ except ImportError:
+ import OpenSSL
+
+
try:
- _ = imp.find_module('OpenSSL')
+ _TryOpenSslImport()
class OpenSSLVerifier(object):
"""Verifies the signature on a message."""