Skip to content

bpo-38206: Clarify tp_dealloc requirements for heap allocated types. #16248

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 2 commits into from
Sep 27, 2019

Conversation

ammaraskar
Copy link
Member

@ammaraskar ammaraskar commented Sep 18, 2019

As mentioned in the bpo ticket, this mistake came up on two reviews:

Would be nice to have it documented in a more permanent place than 3.8's whatsnew entry.

https://bugs.python.org/issue38206

Automerge-Triggered-By: @encukou

@ammaraskar
Copy link
Member Author

cc @eduardo-elizondo

Happy to take any suggestions on the wording.

@eduardo-elizondo
Copy link
Contributor

Yup! Looks mostly good, just a couple of comments.

@eduardo-elizondo
Copy link
Contributor

One last comment. This looks good now! Ping @DinoV , could you help getting this merged?

static void foo_dealloc(foo_object *self) {
PyTypeObject *tp = Py_TYPE(self);
// free references and buffers here
tp->tp_free(self);
Copy link
Contributor

Choose a reason for hiding this comment

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

Since this is a heap type, change this to use the slot API to be compliant with PEP384.

freefunc free_func = PyType_GetSlot(tp, Py_tp_free);
free_func(self);

Copy link
Member

Choose a reason for hiding this comment

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

PEP 384 defines a stable ABI – an API subset you can use when you can't recompile extensions for each minor version of CPython.

How is it relevant here?

Copy link
Contributor

@eduardo-elizondo eduardo-elizondo Sep 24, 2019

Choose a reason for hiding this comment

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

@encukou it is very relevant! What you said is correct, but this PEP has the added benefit of making PyTypeObject opaque. This gives the type system control over the implementation of the type object. For instance, in our system, the type object has a very different implementation. Yet, we can correctly interface with C Extensions. Using tp_free directly ties the code to CPython's implementation.

By the way, this is only relevant for C extensions, that is, cpython/Modules + third-party libs. Internal code CPython code should be free to use all the implementation details it wants! Given that the updated doc has the context of heap types, it's a fair assumption to make :-)

Copy link
Member

Choose a reason for hiding this comment

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

I agree that using the stable ABI is good, and we all migrate to it eventually. However, it's orthogonal to this change, which is about making the documentation correct. Pushing a stable ABI agenda in a fix for "Clarify that tp_dealloc must decref for heap allocated type" seems too sneaky for me – even though I count myself as a proponent of the stable ABI :)

In CPython, PyType_GetSlot is likely* much slower than using the slot directly, so it is not a clear win.
I'm for avoiding direct slot access (both in stdlib and extension modules), but only after a wider discussion and perhaps after also adding a fast (and unstable) API.

And again: the text above talks about PyTypeObject.tp_free. The example is confusingly out of sync with that. I think we should change both occurences, but please, do it in a different PR.


  • (I haven't measured, but there's a bunch of extra checks in CPython's PyType_GetSlot)

Copy link
Member Author

Choose a reason for hiding this comment

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

Reverted back to tp->tp_free(self);

Copy link
Contributor

Choose a reason for hiding this comment

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

Woops, didn't mean to try any sneaky stuff! 😛

I agree with your point, let's get to anything related to PEP384 independently.

Copy link
Contributor

@eduardo-elizondo eduardo-elizondo left a comment

Choose a reason for hiding this comment

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

LGTM

@miss-islington
Copy link
Contributor

Thanks @ammaraskar for the PR 🌮🎉.. I'm working now to backport this PR to: 3.8.
🐍🍒⛏🤖

@bedevere-bot
Copy link

GH-16436 is a backport of this pull request to the 3.8 branch.

miss-islington added a commit that referenced this pull request Sep 27, 2019
…H-16248)

As mentioned in the bpo ticket, this mistake came up on two reviews:
- https://github.com/python/cpython/pull/16127GH-pullrequestreview-288312751
- https://github.com/python/cpython/pull/16071GH-pullrequestreview-287819525

Would be nice to have it documented in a more permanent place than 3.8's whatsnew entry.

https://bugs.python.org/issue38206

Automerge-Triggered-By: @encukou
(cherry picked from commit 5faff97)

Co-authored-by: Ammar Askar <[email protected]>
jacobneiltaylor pushed a commit to jacobneiltaylor/cpython that referenced this pull request Dec 5, 2019
…ythonGH-16248)

As mentioned in the bpo ticket, this mistake came up on two reviews:
- python#16127 (review)
- python#16071 (review)

Would be nice to have it documented in a more permanent place than 3.8's whatsnew entry.


https://bugs.python.org/issue38206



Automerge-Triggered-By: @encukou
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir skip news
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants