Skip to content

gh-137976: Exclude symlinks from zoneinfo.available_timezones() results #137977

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Lib/test/test_zoneinfo/test_zoneinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -1941,6 +1941,27 @@ def test_exclude_posixrules(self):
actual = self.module.available_timezones()
self.assertEqual(actual, expected)

def test_exclude_symlinks(self):
expected = {
"America/New_York",
"Europe/London",
}

tree = list(expected) + ["localtime"]

with tempfile.TemporaryDirectory() as td:
# Create regular timezone files
for key in expected:
self.touch_zone(key, td)

# Create a symlink named "localtime" pointing to one of the timezone files
os.symlink("America/New_York", os.path.join(td, "localtime"))

with self.tzpath_context([td]):
actual = self.module.available_timezones()
self.assertEqual(actual, expected)
self.assertNotIn("localtime", actual)


class CTestModule(TestModule):
module = c_zoneinfo
Expand Down
6 changes: 5 additions & 1 deletion Lib/zoneinfo/_tzpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def _reset_tzpath(to=None, stacklevel=4):
if tzpaths is not None:
if isinstance(tzpaths, (str, bytes)):
raise TypeError(
f"tzpaths must be a list or tuple, "
"tzpaths must be a list or tuple, "
+ f"not {type(tzpaths)}: {tzpaths!r}"
)

Expand Down Expand Up @@ -156,6 +156,10 @@ def valid_key(fpath):
for file in files:
fpath = os.path.join(root, file)

# Skip symlinks to avoid including files like 'localtime'
if os.path.islink(fpath):
continue

key = os.path.relpath(fpath, start=tz_root)
if os.sep != "/": # pragma: nocover
key = key.replace(os.sep, "/")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exclude symlinks from zoneinfo.available_timezones() results
Loading