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

pep-567: Add C API section. #508

Merged
merged 1 commit into from
Dec 13, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 30 additions & 3 deletions pep-0567.rst
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,37 @@ as follows::
...


CPython C API
-------------
C API
-----

TBD
1. ``PyContextVar * PyContextVar_New(char *name)``: create a
``ContextVar`` object.

2. ``PyObject * PyContextVar_Get(PyContextVar *)``:
return the value of the variable in the current context.

3. ``PyContextToken * PyContextVar_Set(PyContextVar *, PyObject *)``:
set the value of the variable in the current context.

4. ``PyContextVar_Reset(PyContextToken *)``:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's surprising that this doesn't take a ContextVar argument. Even though it's technically redundant, I'd add it -- it makes the API more regular and gives the caller the confidence that it's resetting the correct variable.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense.

reset the value of the context variable.

5. ``PyContext * PyContext_New()``: create a new empty context.

6. ``PyContext * PyContext_Get()``: get the current context.

7. ``int PyContext_Set(PyContext *)``: set a new context as the
current for the current OS thread. It is required to always
restore the previous context::

PyContext *old_ctx = PyContext_Get();
if (old_ctx == NULL) goto error;

if (PyContext_Set(new_ctx)) goto error;

// run some code

if (PyContext_Set(old_ctx)) goto error;


Implementation
Expand Down