Skip to content

Conversation

@WillAyd WillAyd requested a review from mroeschke as a code owner December 1, 2023 04:59

// Scales value inplace from nanosecond resolution to unit resolution
int scaleNanosecToUnit(npy_int64 *value, NPY_DATETIMEUNIT unit);
int scaleNanosecToUnit(int64_t *value, NPY_DATETIMEUNIT unit);
Copy link
Member Author

Choose a reason for hiding this comment

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

@phofl pointed out on macOS that npy_int64 is a typedef for a long whereas int64_t is a typedef for a long long. Unfortunately referencing these via pointer violates the strict aliasing rule

For now just picked int64_t as it was bigger. I'm not sure in practice that this matters to much, but we do freely mix these uses today

if (PyTypeNum_ISDATETIME(type_num)) {
is_datetimelike = 1;
i8date = *(npy_int64 *)dataptr;
i8date = *(int64_t *)dataptr;
Copy link
Member Author

Choose a reason for hiding this comment

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

This is not a safe cast, but leaving to another PR to fix. The proper way to do this would be via memcpy

Copy link
Contributor

Choose a reason for hiding this comment

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

I think @seberg said the cast was fine, when I added this in #56114.

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 is likely pedantic but I'm fairly certain this is a strict aliasing violation. Where/when that matters may be up for debate. The memcpy from that comment would seemingly be safer

@WillAyd
Copy link
Member Author

WillAyd commented Dec 5, 2023

Any feedback on this? Would love to get this one in before any other PRs that touch extensions

// value exceeds number of bits that significand can hold
int64_t value = PyObject_HasAttrString(obj, "_value")
? get_long_attr(obj, "_value")
: (int64_t)total_seconds(obj) * 1000000000LL;
Copy link
Member

Choose a reason for hiding this comment

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

Does the int64_t cast apply to total_seconds(obj) first? I think this would truncate e.g. Timedelta(milliseconds=1) to 0 first before multiplying by 1000000000LL?

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh nice catch - I think you are right about that. Let me see if we can add a test for that

@mroeschke mroeschke added the CI Continuous Integration label Dec 5, 2023
@mroeschke mroeschke added this to the 2.2 milestone Dec 6, 2023
@mroeschke mroeschke merged commit 7808ecf into pandas-dev:main Dec 6, 2023
@mroeschke
Copy link
Member

Great to have this back running @WillAyd

@WillAyd WillAyd deleted the ci-werror branch December 10, 2023 03:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI Continuous Integration

3 participants