Skip to content

Commit

Permalink
Revert "pythongh-46376: Return existing pointer when possible in ctyp…
Browse files Browse the repository at this point in the history
…es (#1… (python#108688)

This reverts commit 08447b5.

Revert also _ctypes.c changes of the PyDict_ContainsString() change,
commit 6726626.
  • Loading branch information
vstinner authored Sep 4, 2023
1 parent 44fc378 commit 5a79d2a
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 63 deletions.
27 changes: 0 additions & 27 deletions Lib/test/test_ctypes/test_keeprefs.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,33 +98,6 @@ def test_p_cint(self):
x = pointer(i)
self.assertEqual(x._objects, {'1': i})

def test_pp_ownership(self):
d = c_int(123)
n = c_int(456)

p = pointer(d)
pp = pointer(p)

self.assertIs(pp._objects['1'], p)
self.assertIs(pp._objects['0']['1'], d)

pp.contents.contents = n

self.assertIs(pp._objects['1'], p)
self.assertIs(pp._objects['0']['1'], n)

self.assertIs(p._objects['1'], n)
self.assertEqual(len(p._objects), 1)

del d
del p

self.assertIs(pp._objects['0']['1'], n)
self.assertEqual(len(pp._objects), 2)

del n

self.assertEqual(len(pp._objects), 2)

class PointerToStructure(unittest.TestCase):
def test(self):
Expand Down

This file was deleted.

35 changes: 0 additions & 35 deletions Modules/_ctypes/_ctypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -5148,41 +5148,6 @@ Pointer_get_contents(CDataObject *self, void *closure)

stgdict = PyObject_stgdict((PyObject *)self);
assert(stgdict); /* Cannot be NULL for pointer instances */

PyObject *keep = GetKeepedObjects(self);
if (keep != NULL) {
// check if it's a pointer to a pointer:
// pointers will have '0' key in the _objects
int ptr_probe = PyDict_ContainsString(keep, "0");
if (ptr_probe < 0) {
return NULL;
}
if (ptr_probe) {
PyObject *item;
if (PyDict_GetItemStringRef(keep, "1", &item) < 0) {
return NULL;
}
if (item == NULL) {
PyErr_SetString(PyExc_ValueError,
"Unexpected NULL pointer in _objects");
return NULL;
}
#ifndef NDEBUG
CDataObject *ptr2ptr = (CDataObject *)item;
// Don't construct a new object,
// return existing one instead to preserve refcount.
// Double-check that we are returning the same thing.
assert(
*(void**) self->b_ptr == ptr2ptr->b_ptr ||
*(void**) self->b_value.c == ptr2ptr->b_ptr ||
*(void**) self->b_ptr == ptr2ptr->b_value.c ||
*(void**) self->b_value.c == ptr2ptr->b_value.c
);
#endif
return item;
}
}

return PyCData_FromBaseObj(stgdict->proto,
(PyObject *)self, 0,
*(void **)self->b_ptr);
Expand Down

0 comments on commit 5a79d2a

Please sign in to comment.