File tree Expand file tree Collapse file tree 2 files changed +22
-1
lines changed Expand file tree Collapse file tree 2 files changed +22
-1
lines changed Original file line number Diff line number Diff line change @@ -45,7 +45,13 @@ def _get_localzone(_root: str = '/') -> datetime.tzinfo:
45
45
else :
46
46
pos = link_dst .find ('/zoneinfo/' )
47
47
if pos >= 0 :
48
- zone_name = link_dst [pos + 10 :]
48
+ # On occasion, the `/etc/localtime` symlink has a double slash, e.g.
49
+ # "/usr/share/zoneinfo//UTC", which would make `zoneinfo.ZoneInfo`
50
+ # complain (no absolute paths allowed), and we'd end up returning
51
+ # `None` (as a fix for #1092).
52
+ # Instead, let's just "fix" the double slash symlink by stripping
53
+ # leading slashes before passing the assumed zone name forward.
54
+ zone_name = link_dst [pos + 10 :].lstrip ("/" )
49
55
tzinfo = _get_tzinfo (zone_name )
50
56
if tzinfo is not None :
51
57
return tzinfo
Original file line number Diff line number Diff line change
1
+ import os
1
2
import sys
3
+ from unittest .mock import Mock
2
4
3
5
import pytest
4
6
@@ -27,3 +29,16 @@ def test_issue_1092_with_pytz(monkeypatch):
27
29
monkeypatch .setenv ("TZ" , "/UTC" ) # Malformed timezone name.
28
30
with pytest .raises (LookupError ):
29
31
get_localzone ()
32
+
33
+
34
+ @pytest .mark .skipif (
35
+ sys .platform == "win32" ,
36
+ reason = "Issue 990 is not applicable on Windows" ,
37
+ )
38
+ def test_issue_990 (monkeypatch ):
39
+ monkeypatch .setenv ("TZ" , "" )
40
+ fake_readlink = Mock (return_value = "/usr/share/zoneinfo////UTC" ) # Double slash, oops!
41
+ monkeypatch .setattr (os , "readlink" , fake_readlink )
42
+ from babel .localtime ._unix import _get_localzone
43
+ assert _get_localzone () is not None
44
+ fake_readlink .assert_called_with ("/etc/localtime" )
You can’t perform that action at this time.
0 commit comments