Skip to content

Commit

Permalink
Workaround for zipimporter not having exec_module before Python 3.10 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
georgevreilly committed Dec 9, 2022
1 parent 5cce0e7 commit 9b6c15a
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions dateparser/utils/strptime.py
Expand Up @@ -14,6 +14,14 @@

MS_SEARCHER = re.compile(r'\.(?P<microsecond>[0-9]{1,6})')

def _exec_module(spec, module):
if hasattr(spec.loader, "exec_module"):
spec.loader.exec_module(module)
else:
# This can happen before Python 3.10
# if spec.loader is a zipimporter and the Python runtime is in a zipfile
code = spec.loader.get_code(module.__name__)
exec(code, module.__dict__)

def patch_strptime():
"""Monkey patching _strptime to avoid problems related with non-english
Expand All @@ -23,13 +31,12 @@ def patch_strptime():
any date since all languages are translated to english dates.
"""
_strptime_spec = importlib.util.find_spec('_strptime')

_strptime = importlib.util.module_from_spec(_strptime_spec)
_strptime_spec.loader.exec_module(_strptime)
_exec_module(_strptime_spec, _strptime)
sys.modules['strptime_patched'] = _strptime

_calendar = importlib.util.module_from_spec(_strptime_spec)
_strptime_spec.loader.exec_module(_calendar)
_exec_module(_strptime_spec, _calendar)
sys.modules['calendar_patched'] = _calendar

_strptime._getlang = lambda: ('en_US', 'UTF-8')
Expand Down

0 comments on commit 9b6c15a

Please sign in to comment.