Skip to content

RecursionError when add or subtract-ing with a fixed offset timezone #422

@euan-dietchef

Description

@euan-dietchef

The following:

import pendulum
pendulum.datetime(2019, 11, 1, tz=pendulum.tz.fixed_timezone(0)).subtract(seconds=1)
import dateutil, pendulum
pendulum.datetime(2019, 11, 1, tz=dateutil.tz.tzoffset(name=None, offset=0)).subtract(seconds=1)
import aniso8601, pendulum
pendulum.datetime(2019, 11, 1, tz=aniso8601.utcoffset.UTCOffset(minutes=0)).subtract(seconds=1)

all result in this RecursionError:

  ....
  File "/usr/local/lib/python3.8/site-packages/pendulum/tz/timezone.py", line 66, in convert
    return self._convert(dt)
  File "/usr/local/lib/python3.8/site-packages/pendulum/tz/timezone.py", line 334, in _convert
    return dt.astimezone(self)
  File "/usr/local/lib/python3.8/site-packages/pendulum/datetime.py", line 1466, in astimezone
    return pendulum.instance(super(DateTime, self).astimezone(tz))
  File "/usr/local/lib/python3.8/site-packages/pendulum/tz/timezone.py", line 345, in fromutc
    return (dt + self._utcoffset).replace(tzinfo=self)
  File "/usr/local/lib/python3.8/site-packages/pendulum/datetime.py", line 1442, in __add__
    return self._add_timedelta(other)
  File "/usr/local/lib/python3.8/site-packages/pendulum/datetime.py", line 750, in _add_timedelta
    return self.add(seconds=delta.total_seconds())
  File "/usr/local/lib/python3.8/site-packages/pendulum/datetime.py", line 660, in add
    dt = self.tz.convert(dt)
  File "/usr/local/lib/python3.8/site-packages/pendulum/tz/timezone.py", line 66, in convert
    return self._convert(dt)
  File "/usr/local/lib/python3.8/site-packages/pendulum/tz/timezone.py", line 334, in _convert
    return dt.astimezone(self)
  File "/usr/local/lib/python3.8/site-packages/pendulum/datetime.py", line 1466, in astimezone
    return pendulum.instance(super(DateTime, self).astimezone(tz))
  File "/usr/local/lib/python3.8/site-packages/pendulum/tz/timezone.py", line 345, in fromutc
    return (dt + self._utcoffset).replace(tzinfo=self)
  File "/usr/local/lib/python3.8/site-packages/pendulum/datetime.py", line 1442, in __add__
    return self._add_timedelta(other)
  File "/usr/local/lib/python3.8/site-packages/pendulum/datetime.py", line 750, in _add_timedelta
    return self.add(seconds=delta.total_seconds())
  File "/usr/local/lib/python3.8/site-packages/pendulum/datetime.py", line 621, in add
    offset = self.utcoffset()
RecursionError: maximum recursion depth exceeded

The actual date, offset, or number of seconds to add/subtract doesn't seem to matter, and using 'add' instead of subtract also causes the issue. A 'normal' timezone like 'Europe/London' won't cause the issue.

This came up with dates returned from our graphql API (which are parsed by graphene using aniso8601). Since all the fixed-offset tzinfo objects above trigger the same issue, I'm not sure what a good workaround would be, other than finding a 'normal' timezone that happens to have the same offset.

This is on CPython 3.8.0 on Linux, and I've tried with pendulum 2.0.4 and 2.0.5. I can't repro the issue on CPython 3.7.4, so I'm assuming it's 3.8-specific.

The closest existing issue I could find is #361 , but doesn't seem related - it's PyPy-specific, and in_timezone works okay on my example datetimes above.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions