Skip to content

Commit d47f9fb

Browse files
mjbommarclaude
andcommitted
gh-151788: Apply review suggestions
- Drop the local `name` variable in favor of entry.name. - Reword the OSError comment per review. - Move the "Append /" comment above the is_dir check. - Reword the NEWS entry per review (fix call -> calls). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 1138200 commit d47f9fb

2 files changed

Lines changed: 11 additions & 12 deletions

File tree

Lib/http/server.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -900,11 +900,9 @@ def list_directory(self, path):
900900
r.append(f'<body>\n<h1>{title}</h1>')
901901
r.append('<hr>\n<ul>')
902902
for entry in entries:
903-
name = entry.name
904-
displayname = linkname = name
905-
# Append / for directories or @ for symbolic links. is_dir() and
906-
# is_symlink() can raise OSError where os.path.isdir()/islink()
907-
# return False, so fall back to False to keep the same behavior.
903+
displayname = linkname = entry.name
904+
# Ignore any OSError raised by the os.DirEntry methods
905+
# to match the behavior of their os.path.* counterpart.
908906
try:
909907
is_dir = entry.is_dir()
910908
except OSError:
@@ -913,11 +911,12 @@ def list_directory(self, path):
913911
is_symlink = entry.is_symlink()
914912
except OSError:
915913
is_symlink = False
914+
# Append / for directories or @ for symbolic links
916915
if is_dir:
917-
displayname = name + "/"
918-
linkname = name + "/"
916+
displayname = entry.name + "/"
917+
linkname = entry.name + "/"
919918
if is_symlink:
920-
displayname = name + "@"
919+
displayname = entry.name + "@"
921920
# Note: a link to a directory displays with @ and links with /
922921
r.append('<li><a href="%s">%s</a></li>'
923922
% (urllib.parse.quote(linkname,
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
:class:`http.server.SimpleHTTPRequestHandler` now uses :func:`os.scandir`
2-
instead of :func:`os.listdir` to build directory listings, avoiding an
3-
:func:`os.stat` call per entry. This can improve directory listing performance
4-
where ``stat`` calls are slow.
1+
:class:`http.server.SimpleHTTPRequestHandler` now relies on :func:`os.scandir`
2+
instead of :func:`os.listdir` to build directory listings, thereby reducing
3+
the total number of :func:`os.stat` calls per entry. This typically improves
4+
directory listing performance on filesystems where such calls are slow.

0 commit comments

Comments
 (0)