New submission from Mark Shannon <m...@hotpy.org>:

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.

----------
messages: 385851
nosy: Mark.Shannon, gvanrossum, petr.viktorin, vstinner
priority: normal
severity: normal
status: open
title: What does the existence of a struct in a header file imply about the 
C-API

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue43054>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to