New GitHub issue #119333 from fried:<br>

<hr>

<pre>
# Feature or enhancement

### Proposal:

In python every time a context is switch two c-api are called. 
```
PyContext_Enter(PyObject *);
PyContext_Exit(PyObject *);
```
I need a way to know when those api's are called on a context.   The purpose is that at Meta we have a similar system to `contextvars` in C++ called `folly::requestcontext`.   When the python context switches we need to inform C++ to swap its active context. Right now we have been carry forward patches to Asyncio.Task to do this on the `_step()` since way back in py3.6 which is generally the wrong place to do it. 

There is prior art in #91054 which provides watching creation/destruction of Code objects.  The proposed solution would mirror that functionality closely. 

### Has this already been discussed elsewhere?

I have already discussed this feature proposal on Discourse

### Links to previous discussion of this feature:

https://discuss.python.org/t/request-can-we-get-a-c-api-hook-into-pycontext-enter-and-pycontext-exit/51730

There was an in person meeting between @gvanrossum, @itamaro and myself where we discussed the plausibility of this feature request. 
</pre>

<hr>

<a href="https://github.com/python/cpython/issues/119333">View on GitHub</a>
<p>Labels: type-feature</p>
<p>Assignee: </p>