Skip to content

bpo-40077: Convert _abc module to use PyType_FromSpec() #19202

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 8 commits into from
Mar 30, 2020

Conversation

corona10
Copy link
Member

@corona10 corona10 commented Mar 28, 2020

Replace statically allocated types with heap allocated types:
use PyType_FromSpec().

Add a module state to store the _abc_data_type..
Add traverse, clear and free functions to the module.

https://bugs.python.org/issue40077

Replace statically allocated types with heap allocated types:
use PyType_FromSpec().

Add a module state to store the _abc_data_type..
Add traverse, clear and free functions to the module.
typedef struct {
PyObject *_abc_data_type;
} _abcmodulestate;

/* A global counter that is incremented each time a class is
registered as a virtual subclass of anything. It forces the
negative cache to be cleared before its next use.
Note: this counter is private. Use `abc.get_cache_token()` for
external code. */
static unsigned long long abc_invalidation_counter = 0;
Copy link
Member Author

Choose a reason for hiding this comment

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

I am not sure that the abc_invalidation_counter is okay to be shared as static from interpreter Isolation point of view.

Copy link
Member Author

Choose a reason for hiding this comment

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

Should we move this into PyInterpreterState?

Copy link
Member

Choose a reason for hiding this comment

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

It seems like it would be safe to put it in the module state. The purpose of the counter is to invalidate the module cache. Registering a class in module instance 1 should not invalidate module instance 2. What do you think?

Copy link
Member Author

Choose a reason for hiding this comment

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

I agree, I 've updated the PR.

@corona10 corona10 requested a review from vstinner March 28, 2020 05:47
@@ -326,7 +326,7 @@ class B:
token_old = abc_get_cache_token()
A.register(B)
token_new = abc_get_cache_token()
self.assertNotEqual(token_old, token_new)
self.assertGreater(token_new, token_old)
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 test might be what we wanted exactly.
I just updated the test for more validation.

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.

It seems like you cannot access to the module state in abc_data_new() which prevents you to move abc_invalidation_counter into the module state :-(

I suggest to leave abc_invalidation_counter as a "static" variable, but please add a FIXME like:

/* FIXME: PEP 573: Move abc_invalidation_counter into _abcmodule_state */

It's safe to keep it as a static variable until each interpreter gets its own GIL.

Until it happens, the change should move the module closer to the goal of isolated interpreters.

Copy link
Member Author

@corona10 corona10 left a comment

Choose a reason for hiding this comment

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

@vstinner
Thanks for the review.

PEP 573: Move abc_invalidation_counter into _abcmodule_state.

I agree, PEP 573 will solve this issue.
I 've updated the PR. Please take a look.

corona10 and others added 2 commits March 30, 2020 11:29
Co-Authored-By: Victor Stinner <vstinner@python.org>
@corona10
Copy link
Member Author

@vstinner Updated! Please take a look

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.

LGTM. Sorry, just one last change request, I promise ;-)

@vstinner vstinner merged commit 53e4c91 into python:master Mar 30, 2020
@vstinner
Copy link
Member

Thanks, I merged your PR.

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