Skip to content

BUG: Fix Segfault with pd.tslib.get_period_field (GH4519, GH4520) #4562

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

Merged
merged 1 commit into from
Aug 15, 2013
Merged
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
2 changes: 2 additions & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ pandas 0.13
- Fix arithmetic with series/datetimeindex and ``np.timedelta64`` not working the same (:issue:`4134`)
and buggy timedelta in numpy 1.6 (:issue:`4135`)
- Fix bug in ``pd.read_clipboard`` on windows with PY3 (:issue:`4561`); not decoding properly
- ``tslib.get_period_field()`` and ``tslib.get_period_field_arr()`` now raise
if code argument out of range (:issue:`4519`, :issue:`4520`)

pandas 0.12
===========
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/test_tseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,16 @@ def test_to_datetime_bijective(self):
self.assertEqual(Timestamp(Timestamp.max.to_pydatetime()).value/1000, Timestamp.max.value/1000)
self.assertEqual(Timestamp(Timestamp.min.to_pydatetime()).value/1000, Timestamp.min.value/1000)

class TestPeriodField(unittest.TestCase):

def test_get_period_field_raises_on_out_of_range(self):
from pandas import tslib
self.assertRaises(ValueError, tslib.get_period_field, -1, 0, 0)

def test_get_period_field_array_raises_on_out_of_range(self):
from pandas import tslib
self.assertRaises(ValueError, tslib.get_period_field_arr, -1, np.empty(1), 0)

if __name__ == '__main__':
import nose
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],
Expand Down
7 changes: 5 additions & 2 deletions pandas/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2419,6 +2419,8 @@ ctypedef int (*accessor)(int64_t ordinal, int freq) except INT32_MIN

def get_period_field(int code, int64_t value, int freq):
cdef accessor f = _get_accessor_func(code)
if f is NULL:
Copy link
Contributor

Choose a reason for hiding this comment

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

FYI I think you can just declare this function: cdef accessor get_accessor_func(int code) except -1: and then an exception occuring int the cdef'd function will propogate up the stack

raise ValueError('Unrecognized period code: %d' % code)
return f(value, freq)

def get_period_field_arr(int code, ndarray[int64_t] arr, int freq):
Expand All @@ -2428,6 +2430,8 @@ def get_period_field_arr(int code, ndarray[int64_t] arr, int freq):
accessor f

f = _get_accessor_func(code)
if f is NULL:
raise ValueError('Unrecognized period code: %d' % code)

sz = len(arr)
out = np.empty(sz, dtype=np.int64)
Expand Down Expand Up @@ -2462,8 +2466,7 @@ cdef accessor _get_accessor_func(int code):
return &pday_of_year
elif code == 10:
return &pweekday
else:
raise ValueError('Unrecognized code: %s' % code)
return NULL


def extract_ordinals(ndarray[object] values, freq):
Expand Down