[Python-Dev] enum in the stable ABI (Was: PEP 558: Defined semantics for locals)

2021年7月23日 07:41:26 -0700

On 22. 07. 21 12:41, Nick Coghlan wrote:
On 2021年7月22日, 6:01 pm Petr Viktorin, <[email protected] <mailto:[email protected]>> wrote:
 On 21. 07. 21 14:18, Nick Coghlan wrote:
 > On 2021年7月19日 at 21:32, Petr Viktorin <[email protected]
 <mailto:[email protected]>> wrote:
 >> The proposal assumes that in the future, ``PyLocals_Get``, and thus
 >> ``locals()``, will never gain another kind of return value, however
 >> unlikely that is.
 >> AFAICS, code that uses this will usually check for a single
 special case
 >> and fall back (or error) for the other(s), so I think it'd be
 reasonable
 >> to make this an "enum" with two values. e.g.:
 >>
 >> int PyLocals_GetReturnBehavior(); # better name?
 >> #define PyLocals_DIRECT_REFERENCE 0
 >> #define PyLocals_SHALLOW_COPY 1
 >
 > After looking at PyUnicode_Kind, PySendResult, and other already
 > public enums for inspiration, my proposed spelling is as follows:
 >
 > ====================
 > typedef enum {
 >   PyLocals_UNDEFINED = -1;
 >   PyLocals_DIRECT_REFERENCE = 0,
 >   PyLocals_SHALLOW_COPY = 1
 > } PyLocals_Kind;
 >
 > PyLocals_Kind PyLocals_GetKind(void);
 > PyLocals_Kind PyFrame_GetLocalsKind(PyFrameObject *);
 > ====================
 >
 > The PyLocals_UNDEFINED case comes from PyLocals_GetKind() needing an
 > error value to return when the query API is called with no active
 > thread state.
 >
 > I've updated the draft reference implementation to use this API, and
 > added the associated PEP changes to the review PR at
 > https://github.com/python/peps/pull/2038/files
 <https://github.com/python/peps/pull/2038/files>
 Please don't put the enum in the stable ABI. If we would add another
 value and then an older extension would receive it, we'd get undefined
 behavior.
Hmm, I was copying an example that is already in the stable ABI (PySendResult). I think it's new in 3.10, though, so it should still be possible to fix that.
After researching a bit more, I see that casting unknown values to enum is only undefined/unspecified behavior in C++. But we do support C++ extensions, and so I'll try to get enums out of the stable ABI. (In both C & C++, the size of an `enum` is implementation-defined. That's unlikely to be a problem in practice, but one more point against enum.) NB. I don't have access to the actual standards; feel free to check this if you do!
_______________________________________________
Python-Dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/MRNSY5BWP7LBOA2MXHADSHM3WDNODI5O/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to