Skip to content

Commit 4b79cb7

Browse files
authored
Merge pull request #594 from beci/master
Fix #256 : check the file existence instead of check if path exists before load a timezone related file
2 parents 18d1ad2 + 496140b commit 4b79cb7

File tree

6 files changed

+24
-6
lines changed

6 files changed

+24
-6
lines changed

pendulum/tz/local_timezone.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ def _get_unix_timezone(_root="/"): # type: (str) -> Timezone
161161
# Now look for distribution specific configuration files
162162
# that contain the timezone name.
163163
tzpath = os.path.join(_root, "etc/timezone")
164-
if os.path.exists(tzpath):
164+
if os.path.isfile(tzpath):
165165
with open(tzpath, "rb") as tzfile:
166166
data = tzfile.read()
167167

@@ -187,7 +187,7 @@ def _get_unix_timezone(_root="/"): # type: (str) -> Timezone
187187

188188
for filename in ("etc/sysconfig/clock", "etc/conf.d/clock"):
189189
tzpath = os.path.join(_root, filename)
190-
if not os.path.exists(tzpath):
190+
if not os.path.isfile(tzpath):
191191
continue
192192

193193
with open(tzpath, "rt") as tzfile:
@@ -218,7 +218,7 @@ def _get_unix_timezone(_root="/"): # type: (str) -> Timezone
218218
# systemd distributions use symlinks that include the zone name,
219219
# see manpage of localtime(5) and timedatectl(1)
220220
tzpath = os.path.join(_root, "etc", "localtime")
221-
if os.path.exists(tzpath) and os.path.islink(tzpath):
221+
if os.path.isfile(tzpath) and os.path.islink(tzpath):
222222
parts = list(
223223
reversed(os.path.realpath(tzpath).replace(" ", "_").split(os.path.sep))
224224
)
@@ -234,7 +234,7 @@ def _get_unix_timezone(_root="/"): # type: (str) -> Timezone
234234
for filename in ("etc/localtime", "usr/local/etc/localtime"):
235235
tzpath = os.path.join(_root, filename)
236236

237-
if not os.path.exists(tzpath):
237+
if not os.path.isfile(tzpath):
238238
continue
239239

240240
return TimezoneFile(tzpath)
@@ -247,7 +247,7 @@ def _tz_from_env(tzenv): # type: (str) -> Timezone
247247
tzenv = tzenv[1:]
248248

249249
# TZ specifies a file
250-
if os.path.exists(tzenv):
250+
if os.path.isfile(tzenv):
251251
return TimezoneFile(tzenv)
252252

253253
# TZ specifies a zoneinfo zone.

pendulum/tz/zoneinfo/reader.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def read(self, file_path): # type: (str) -> Timezone
5959
6060
:param file_path: The path of a zoneinfo file.
6161
"""
62-
if not os.path.exists(file_path):
62+
if not os.path.isfile(file_path):
6363
raise InvalidZoneinfoFile("The tzinfo file does not exist")
6464

6565
with open(file_path, "rb") as fd:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../usr/share/zoneinfo/Europe/Paris
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Blank file
2+
3+
Necessary for environments, which not handle empty folder synchronization well, to be sure the parent folder is created.
4+
5+
The `/etc/timezone` folder is necessary to ensure that the package not fail if that folder exists.
Binary file not shown.

tests/tz/test_local_timezone.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,15 @@ def test_windows_timezone():
3636
timezone = _get_windows_timezone()
3737

3838
assert timezone is not None
39+
40+
41+
@pytest.mark.skipif(
42+
sys.platform == "win32", reason="Test only available for UNIX systems"
43+
)
44+
def test_unix_etc_timezone_dir():
45+
# Should not fail if `/etc/timezone` is a folder
46+
local_path = os.path.join(os.path.split(__file__)[0], "..")
47+
root_path = os.path.join(local_path, "fixtures", "tz", "timezone_dir")
48+
tz = _get_unix_timezone(_root=root_path)
49+
50+
assert tz.name == "Europe/Paris"

0 commit comments

Comments
 (0)