Fixed #29689 -- Improved performance of FileSystemStorage.listdir() and FilePathField with os.scandir().
This commit is contained in:
parent
371ece2f06
commit
a0ca4b5694
|
@ -309,11 +309,11 @@ class FileSystemStorage(Storage):
|
|||
def listdir(self, path):
|
||||
path = self.path(path)
|
||||
directories, files = [], []
|
||||
for entry in os.listdir(path):
|
||||
if os.path.isdir(os.path.join(path, entry)):
|
||||
directories.append(entry)
|
||||
for entry in os.scandir(path):
|
||||
if entry.is_dir():
|
||||
directories.append(entry.name)
|
||||
else:
|
||||
files.append(entry)
|
||||
files.append(entry.name)
|
||||
return directories, files
|
||||
|
||||
def path(self, name):
|
||||
|
|
|
@ -5,6 +5,7 @@ Field classes.
|
|||
import copy
|
||||
import datetime
|
||||
import math
|
||||
import operator
|
||||
import os
|
||||
import re
|
||||
import uuid
|
||||
|
@ -1104,17 +1105,16 @@ class FilePathField(ChoiceField):
|
|||
f = os.path.join(root, f)
|
||||
self.choices.append((f, f.replace(path, "", 1)))
|
||||
else:
|
||||
try:
|
||||
for f in sorted(os.listdir(self.path)):
|
||||
if f == '__pycache__':
|
||||
continue
|
||||
full_file = os.path.join(self.path, f)
|
||||
if (((self.allow_files and os.path.isfile(full_file)) or
|
||||
(self.allow_folders and os.path.isdir(full_file))) and
|
||||
(self.match is None or self.match_re.search(f))):
|
||||
self.choices.append((full_file, f))
|
||||
except OSError:
|
||||
pass
|
||||
choices = []
|
||||
for f in os.scandir(self.path):
|
||||
if f.name == '__pycache__':
|
||||
continue
|
||||
if (((self.allow_files and f.is_file()) or
|
||||
(self.allow_folders and f.is_dir())) and
|
||||
(self.match is None or self.match_re.search(f.name))):
|
||||
choices.append((f.path, f.name))
|
||||
choices.sort(key=operator.itemgetter(1))
|
||||
self.choices.extend(choices)
|
||||
|
||||
self.widget.choices = self.choices
|
||||
|
||||
|
|
|
@ -39,11 +39,11 @@ class PathNotImplementedStorage(storage.Storage):
|
|||
def listdir(self, path):
|
||||
path = self._path(path)
|
||||
directories, files = [], []
|
||||
for entry in os.listdir(path):
|
||||
if os.path.isdir(os.path.join(path, entry)):
|
||||
directories.append(entry)
|
||||
for entry in os.scandir(path):
|
||||
if entry.is_dir():
|
||||
directories.append(entry.name)
|
||||
else:
|
||||
files.append(entry)
|
||||
files.append(entry.name)
|
||||
return directories, files
|
||||
|
||||
def delete(self, name):
|
||||
|
|
Loading…
Reference in New Issue