This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Mark.Shannon
Recipients Mark.Shannon, gvanrossum, petr.viktorin, vstinner
Date 2021-01-28.11:12:05
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1611832325.97.0.674198865532.issue43054@roundup.psfhosted.org>
In-reply-to
Content
Given the lack of explicit documentation on this subject, and differing opinions among core developers, I though it would be good to discuss how the existence of a struct in a header file constrains the C-API.

Original PR provoking this discussion: https://github.com/python/cpython/pull/24298

Suppose a header file, e.g. funcobject.h, contains a struct, e.g. PyFunctionObject, to what extent is that struct part of the API?


If a struct is present in a header file, there are three options for what means in terms of the API (that make sense to me).

1. That the struct is not part of the API and may be freely changed or deleted.
2. That the struct is produced, or initialized, by an API function, which implies that existing fields will continue to exist, but they can be reorder or added to.
3. That the struct is consumed by an API function, which implies that the struct must keep its exact shape, only adding fields if flags are present in the pre-existing fields to indicate the use of the extension.

PyTypeObject is an example of (3).

We should be able to infer which of the above cases applies, if not explicitly documented, for any struct.

Using PyFunctionObject in funcobject.h as an example:

There is no API function or macro that directly produces or consumes the struct, which would imply case 1. But, the struct is documented as the struct for Python functions, and `PyFunction_Check()` exists, which strongly implies that the following code is OK:

if (PyFunction_Check(obj)) {  
    PyFunctionObject *func = (PyFunctionObject *)obj;
    ...

which therefore implies that (2) applies.
(3) does not apply as there is no API that takes a PyFunctionObject struct as a parameter.

Similar logic can be applied to other parts of the API.


Rather than go through this tortuous analysis for all headers, it might be better to document which structs are part of the API.
History
Date User Action Args
2021-01-28 11:12:06Mark.Shannonsetrecipients: + Mark.Shannon, gvanrossum, vstinner, petr.viktorin
2021-01-28 11:12:05Mark.Shannonsetmessageid: <1611832325.97.0.674198865532.issue43054@roundup.psfhosted.org>
2021-01-28 11:12:05Mark.Shannonlinkissue43054 messages
2021-01-28 11:12:05Mark.Shannoncreate