Skip to content

Commit 2a13286

Browse files
committed
Backport all of the CObject -> PyCapsule changes to Python 2.7 from the Python 3.x side.
1 parent 0cee09d commit 2a13286

5 files changed

+70
-15
lines changed

CXX/Python2/ExtensionModule.hxx

+10-2
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,19 @@ namespace Py
136136
{
137137
MethodDefExt<T> *method_def = (*i).second;
138138

139-
static PyObject *self = PyCObject_FromVoidPtr( this, do_not_dealloc );
139+
#if PY_VERSION_HEX < 0x02070000
140+
static PyObject *self = PyCObject_FromVoidPtr( this, do_not_dealloc );
141+
#else
142+
static PyObject *self = PyCapsule_New( this, NULL, NULL );
143+
#endif
140144

141145
Tuple args( 2 );
142146
args[0] = Object( self );
143-
args[1] = Object( PyCObject_FromVoidPtr( method_def, do_not_dealloc ) );
147+
#if PY_VERSION_HEX < 0x02070000
148+
args[1] = Object( PyCObject_FromVoidPtr( method_def, do_not_dealloc ) );
149+
#else
150+
args[1] = Object( PyCapsule_New( method_def, NULL, NULL ) );
151+
#endif
144152

145153
PyObject *func = PyCFunction_New
146154
(

CXX/Python2/ExtensionOldType.hxx

+23-8
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,11 @@ namespace Py
178178
Tuple self( 2 );
179179

180180
self[0] = Object( this );
181-
self[1] = Object( PyCObject_FromVoidPtr( method_def, do_not_dealloc ), true );
182-
181+
#if PY_VERSION_HEX < 0x02070000
182+
self[1] = Object( PyCObject_FromVoidPtr( method_def, do_not_dealloc ), true );
183+
#else
184+
self[1] = Object( PyCapsule_New( method_def, NULL, NULL ), true );
185+
#endif
183186
PyObject *func = PyCFunction_New( &method_def->ext_meth_def, self.ptr() );
184187

185188
return Object(func, true);
@@ -235,8 +238,12 @@ namespace Py
235238

236239
PyObject *self_in_cobject = self_and_name_tuple[0].ptr();
237240
T *self = static_cast<T *>( self_in_cobject );
238-
MethodDefExt<T> *meth_def = reinterpret_cast<MethodDefExt<T> *>(
239-
PyCObject_AsVoidPtr( self_and_name_tuple[1].ptr() ) );
241+
#if PY_VERSION_HEX < 0x02070000
242+
void *capsule = PyCObject_AsVoidPtr( self_and_name_tuple[1].ptr() );
243+
#else
244+
void *capsule = PyCapsule_GetPointer( self_and_name_tuple[1].ptr(), NULL );
245+
#endif
246+
MethodDefExt<T> *meth_def = reinterpret_cast<MethodDefExt<T> *>( capsule );
240247
Object result;
241248

242249
// Adding try & catch in case of STL debug-mode exceptions.
@@ -271,8 +278,12 @@ namespace Py
271278
PyObject *self_in_cobject = self_and_name_tuple[0].ptr();
272279
T *self = static_cast<T *>( self_in_cobject );
273280

274-
MethodDefExt<T> *meth_def = reinterpret_cast<MethodDefExt<T> *>(
275-
PyCObject_AsVoidPtr( self_and_name_tuple[1].ptr() ) );
281+
#if PY_VERSION_HEX < 0x02070000
282+
void *capsule = PyCObject_AsVoidPtr( self_and_name_tuple[1].ptr() );
283+
#else
284+
void *capsule = PyCapsule_GetPointer( self_and_name_tuple[1].ptr(), NULL );
285+
#endif
286+
MethodDefExt<T> *meth_def = reinterpret_cast<MethodDefExt<T> *>( capsule );
276287
Tuple args( _args );
277288

278289
Object result;
@@ -308,8 +319,12 @@ namespace Py
308319
PyObject *self_in_cobject = self_and_name_tuple[0].ptr();
309320
T *self = static_cast<T *>( self_in_cobject );
310321

311-
MethodDefExt<T> *meth_def = reinterpret_cast<MethodDefExt<T> *>(
312-
PyCObject_AsVoidPtr( self_and_name_tuple[1].ptr() ) );
322+
#if PY_VERSION_HEX < 0x02070000
323+
void *capsule = PyCObject_AsVoidPtr( self_and_name_tuple[1].ptr() );
324+
#else
325+
void *capsule = PyCapsule_GetPointer( self_and_name_tuple[1].ptr(), NULL );
326+
#endif
327+
MethodDefExt<T> *meth_def = reinterpret_cast<MethodDefExt<T> *>( capsule );
313328

314329
Tuple args( _args );
315330

CXX/Python2/IndirectPythonInterface.cxx

+10
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ namespace Py
4242
bool _Buffer_Check( PyObject *op ) { return (op)->ob_type == _Buffer_Type(); }
4343
bool _CFunction_Check( PyObject *op ) { return (op)->ob_type == _CFunction_Type(); }
4444
bool _Class_Check( PyObject *op ) { return (op)->ob_type == _Class_Type(); }
45+
#if PY_VERSION_HEX < 0x02070000
4546
bool _CObject_Check( PyObject *op ) { return (op)->ob_type == _CObject_Type(); }
47+
#endif
4648
bool _Complex_Check( PyObject *op ) { return (op)->ob_type == _Complex_Type(); }
4749
bool _Dict_Check( PyObject *op ) { return (op)->ob_type == _Dict_Type(); }
4850
bool _File_Check( PyObject *op ) { return (op)->ob_type == _File_Type(); }
@@ -123,7 +125,9 @@ static PyObject *ptr__PyTrue = NULL;
123125
static PyTypeObject *ptr__Buffer_Type = NULL;
124126
static PyTypeObject *ptr__CFunction_Type = NULL;
125127
static PyTypeObject *ptr__Class_Type = NULL;
128+
#if PY_VERSION_HEX < 0x02070000
126129
static PyTypeObject *ptr__CObject_Type = NULL;
130+
#endif
127131
static PyTypeObject *ptr__Complex_Type = NULL;
128132
static PyTypeObject *ptr__Dict_Type = NULL;
129133
static PyTypeObject *ptr__File_Type = NULL;
@@ -310,7 +314,9 @@ bool InitialisePythonIndirectInterface()
310314
ptr__Buffer_Type = GetPyTypeObject_As_PyTypeObjectPointer( "PyBuffer_Type" );
311315
ptr__CFunction_Type = GetPyTypeObject_As_PyTypeObjectPointer( "PyCFunction_Type" );
312316
ptr__Class_Type = GetPyTypeObject_As_PyTypeObjectPointer( "PyClass_Type" );
317+
#if PY_VERSION_HEX < 0x02070000
313318
ptr__CObject_Type = GetPyTypeObject_As_PyTypeObjectPointer( "PyCObject_Type" );
319+
#endif
314320
ptr__Complex_Type = GetPyTypeObject_As_PyTypeObjectPointer( "PyComplex_Type" );
315321
ptr__Dict_Type = GetPyTypeObject_As_PyTypeObjectPointer( "PyDict_Type" );
316322
ptr__File_Type = GetPyTypeObject_As_PyTypeObjectPointer( "PyFile_Type" );
@@ -398,7 +404,9 @@ PyObject * _True() { return ptr__PyTrue; }
398404
PyTypeObject * _Buffer_Type() { return ptr__Buffer_Type; }
399405
PyTypeObject * _CFunction_Type(){ return ptr__CFunction_Type; }
400406
PyTypeObject * _Class_Type() { return ptr__Class_Type; }
407+
#if PY_VERSION_HEX < 0x02070000
401408
PyTypeObject * _CObject_Type() { return ptr__CObject_Type; }
409+
#endif
402410
PyTypeObject * _Complex_Type() { return ptr__Complex_Type; }
403411
PyTypeObject * _Dict_Type() { return ptr__Dict_Type; }
404412
PyTypeObject * _File_Type() { return ptr__File_Type; }
@@ -542,7 +550,9 @@ PyObject * _True() { return Py_True; }
542550
PyTypeObject * _Buffer_Type() { return &PyBuffer_Type; }
543551
PyTypeObject * _CFunction_Type() { return &PyCFunction_Type; }
544552
PyTypeObject * _Class_Type() { return &PyClass_Type; }
553+
#if PY_VERSION_HEX < 0x02070000
545554
PyTypeObject * _CObject_Type() { return &PyCObject_Type; }
555+
#endif
546556
PyTypeObject * _Complex_Type() { return &PyComplex_Type; }
547557
PyTypeObject * _Dict_Type() { return &PyDict_Type; }
548558
PyTypeObject * _File_Type() { return &PyFile_Type; }

CXX/Python2/IndirectPythonInterface.hxx

+2
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,10 @@ bool _Instance_Check( PyObject *op );
113113
PyTypeObject * _Method_Type();
114114
bool _Method_Check( PyObject *op );
115115

116+
#if PY_VERSION_HEX < 0x02070000
116117
PyTypeObject * _CObject_Type();
117118
bool _CObject_Check( PyObject *op );
119+
#endif
118120

119121
PyTypeObject * _Complex_Type();
120122
bool _Complex_Check( PyObject *op );

CXX/Python2/cxx_extensions.cxx

+25-5
Original file line numberDiff line numberDiff line change
@@ -1719,7 +1719,11 @@ extern "C" PyObject *method_keyword_call_handler( PyObject *_self_and_name_tuple
17191719
Tuple self_and_name_tuple( _self_and_name_tuple );
17201720

17211721
PyObject *self_in_cobject = self_and_name_tuple[0].ptr();
1722-
void *self_as_void = PyCObject_AsVoidPtr( self_in_cobject );
1722+
#if PY_VERSION_HEX < 0x02070000
1723+
void *self_as_void = PyCObject_AsVoidPtr( self_in_cobject );
1724+
#else
1725+
void *self_as_void = PyCapsule_GetPointer( self_in_cobject, NULL );
1726+
#endif
17231727
if( self_as_void == NULL )
17241728
return NULL;
17251729

@@ -1735,7 +1739,11 @@ extern "C" PyObject *method_keyword_call_handler( PyObject *_self_and_name_tuple
17351739
(
17361740
self->invoke_method_keyword
17371741
(
1738-
PyCObject_AsVoidPtr( self_and_name_tuple[1].ptr() ),
1742+
#if PY_VERSION_HEX < 0x02070000
1743+
PyCObject_AsVoidPtr( self_and_name_tuple[1].ptr() ),
1744+
#else
1745+
PyCapsule_GetPointer( self_and_name_tuple[1].ptr(), NULL ),
1746+
#endif
17391747
args,
17401748
keywords
17411749
)
@@ -1751,7 +1759,11 @@ extern "C" PyObject *method_keyword_call_handler( PyObject *_self_and_name_tuple
17511759
(
17521760
self->invoke_method_keyword
17531761
(
1754-
PyCObject_AsVoidPtr( self_and_name_tuple[1].ptr() ),
1762+
#if PY_VERSION_HEX < 0x02070000
1763+
PyCObject_AsVoidPtr( self_and_name_tuple[1].ptr() ),
1764+
#else
1765+
PyCapsule_GetPointer( self_and_name_tuple[1].ptr(), NULL ),
1766+
#endif
17551767
args,
17561768
keywords
17571769
)
@@ -1773,7 +1785,11 @@ extern "C" PyObject *method_varargs_call_handler( PyObject *_self_and_name_tuple
17731785
Tuple self_and_name_tuple( _self_and_name_tuple );
17741786

17751787
PyObject *self_in_cobject = self_and_name_tuple[0].ptr();
1776-
void *self_as_void = PyCObject_AsVoidPtr( self_in_cobject );
1788+
#if PY_VERSION_HEX < 0x02070000
1789+
void *self_as_void = PyCObject_AsVoidPtr( self_in_cobject );
1790+
#else
1791+
void *self_as_void = PyCapsule_GetPointer( self_in_cobject, NULL );
1792+
#endif
17771793
if( self_as_void == NULL )
17781794
return NULL;
17791795

@@ -1784,7 +1800,11 @@ extern "C" PyObject *method_varargs_call_handler( PyObject *_self_and_name_tuple
17841800
(
17851801
self->invoke_method_varargs
17861802
(
1787-
PyCObject_AsVoidPtr( self_and_name_tuple[1].ptr() ),
1803+
#if PY_VERSION_HEX < 0x02070000
1804+
PyCObject_AsVoidPtr( self_and_name_tuple[1].ptr() ),
1805+
#else
1806+
PyCapsule_GetPointer( self_and_name_tuple[1].ptr(), NULL ),
1807+
#endif
17881808
args
17891809
)
17901810
);

0 commit comments

Comments
 (0)