Skip to content

Commit e6bc5fb

Browse files
author
christian.heimes
committed
Added clear cache methods to clear the internal type lookup cache for ref leak test runs.
git-svn-id: http://svn.python.org/projects/python/trunk@60378 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 901d62d commit e6bc5fb

8 files changed

Lines changed: 55 additions & 0 deletions

File tree

Doc/c-api/type.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ Type Objects
3535
.. versionadded:: 2.2
3636

3737

38+
.. cfunction:: unsigned int PyType_ClearCache(void)
39+
40+
Clears the internal lookup cache. Return the current version tag.
41+
42+
.. versionadded:: 2.6
43+
44+
3845
.. cfunction:: int PyType_HasFeature(PyObject *o, int feature)
3946

4047
Return true if the type object *o* sets the feature *feature*. Type features

Doc/library/sys.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ always available.
5858
A string containing the copyright pertaining to the Python interpreter.
5959

6060

61+
.. function:: _cleartypecache()
62+
63+
Clear the internal type lookup cache.
64+
65+
.. versionadded:: 2.6
66+
67+
6168
.. function:: _current_frames()
6269

6370
Return a dictionary mapping each thread's identifier to the topmost stack frame

Include/object.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ PyAPI_FUNC(PyObject *) PyType_GenericAlloc(PyTypeObject *, Py_ssize_t);
399399
PyAPI_FUNC(PyObject *) PyType_GenericNew(PyTypeObject *,
400400
PyObject *, PyObject *);
401401
PyAPI_FUNC(PyObject *) _PyType_Lookup(PyTypeObject *, PyObject *);
402+
PyAPI_FUNC(unsigned int) PyType_ClearCache(void);
402403

403404
/* Generic operations on objects */
404405
PyAPI_FUNC(int) PyObject_Print(PyObject *, FILE *, int);

Lib/test/regrtest.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,9 @@ def dash_R_cleanup(fs, ps, pic, abcs):
709709
sys.path_importer_cache.clear()
710710
sys.path_importer_cache.update(pic)
711711

712+
# clear type cache
713+
sys._cleartypecache()
714+
712715
# Clear ABC registries, restoring previously saved ABC registries.
713716
for abc in [getattr(_abcoll, a) for a in _abcoll.__all__]:
714717
if not issubclass(abc, _Abstract):

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ What's New in Python 2.6 alpha 1?
1212
Core and builtins
1313
-----------------
1414

15+
- Added ``PyType_ClearCache()`` and ``sys._cleartypecache`` to clear the
16+
internal lookup cache for ref leak tests.
17+
1518
- Patch #1473257: generator objects gain a gi_code attribute. This is the
1619
same object as the func_code attribute of the function that produced the
1720
generator.

Objects/typeobject.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,24 @@ struct method_cache_entry {
3232

3333
static struct method_cache_entry method_cache[1 << MCACHE_SIZE_EXP];
3434
static unsigned int next_version_tag = 0;
35+
static void type_modified(PyTypeObject *);
36+
37+
unsigned int
38+
PyType_ClearCache(void)
39+
{
40+
Py_ssize_t i;
41+
unsigned int cur_version_tag = next_version_tag - 1;
42+
43+
for (i = 0; i < (1 << MCACHE_SIZE_EXP); i++) {
44+
method_cache[i].version = 0;
45+
Py_CLEAR(method_cache[i].name);
46+
method_cache[i].value = NULL;
47+
}
48+
next_version_tag = 0;
49+
/* mark all version tags as invalid */
50+
type_modified(&PyBaseObject_Type);
51+
return cur_version_tag;
52+
}
3553

3654
static void
3755
type_modified(PyTypeObject *type)

Python/pythonrun.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,9 @@ Py_Finalize(void)
377377
Py_XDECREF(warnings_module);
378378
warnings_module = NULL;
379379

380+
/* Clear type lookup cache */
381+
PyType_ClearCache();
382+
380383
/* Collect garbage. This may call finalizers; it's nice to call these
381384
* before all modules are destroyed.
382385
* XXX If a __del__ or weakref callback is triggered here, and tries to

Python/sysmodule.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,17 @@ a 11-tuple where the entries in the tuple are counts of:\n\
754754
10. Number of stack pops performed by call_function()"
755755
);
756756

757+
static PyObject *
758+
sys_cleartypecache(PyObject* self, PyObject* args)
759+
{
760+
PyType_ClearCache();
761+
Py_RETURN_NONE;
762+
}
763+
764+
PyDoc_STRVAR(cleartypecache_doc,
765+
"_cleartypecache() -> None\n\
766+
Clear the internal type lookup cache.");
767+
757768
#ifdef __cplusplus
758769
extern "C" {
759770
#endif
@@ -776,6 +787,8 @@ static PyMethodDef sys_methods[] = {
776787
/* Might as well keep this in alphabetic order */
777788
{"callstats", (PyCFunction)PyEval_GetCallStats, METH_NOARGS,
778789
callstats_doc},
790+
{"_cleartypecache", sys_cleartypecache, METH_NOARGS,
791+
cleartypecache_doc},
779792
{"_current_frames", sys_current_frames, METH_NOARGS,
780793
current_frames_doc},
781794
{"displayhook", sys_displayhook, METH_O, displayhook_doc},

0 commit comments

Comments
 (0)