Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workaround for zipimporter not having exec_module before Python 3.10 #1069

Merged
merged 1 commit into from Dec 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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__)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This, in essence, is what loader.exec_module does.

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