Skip to content

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

ashwani-dumca
Copy link

#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.

@@ -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);
Copy link
Member

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?

Copy link
Author

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.

@ashwani-dumca
Copy link
Author

build failure is not related to this change.

@ashwani-dumca
Copy link
Author

@eric-wieser : code changes looks good now ?

Comment on lines -2126 to +2128
if (tmp == NULL) {
if (PyObject_GetOptionalAttr(obj, npy_interned_str.hour, &tmp) == -1) {
Copy link
Member

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.

Copy link
Author

@ashwani-dumca ashwani-dumca Sep 25, 2024

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)

Copy link
Member

@eric-wieser eric-wieser Sep 26, 2024

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

@charris charris changed the title EnH:ISSUE-27120: Changed in the datetime.c ENH: Use PyObject_GetAttrString in the datetime.c Sep 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Pending authors' response
Development

Successfully merging this pull request may close these issues.

2 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy