Skip to content

gh-92869: ctypes: Add c_time_t #92870

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 15 commits into from
Jul 3, 2022
Merged

gh-92869: ctypes: Add c_time_t #92870

merged 15 commits into from
Jul 3, 2022

Conversation

thp
Copy link
Contributor

@thp thp commented May 17, 2022

This implements a simple way of using ctypes.c_time_t for 32-bit and 64-bit time_t sizes. See #92869 for a discussion on why this is useful and considerations for different platforms. This doesn't take into account platforms where time_t is defined as double or anything else that's not a signed integer.

@ghost
Copy link

ghost commented May 17, 2022

All commit authors signed the Contributor License Agreement.
CLA signed

@thp
Copy link
Contributor Author

thp commented Jun 10, 2022

Bump after 24 days of no activity. Any takers for reviewing this PR?

@m-tmatma
Copy link
Contributor

m-tmatma commented Jun 10, 2022

@thp
It seems azure pipeline test was failed.
You should re-trigger it.

@thp
Copy link
Contributor Author

thp commented Jun 10, 2022

@thp
It seems azure pipeline test was failed.
You should re-trigger it.

Is there a way to manually re-trigger it? I don't seem to have permissions. I can of course amend the commits and force-push to indirectly re-trigger a build?

@arhadthedev
Copy link
Member

Is there a way to manually re-trigger it

You can close and reopen the PR.

@thp thp closed this Jun 10, 2022
@thp thp reopened this Jun 10, 2022
@m-tmatma
Copy link
Contributor

@thp

Is there a way to manually re-trigger it? I don't seem to have permissions. I can of course amend the commits and force-push to indirectly re-trigger a build?

There is another way to re-trigger.

You can comment /AzurePipelines run or /azp run to a PR.
https://docs.microsoft.com/en-us/azure/devops/pipelines/repos/github?view=azure-devops&tabs=yaml#comment-triggers

Copy link
Member

@AA-Turner AA-Turner left a comment

Choose a reason for hiding this comment

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

Seems reasonable, please add tests though.

A

@AA-Turner
Copy link
Member

Pinging listed names from the experts index for review.

A

@AA-Turner AA-Turner added the type-feature A feature request or enhancement label Jun 11, 2022
thp and others added 4 commits June 11, 2022 20:35
…oBkw.rst

Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
@thp
Copy link
Contributor Author

thp commented Jun 11, 2022

Found that the ctypes documentation's "Calling functions" documentation does this (without setting restype):

>>> print(libc.time(None))  
1150640792

This uses the default return type of int, which is wrong on systems that use 64-bit time_t (and just a happy coincidence on systems that use 32-bit time_t and happen to have 32-bit int):

>>> from ctypes import *
>>> libc = CDLL("libc.so.6")
>>> libc.time
<_FuncPtr object at 0x...>
>>> libc.time.restype
<class 'ctypes.c_int'>

Updated the documentation accordingly with setting libc.time.restype to the newly-added c_time_t, even though this introduces the concept of restype and argtypes quite early, but it's probably for the best, as not doing it can result in wrong return types as seen here. Alternatively, another libc function could be used for the "Calling functions" section, and libc.time used as an example for the "Return types" section.

The libc.time.restype == c_int error will be noticeable from 2038 onwards due to 32-bit overflow, at least on systems that have 64-bit time_t and 32-bit int (all modern systems?).

@AA-Turner
Copy link
Member

Alternatively, another libc function could be used for the "Calling functions" section, and libc.time used as an example for the "Return types" section.

I think this is a better approach. (Thanks for noticing!)

A

@thp
Copy link
Contributor Author

thp commented Jun 12, 2022

Alternatively, another libc function could be used for the "Calling functions" section, and libc.time used as an example for the "Return types" section.

I think this is a better approach. (Thanks for noticing!)

Moved the time() function into the return type docs, and used rand() from libc instead (which conveniently has no arguments and returns a C int).

@thp
Copy link
Contributor Author

thp commented Jun 22, 2022

Merged upstream main branch, as there was a merge conflict shown in the Github Web UI.

Review/feedback/merge appreciated.

@thp
Copy link
Contributor Author

thp commented Jul 2, 2022

Bump after 21 days of no feedback.

I know this isn't high priority, but maybe someone can review/approve this. Or just any outlook like "I won't have time to look into this before after summer".

If I should stop bumping this PR, please let me know - I hope the practice of waiting at least 20 days before nagging again is a good compromise between not being too annoying but still maybe getting it in front of the right people to review.

Copy link
Member

@AA-Turner AA-Turner left a comment

Choose a reason for hiding this comment

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

This looks good from my perspective, but I'm not a ctypes specialist. I've asked if a core dev can review.

A

Copy link
Contributor

@kumaraditya303 kumaraditya303 left a comment

Choose a reason for hiding this comment

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

LGTM

I also removed the 32 or 64 bit wording as that isn't really important. it abstracts that from the user to use the right size.
@gpshead gpshead self-assigned this Jul 3, 2022
@gpshead gpshead merged commit b296c74 into python:main Jul 3, 2022
@gpshead
Copy link
Member

gpshead commented Jul 3, 2022

apologies about the commit message, i think i misattributed primary authorship in there by misreading github. Thanks for the PR!

@AA-Turner
Copy link
Member

To confirm this was all @thp's work! Thanks for the review & merge @gpshead.

A

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-ctypes type-feature A feature request or enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants