Difference between revisions of "Python Problems"

From Official Kodi Wiki
Jump to navigation Jump to search
(Created page with "This page is meant to document Python issues of general interest to Kodi addon developers. == datetime.strptime == There is an old Python bug (https://bugs.python.org/issue274...")
 
Line 5: Line 5:
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 (script.module.youtube.dl crashed Kodi for a while).
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 (script.module.youtube.dl 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.
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 (https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior). 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 the attached patch.
This patch simply replaces datetime.strptime with time.strptime. They are nearly identical in function. For more information, see: https://github.com/fbacher/script.module.kutils/blob/master/lib/kutils/strptime_patch.py. Essentially the patch is:
 
<nowiki> @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
</nowiki>

Revision as of 18:14, 11 January 2022

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

datetime.strptime

There is an old Python bug (https://bugs.python.org/issue27400) 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 (script.module.youtube.dl 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 (https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior). 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: https://github.com/fbacher/script.module.kutils/blob/master/lib/kutils/strptime_patch.py. 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