Skip to content

Commit e0f6080

Browse files
ambvYhg1s
andauthored
[3.12] gh-46376: Revert "Return existing pointer when possible in ctypes (GH-107131) (GH-107487)" (#108864)
This reverts commit 54aaaad. Co-authored-by: T. Wouters <[email protected]>
1 parent 7b936ac commit e0f6080

File tree

2 files changed

+0
-56
lines changed

2 files changed

+0
-56
lines changed

Lib/test/test_ctypes/test_keeprefs.py

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -93,33 +93,6 @@ def test_p_cint(self):
9393
x = pointer(i)
9494
self.assertEqual(x._objects, {'1': i})
9595

96-
def test_pp_ownership(self):
97-
d = c_int(123)
98-
n = c_int(456)
99-
100-
p = pointer(d)
101-
pp = pointer(p)
102-
103-
self.assertIs(pp._objects['1'], p)
104-
self.assertIs(pp._objects['0']['1'], d)
105-
106-
pp.contents.contents = n
107-
108-
self.assertIs(pp._objects['1'], p)
109-
self.assertIs(pp._objects['0']['1'], n)
110-
111-
self.assertIs(p._objects['1'], n)
112-
self.assertEqual(len(p._objects), 1)
113-
114-
del d
115-
del p
116-
117-
self.assertIs(pp._objects['0']['1'], n)
118-
self.assertEqual(len(pp._objects), 2)
119-
120-
del n
121-
122-
self.assertEqual(len(pp._objects), 2)
12396

12497
class PointerToStructure(unittest.TestCase):
12598
def test(self):

Modules/_ctypes/_ctypes.c

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5122,8 +5122,6 @@ static PyObject *
51225122
Pointer_get_contents(CDataObject *self, void *closure)
51235123
{
51245124
StgDictObject *stgdict;
5125-
PyObject *keep, *ptr_probe;
5126-
CDataObject *ptr2ptr;
51275125

51285126
if (*(void **)self->b_ptr == NULL) {
51295127
PyErr_SetString(PyExc_ValueError,
@@ -5133,33 +5131,6 @@ Pointer_get_contents(CDataObject *self, void *closure)
51335131

51345132
stgdict = PyObject_stgdict((PyObject *)self);
51355133
assert(stgdict); /* Cannot be NULL for pointer instances */
5136-
5137-
keep = GetKeepedObjects(self);
5138-
if (keep != NULL) {
5139-
// check if it's a pointer to a pointer:
5140-
// pointers will have '0' key in the _objects
5141-
ptr_probe = PyDict_GetItemString(keep, "0");
5142-
5143-
if (ptr_probe != NULL) {
5144-
ptr2ptr = (CDataObject*) PyDict_GetItemString(keep, "1");
5145-
if (ptr2ptr == NULL) {
5146-
PyErr_SetString(PyExc_ValueError,
5147-
"Unexpected NULL pointer in _objects");
5148-
return NULL;
5149-
}
5150-
// don't construct a new object,
5151-
// return existing one instead to preserve refcount
5152-
assert(
5153-
*(void**) self->b_ptr == ptr2ptr->b_ptr ||
5154-
*(void**) self->b_value.c == ptr2ptr->b_ptr ||
5155-
*(void**) self->b_ptr == ptr2ptr->b_value.c ||
5156-
*(void**) self->b_value.c == ptr2ptr->b_value.c
5157-
); // double-check that we are returning the same thing
5158-
Py_INCREF(ptr2ptr);
5159-
return (PyObject *) ptr2ptr;
5160-
}
5161-
}
5162-
51635134
return PyCData_FromBaseObj(stgdict->proto,
51645135
(PyObject *)self, 0,
51655136
*(void **)self->b_ptr);

0 commit comments

Comments
 (0)