@@ -5129,6 +5129,7 @@ static PyObject *
5129
5129
Pointer_get_contents (CDataObject * self , void * closure )
5130
5130
{
5131
5131
StgDictObject * stgdict ;
5132
+ PyObject * keep , * pointer_probe , * pointer_to_pointer ;
5132
5133
5133
5134
if (* (void * * )self -> b_ptr == NULL ) {
5134
5135
PyErr_SetString (PyExc_ValueError ,
@@ -5138,6 +5139,27 @@ Pointer_get_contents(CDataObject *self, void *closure)
5138
5139
5139
5140
stgdict = PyObject_stgdict ((PyObject * )self );
5140
5141
assert (stgdict ); /* Cannot be NULL for pointer instances */
5142
+
5143
+ keep = GetKeepedObjects (self );
5144
+ if (keep != NULL ) {
5145
+ // check if it's a pointer to a pointer:
5146
+ // pointers will have '0' key in the _objects
5147
+
5148
+ pointer_probe = PyDict_GetItemString (keep , "0" );
5149
+
5150
+ if (pointer_probe != NULL ) {
5151
+ pointer_to_pointer = PyDict_GetItemString (keep , "1" );
5152
+ if (pointer_to_pointer == NULL ) {
5153
+ PyErr_SetString (PyExc_ValueError ,
5154
+ "Unexpected NULL pointer in _objets" );
5155
+ }
5156
+ // don't construct a new object,
5157
+ // return existing one instead to preserve refcount
5158
+ Py_INCREF (pointer_to_pointer );
5159
+ return pointer_to_pointer ;
5160
+ }
5161
+ }
5162
+
5141
5163
return PyCData_FromBaseObj (stgdict -> proto ,
5142
5164
(PyObject * )self , 0 ,
5143
5165
* (void * * )self -> b_ptr );
0 commit comments