Browse Source

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.
Brad Solomon 5 years ago
parent
commit
28eac41510
1 changed files with 3 additions and 5 deletions
  1. 3 5
      django/contrib/auth/password_validation.py

+ 3 - 5
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: