From 28eac41510eb9de728bdfbc22a36f33ac75394f2 Mon Sep 17 00:00:00 2001 From: Brad Solomon Date: Tue, 21 May 2019 21:36:56 -0400 Subject: [PATCH] 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. --- django/contrib/auth/password_validation.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/django/contrib/auth/password_validation.py b/django/contrib/auth/password_validation.py index 30c82f0b93..2b43ed9424 100644 --- a/django/contrib/auth/password_validation.py +++ b/django/contrib/auth/password_validation.py @@ -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: