Skip to content

Commit

Permalink
Fix numpy.datetime64 reference handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ijl committed Sep 7, 2023
1 parent d1cd27e commit a52679a
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/serialize/numpy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1137,17 +1137,15 @@ impl NumpyDatetimeUnit {
fn from_pyobject(ptr: *mut PyObject) -> Self {
let dtype = ffi!(PyObject_GetAttr(ptr, DTYPE_STR));
let descr = ffi!(PyObject_GetAttr(dtype, DESCR_STR));
ffi!(Py_DECREF(dtype));
let el0 = ffi!(PyList_GET_ITEM(descr, 0));
ffi!(Py_DECREF(descr));
let descr_str = ffi!(PyTuple_GET_ITEM(el0, 1));
let uni = crate::str::unicode_to_str(descr_str).unwrap();
if uni.len() < 5 {
return Self::NaT;
}
// unit descriptions are found at
// https://github.com/numpy/numpy/blob/b235f9e701e14ed6f6f6dcba885f7986a833743f/numpy/core/src/multiarray/datetime.c#L79-L96.
match &uni[4..uni.len() - 1] {
let ret = match &uni[4..uni.len() - 1] {
"Y" => Self::Years,
"M" => Self::Months,
"W" => Self::Weeks,
Expand All @@ -1163,7 +1161,10 @@ impl NumpyDatetimeUnit {
"as" => Self::Attoseconds,
"generic" => Self::Generic,
_ => unreachable!(),
}
};
ffi!(Py_DECREF(dtype));
ffi!(Py_DECREF(descr));
ret
}

/// Return a `NumpyDatetime64Repr` for a value in array with this unit.
Expand Down

0 comments on commit a52679a

Please sign in to comment.