Skip to content

Commit 9267754

Browse files
committed
bpo-XXXXX: Exclude symlinks from zoneinfo.available_timezones() results\n\nSometimes available_timezones() was including 'localtime' in its results\nbecause it wasn't properly handling symlinks. This change ensures that\nsymlinks like 'localtime' are excluded from the available timezones list,\nproviding consistent results across different systems.\n\nAdded a test case to verify that symlinks are properly excluded.
1 parent d22a745 commit 9267754

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

Lib/test/test_zoneinfo/test_zoneinfo.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1941,6 +1941,27 @@ def test_exclude_posixrules(self):
19411941
actual = self.module.available_timezones()
19421942
self.assertEqual(actual, expected)
19431943

1944+
def test_exclude_symlinks(self):
1945+
expected = {
1946+
"America/New_York",
1947+
"Europe/London",
1948+
}
1949+
1950+
tree = list(expected) + ["localtime"]
1951+
1952+
with tempfile.TemporaryDirectory() as td:
1953+
# Create regular timezone files
1954+
for key in expected:
1955+
self.touch_zone(key, td)
1956+
1957+
# Create a symlink named "localtime" pointing to one of the timezone files
1958+
os.symlink("America/New_York", os.path.join(td, "localtime"))
1959+
1960+
with self.tzpath_context([td]):
1961+
actual = self.module.available_timezones()
1962+
self.assertEqual(actual, expected)
1963+
self.assertNotIn("localtime", actual)
1964+
19441965

19451966
class CTestModule(TestModule):
19461967
module = c_zoneinfo

Lib/zoneinfo/_tzpath.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ def valid_key(fpath):
155155

156156
for file in files:
157157
fpath = os.path.join(root, file)
158+
159+
# Skip symlinks to avoid including files like 'localtime'
160+
if os.path.islink(fpath):
161+
continue
158162

159163
key = os.path.relpath(fpath, start=tz_root)
160164
if os.sep != "/": # pragma: nocover

0 commit comments

Comments
 (0)