Skip to content

Catch SystemError in py3 Period.__richcmp__ #18205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion pandas/_libs/period.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,12 @@ cdef class _Period(object):

def __richcmp__(self, other, op):
if is_period_object(other):
if other.freq != self.freq:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why does other.freq != self.freq ever raise?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your guess is as good as mine. Googling turned up some similar errors in Django, a suggestion that the GIL is involved. Hopefully we'll get to the root of the problem eventually. For now I'll be happy to just raise the right error.

Ref #17112, or to reproduce in py3:

>>> ser = pd.Series([pd.Period('2017'), pd.Period('2016Q1')])
>>> ser.sort_values()
[...]
SystemError: <built-in function isinstance> returned a result with an error set

try:
if other.freq != self.freq:
msg = _DIFFERENT_FREQ.format(self.freqstr, other.freqstr)
raise IncompatibleFrequency(msg)
except SystemError:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think SystemError exists in py2?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least in 2.7.14 it does. I don't know about earlier off the top.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's there in 2.7.12 and 2.7.9

# See GH#17112 in python3
msg = _DIFFERENT_FREQ.format(self.freqstr, other.freqstr)
raise IncompatibleFrequency(msg)
return PyObject_RichCompareBool(self.ordinal, other.ordinal, op)
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/scalar/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -1418,3 +1418,12 @@ def test_period_immutable():
freq = per.freq
with pytest.raises(AttributeError):
per.freq = 2 * freq


def test_comparison_catches_system_error():
# see GH#17112 in py3 this comparison could raise SystemError instead
# of IncompatibleFrequency
per1 = pd.Period('2014Q1')
per2 = pd.Period('2015', freq='A')
with pytest.raises(period.IncompatibleFrequency):
per1 < per2