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

Stable ABI should avoid enum #88890

Open
encukou opened this issue Jul 23, 2021 · 3 comments
Open

Stable ABI should avoid enum #88890

encukou opened this issue Jul 23, 2021 · 3 comments

Comments

@encukou
Copy link
Member

encukou commented Jul 23, 2021

BPO 44727
Nosy @encukou

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2021-07-23.15:23:37.889>
labels = ['expert-C-API']
title = 'Stable ABI should avoid `enum`'
updated_at = <Date 2021-07-23.15:49:43.063>
user = 'https://github.com/encukou'

bugs.python.org fields:

activity = <Date 2021-07-23.15:49:43.063>
actor = 'petr.viktorin'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['C API']
creation = <Date 2021-07-23.15:23:37.889>
creator = 'petr.viktorin'
dependencies = []
files = []
hgrepos = []
issue_num = 44727
keywords = []
message_count = 3.0
messages = ['398067', '398070', '398071']
nosy_count = 1.0
nosy_names = ['petr.viktorin']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = None
url = 'https://bugs.python.org/issue44727'
versions = []

@encukou
Copy link
Member Author

encukou commented Jul 23, 2021

Adding a new enumerator to a C enum can change the size of the type,
which would break the ABI.
This is not often a problem in practice, but the rules around when it is a problem and when it isn't are complicated enough that I believe enum should not be used in the stable ABI (possibly with well-reasoned exceptions)

AFAICS, the rules are:

  • In C++, an incompatible change to an enum is one that changes the size of the smallest bit field large enough to hold all enumerators. Values outside the range cause undefined/unspecified behavior.
  • In C, it looks like enums that fit in char are safe.

(Also, the compiler-defined size of enums will make it more cumbersome to formally define an ABI for non-C languages.)

@encukou
Copy link
Member Author

encukou commented Jul 23, 2021

Devguide PR: python/devguide#730

@encukou
Copy link
Member Author

encukou commented Jul 23, 2021

As far as I can see, the current enums in the stable ABI are:

PySendResult from object.h, return value of PyObject_Send:
typedef enum {
PYGEN_RETURN = 0,
PYGEN_ERROR = -1,
PYGEN_NEXT = 1,
} PySendResult;
(This is unlikely to change in the future, but added in 3.10, maybe it can be converted to int.)

PyLockStatus from pythread.h, return value of PyThread_acquire_lock_timed:
typedef enum PyLockStatus {
PY_LOCK_FAILURE = 0,
PY_LOCK_ACQUIRED = 1,
PY_LOCK_INTR
} PyLockStatus;
(This has been there for a long time so shouldn't be changed now.)

PyGILState_STATE from pystate.h, for PyGILState_Ensure/PyGILState_Release:
typedef
enum {PyGILState_LOCKED, PyGILState_UNLOCKED}
PyGILState_STATE;
(Also is unlikely to change in the future.)

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant