Skip to content

gh-134786: Py_TPFLAGS_MANAGED_WEAKREF and Py_TPFLAGS_MANAGED_DICT must be used only if Py_TPFLAGS_HAVE_GC set - #135863

Merged
kumaraditya303 merged 17 commits into
python:mainfrom
sergey-miryanov:gh-134786-managed-weakref
Nov 2, 2025
Merged

kumaraditya303 merged 17 commits into
python:mainfrom
sergey-miryanov:gh-134786-managed-weakref

Conversation

@sergey-miryanov

@sergey-miryanov sergey-miryanov commented Jun 23, 2025 •

Copy link
Copy Markdown
Contributor

According to discussion Py_TPFLAGS_MANAGED_WEAKREF and Py_TPFLAGS_MANAGED_DICT must be used only if Py_TPFLAGS_HAVE_GC is set.


📚 Documentation preview 📚: https://cpython-previews--135863.org.readthedocs.build/

@sergey-miryanov

Copy link
Copy Markdown
Contributor Author

@markshannon Please take a look.

@chris-se

Copy link
Copy Markdown

As the person who reported #134786: I would suggest making the documentation more explicit and replacing should with must here, especially since the code now errors out if Py_TPFLAGS_HAVE_GC is not set.

@sergey-miryanov
sergey-miryanov marked this pull request as draft July 26, 2025 18:47
@Starbuck5

Copy link
Copy Markdown

As the person who reported #134786: I would suggest making the documentation more explicit and replacing should with must here, especially since the code now errors out if Py_TPFLAGS_HAVE_GC is not set.

+1

Additionally, this PR puts this into a WhatsNew entry for 3.15. This is not a new thing, this is a documentation fix + fix for the runtime to not allow these invalid types to be created. So I think the added whatsnew is misleading.

Comment thread Objects/typeobject.c Resolved
@sergey-miryanov

Copy link
Copy Markdown
Contributor Author

I'm not sure if we should support Py_TPFLAGS_MANAGED_DICT without Py_TPFLAGS_HAVE_GC, because Py_TPFLAGS_MANAGED_DICT implies Py_TPFLAGS_HAVE_GC. However, I have added it, though, to confirm that it is doable.

I think the current version needs some updates and clarification in the docs. First, though, I would like to get some feedback on the overall approach. Is this the right way to do after all?

Please take a look if someone interested.

@sergey-miryanov
sergey-miryanov marked this pull request as ready for review September 25, 2025 19:42
@sergey-miryanov sergey-miryanov changed the title gh-134786: Py_TPFLAGS_MANAGED_WEAKREF implies Py_TPFLAGS_HAVE_GC too and force checking of its presence Sep 26, 2025
@sergey-miryanov sergey-miryanov changed the title gh-134786: Allow use Py_TPFLAGS_MANAGED_WEAKREF without Py_TPFLAGS_HAVE_GC Sep 26, 2025
@sergey-miryanov

Copy link
Copy Markdown
Contributor Author

@markshannon Please take a look.

@efimov-mikhail efimov-mikhail left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe we should rename this PR?

Comment thread Doc/whatsnew/3.15.rst Resolved
@sergey-miryanov

Copy link
Copy Markdown
Contributor Author

Maybe we should rename this PR?

I forgot to apply changes :(

@sergey-miryanov sergey-miryanov changed the title gh-134786: Allow to use Py_TPFLAGS_MANAGED_WEAKREF without Py_TPFLAGS_HAVE_GC Sep 26, 2025
Comment thread Doc/whatsnew/3.15.rst Resolved
@markshannon

Copy link
Copy Markdown
Member

Once the remaining comment has been addressed, I'm happy to merge this.

For the record, the requirements may change in the future:
For Py_TPFLAGS_MANAGED_DICT things are clear: Py_TPFLAGS_HAVE_GC must be set.
For Py_TPFLAGS_MANAGED_WEAKREF things are more complex. The current implementation requires that Py_TPFLAGS_HAVE_GC to be set, but there is no fundamental reason why the VM cannot manage the weakrefs of objects that cannot be part of cycles.

Co-authored-by: Mikhail Efimov <efimov.mikhail@gmail.com>
@sergey-miryanov

Copy link
Copy Markdown
Contributor Author

For Py_TPFLAGS_MANAGED_WEAKREF things are more complex. The current implementation requires that Py_TPFLAGS_HAVE_GC to be set, but there is no fundamental reason why the VM cannot manage the weakrefs of objects that cannot be part of cycles.

I had such commit 42c0932, we decided to revert it for now. Maybe we should open a separate PR for it?

@kumaraditya303

Copy link
Copy Markdown
Contributor

If it isn't too much work, it is worth adding a test for this.

@sergey-miryanov

Copy link
Copy Markdown
Contributor Author

@kumaraditya303 I have added test.

@sergey-miryanov

Copy link
Copy Markdown
Contributor Author

@markshannon @kumaraditya303 It is ready to review, could you please take a look.

Comment thread Modules/_testcapimodule.c Resolved
Comment thread Objects/typeobject.c Resolved
@sergey-miryanov

Copy link
Copy Markdown
Contributor Author

Thanks all!

StanFromIreland pushed a commit to StanFromIreland/cpython that referenced this pull request Dec 6, 2025
…PFLAGS_MANAGED_DICT` is used without `Py_TPFLAGS_HAVE_GC` set (python#135863)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

7 participants