Skip to content
This repository has been archived by the owner on Nov 5, 2019. It is now read-only.

Commit

Permalink
Merge pull request #186 from craigcitro/openssl_import
Browse files Browse the repository at this point in the history
Fall back to importing for OpenSSL detection.
  • Loading branch information
craigcitro committed May 19, 2015
2 parents 103c354 + b726ce8 commit 9f18282
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
27 changes: 26 additions & 1 deletion oauth2client/crypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
7 changes: 7 additions & 0 deletions tests/test_crypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,22 @@ def test_succeeds(self):
def test_without_openssl(self):
import imp
imp_find_module = imp.find_module
orig_sys_path = sys.path
def find_module(module_name):
raise ImportError('No module named %s' % module_name)
try:
for m in list(sys.modules):
if m.startswith('OpenSSL'):
sys.modules.pop(m)
sys.path = []
imp.find_module = find_module
reload(crypt)
self.assertRaises(NotImplementedError, crypt.pkcs12_key_as_pem,
'FOO', 'BAR')
finally:
sys.path = orig_sys_path
imp.find_module = imp_find_module
import OpenSSL
reload(crypt)

def test_with_nonsense_key(self):
Expand Down

0 comments on commit 9f18282

Please sign in to comment.