Check for OpenSSL.crypto when detecting OpenSSL.

Previously, we assumed that anyone using `OpenSSL` would have installed it via
a mechanism like `pip`; this has come back to bite us.

We modify our code to look for `OpenSSL.crypto`, not just `OpenSSL`, and add
another (unpleasant) test.

Fixes #190.
This commit is contained in:
Craig Citro
2015-05-22 23:28:00 -07:00
parent c61bdf4565
commit df8f98e09f
2 changed files with 34 additions and 3 deletions

View File

@@ -57,10 +57,14 @@ def _TryOpenSslImport():
"""
try:
_ = imp.find_module('OpenSSL')
_, _package_dir, _ = imp.find_module('OpenSSL')
if not (os.path.isfile(os.path.join(_package_dir, 'crypto.py')) or
os.path.isfile(os.path.join(_package_dir, 'crypto.so')) or
os.path.isdir(os.path.join(_package_dir, 'crypto'))):
raise ImportError('No module named OpenSSL.crypto')
return
except ImportError:
import OpenSSL
import OpenSSL.crypto
try:

View File

@@ -75,7 +75,34 @@ class Test_pkcs12_key_as_pem(unittest.TestCase):
finally:
sys.path = orig_sys_path
imp.find_module = imp_find_module
import OpenSSL
import OpenSSL.crypto
reload(crypt)
def test_without_openssl_crypto(self):
import imp
imp_find_module = imp.find_module
orig_sys_path = sys.path
orig_isfile = os.path.isfile
openssl_module = imp.find_module('OpenSSL')
def find_module(module_name):
if module_name == 'OpenSSL':
return openssl_module
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
os.path.isfile = lambda filename: False
reload(crypt)
self.assertRaises(NotImplementedError, crypt.pkcs12_key_as_pem,
'FOO', 'BAR')
finally:
sys.path = orig_sys_path
imp.find_module = imp_find_module
os.path.isfile = orig_isfile
import OpenSSL.crypto
reload(crypt)
def test_with_nonsense_key(self):