Skip to content

Commit bac8a24

Browse files
committed
gh-129027: Raise DeprecationWarning for sys._clear_type_cache
1 parent e2064d6 commit bac8a24

File tree

7 files changed

+29
-3
lines changed

7 files changed

+29
-3
lines changed

Doc/deprecations/pending-removal-in-future.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,6 @@ although there is currently no date scheduled for their removal.
155155

156156
* :meth:`zipimport.zipimporter.load_module` is deprecated:
157157
use :meth:`~zipimport.zipimporter.exec_module` instead.
158+
159+
* :func:`sys._clear_type_cache` is deprecated.
160+
use :func:`sys._clear_internal_caches` instead.

Doc/whatsnew/3.14.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,8 @@ sys
661661
* On FreeBSD, :data:`sys.platform` doesn't contain the major version anymore.
662662
It is always ``'freebsd'``, instead of ``'freebsd13'`` or ``'freebsd14'``.
663663

664+
* Raise :exc:`DeprecationWarning` for :func:`sys._clear_type_cache`. This
665+
function was deprecated in Python 3.13 but it didn't raise a runtime warning.
664666

665667
sys.monitoring
666668
--------------

Lib/test/test_cmd_line.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import unittest
1212
from test import support
1313
from test.support import os_helper
14-
from test.support import force_not_colorized
14+
from test.support import force_not_colorized, warnings_helper
1515
from test.support import threading_helper
1616
from test.support.script_helper import (
1717
spawn_python, kill_python, assert_python_ok, assert_python_failure,
@@ -932,12 +932,14 @@ def test_python_asyncio_debug(self):
932932
self.assertIn(b'True', out)
933933

934934
@unittest.skipUnless(sysconfig.get_config_var('Py_TRACE_REFS'), "Requires --with-trace-refs build option")
935+
@warnings_helper.ignore_warnings(category=DeprecationWarning)
935936
def test_python_dump_refs(self):
936937
code = 'import sys; sys._clear_type_cache()'
937938
rc, out, err = assert_python_ok('-c', code, PYTHONDUMPREFS='1')
938939
self.assertEqual(rc, 0)
939940

940941
@unittest.skipUnless(sysconfig.get_config_var('Py_TRACE_REFS'), "Requires --with-trace-refs build option")
942+
@warnings_helper.ignore_warnings(category=DeprecationWarning)
941943
def test_python_dump_refs_file(self):
942944
with tempfile.NamedTemporaryFile() as dump_file:
943945
code = 'import sys; sys._clear_type_cache()'

Lib/test/test_sys.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,9 @@ def test_sys_getwindowsversion_no_instantiation(self):
850850

851851
@test.support.cpython_only
852852
def test_clear_type_cache(self):
853-
sys._clear_type_cache()
853+
with self.assertWarnsRegex(DeprecationWarning,
854+
r"sys._clear_type_cache\(\) is deprecated.*"):
855+
sys._clear_type_cache()
854856

855857
@force_not_colorized
856858
@support.requires_subprocess()

Lib/test/test_type_cache.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
import unittest
33
import dis
44
from test import support
5-
from test.support import import_helper, requires_specialization, requires_specialization_ft
5+
from test.support import (
6+
import_helper, requires_specialization,
7+
requires_specialization_ft, warnings_helper
8+
)
69
try:
710
from sys import _clear_type_cache
811
except ImportError:
@@ -15,11 +18,13 @@
1518
type_assign_specific_version_unsafe = _testinternalcapi.type_assign_specific_version_unsafe
1619
type_assign_version = _testcapi.type_assign_version
1720
type_modified = _testcapi.type_modified
21+
ignore_deprecation = warnings_helper.ignore_warnings(category=DeprecationWarning)
1822

1923

2024
@support.cpython_only
2125
@unittest.skipIf(_clear_type_cache is None, "requires sys._clear_type_cache")
2226
class TypeCacheTests(unittest.TestCase):
27+
@ignore_deprecation
2328
def test_tp_version_tag_unique(self):
2429
"""tp_version_tag should be unique assuming no overflow, even after
2530
clearing type cache.
@@ -61,6 +66,7 @@ class C:
6166
self.assertNotEqual(type_get_version(C), 0)
6267
self.assertNotEqual(type_get_version(C), c_ver)
6368

69+
@ignore_deprecation
6470
def test_type_assign_specific_version(self):
6571
"""meta-test for type_assign_specific_version_unsafe"""
6672
class C:
@@ -111,6 +117,8 @@ class HolderSub(Holder):
111117

112118
@support.cpython_only
113119
class TypeCacheWithSpecializationTests(unittest.TestCase):
120+
121+
@ignore_deprecation
114122
def tearDown(self):
115123
_clear_type_cache()
116124

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Raise DeprecationWarning for :func:`sys._clear_type_cache`. This function was deprecated in Python 3.13
2+
but it didn't raise a runtime warning.

Python/sysmodule.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2146,6 +2146,13 @@ static PyObject *
21462146
sys__clear_type_cache_impl(PyObject *module)
21472147
/*[clinic end generated code: output=20e48ca54a6f6971 input=127f3e04a8d9b555]*/
21482148
{
2149+
if (PyErr_WarnEx(PyExc_DeprecationWarning,
2150+
"sys._clear_type_cache() is deprecated and scheduled"
2151+
" for removal in a future version. Use the more general"
2152+
" sys._clear_internal_caches() function instead.",
2153+
1) < 0){
2154+
return NULL;
2155+
}
21492156
PyType_ClearCache();
21502157
Py_RETURN_NONE;
21512158
}

0 commit comments

Comments
 (0)