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

Py_SIZE is not useful #10

Open
encukou opened this issue May 10, 2023 · 5 comments
Open

Py_SIZE is not useful #10

encukou opened this issue May 10, 2023 · 5 comments
Labels
theme: implementation flaws problems with the implementation of specific APIs

Comments

@encukou
Copy link
Contributor

encukou commented May 10, 2023

The meaning of a type's Py_SIZE (ob_size) depends entirely on the type. The interpreter generally sets it on allocation, but can't rely on the value afterwards.

  • Py_SIZE is not different from other instance data and doesn't really need special treatment
  • The interpreter doesn't know the size of a PyVarObject instance
@markshannon
Copy link

I would say that it is worse than not useful, it is a trap.
For example, PyLongObject. The "size" has never been the actual size, due to the sign, and it is even less so in 3.12.

@vstinner
Copy link
Contributor

Are there better existing APIs with a well defined behavior?

Py_SIZE() has flaws but can be used correctly if you know the type and so what the result means. If it's bad, we need a migration path.

@encukou
Copy link
Contributor Author

encukou commented May 18, 2023

APIs for what?
For a general-purpose field on an instance, yes.
For allocating variable-sized instances, yes.

There's nothing to read the size of a variable-sized instance in general. That could be useful (but might not be cheap to store).
If we don't add that: if you need it for a type you control, you can reserve a field for it and let tp_new set it. And you can define a getter for it.
If it's not a type you control and there's no getter... you probably shouldn't be looking at its memory, so why ask how big it is.

@iritkatriel
Copy link
Member

Is this issue then simply "let's deprecate Py_SIZE"?

@encukou
Copy link
Contributor Author

encukou commented May 22, 2023

That a possible solution.
But we should only deprecate Py_SIZE after we add/document the preferred replacement. What will that be? That depends on whether we end up adding a reliable way to tell the size.

@iritkatriel iritkatriel added the theme: implementation flaws problems with the implementation of specific APIs label Jun 5, 2023
@iritkatriel iritkatriel added the v label Jul 21, 2023
@iritkatriel iritkatriel removed the v label Oct 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
theme: implementation flaws problems with the implementation of specific APIs
Projects
None yet
Development

No branches or pull requests

4 participants