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:
@@ -57,10 +57,14 @@ def _TryOpenSslImport():
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
try:
|
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
|
return
|
||||||
except ImportError:
|
except ImportError:
|
||||||
import OpenSSL
|
import OpenSSL.crypto
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -75,7 +75,34 @@ class Test_pkcs12_key_as_pem(unittest.TestCase):
|
|||||||
finally:
|
finally:
|
||||||
sys.path = orig_sys_path
|
sys.path = orig_sys_path
|
||||||
imp.find_module = imp_find_module
|
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)
|
reload(crypt)
|
||||||
|
|
||||||
def test_with_nonsense_key(self):
|
def test_with_nonsense_key(self):
|
||||||
|
|||||||
Reference in New Issue
Block a user