Improved performance of loading common passwords in CommonPasswordValidator.
CommonPasswordValidator.__init__ previously called either splitlines or
readlines, creating an unneeded intermediate list in memory. For large
custom password files, this could be burdensome.
@@ -171,13 +171,11 @@ class CommonPasswordValidator:
def __init__(self, password_list_path=DEFAULT_PASSWORD_LIST_PATH):
try:
- with gzip.open(str(password_list_path)) as f:
- common_passwords_lines = f.read().decode().splitlines()
+ with gzip.open(str(password_list_path), 'rt') as f:
+ self.passwords = {x.strip() for x in f}
except OSError:
with open(str(password_list_path)) as f:
- common_passwords_lines = f.readlines()
-
- self.passwords = {p.strip() for p in common_passwords_lines}
+ self.passwords = {x.strip() for x in f}
def validate(self, password, user=None):
if password.lower().strip() in self.passwords:
