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=Noneclosed_at=<Date2018-09-27.19:35:15.549>created_at=<Date2018-09-21.15:06:32.391>labels= ['interpreter-core', '3.8', 'type-feature', '3.7']
title='Change contextvars C API to use PyObject'updated_at=<Date2018-09-27.19:35:15.547>user='https://github.com/1st1'
Unfortunately, the current C API for PEP-567 has a flaw: it uses non-PyObject pointers.
This causes problems with integrating with tools like Cython, where PyObject is special and a lot of machinery recognizes it and manages refcounts correctly.
It also goes against the recent push to improve C API; one of the things we want to fix is to eliminate non-PyObject pointer types from public APIs entirely.
Because this C API is new (landed in 3.7.0) I think it might make sense to change it in 3.7.1. I *think* this is a relatively safe (albeit annoying) change:
I don't expect that a lot of people use this new C API. I googled recently if anyone uses contextvars at all, and found that some people are using the Python API. The only user of C API that I'm aware of is uvloop, which I'll be happy to fix.
My current understanding is that this change won't break existing extensions compiled against Python 3.7.0, as it's just a change in pointers' types.
For example, clang spits out the following *warning* if it sees mismatched pointer types (and still compiles the extension):
warning: incompatible pointer types passing 'PyContextVar *' (aka
'struct _pycontextvarobject *') to parameter of type 'PyObject *' (aka 'struct _object *')
[-Wincompatible-pointer-types]
This would make it slightly harder to create extension that supports both 3.7.0 and 3.7.1 and compiles without warnings. (It's possible to avoid warnings by adding some #ifdefs macro).
If we don't change this API now, we'll likely have to either live with it, or face a very slow deprecation period.
Just to add to this issue: I originally realized that something is wrong with the design when we had a super hard to track memory leak in uvloop, caused by Cython being unable to automatically manage increfs/decrefs for PyContext* pointers. So I do believe this is a serious pitfall.
Adding Guido to the nosy list as he accepted the PEP and IMO still has a say in this.
IMHO it's not too late to change the public C API in Python 3.7.1, since it's a very new API, I don't expect many users, and I only expect compiler warnings nor hard error if existing code pass PyContextVar* instead of PyObject*.
I agree that it's way better to use PyObject* rather than specific PyContextVar*. The C API should not leak implementation details ;-)
IMHO it's not too late to change the public C API in Python 3.7.1, since
it's a very new API, I don't expect many users, and I only expect compiler
warnings nor hard error if existing code pass PyContextVar* instead of
PyObject*.
I agree that it's way better to use PyObject* rather than specific
PyContextVar*. The C API should not leak implementation details ;-)
one of the things we want to fix is to eliminate non-PyObject
pointer types from public APIs entirely.
A notable exception is Py_buffer. [1] As well, cross-interpreter data must not be PyObject, though that isn't much of an issue (yet). :) At some point it may make sense to make _PyCrossInterpreterData [2] part of the public C-API. However, we can cross *that* bridge later. :)
Using PyObject for contextvars makes sense (for the reasons you described) as long as they won't be shared between interpreters. I'm not super familiar with the contextvars C-API, but it looks like cross-interpreter issues are *not* a factor. If that's the case then there's nothing further to discuss. :)
> one of the things we want to fix is to eliminate non-PyObject
pointer types from public APIs entirely.
A notable exception is Py_buffer. [1]
Right, because Py_buffer isn't a PyObject at all :)
Using PyObject for contextvars makes sense (for the reasons you described) as long as they won't be shared between interpreters.
Yeah, PyContext, PyContextVar, and PyContextToken aren't supposed to be shared between sub-interpreters directly. Context is essentially a mapping of Context Variables to arbitrary Python Objects, so sharing it transparently isn't possible.
Activity
1st1 commented on Sep 21, 2018
Unfortunately, the current C API for PEP-567 has a flaw: it uses non-PyObject pointers.
This causes problems with integrating with tools like Cython, where PyObject is special and a lot of machinery recognizes it and manages refcounts correctly.
It also goes against the recent push to improve C API; one of the things we want to fix is to eliminate non-PyObject pointer types from public APIs entirely.
Because this C API is new (landed in 3.7.0) I think it might make sense to change it in 3.7.1. I *think* this is a relatively safe (albeit annoying) change:
I don't expect that a lot of people use this new C API. I googled recently if anyone uses contextvars at all, and found that some people are using the Python API. The only user of C API that I'm aware of is uvloop, which I'll be happy to fix.
My current understanding is that this change won't break existing extensions compiled against Python 3.7.0, as it's just a change in pointers' types.
For example, clang spits out the following *warning* if it sees mismatched pointer types (and still compiles the extension):
warning: incompatible pointer types passing 'PyContextVar *' (aka
'struct _pycontextvarobject *') to parameter of type 'PyObject *' (aka 'struct _object *')
[-Wincompatible-pointer-types]
This would make it slightly harder to create extension that supports both 3.7.0 and 3.7.1 and compiles without warnings. (It's possible to avoid warnings by adding some #ifdefs macro).
If we don't change this API now, we'll likely have to either live with it, or face a very slow deprecation period.
1st1 commented on Sep 21, 2018
Just to add to this issue: I originally realized that something is wrong with the design when we had a super hard to track memory leak in uvloop, caused by Cython being unable to automatically manage increfs/decrefs for PyContext* pointers. So I do believe this is a serious pitfall.
Adding Guido to the nosy list as he accepted the PEP and IMO still has a say in this.
vstinner commented on Sep 21, 2018
IMHO it's not too late to change the public C API in Python 3.7.1, since it's a very new API, I don't expect many users, and I only expect compiler warnings nor hard error if existing code pass PyContextVar* instead of PyObject*.
I agree that it's way better to use PyObject* rather than specific PyContextVar*. The C API should not leak implementation details ;-)
gvanrossum commented on Sep 21, 2018
Let’s change it ASAP. It’s still up to Ned whether to hold up 3.7.1, if he
won’t it should go into 3.7.2.
On Fri, Sep 21, 2018 at 8:28 AM STINNER Victor <report@bugs.python.org>
wrote:
--
--Guido (mobile)
rhettinger commented on Sep 21, 2018
I concur that the sooner the change is applied, the better it will be for everyone. We'll need to make a prominent notice in the release notes though.
vstinner commented on Sep 21, 2018
Something can be added at:
https://docs.python.org/dev/whatsnew/3.7.html#notable-changes-in-python-3-7-1
1st1 commented on Sep 21, 2018
Yeah, thank you for suggestion, I'll do that. I'll also update the PEP, docs in other places, and try to spread the word.
Now I'm just waiting for Ned to confirm if this goes into 3.7.1 or 3.7.2. It's his say.
ericsnowcurrently commented on Sep 21, 2018
A notable exception is Py_buffer. [1] As well, cross-interpreter data must not be PyObject, though that isn't much of an issue (yet). :) At some point it may make sense to make _PyCrossInterpreterData [2] part of the public C-API. However, we can cross *that* bridge later. :)
Using PyObject for contextvars makes sense (for the reasons you described) as long as they won't be shared between interpreters. I'm not super familiar with the contextvars C-API, but it looks like cross-interpreter issues are *not* a factor. If that's the case then there's nothing further to discuss. :)
[1] https://docs.python.org/3/c-api/buffer.html#buffer-structure
[2] https://github.com/python/cpython/blob/master/Include/internal/pystate.h#L137
1st1 commented on Sep 21, 2018
Right, because Py_buffer isn't a PyObject at all :)
Yeah, PyContext, PyContextVar, and PyContextToken aren't supposed to be shared between sub-interpreters directly. Context is essentially a mapping of Context Variables to arbitrary Python Objects, so sharing it transparently isn't possible.
scoder commented on Sep 21, 2018
It owns a PyObject reference to the buffer owner, though.
ned-deily commented on Sep 21, 2018
Since we've already delayed 3.7.1rc cutoff for a few days, let's get it into 3.7.1 if you have the time, Yury.
1st1 commented on Sep 21, 2018
New changeset 2ec872b by Yury Selivanov in branch 'master':
bpo-34762: Fix contextvars C API to use PyObject* pointer types. (GH-9473)
2ec872b
3 remaining items
serhiy-storchaka commented on Sep 27, 2018
Perhaps this change caused new compiler warnings:
1st1 commented on Sep 27, 2018
Right, I'll make a PR.
1st1 commented on Sep 27, 2018
New changeset 994269c by Yury Selivanov in branch 'master':
bpo-34762: Update PyContext* to PyObject* in asyncio and decimal (GH-9609)
994269c
1st1 commented on Sep 27, 2018
New changeset 24cb7de by Yury Selivanov in branch '3.7':
[3.7] bpo-34762: Update PyContext* refs to PyObject* in asyncio and decimal (GH-9610)
24cb7de
1st1 commented on Sep 27, 2018
Thank you Serhiy, for re-opening this! I've pushed fixes to 3.7 and master branches.
pythongh-119333: Change PyContext_WatchCallback to take PyObject
pythongh-119333: Change PyContext_WatchCallback to take PyObject
pythongh-119333: Change PyContext_WatchCallback to take PyObject
pythongh-124872: Change PyContext_WatchCallback to take PyObject
gh-124872: Change PyContext_WatchCallback to take PyObject (#124737)