Skip to content

Commit c3d129f

Browse files
authored
[3.11] Revert "gh-46376: Return existing pointer when possible in ctypes (GH-107131) (GH-107488)" (#108412)
This reverts commit 57f27e4. The fix caused gh-107940. Until we have a bulletproof fix for that, the 3.11 backport needs to be reverted to make way for 3.11.5.
1 parent d90f2f3 commit c3d129f

File tree

3 files changed

+0
-57
lines changed

3 files changed

+0
-57
lines changed

Lib/ctypes/test/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):

Misc/NEWS.d/next/Library/2023-07-24-01-21-16.gh-issue-46376.w-xuDL.rst

Lines changed: 0 additions & 1 deletion
This file was deleted.

Modules/_ctypes/_ctypes.c

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5158,8 +5158,6 @@ static PyObject *
51585158
Pointer_get_contents(CDataObject *self, void *closure)
51595159
{
51605160
StgDictObject *stgdict;
5161-
PyObject *keep, *ptr_probe;
5162-
CDataObject *ptr2ptr;
51635161

51645162
if (*(void **)self->b_ptr == NULL) {
51655163
PyErr_SetString(PyExc_ValueError,
@@ -5169,33 +5167,6 @@ Pointer_get_contents(CDataObject *self, void *closure)
51695167

51705168
stgdict = PyObject_stgdict((PyObject *)self);
51715169
assert(stgdict); /* Cannot be NULL for pointer instances */
5172-
5173-
keep = GetKeepedObjects(self);
5174-
if (keep != NULL) {
5175-
// check if it's a pointer to a pointer:
5176-
// pointers will have '0' key in the _objects
5177-
ptr_probe = PyDict_GetItemString(keep, "0");
5178-
5179-
if (ptr_probe != NULL) {
5180-
ptr2ptr = (CDataObject*) PyDict_GetItemString(keep, "1");
5181-
if (ptr2ptr == NULL) {
5182-
PyErr_SetString(PyExc_ValueError,
5183-
"Unexpected NULL pointer in _objects");
5184-
return NULL;
5185-
}
5186-
// don't construct a new object,
5187-
// return existing one instead to preserve refcount
5188-
assert(
5189-
*(void**) self->b_ptr == ptr2ptr->b_ptr ||
5190-
*(void**) self->b_value.c == ptr2ptr->b_ptr ||
5191-
*(void**) self->b_ptr == ptr2ptr->b_value.c ||
5192-
*(void**) self->b_value.c == ptr2ptr->b_value.c
5193-
); // double-check that we are returning the same thing
5194-
Py_INCREF(ptr2ptr);
5195-
return (PyObject *) ptr2ptr;
5196-
}
5197-
}
5198-
51995170
return PyCData_FromBaseObj(stgdict->proto,
52005171
(PyObject *)self, 0,
52015172
*(void **)self->b_ptr);

0 commit comments

Comments
 (0)