arrow-py / arrow

🏹 Better dates & times for Python
https://arrow.readthedocs.io
Apache License 2.0
8.67k stars 668 forks source link

'datetime.timedelta' object has no attribute 'tzinfo' on windows 10 #738

Closed razzeee closed 4 years ago

razzeee commented 4 years ago

Issue Description

This is a bit specific, but I'm grasping for straws at this point. So I'm using arrow via a kodi script. And it works without problems on linux. A team member tried using my script on windows 10, he get's this stack trace.

2019-12-14 10:29:01.961 T:1472   ERROR: EXCEPTION Thrown (PythonToCppException) : -->Python callback/script returned the following error<--
                                             - NOTE: IGNORING THIS CAN LEAD TO MEMORY LEAKS!
                                            Error Type: <class 'AttributeError'>
                                            Error Contents: 'datetime.timedelta' object has no attribute 'tzinfo'
                                            Traceback (most recent call last):
                                              File "C:\KODI\KODI- v19 nightlies\portable_data\addons\metadata.tvdb.com.python\scraper.py", line 3, in <module>
                                                from resources.lib import actions
                                              File "C:\KODI\KODI- v19 nightlies\portable_data\addons\metadata.tvdb.com.python\resources\lib\actions.py", line 10, in <module>
                                                from . import series
                                              File "C:\KODI\KODI- v19 nightlies\portable_data\addons\metadata.tvdb.com.python\resources\lib\series.py", line 11, in <module>
                                                from .ratings import ratings
                                              File "C:\KODI\KODI- v19 nightlies\portable_data\addons\metadata.tvdb.com.python\resources\lib\ratings.py", line 6, in <module>
                                                from .trakt_rating import get_trakt_rating_and_votes
                                              File "C:\KODI\KODI- v19 nightlies\portable_data\addons\metadata.tvdb.com.python\resources\lib\trakt_rating.py", line 1, in <module>
                                                from trakt import Trakt
                                              File "C:\KODI\KODI- v19 nightlies\portable_data\addons\script.module.trakt\lib\trakt\__init__.py", line 3, in <module>
                                                from trakt.client import TraktClient
                                              File "C:\KODI\KODI- v19 nightlies\portable_data\addons\script.module.trakt\lib\trakt\client.py", line 3, in <module>
                                                from trakt.core.configuration import ConfigurationManager
                                              File "C:\KODI\KODI- v19 nightlies\portable_data\addons\script.module.trakt\lib\trakt\core\configuration.py", line 3, in <module>
                                                from trakt.core.context_collection import ContextCollection
                                              File "C:\KODI\KODI- v19 nightlies\portable_data\addons\script.module.trakt\lib\trakt\core\context_collection.py", line 3, in <module>
                                                from trakt.core.helpers import synchronized
                                              File "C:\KODI\KODI- v19 nightlies\portable_data\addons\script.module.trakt\lib\trakt\core\helpers.py", line 9, in <module>
                                                import arrow
                                              File "C:\KODI\KODI- v19 nightlies\portable_data\addons\script.module.arrow\lib\arrow\__init__.py", line 3, in <module>
                                                from .api import get, now, utcnow
                                              File "C:\KODI\KODI- v19 nightlies\portable_data\addons\script.module.arrow\lib\arrow\api.py", line 10, in <module>
                                                from arrow.factory import ArrowFactory
                                              File "C:\KODI\KODI- v19 nightlies\portable_data\addons\script.module.arrow\lib\arrow\factory.py", line 19, in <module>
                                                from arrow.arrow import Arrow
                                              File "C:\KODI\KODI- v19 nightlies\portable_data\addons\script.module.arrow\lib\arrow\arrow.py", line 1396, in <module>
                                                Arrow.max = Arrow.fromdatetime(datetime.max)
                                              File "C:\KODI\KODI- v19 nightlies\portable_data\addons\script.module.arrow\lib\arrow\arrow.py", line 217, in fromdatetime
                                                if dt.tzinfo is None:
                                            AttributeError: 'datetime.timedelta' object has no attribute 'tzinfo'
                                            -->End of Python script error report<--

System Info

jadchaar commented 4 years ago

Hmm thanks for reporting this, it seems like a very peculiar bug. It seems like datetime.max on Windows is producing a timedelta for some reason.

@systemcatch if you have a Windows 10 machine with Python installed, mind checking out the output of the following:

env ❯ python3
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 16:52:21)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import arrow
>>> from datetime import datetime, timedelta
>>> arrow.Arrow.fromdatetime(datetime.max)
<Arrow [9999-12-31T23:59:59.999999+00:00]>
>>> datetime.max
datetime.datetime(9999, 12, 31, 23, 59, 59, 999999)
>>> type(datetime.max)
<class 'datetime.datetime'>
>>> timedelta.max
datetime.timedelta(days=999999999, seconds=86399, microseconds=999999)
>>> type(timedelta.max)
<class 'datetime.timedelta'>

@Razzeee do you know what particular set of actions led to this error occurring?

razzeee commented 4 years ago

It seems to happen on import, as the stacktrace shows. I don't think any specific logic is running at that point.

here's a finding from another team member, just to log it, feel free to ignore it:

spooky. That Python error is caused by datetime.max returning an object of type "bytes" instead of a datetime. I can reproduce it (on Windows). Further testing shows sometimes this happens to datetime.min, and sometimes either of them can also return an object of type datetime.timedelta.

jadchaar commented 4 years ago

Thanks for the details. Is he able to completely reproduce this for both datetime.min and datetime.max? Or is it sometimes the case that it returns bytes and sometimes the case that it returns a datetime.timedelta object? Also are these two different team members?

I am looking through the cpython datetime source code and I am wondering if this could potentially be an issue with the compiled version of Python for Windows (definitely the worst case, but a potential lead).

razzeee commented 4 years ago

It's a different team member, but please keep in mind, that we might deal with a bug in the python version we include in kodi. Which might have patches. I ask him for more info on the sometimes stuff.

Also the first team member I mentioned, had no problems with my script some days ago, then it broke. He might have updated to the latest nightly. I'll let him try an older build, just to make sure.

systemcatch commented 4 years ago

@jadchaar here's the output you wanted, running on Microsoft Windows 10 Home, Version 10.0.17763 Build 17763

(arrow) C:\Users\Chris>python
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import arrow
>>> arrow.__version__
'0.15.4'
>>> from datetime import datetime, timedelta
>>> arrow.Arrow.fromdatetime(datetime.max)
<Arrow [9999-12-31T23:59:59.999999+00:00]>
>>> datetime.max
datetime.datetime(9999, 12, 31, 23, 59, 59, 999999)
>>> type(datetime.max)
<class 'datetime.datetime'>
>>> timedelta.max
datetime.timedelta(days=999999999, seconds=86399, microseconds=999999)
>>> type(timedelta.max)
<class 'datetime.timedelta'>
jadchaar commented 4 years ago

Any updates on this @Razzeee?

razzeee commented 4 years ago

We're investigating, at this point I'm pretty convinced, that it's due to an implementation bug on our side. The windows guys are still working on it.

You can close this issue for now. Thank you.

On Sat, 21 Dec 2019, 14:32 Jad Chaar, notifications@github.com wrote:

Any updates on this @Razzeee https://github.com/Razzeee?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/crsmithdev/arrow/issues/738?email_source=notifications&email_token=ABNLEZDX45MZRXFOZF4Q6F3QZYLGDA5CNFSM4J2X2L32YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEHO4FTA#issuecomment-568181452, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABNLEZAIHGXEJXURLEK7QHTQZYLGDANCNFSM4J2X2L3Q .

jadchaar commented 4 years ago

Please keep us updated :). I will close this for now, but please feel free to re-open it if the issue persists so we can investigate further.

SoundsSerious commented 3 years ago

Also seeing this bug on windows system linux

Is there a stable version of arrow that doesn't exhibit this behavior?

jadchaar commented 3 years ago

Also seeing this bug on windows system linux

Is there a stable version of arrow that doesn't exhibit this behavior?

Could you post some more information on the issue you are seeing? E.g.a snippet of code that is triggering the exception?

That will help us debug this better :).

syd3n commented 3 years ago

I am seeing this error message on Windows when trying to install a Kodi addon/script (one which @Razzeee is a contributor for) - so not sure if it is specific to Arrow or the script itself:

2021-03-09 15:44:33.689 T:23256   ERROR <general>: EXCEPTION Thrown (PythonToCppException) : -->Python callback/script returned the following error<--
                                                    - NOTE: IGNORING THIS CAN LEAD TO MEMORY LEAKS!
                                                   Error Type: <class 'AttributeError'>
                                                   Error Contents: 'datetime.timedelta' object has no attribute 'tzinfo'
                                                   Traceback (most recent call last):
                                                     File "C:\Users\myusername\AppData\Roaming\Kodi\addons\script.trakt\default.py", line 6, in <module>
                                                       from resources.lib.service import traktService
                                                     File "C:\Users\myusername\AppData\Roaming\Kodi\addons\script.trakt\resources\lib\service.py", line 20, in <module>
                                                       from resources.lib.traktapi import traktAPI
                                                     File "C:\Users\myusername\AppData\Roaming\Kodi\addons\script.trakt\resources\lib\traktapi.py", line 17, in <module>
                                                       from trakt import Trakt
                                                     File "C:\Users\myusername\AppData\Roaming\Kodi\addons\script.module.trakt\lib\trakt\__init__.py", line 3, in <module>
                                                       from trakt.client import TraktClient
                                                     File "C:\Users\myusername\AppData\Roaming\Kodi\addons\script.module.trakt\lib\trakt\client.py", line 3, in <module>
                                                       from trakt.core.configuration import ConfigurationManager
                                                     File "C:\Users\myusername\AppData\Roaming\Kodi\addons\script.module.trakt\lib\trakt\core\configuration.py", line 3, in <module>
                                                       from trakt.core.context_collection import ContextCollection
                                                     File "C:\Users\myusername\AppData\Roaming\Kodi\addons\script.module.trakt\lib\trakt\core\context_collection.py", line 3, in <module>
                                                       from trakt.core.helpers import synchronized
                                                     File "C:\Users\myusername\AppData\Roaming\Kodi\addons\script.module.trakt\lib\trakt\core\helpers.py", line 9, in <module>
                                                       import arrow
                                                     File "C:\Users\myusername\AppData\Roaming\Kodi\addons\script.module.arrow\lib\arrow\__init__.py", line 3, in <module>
                                                       from .api import get, now, utcnow
                                                     File "C:\Users\myusername\AppData\Roaming\Kodi\addons\script.module.arrow\lib\arrow\api.py", line 10, in <module>
                                                       from arrow.factory import ArrowFactory
                                                     File "C:\Users\myusername\AppData\Roaming\Kodi\addons\script.module.arrow\lib\arrow\factory.py", line 19, in <module>
                                                       from arrow.arrow import Arrow
                                                     File "C:\Users\myusername\AppData\Roaming\Kodi\addons\script.module.arrow\lib\arrow\arrow.py", line 1396, in <module>
                                                       Arrow.max = Arrow.fromdatetime(datetime.max)
                                                     File "C:\Users\myusername\AppData\Roaming\Kodi\addons\script.module.arrow\lib\arrow\arrow.py", line 217, in fromdatetime
                                                       if dt.tzinfo is None:
                                                   AttributeError: 'datetime.timedelta' object has no attribute 'tzinfo'
                                                   -->End of Python script error report<--
systemcatch commented 3 years ago

Hey @syd3n what version of arrow are you using? Try upgrading to the latest release (1.0.3) if possible.

Also can you point me to the addon script's source code and tell me what version of python you're using?

syd3n commented 3 years ago

@systemcatch thanks for the response!

Here's the addon script I was installing within Kodi: https://github.com/Razzeee/script.trakt

syd3n commented 3 years ago

Actually, I believe it is this one as it has more recent commits: https://github.com/trakt/script.trakt

razzeee commented 3 years ago

That's not the correct source (hopefully) Should be https://github.com/Razzeee/script.trakt/tree/3.1.12

But arrow is used in a dependency, which I packaged here: https://github.com/Razzeee/script.module.trakt/blob/master/addon.xml

Problem with updating is right now it depends on version < 1.0.0 https://github.com/fuzeman/trakt.py/blob/master/requirements.txt

systemcatch commented 3 years ago

Woah 0.10.0 is pretty ancient! :sweat_smile:

What python versions do you need to support? Latest 1.0.3 is python 3.6+ only but has specific fixes for datetime.max on Windows.

syd3n commented 3 years ago

That's not the correct source (hopefully) Should be https://github.com/Razzeee/script.trakt/tree/3.1.12

Thank you for correcting - I am confirming that I installed from the official kodi repository.

razzeee commented 3 years ago

I think 3.8+ would be fine for us. But I think trakt.py itself wants to be py2 and py3. Not sure if that's still what they want, as it has been like that for years.

systemcatch commented 3 years ago

@Razzeee well in that case 0.17.0 is the last version they could use, still a big improvement over 0.10.0 though!

razzeee commented 3 years ago

I think in reality, we end up using 0.15.5 https://github.com/Razzeee/script.module.arrow/blob/master/addon.xml

MarkusEh commented 2 years ago

Hi,

I checked in git, and if tzinfo is None: if dt.tzinfo is None: tzinfo = dateutil_tz.tzutc() else: tzinfo = dt.tzinfo is still part of "https://github.com/arrow-py/arrow/blob/master/arrow/arrow.py". On my rpi3 system: Kodi (19.4 (19.4.0) Git:20220315-c9de65b422). Platform: Linux ARM 32-bit, `2022-04-01 06:00:41.538 T:3290 ERROR : EXCEPTION Thrown (PythonToCppException) : -->Python callback/script returned the following error<--

Same error also have others, see https://github.com/xbmc/xbmc/issues/17311

thezoggy commented 2 years ago

still running into this periodically on win10 with kodi's script.module.arrow addon

thezoggy commented 2 years ago

fix noted here: https://kodi.wiki/view/Python_Problems

wesnm commented 2 years ago

fix noted here: https://kodi.wiki/view/Python_Problems

I don't think that's the fix for the "timedelta" bug. What's happening in this case is that datetime.datetime.max is being corrupted and replaced by a datetime.timedelta object.

One simple workaround is to replace the last two lines of arrow.py as such:

Arrow.min = Arrow.fromdatetime(dt_datetime.min) Arrow.max = Arrow.fromdatetime(dt_datetime.max)

with

Arrow.min = Arrow.fromdatetime(dt_datetime(1, 1, 1, 0, 0)) Arrow.max = Arrow.fromdatetime(dt_datetime(9999, 12, 31, 23, 59, 59, 999999))

Which isn't perfect, but it works in this case and is unlikely to ever be wrong unless datetime objects support dates further into the past or future.