]> granicus.if.org Git - python/commitdiff
Added clear cache methods to clear the internal type lookup cache for ref leak test...
authorChristian Heimes <christian@cheimes.de>
Sun, 27 Jan 2008 23:34:59 +0000 (23:34 +0000)
committerChristian Heimes <christian@cheimes.de>
Sun, 27 Jan 2008 23:34:59 +0000 (23:34 +0000)
Doc/c-api/type.rst
Doc/library/sys.rst
Include/object.h
Lib/test/regrtest.py
Misc/NEWS
Objects/typeobject.c
Python/pythonrun.c
Python/sysmodule.c

index 1ee5f58aadf65773fd87bf7d7a4f2c46c4393422..762c003c7470e2874d89ba629d4ede76fc651c82 100644 (file)
@@ -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
index 4b2ff4a80eb99f89c41196d41035ccbbd461e419..7c88251523fe862e47ccc328b8f3b6be0a9098b5 100644 (file)
@@ -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
index 7294158dd441f4d1b6cc07a0a71cf67aa2158b91..65440a6511df63c2fbf768b7c7d54941d492e436 100644 (file)
@@ -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);
index 1da6967ae57574d6b1dacdb985c7cfb922740814..72d103974abc8ad2dedfa04c99f707fb68593a0e 100755 (executable)
@@ -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):
index 78eb6a3c70d3cf8bbc5a8fa2fe2d3b0ee065a8cd..136494cf41bd4cd72b00f38f18711abb41b373ec 100644 (file)
--- 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.
index ce413e604908822d57ebe4606e22ce417beea814..073ee311f56af07cb12a4a488a7e4202eeb1283e 100644 (file)
@@ -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)
index 4939f5489083312d667abc7884678ff2e03db78b..f5465c5edeea64076e2bd1a727666c48d38e7323 100644 (file)
@@ -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
index 12ad828bbbc233dff5bd28d3681ab88eb4c4f20e..f9fb81596e416946a86fdd62443e149ac05ec51b 100644 (file)
@@ -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},