summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorƁukasz Langa <lukasz@langa.pl>2023-09-04 14:24:16 (GMT)
committerGitHub <noreply@github.com>2023-09-04 14:24:16 (GMT)
commite0f6080819f00d456215646e3117ae77b9af40d1 (patch)
tree0f7c58f493803359bb83cf15f19672c8773cb647
parent7b936ac12e412e749b7b8bb918d9ea8111174ed7 (diff)
downloadcpython-e0f6080819f00d456215646e3117ae77b9af40d1.zip
cpython-e0f6080819f00d456215646e3117ae77b9af40d1.tar.gz
cpython-e0f6080819f00d456215646e3117ae77b9af40d1.tar.bz2
[3.12] gh-46376: Revert "Return existing pointer when possible in ctypes (GH-107131) (GH-107487)" (#108864)
This reverts commit 54aaaadef8a44324f6be674707c67a3516470ff6. Co-authored-by: T. Wouters <thomas@python.org>
-rw-r--r--Lib/test/test_ctypes/test_keeprefs.py27
-rw-r--r--Modules/_ctypes/_ctypes.c29
2 files changed, 0 insertions, 56 deletions
diff --git a/Lib/test/test_ctypes/test_keeprefs.py b/Lib/test/test_ctypes/test_keeprefs.py
index 61650ad..e20adc7 100644
--- a/Lib/test/test_ctypes/test_keeprefs.py
+++ b/Lib/test/test_ctypes/test_keeprefs.py
@@ -93,33 +93,6 @@ class PointerTestCase(unittest.TestCase):
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):
diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c
index 8496077..534ef8c 100644
--- a/Modules/_ctypes/_ctypes.c
+++ b/Modules/_ctypes/_ctypes.c
@@ -5122,8 +5122,6 @@ static PyObject *
Pointer_get_contents(CDataObject *self, void *closure)
{
StgDictObject *stgdict;
- PyObject *keep, *ptr_probe;
- CDataObject *ptr2ptr;
if (*(void **)self->b_ptr == NULL) {
PyErr_SetString(PyExc_ValueError,
@@ -5133,33 +5131,6 @@ Pointer_get_contents(CDataObject *self, void *closure)
stgdict = PyObject_stgdict((PyObject *)self);
assert(stgdict); /* Cannot be NULL for pointer instances */
-
- keep = GetKeepedObjects(self);
- if (keep != NULL) {
- // check if it's a pointer to a pointer:
- // pointers will have '0' key in the _objects
- ptr_probe = PyDict_GetItemString(keep, "0");
-
- if (ptr_probe != NULL) {
- ptr2ptr = (CDataObject*) PyDict_GetItemString(keep, "1");
- if (ptr2ptr == NULL) {
- PyErr_SetString(PyExc_ValueError,
- "Unexpected NULL pointer in _objects");
- return NULL;
- }
- // don't construct a new object,
- // return existing one instead to preserve refcount
- 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
- ); // double-check that we are returning the same thing
- Py_INCREF(ptr2ptr);
- return (PyObject *) ptr2ptr;
- }
- }
-
return PyCData_FromBaseObj(stgdict->proto,
(PyObject *)self, 0,
*(void **)self->b_ptr);