Skip to content

Commit 3a0513c

Browse files
committed
Fix an error when comparing a Period to a timedelta in PyPy
1 parent 1e46765 commit 3a0513c

File tree

6 files changed

+33
-2
lines changed

6 files changed

+33
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ nosetests.xml
1919

2020
.DS_Store
2121
.idea/*
22+
.python-version
2223

2324
/test.py
2425
/test_*.*

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ matrix:
1919
- python: 3.6
2020
env: PENDULUM_EXTENSIONS=0
2121
- python: pypy
22+
- python: pypy3
2223

2324
before_install:
2425
- pip install codecov

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Change Log
22

3+
## [Unreleased]
4+
5+
### Fixed
6+
7+
- Fixed an error when comparing a Period to a timedelta in PyPy.
8+
9+
310
## [1.4.0] - 2018-01-22
411

512
### Changed

pendulum/period.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import operator
44
import pendulum
55

6-
from datetime import datetime, date
6+
from datetime import datetime, date, timedelta
77

88
from .mixins.interval import WordableIntervalMixin
99
from .interval import BaseInterval, Interval
@@ -300,6 +300,17 @@ def __repr__(self):
300300
self._start, self._end
301301
)
302302

303+
def _cmp(self, other):
304+
# Only needed for PyPy3
305+
assert isinstance(other, timedelta)
306+
307+
if isinstance(other, Period):
308+
other = other.as_timedelta()
309+
310+
td = self.as_timedelta()
311+
312+
return 0 if td == other else 1 if td > other else -1
313+
303314
def _getstate(self, protocol=3):
304315
start, end = self.start, self.end
305316

tests/period_tests/test_behavior.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import pickle
44
import pendulum
5+
6+
from datetime import timedelta
7+
58
from .. import AbstractTestCase
69

710

@@ -34,3 +37,11 @@ def test_pickle(self):
3437
self.assertEqual(p.start, p2.start)
3538
self.assertEqual(p.end, p2.end)
3639
self.assertEqual(p.invert, p2.invert)
40+
41+
def test_comparison_to_timedelta(self):
42+
dt1 = pendulum.create(2016, 11, 18)
43+
dt2 = pendulum.create(2016, 11, 20)
44+
45+
period = dt2 - dt1
46+
47+
assert period < timedelta(days=4)

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[tox]
2-
envlist = py{27,35,36}-{without,with}-extensions, pypy
2+
envlist = py{27,35,36}-{without,with}-extensions, pypy, pypy3
33

44
[testenv]
55
deps = -rtests-requirements.txt

0 commit comments

Comments
 (0)