Skip to content

Commit 575eb6e

Browse files
authored
Merge pull request #14 from dhondta/copilot/add-brew-install-option
Fix crash on import when libcrypt/libxcrypt is missing
2 parents fb7e4be + e25289b commit 575eb6e

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

src/codext/hashing/crypt.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,22 @@
1515
try:
1616
import crypt
1717
except ImportError:
18-
import legacycrypt as crypt
18+
try:
19+
import legacycrypt as crypt
20+
except ImportError:
21+
crypt = None
1922

20-
METHODS = [x[7:].lower() for x in crypt.__dict__ if x.startswith("METHOD_")]
21-
22-
def crypt_hash(method):
23-
method = (method or "").lstrip("-_") or "blowfish"
24-
if method not in METHODS:
25-
raise NotImplementedError("method '%s' is not implemented" % method)
26-
def _encode(input, error="strict"):
27-
m = getattr(crypt, "METHOD_" + method.upper())
28-
return crypt.crypt(ensure_str(input), crypt.mksalt(m)), len(input)
29-
return _encode
30-
31-
add("crypt", crypt_hash, pattern=r"^crypt(|[-_](?:%s))$" % "|".join(METHODS), guess=None)
23+
if crypt is not None:
24+
METHODS = [x[7:].lower() for x in crypt.__dict__ if x.startswith("METHOD_")]
25+
26+
def crypt_hash(method):
27+
method = (method or "").lstrip("-_") or "blowfish"
28+
if method not in METHODS:
29+
raise NotImplementedError("method '%s' is not implemented" % method)
30+
def _encode(input, error="strict"):
31+
m = getattr(crypt, "METHOD_" + method.upper())
32+
return crypt.crypt(ensure_str(input), crypt.mksalt(m)), len(input)
33+
return _encode
34+
35+
add("crypt", crypt_hash, pattern=r"^crypt(|[-_](?:%s))$" % "|".join(METHODS), guess=None)
3236

tests/test_manual.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,12 @@ def test_codec_hash_functions(self):
128128
try:
129129
import crypt
130130
except ImportError:
131-
import legacycrypt as crypt
132-
METHODS = [x[7:].lower() for x in crypt.__dict__ if x.startswith("METHOD_")]
131+
try:
132+
import legacycrypt as crypt
133+
except ImportError:
134+
crypt = None
135+
METHODS = [x[7:].lower() for x in crypt.__dict__ if x.startswith("METHOD_")] \
136+
if crypt is not None else []
133137
for m in METHODS:
134138
h = "crypt-" + m
135139
self.assertIsNotNone(codecs.encode(STR, h))

0 commit comments

Comments
 (0)