Fix full-day Durations collapsing to zero on PyPy (#876)#980
Open
uttam12331 wants to merge 1 commit into
Open
Conversation
On PyPy, `Duration.total_seconds()` is overridden to compute the total from `_days`/`_seconds`/`_microseconds`/etc. But `Duration.__new__` called `self.total_seconds()` to derive `_total` *before* those attributes were populated (they are still at their class defaults of 0 at that point), so the computed total was 0 and the whole duration collapsed to `Duration()`. As a result `duration(days=1).total_seconds()` returned 0 on PyPy. Derive the total from `timedelta.total_seconds(self)` instead, which reads the already-initialized underlying `timedelta` value and is independent of the PyPy override. Behavior on CPython is unchanged (there the method is not overridden, so both calls are equivalent). Closes python-pendulum#876
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #876.
On PyPy,
duration(days=1)(and any whole-day / full-value duration) returned an emptyDuration()withtotal_seconds() == 0.Root cause
On PyPy,
Durationdefines its owntotal_seconds()(theif PYPY:branch) that computes the total fromself._years,self._months,self._weeks/self._remaining_days/self._days,self._secondsandself._microseconds.But
Duration.__new__usedself.total_seconds()to deriveself._total:at a point before those
_days/_seconds/… attributes are assigned (a few lines later). During construction they are still at their class defaults of0, so on PyPytotal_seconds()returns0,_totalbecomes0, and the subsequent normalization zeroes out the whole duration. On CPython the method isn't overridden, sotimedelta.total_seconds()runs and the value is correct — which is why the bug is PyPy-only.Fix
Derive the total from
timedelta.total_seconds(self), which reads the already-initialized underlyingtimedeltavalue and is independent of the PyPy override. On CPython this is equivalent to the previous call, so behavior there is unchanged.Tests
Added
test_new_does_not_depend_on_total_seconds_override, which runs on CPython: it monkeypatchesDuration.total_secondsto return0.0(mimicking the PyPy override reading not-yet-set attributes) and assertsduration(days=1)._total == 86400.0. It fails onmaster(_total == 0.0) and passes with this change. Fulltests/duration/suite passes (48 passed);ruff check/ruff formatclean.