-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
ENH: Use PyObject_GetAttrString in the datetime.c #27449
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
base: main
Are you sure you want to change the base?
ENH: Use PyObject_GetAttrString in the datetime.c #27449
Conversation
@@ -2076,8 +2080,8 @@ NpyDatetime_ConvertPyDateTimeToDatetimeStruct( | |||
Py_DECREF(tmp); | |||
|
|||
/* Get the month */ | |||
tmp = PyObject_GetAttrString(obj, "month"); | |||
if (tmp == NULL) { | |||
res = PyObject_GetOptionalAttr(obj, npy_interned_str.month, &tmp); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of interning all these, can you use PyObject_GetOptionalAttrString
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @eric-wieser , Thank You for the feedback.
the interned version would help us get faster as we would be saving time, compare to the PyObject_GetOptionalAttrString which will be initialising the new string every time.
build failure is not related to this change. |
@eric-wieser : code changes looks good now ? |
if (tmp == NULL) { | ||
if (PyObject_GetOptionalAttr(obj, npy_interned_str.hour, &tmp) == -1) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This still isn't right; if this returns 0, then you need to return the date-only version. Continuing with tmp
not set as you currently do is a bug.
Perhaps it would be best to write some tests before going further; to be able to see the impact here, you need to make some custom objects, which:
- Throw errors when
.hour
is accessed - Succeed the first time that
.hour
is accessed, but fails the second time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, what i thought is PyObject_HasAttrString will be good to check for the 0 cases ?
if (!PyObject_HasAttrString(obj, "hour") || !PyObject_HasAttrString(obj, "minute") || !PyObject_HasAttrString(obj, "second") || !PyObject_HasAttrString(obj, "microsecond")) { /* The best unit for date is 'D' */ if (out_bestunit != NULL) { *out_bestunit = NPY_FR_D; } return 0; }
-1 we are handling with : if (PyObject_GetOptionalAttr(obj, npy_interned_str.hour, &tmp) == -1)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is possible for HasAttr
to return true, but for the attribute to not be present when you ask a second time:
class OhNo:
_count = 0
@property
def date(self):
self._count += 1
if self._count % 2 == 0:
raise AttributeError
else:
return "today"
o = OhNo()
print(hasattr(o, "date")) # prints True
print(o.date) # fails
#27120
So we are using PyObject_GetAttrString, which will take some extra time while loading up the entries, so have fixed this for 1 file datetime.c,
Have tested this all the test cases are working fine post this changes so we are good.