Skip to content

Commit

Permalink
Attempt to make Python strptime bug workaround thread safe
Browse files Browse the repository at this point in the history
  • Loading branch information
MoojMidge committed May 13, 2024
1 parent f71ca6d commit e46bf5d
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions resources/lib/youtube_plugin/kodion/utils/datetime_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from datetime import date, datetime, time as dt_time, timedelta
from importlib import import_module
from sys import modules
from threading import Condition, Lock

from ..exceptions import KodionException
from ..logger import log_error
Expand Down Expand Up @@ -278,18 +279,32 @@ def strptime(datetime_str, fmt=None):
try:
return datetime.strptime(datetime_str, fmt)
except TypeError:
log_error('Python strptime bug workaround.\n'
'Refer to https://github.com/python/cpython/issues/71587')

if '_strptime' not in modules:
modules['_strptime'] = import_module('_strptime')
_strptime = modules['_strptime']
if '_strptime' not in modules or strptime.reloading.locked():
if strptime.reloaded.acquire(blocking=False):
_strptime = import_module('_strptime')
modules['_strptime'] = _strptime
log_error('Python strptime bug workaround - '
'https://github.com/python/cpython/issues/71587')
strptime.reloaded.notify_all()
strptime.reloaded.release()
else:
strptime.reloaded.acquire()
while '_strptime' not in modules:
strptime.reloaded.wait()
_strptime = modules['_strptime']
strptime.reloaded.release()
else:
_strptime = modules['_strptime']

if timezone:
return _strptime._strptime_datetime(datetime, datetime_str, fmt)
return datetime(*(_strptime._strptime(datetime_str, fmt)[0][0:6]))


strptime.reloading = Lock()
strptime.reloaded = Condition(lock=strptime.reloading)


def since_epoch(dt_object=None):
if dt_object is None:
dt_object = now(tz=timezone.utc) if timezone else datetime.utcnow()
Expand Down

0 comments on commit e46bf5d

Please sign in to comment.