Skip to content

Commit 5ac006d

Browse files
committed
Capsule-related changes:
* PyCObject_AsVoidPtr() can now open capsules. This addresses most of the remaining backwards-compatibility concerns about the conversion of Python 2.7 from CObjects to capsules. * CObjects were marked Pending Deprecation. * Documentation about this pending deprecation was added to cobject.h. * The capsule source files were added to the legacy PC build processes.
1 parent f3eeca1 commit 5ac006d

6 files changed

Lines changed: 62 additions & 3 deletions

File tree

Include/cobject.h

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,29 @@
1+
/*
2+
CObjects are marked Pending Deprecation as of Python 2.7.
3+
The full schedule for 2.x is as follows:
4+
- CObjects are marked Pending Deprecation in Python 2.7.
5+
- CObjects will be marked Deprecated in Python 2.8
6+
(if there is one).
7+
- CObjects will be removed in Python 2.9 (if there is one).
8+
9+
Additionally, for the Python 3.x series:
10+
- CObjects were marked Deprecated in Python 3.1.
11+
- CObjects will be removed in Python 3.2.
12+
13+
You should switch all use of CObjects to capsules. Capsules
14+
have a safer and more consistent API. For more information,
15+
see Include/pycapsule.h, or read the "Capsules" topic in
16+
the "Python/C API Reference Manual".
17+
18+
Python 2.7 no longer uses CObjects itself; all objects which
19+
were formerly CObjects are now capsules. Note that this change
20+
does not by itself break binary compatibility with extensions
21+
built for previous versions of Python--PyCObject_AsVoidPtr()
22+
has been changed to also understand capsules.
23+
24+
*/
25+
26+
/* original file header comment follows: */
127

228
/* C objects to be exported from one extension module to another.
329
@@ -6,8 +32,6 @@
632
to other extension modules, so that extension modules can use the
733
Python import mechanism to link to one another.
834
9-
DEPRECATED - Use PyCapsule objects instead.
10-
CObject will be removed in 2.8 (if there is one).
1135
*/
1236

1337
#ifndef Py_COBJECT_H

Misc/NEWS

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,9 @@ C-API
184184
is now removed (the macro was introduced in 1997!).
185185

186186
- Issue #7992: A replacement PyCObject API, PyCapsule, has been backported
187-
from Python 3.1.
187+
from Python 3.1. All existing Python CObjects in the main distribution
188+
have been converted to capsules. To address backwards-compatibility
189+
concerns, PyCObject_AsVoidPtr() was changed to understand capsules.
188190

189191
Tests
190192
-----

Objects/cobject.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,23 @@
99
typedef void (*destructor1)(void *);
1010
typedef void (*destructor2)(void *, void*);
1111

12+
static int cobject_deprecation_warning(void)
13+
{
14+
return PyErr_WarnEx(PyExc_PendingDeprecationWarning,
15+
"The CObject type is marked Pending Deprecation in Python 2.7. "
16+
"Please use capsule objects instead.", 1);
17+
}
18+
19+
1220
PyObject *
1321
PyCObject_FromVoidPtr(void *cobj, void (*destr)(void *))
1422
{
1523
PyCObject *self;
1624

25+
if (cobject_deprecation_warning()) {
26+
return NULL;
27+
}
28+
1729
self = PyObject_NEW(PyCObject, &PyCObject_Type);
1830
if (self == NULL)
1931
return NULL;
@@ -30,6 +42,10 @@ PyCObject_FromVoidPtrAndDesc(void *cobj, void *desc,
3042
{
3143
PyCObject *self;
3244

45+
if (cobject_deprecation_warning()) {
46+
return NULL;
47+
}
48+
3349
if (!desc) {
3450
PyErr_SetString(PyExc_TypeError,
3551
"PyCObject_FromVoidPtrAndDesc called with null"
@@ -50,6 +66,10 @@ void *
5066
PyCObject_AsVoidPtr(PyObject *self)
5167
{
5268
if (self) {
69+
if (PyCapsule_CheckExact(self)) {
70+
const char *name = PyCapsule_GetName(self);
71+
return (void *)PyCapsule_GetPointer(self, name);
72+
}
5373
if (self->ob_type == &PyCObject_Type)
5474
return ((PyCObject *)self)->cobject;
5575
PyErr_SetString(PyExc_TypeError,

PC/VC6/pythoncore.dsp

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

PC/os2emx/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ SRC.OBJECT= $(addprefix $(TOP), \
384384
Objects/bytes_methods.c \
385385
Objects/cellobject.c \
386386
Objects/classobject.c \
387+
Objects/capsule.c \
387388
Objects/cobject.c \
388389
Objects/codeobject.c \
389390
Objects/complexobject.c \

PCbuild/pythoncore.vcproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,10 @@
854854
RelativePath="..\Include\pyarena.h"
855855
>
856856
</File>
857+
<File
858+
RelativePath="..\Include\pycapsule.h"
859+
>
860+
</File>
857861
<File
858862
RelativePath="..\Include\pyctype.h"
859863
>
@@ -1422,6 +1426,10 @@
14221426
RelativePath="..\Objects\stringobject.c"
14231427
>
14241428
</File>
1429+
<File
1430+
RelativePath="..\Objects\capsule.c"
1431+
>
1432+
</File>
14251433
<File
14261434
RelativePath="..\Objects\cellobject.c"
14271435
>

0 commit comments

Comments
 (0)