Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
7 changes: 7 additions & 0 deletions Lib/test/test_zoneinfo/test_zoneinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -1784,6 +1784,7 @@ def test_reset_tzpath_relative_paths(self):
("/usr/share/zoneinfo", "../relative/path",),
("path/to/somewhere", "../relative/path",),
("/usr/share/zoneinfo", "path/to/somewhere", "../relative/path",),
(pathlib.Path("path/to/somewhere"),)
Comment thread
tungol marked this conversation as resolved.
Outdated
Comment thread
tungol marked this conversation as resolved.
Outdated
]
for input_paths in bad_values:
with self.subTest(input_paths=input_paths):
Expand All @@ -1795,6 +1796,7 @@ def test_tzpath_type_error(self):
"/etc/zoneinfo:/usr/share/zoneinfo",
b"/etc/zoneinfo:/usr/share/zoneinfo",
0,
(b"/bytes/path", "/valid/path"),
Comment thread
tungol marked this conversation as resolved.
]

for bad_value in bad_values:
Expand All @@ -1805,15 +1807,20 @@ def test_tzpath_type_error(self):
def test_tzpath_attribute(self):
tzpath_0 = [f"{DRIVE}/one", f"{DRIVE}/two"]
tzpath_1 = [f"{DRIVE}/three"]
tzpath_pathlike = (pathlib.Path("{DRIVE}/usr/share/zoneinfo"),)
Comment thread
tungol marked this conversation as resolved.
Outdated

with self.tzpath_context(tzpath_0):
query_0 = self.module.TZPATH

with self.tzpath_context(tzpath_1):
query_1 = self.module.TZPATH

with self.tzpath_context(tzpath_pathlike):
query_pathlike = self.module.TZPATH

self.assertSequenceEqual(tzpath_0, query_0)
self.assertSequenceEqual(tzpath_1, query_1)
self.assertSequenceEqual(tuple([os.fspath(p) for p in tzpath_pathlike]), query_pathlike)


class CTzPathTest(TzPathTest):
Expand Down
8 changes: 8 additions & 0 deletions Lib/zoneinfo/_tzpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ def _reset_tzpath(to=None, stacklevel=4):
+ f"not {type(tzpaths)}: {tzpaths!r}"
)

tzpaths = [os.fspath(p) for p in tzpaths]
nonstr_paths = [p for p in tzpaths if not isinstance(p, str)]
if nonstr_paths:
Comment thread
tungol marked this conversation as resolved.
Outdated
raise TypeError(
"All elements of a tzpath sequence must be strings or "
"os.PathLike objects which convert to strings."
)

if not all(map(os.path.isabs, tzpaths)):
raise ValueError(_get_invalid_paths_message(tzpaths))
base_tzpath = tzpaths
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
:meth:`zoneinfo.reset_tzpath` will now convert any :class:`os.PathLike` objects
it receives into strings before adding them to ``TZPATH``. It will raise
``TypeError`` if anything other than a string is found after this conversion.
If given an :class:`os.PathLike` object that represents a relative path, it
will now raise ``ValueError`` instead of ``TypeError``, and present a more
informative error message.
Loading