Python Problems

From Official Kodi Wiki
Revision as of 18:14, 11 January 2022 by Fbacher (talk | contribs)
Jump to navigation Jump to search

This page is meant to document Python issues of general interest to Kodi addon developers.


There is an old Python bug ( which ONLY impacts embedded python applications, such as the Kodi Python environment. The issue is that datetime.strptime is only initialized once per process and not every time the embedded environment is reinitialized. This causes datetime.strptime to return None and perhaps other strange behavior.

One option is for you to replace every reference to datetime.strptime to use a strptime_patch attached to this page. This is less voodoo, but there is always the possibility that some library code uses strptime and there will still be potential for incorrect results or a Kodi crash ( crashed Kodi for a while).

The other option is to monkey-patch datetime.strptime so that any user of the python runtime will use it. It is voodoo like, but that is why Python supports Monkey patching. See Python documentation on datetime.strptime ( It discusses some of the differences between datetime.strptime and time.strptime.

This patch simply replaces datetime.strptime with time.strptime. They are nearly identical in function. For more information, see: Essentially the patch is:

@staticmethod def monkey_patch_strptime(): # Check if problem exists (don't want to stomp on patch applied earlier) try: datetime.datetime.strptime('0', '%H') except TypeError: # Globally replace Python's datetime.datetime.strptime with # the version here. datetime.datetime = StripTimePatch.strptime @staticmethod def strptime(date_string: str, date_format: str) -> datetime.datetime: result: datetime.datetime result = datetime.datetime(*(time.strptime(date_string, date_format)[0:6])) return result