From aa165bcb017cff170707ccced03ff513971e0031 Mon Sep 17 00:00:00 2001 From: Kirill Podoprigora Date: Wed, 16 Apr 2025 12:00:41 +0300 Subject: [PATCH 1/4] Fix error message for empty bytes object; adjust the check for an empty string --- Lib/test/test_zoneinfo/test_zoneinfo.py | 1 + Lib/zoneinfo/_tzpath.py | 6 +++++- .../Library/2025-04-16-11-58-20.gh-issue-114713.asdj1k.rst | 2 ++ 3 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2025-04-16-11-58-20.gh-issue-114713.asdj1k.rst diff --git a/Lib/test/test_zoneinfo/test_zoneinfo.py b/Lib/test/test_zoneinfo/test_zoneinfo.py index fff6c0d865b441..6f6e2d4e700dc3 100644 --- a/Lib/test/test_zoneinfo/test_zoneinfo.py +++ b/Lib/test/test_zoneinfo/test_zoneinfo.py @@ -237,6 +237,7 @@ def test_bad_keys_paths(self): "America/../America/Los_Angeles", # Not normalized "America/./Los_Angeles", "", + b"", ] for bad_key in bad_keys: diff --git a/Lib/zoneinfo/_tzpath.py b/Lib/zoneinfo/_tzpath.py index 990a5c8b6a9372..5951b3a749a7cc 100644 --- a/Lib/zoneinfo/_tzpath.py +++ b/Lib/zoneinfo/_tzpath.py @@ -83,10 +83,14 @@ def find_tzfile(key): def _validate_tzfile_path(path, _base=_TEST_PATH): - if not path: + if path == "": raise ValueError( "ZoneInfo key must not be an empty string" ) + elif path == b"": + raise ValueError( + "ZoneInfo key must not be an empty bytes object" + ) if os.path.isabs(path): raise ValueError( diff --git a/Misc/NEWS.d/next/Library/2025-04-16-11-58-20.gh-issue-114713.asdj1k.rst b/Misc/NEWS.d/next/Library/2025-04-16-11-58-20.gh-issue-114713.asdj1k.rst new file mode 100644 index 00000000000000..22bb639f90cbe9 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-04-16-11-58-20.gh-issue-114713.asdj1k.rst @@ -0,0 +1,2 @@ +Handle case of an empty bytes object passed to :class:`zoneinfo.ZoneInfo`. + From 1dcee41919b2898ca643bb1502064db83938090e Mon Sep 17 00:00:00 2001 From: Kirill Podoprigora Date: Wed, 16 Apr 2025 12:10:30 +0300 Subject: [PATCH 2/4] Add isinstance check to bypass warning about string and bytes comparison --- Lib/zoneinfo/_tzpath.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/zoneinfo/_tzpath.py b/Lib/zoneinfo/_tzpath.py index 5951b3a749a7cc..8fd4259b1c6e52 100644 --- a/Lib/zoneinfo/_tzpath.py +++ b/Lib/zoneinfo/_tzpath.py @@ -87,7 +87,7 @@ def _validate_tzfile_path(path, _base=_TEST_PATH): raise ValueError( "ZoneInfo key must not be an empty string" ) - elif path == b"": + elif isinstance(path, bytes) and path == b"": raise ValueError( "ZoneInfo key must not be an empty bytes object" ) From df20358ac215605cf1eb601099c7a636af3d0a1a Mon Sep 17 00:00:00 2001 From: Kirill Podoprigora Date: Wed, 16 Apr 2025 12:23:50 +0300 Subject: [PATCH 3/4] same for string --- Lib/zoneinfo/_tzpath.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/zoneinfo/_tzpath.py b/Lib/zoneinfo/_tzpath.py index 8fd4259b1c6e52..c08f6b92adddd2 100644 --- a/Lib/zoneinfo/_tzpath.py +++ b/Lib/zoneinfo/_tzpath.py @@ -83,7 +83,7 @@ def find_tzfile(key): def _validate_tzfile_path(path, _base=_TEST_PATH): - if path == "": + if isintance(str, path) and path == "": raise ValueError( "ZoneInfo key must not be an empty string" ) From 6e690a8bf35e1d195b71dc0927b6ea9112473f04 Mon Sep 17 00:00:00 2001 From: Kirill Podoprigora Date: Wed, 16 Apr 2025 12:25:36 +0300 Subject: [PATCH 4/4] Fix typo --- Lib/zoneinfo/_tzpath.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/zoneinfo/_tzpath.py b/Lib/zoneinfo/_tzpath.py index c08f6b92adddd2..7a9d15a41acbdb 100644 --- a/Lib/zoneinfo/_tzpath.py +++ b/Lib/zoneinfo/_tzpath.py @@ -83,7 +83,7 @@ def find_tzfile(key): def _validate_tzfile_path(path, _base=_TEST_PATH): - if isintance(str, path) and path == "": + if isinstance(path, str) and path == "": raise ValueError( "ZoneInfo key must not be an empty string" )