Skip to content
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

gh-117398: datetime: Access C-API via PyInterpreterState #118357

Closed
wants to merge 24 commits into from
Closed

gh-117398: datetime: Access C-API via PyInterpreterState #118357

wants to merge 24 commits into from

Conversation

neonene
Copy link
Contributor

@neonene neonene commented Apr 27, 2024

An alternative PR to #117413, which also makes the datetime C-API available to the isolated subinterpreters.

The pointer to the PyDateTime_CAPI struct is stored in PyInterpreterState right after the PyCapsule_Import() call. This version does not change Modules/_testcapi/datetime.c for now. Also, the overhead should be even across interpreters. However, this extends the PyDateTime_CAPI struct.

cc @pganssle @vstinner @ericsnowcurrently @encukou @erlend-aasland

@neonene neonene marked this pull request as draft April 29, 2024 23:09
@neonene

This comment was marked as resolved.

static PyDateTime_CAPI *(*_get_pydatetime_capi)(void) = _get_pydatetime_capi_dummy;
#define PyDateTimeAPI _get_pydatetime_capi()

static inline void pydatetime_import(void) {
Copy link
Member

Choose a reason for hiding this comment

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

I would prefer to not leak such implementation detail at the ABI level, and so use a regular function rather than a static inline function. I would prefer that _pydatetime_capi is private.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How can I resolve non-Windows errors? I'm not sure how to use private struct members here.

Error:  _testcapi failed to import: /home/runner/work/cpython/cpython-builddir/
build/lib.linux-x86_64-3.13-pydebug/_testcapi.cpython-313d-x86_64-linux-gnu.so:
undefined symbol: _PyDateTimeAPI_Get

Modules/_testmultiphase.c Outdated Show resolved Hide resolved
Modules/_testmultiphase.c Outdated Show resolved Hide resolved
Modules/_testmultiphase.c Outdated Show resolved Hide resolved
Modules/_testmultiphase.c Outdated Show resolved Hide resolved
@neonene neonene marked this pull request as ready for review April 30, 2024 20:58
@neonene
Copy link
Contributor Author

neonene commented May 1, 2024

I added another capsule which points to the _PyDateTimeAPI_Import function in _datetimemodule.c. Any buildable alternative is welcome.

@neonene
Copy link
Contributor Author

neonene commented May 1, 2024

I have mixed this patch into the multi-phase init PR: #118337.

@encukou Is there any chance that users could test this or #117413 in the beta phase? If these approaches work, I think _detetime can be isolated in a regular way.

@vstinner
Copy link
Member

vstinner commented May 1, 2024

Such change is too late for beta1. It's better to wait for the beginning of the 3.14 dev cycle.

@neonene
Copy link
Contributor Author

neonene commented May 1, 2024

Such change is too late for beta1.

How do you think of the situation?

@vstinner
Copy link
Member

vstinner commented May 2, 2024

In my experience, such change is likely to cause regression and need more time to be stabilized. I prefer to wait Wednesday.

@neonene
Copy link
Contributor Author

neonene commented May 2, 2024

Otherwise, 3.13 should take specific workarounds for the crash during after the interned-strings cleanup.

@neonene
Copy link
Contributor Author

neonene commented May 3, 2024

A workaround is ready for review: #118531

@vstinner
Copy link
Member

vstinner commented May 3, 2024

Otherwise, 3.13 should take specific workarounds for the crash during after the interned-strings cleanup.

Which crash? This PR is related to issue gh-117398 which doesn't mention any crash.

@ericsnowcurrently
Copy link
Member

ericsnowcurrently commented May 3, 2024

FYI, I asked @Yhg1s if we could get an exception for landing this after beta 1: #103092 (comment). He gave us a similar exception for 3.12. Thomas might not do that this time, but at least I've asked.

@neonene neonene mentioned this pull request May 3, 2024
25 tasks
@Yhg1s
Copy link
Member

Yhg1s commented May 5, 2024

FYI, I asked @Yhg1s if we could get an exception for landing this after beta 1: #103092 (comment). He gave us a similar exception for 3.12. Thomas might not do that this time, but at least I've asked.

This particular change is just one that adds a new capsule C API, which can then be used when in the future the datetime module switches to multi-phase init and better subinterpreter support, right? Given that it's of mild impact and it gives us an API that allows us to deprecate the old one and use multi-phase init for datetime, I'm okay with this going into beta 2.

@vstinner
Copy link
Member

vstinner commented May 5, 2024

It doesn't sound safe to me to announce that the _datetime extension is compatible with subinterpreters before static types are converted to heap types. I suggest to discuss the plan in the issue: #117398 (comment)

@neonene
Copy link
Contributor Author

neonene commented May 8, 2024

@vstinner I think this should be tested from 3.13.0b1+. The change is easy to diagnose or revert. If an inevitable problem is found, the case will support your exceptional approach (mix _datetime types into PyInterpreterState) whose rationale is currently not proposed.

EDIT: Ultimately, PyInterpreterState does not need to be used, as the alternative PR proves.

Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

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

IMO Py_MOD_PER_INTERPRETER_GIL_SUPPORTED cannot be added before other changes are done, such as converting static types to heap types.

I prefer to do the conversion in a different order, with multiple steps: #117398 (comment)

@bedevere-app
Copy link

bedevere-app bot commented May 9, 2024

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@neonene
Copy link
Contributor Author

neonene commented May 9, 2024

IMO Py_MOD_PER_INTERPRETER_GIL_SUPPORTED cannot be added before other changes are done, such as converting static types to heap types.

This PR does not add the flag in _datetimemodule.c. The test case in _testmultiphase.c is a kind of _zoneinfo multi-phase init module with the flag.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants