From a4d98cecea9c5c271a47c16e9eb951137dd52783 Mon Sep 17 00:00:00 2001 From: Thomas Watson Date: Fri, 5 Jan 2024 22:01:53 -0600 Subject: [PATCH] local_timezone: gracefully handle retrieval errors on Darwin In some sandbox environments, the program may not have permission to access /etc/localtime or it may not exist. Catch any exceptions accessing and parsing its value and return UTC by default to avoid crashing the program. --- src/pendulum/tz/local_timezone.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/pendulum/tz/local_timezone.py b/src/pendulum/tz/local_timezone.py index 3cb488cd..c3816141 100644 --- a/src/pendulum/tz/local_timezone.py +++ b/src/pendulum/tz/local_timezone.py @@ -146,9 +146,16 @@ def _get_windows_timezone() -> Timezone: def _get_darwin_timezone() -> Timezone: - # link will be something like /usr/share/zoneinfo/America/Los_Angeles. - link = os.readlink("/etc/localtime") - tzname = link[link.rfind("zoneinfo/") + 9 :] + try: + # link will be something like /usr/share/zoneinfo/America/Los_Angeles. + link = os.readlink("/etc/localtime") + tzname = link[link.rfind("zoneinfo/") + 9 :] + except Exception: + warnings.warn( + "Unable to find any timezone configuration, defaulting to UTC.", stacklevel=1 + ) + + return UTC return Timezone(tzname)