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.
This commit is contained in:
Brad Solomon 2019-05-21 21:36:56 -04:00 committed by Mariusz Felisiak
parent f011d9ea56
commit 28eac41510
1 changed files with 3 additions and 5 deletions

View File

@ -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: