Thanks to visit codestin.com
Credit goes to github.com

Skip to content

GH-92678: Make sure that tp_dictoffset is correct when Py_TPFLAGS_MANAGED_DICT is set #95578

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

Conversation

markshannon
Copy link
Member

@markshannon markshannon commented Aug 2, 2022

First step in making Py_TPFLAGS_MANAGED_DICT more usable for C extensions, or at least making the errors more easily understood.

I still think the advice should be "don't use Py_TPFLAGS_MANAGED_DICT", but adding tests and making sure that tp_dictoffset and Py_TPFLAGS_MANAGED_DICT are mutually consistent seems like a good idea.

I'm skipping news as this doesn't add any features or actually fix anything.

@encukou
Copy link
Member

encukou commented Aug 3, 2022

Thanks! This makes the feature clearer. It also makes it clearer how good an ides the managed dict is, though I'd still prefer if it was communicated earlier/wider, to catch more edge cases.

One thing I still don't understand: why should tp_offset be set to -size - 3*sizeof(PyObject*)?. If it doesn't point to a dict, how is this useful?

I'm skipping news as this doesn't add any features or actually fix anything.

It does change tp_dictoffset for a bunch of classes, doesn't it?


But most importantly HeapCTypeWithManagedDict as not a good example of how to write a class with Py_TPFLAGS_MANAGED_DICT. It'll cause segfaults when subclassed, as in:

    def test_heaptype_with_managed_dict_subclass(self):
        class C(_testcapi.HeapCTypeWithManagedDict):
            pass
        inst = C()
        inst.foo = inst
        # (run GC)

If it's meant as a bad example, could you name it more ominously?

@markshannon
Copy link
Member Author

HeapCTypeWithManagedDict isn't supposed to be flawed.
Subclassing is problematic (#95589), but I'll try to make it more robust.

@markshannon
Copy link
Member Author

Given that #92678 has been closed, I'm closing this.
I'll add the ability to handle third-party code setting the Py_TPFLAGS_MANAGED_DICT in a new PR for 3.12.

@markshannon markshannon closed this Aug 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants