summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian Heimes <christian@cheimes.de>2008-01-27 23:34:59 (GMT)
committerChristian Heimes <christian@cheimes.de>2008-01-27 23:34:59 (GMT)
commit908caac52e8e62baa1ee54e4e650e1cd3ac37907 (patch)
tree291efd46f16d74de6e49bba69f785883c6cdfcc1
parent3ea7b41b5805c60a05e697211d0bfc14a62a19fb (diff)
downloadcpython-908caac52e8e62baa1ee54e4e650e1cd3ac37907.zip
cpython-908caac52e8e62baa1ee54e4e650e1cd3ac37907.tar.gz
cpython-908caac52e8e62baa1ee54e4e650e1cd3ac37907.tar.bz2
Added clear cache methods to clear the internal type lookup cache for ref leak test runs.
-rw-r--r--Doc/c-api/type.rst7
-rw-r--r--Doc/library/sys.rst7
-rw-r--r--Include/object.h1
-rwxr-xr-xLib/test/regrtest.py3
-rw-r--r--Misc/NEWS3
-rw-r--r--Objects/typeobject.c18
-rw-r--r--Python/pythonrun.c3
-rw-r--r--Python/sysmodule.c13
8 files changed, 55 insertions, 0 deletions
diff --git a/Doc/c-api/type.rst b/Doc/c-api/type.rst
index 1ee5f58..762c003 100644
--- a/Doc/c-api/type.rst
+++ b/Doc/c-api/type.rst
@@ -35,6 +35,13 @@ Type Objects
.. versionadded:: 2.2
+.. cfunction:: unsigned int PyType_ClearCache(void)
+
+ Clears the internal lookup cache. Return the current version tag.
+
+ .. versionadded:: 2.6
+
+
.. cfunction:: int PyType_HasFeature(PyObject *o, int feature)
Return true if the type object *o* sets the feature *feature*. Type features
diff --git a/Doc/library/sys.rst b/Doc/library/sys.rst
index 4b2ff4a..7c88251 100644
--- a/Doc/library/sys.rst
+++ b/Doc/library/sys.rst
@@ -58,6 +58,13 @@ always available.
A string containing the copyright pertaining to the Python interpreter.
+.. function:: _cleartypecache()
+
+ Clear the internal type lookup cache.
+
+ .. versionadded:: 2.6
+
+
.. function:: _current_frames()
Return a dictionary mapping each thread's identifier to the topmost stack frame
diff --git a/Include/object.h b/Include/object.h
index 7294158..65440a6 100644
--- a/Include/object.h
+++ b/Include/object.h
@@ -399,6 +399,7 @@ PyAPI_FUNC(PyObject *) PyType_GenericAlloc(PyTypeObject *, Py_ssize_t);
PyAPI_FUNC(PyObject *) PyType_GenericNew(PyTypeObject *,
PyObject *, PyObject *);
PyAPI_FUNC(PyObject *) _PyType_Lookup(PyTypeObject *, PyObject *);
+PyAPI_FUNC(unsigned int) PyType_ClearCache(void);
/* Generic operations on objects */
PyAPI_FUNC(int) PyObject_Print(PyObject *, FILE *, int);
diff --git a/Lib/test/regrtest.py b/Lib/test/regrtest.py
index 1da6967..72d1039 100755
--- a/Lib/test/regrtest.py
+++ b/Lib/test/regrtest.py
@@ -709,6 +709,9 @@ def dash_R_cleanup(fs, ps, pic, abcs):
sys.path_importer_cache.clear()
sys.path_importer_cache.update(pic)
+ # clear type cache
+ sys._cleartypecache()
+
# Clear ABC registries, restoring previously saved ABC registries.
for abc in [getattr(_abcoll, a) for a in _abcoll.__all__]:
if not issubclass(abc, _Abstract):
diff --git a/Misc/NEWS b/Misc/NEWS
index 78eb6a3..136494c 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@ What's New in Python 2.6 alpha 1?
Core and builtins
-----------------
+- Added ``PyType_ClearCache()`` and ``sys._cleartypecache`` to clear the
+ internal lookup cache for ref leak tests.
+
- Patch #1473257: generator objects gain a gi_code attribute. This is the
same object as the func_code attribute of the function that produced the
generator.
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index ce413e6..073ee31 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -32,6 +32,24 @@ struct method_cache_entry {
static struct method_cache_entry method_cache[1 << MCACHE_SIZE_EXP];
static unsigned int next_version_tag = 0;
+static void type_modified(PyTypeObject *);
+
+unsigned int
+PyType_ClearCache(void)
+{
+ Py_ssize_t i;
+ unsigned int cur_version_tag = next_version_tag - 1;
+
+ for (i = 0; i < (1 << MCACHE_SIZE_EXP); i++) {
+ method_cache[i].version = 0;
+ Py_CLEAR(method_cache[i].name);
+ method_cache[i].value = NULL;
+ }
+ next_version_tag = 0;
+ /* mark all version tags as invalid */
+ type_modified(&PyBaseObject_Type);
+ return cur_version_tag;
+}
static void
type_modified(PyTypeObject *type)
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
index 4939f54..f5465c5 100644
--- a/Python/pythonrun.c
+++ b/Python/pythonrun.c
@@ -377,6 +377,9 @@ Py_Finalize(void)
Py_XDECREF(warnings_module);
warnings_module = NULL;
+ /* Clear type lookup cache */
+ PyType_ClearCache();
+
/* Collect garbage. This may call finalizers; it's nice to call these
* before all modules are destroyed.
* XXX If a __del__ or weakref callback is triggered here, and tries to
diff --git a/Python/sysmodule.c b/Python/sysmodule.c
index 12ad828..f9fb815 100644
--- a/Python/sysmodule.c
+++ b/Python/sysmodule.c
@@ -754,6 +754,17 @@ a 11-tuple where the entries in the tuple are counts of:\n\
10. Number of stack pops performed by call_function()"
);
+static PyObject *
+sys_cleartypecache(PyObject* self, PyObject* args)
+{
+ PyType_ClearCache();
+ Py_RETURN_NONE;
+}
+
+PyDoc_STRVAR(cleartypecache_doc,
+"_cleartypecache() -> None\n\
+Clear the internal type lookup cache.");
+
#ifdef __cplusplus
extern "C" {
#endif
@@ -776,6 +787,8 @@ static PyMethodDef sys_methods[] = {
/* Might as well keep this in alphabetic order */
{"callstats", (PyCFunction)PyEval_GetCallStats, METH_NOARGS,
callstats_doc},
+ {"_cleartypecache", sys_cleartypecache, METH_NOARGS,
+ cleartypecache_doc},
{"_current_frames", sys_current_frames, METH_NOARGS,
current_frames_doc},
{"displayhook", sys_displayhook, METH_O, displayhook_doc},