Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32.2k
gh-100926: Move ctype's pointers cache to StgInfo#131282
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes from9 commits
87a974b
12600a6
2b0d69f
df2167a
f96ae59
481cf59
96b4dd1
4fb5baa
758045f
2f9285f
b070ad5
ef1e633
6ea410d
3754d3d
10e36e4
29bfe9c
f8139ff
e1aaf45
82f74ec
eacc724
0b373d5
9c49abd
3c38aa5
efd4961
e251a7d
cdba1f6
a241cd9
8d85624
aedc4b2
6ac84c7
2373c63
fc93bc8
5768347
a2cff24
87f8cf3
5b9891f
85ff1a0
d11af80
6dd1e1a
33ae038
08bdada
8505d4b
c1bf7cc
ea0bf20
fc3bc33
62d2deb
360303f
a2cc961
df56541
283bf96
7d42666
0bb389e
e31c7e9
56d41e2
4316ad9
9c99f9c
5753fc9
58a1507
7082009
f89da96
1afa7b3
e99277c
ebed23e
c4ee75a
66c68b0
0d2c75b
8597f6b
0b5de27
78b6c15
dfd529c
2fc600b
cb78a03
4ef57e9
b33890d
0acc2b2
3129ef5
8efa28a
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -510,6 +510,11 @@ ctypes | ||
loaded by the current process. | ||
(Contributed by Brian Ward in :gh:`119349`.) | ||
* Move :func:`ctypes.POINTER` types cache from the global ``_pointer_type_cache`` | ||
to the corresponding :mod:`ctypes` types. This will stop the cache from | ||
growing without limits in some situations. | ||
(Contributed by Sergey Miryanov in :gh:`100926`). | ||
sergey-miryanov marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
datetime | ||
-------- | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -319,11 +319,9 @@ def SetPointerType(pointer, cls): | ||
warnings._deprecated("ctypes.SetPointerType", remove=(3, 15)) | ||
if _pointer_type_cache.get(cls, None) is not None: | ||
raise RuntimeError("This type already exists in the cache") | ||
pointer.set_type(cls) | ||
_pointer_type_cache[cls] = pointer | ||
sergey-miryanov marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
def ARRAY(typ, len): | ||
return typ * len | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -5,7 +5,6 @@ | ||
import sys | ||
import unittest | ||
from ctypes import (CDLL, Structure, POINTER, pointer, sizeof, byref, | ||
c_void_p, c_char, c_int, c_long) | ||
from test import support | ||
from test.support import import_helper | ||
@@ -145,8 +144,8 @@ class RECT(Structure): | ||
self.assertEqual(ret.top, top.value) | ||
self.assertEqual(ret.bottom, bottom.value) | ||
self.assertEqual(id(PointInRect.argtypes[0]), id(ReturnRect.argtypes[2])) | ||
self.assertEqual(id(PointInRect.argtypes[0]), id(ReturnRect.argtypes[5])) | ||
sergey-miryanov marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
if __name__ == '__main__': | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Move :func:`ctypes.POINTER` types cache from the global ``_pointer_type_cache`` | ||
to the corresponding :mod:`ctypes` types to avoid unlimited growth of the cache. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -474,6 +474,7 @@ CType_Type_traverse(PyObject *self, visitproc visit, void *arg) | ||
Py_VISIT(info->restype); | ||
Py_VISIT(info->checker); | ||
Py_VISIT(info->module); | ||
Py_VISIT(info->pointer_type); | ||
} | ||
Py_VISIT(Py_TYPE(self)); | ||
return PyType_Type.tp_traverse(self, visit, arg); | ||
@@ -489,6 +490,7 @@ ctype_clear_stginfo(StgInfo *info) | ||
Py_CLEAR(info->restype); | ||
Py_CLEAR(info->checker); | ||
Py_CLEAR(info->module); | ||
Py_CLEAR(info->pointer_type); | ||
sergey-miryanov marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
} | ||
static int | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1990,14 +1990,24 @@ create_pointer_type(PyObject *module, PyObject *cls) | ||
{ | ||
PyObject *result; | ||
PyTypeObject *typ; | ||
assert(module); | ||
ctypes_state *st = get_module_state(module); | ||
StgInfo *info = NULL; | ||
if (PyType_Check(cls)) { | ||
if (PyStgInfo_FromType(st, cls, &info) < 0) { | ||
return NULL; | ||
} | ||
if (info && info->pointer_type) { | ||
return Py_NewRef(info->pointer_type); | ||
} | ||
} | ||
if (PyDict_GetItemRef(st->_ctypes_ptrtype_cache, cls, &result) != 0) { | ||
// found or error | ||
return result; | ||
} | ||
// not found | ||
if (PyUnicode_CheckExact(cls)) { | ||
PyObject *name = PyUnicode_FromFormat("LP_%U", cls); | ||
@@ -2007,11 +2017,6 @@ create_pointer_type(PyObject *module, PyObject *cls) | ||
st->PyCPointer_Type); | ||
if (result == NULL) | ||
return result; | ||
sergey-miryanov marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
} else if (PyType_Check(cls)) { | ||
typ = (PyTypeObject *)cls; | ||
PyObject *name = PyUnicode_FromFormat("LP_%s", typ->tp_name); | ||
@@ -2022,17 +2027,15 @@ create_pointer_type(PyObject *module, PyObject *cls) | ||
"_type_", cls); | ||
if (result == NULL) | ||
return result; | ||
} else { | ||
PyErr_SetString(PyExc_TypeError, "must be a ctypes type"); | ||
return NULL; | ||
} | ||
if (info) { | ||
info->pointer_type = Py_NewRef(result); | ||
} | ||
sergey-miryanov marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
return result; | ||
} | ||
@@ -2056,15 +2059,10 @@ create_pointer_inst(PyObject *module, PyObject *arg) | ||
PyObject *result; | ||
PyObject *typ; | ||
typ = create_pointer_type(module, (PyObject *)Py_TYPE(arg)); | ||
if (typ == NULL) | ||
return NULL; | ||
result = PyObject_CallOneArg(typ, arg); | ||
Py_DECREF(typ); | ||
return result; | ||
Uh oh!
There was an error while loading.Please reload this page.