Skip to content

Commit 8783cec

Browse files
srinivasreddypicnixzhugovk
authored
gh-129027: Raise DeprecationWarning for sys._clear_type_cache (#129043)
Co-authored-by: Bénédikt Tran <[email protected]> Co-authored-by: Hugo van Kemenade <[email protected]>
1 parent b402a48 commit 8783cec

File tree

7 files changed

+37
-7
lines changed

7 files changed

+37
-7
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,6 @@ although there is currently no date scheduled for their removal.
153153
:class:`~xml.etree.ElementTree.Element` is deprecated. In a future release it
154154
will always return ``True``. Prefer explicit ``len(elem)`` or
155155
``elem is not None`` tests instead.
156+
157+
* :func:`sys._clear_type_cache` is deprecated:
158+
use :func:`sys._clear_internal_caches` instead.

Doc/whatsnew/3.14.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,6 +1246,9 @@ sys
12461246
* On FreeBSD, :data:`sys.platform` doesn't contain the major version anymore.
12471247
It is always ``'freebsd'``, instead of ``'freebsd13'`` or ``'freebsd14'``.
12481248

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

12501253
sys.monitoring
12511254
--------------

Lib/test/test_cmd_line.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import tempfile
1010
import textwrap
1111
import unittest
12+
import warnings
1213
from test import support
1314
from test.support import os_helper
1415
from test.support import force_not_colorized
@@ -936,14 +937,20 @@ def test_python_asyncio_debug(self):
936937
@unittest.skipUnless(sysconfig.get_config_var('Py_TRACE_REFS'), "Requires --with-trace-refs build option")
937938
def test_python_dump_refs(self):
938939
code = 'import sys; sys._clear_type_cache()'
939-
rc, out, err = assert_python_ok('-c', code, PYTHONDUMPREFS='1')
940+
# TODO: Remove warnings context manager once sys._clear_type_cache is removed
941+
with warnings.catch_warnings():
942+
warnings.simplefilter("ignore", DeprecationWarning)
943+
rc, out, err = assert_python_ok('-c', code, PYTHONDUMPREFS='1')
940944
self.assertEqual(rc, 0)
941945

942946
@unittest.skipUnless(sysconfig.get_config_var('Py_TRACE_REFS'), "Requires --with-trace-refs build option")
943947
def test_python_dump_refs_file(self):
944948
with tempfile.NamedTemporaryFile() as dump_file:
945949
code = 'import sys; sys._clear_type_cache()'
946-
rc, out, err = assert_python_ok('-c', code, PYTHONDUMPREFSFILE=dump_file.name)
950+
# TODO: Remove warnings context manager once sys._clear_type_cache is removed
951+
with warnings.catch_warnings():
952+
warnings.simplefilter("ignore", DeprecationWarning)
953+
rc, out, err = assert_python_ok('-c', code, PYTHONDUMPREFSFILE=dump_file.name)
947954
self.assertEqual(rc, 0)
948955
with open(dump_file.name, 'r') as file:
949956
contents = file.read()

Lib/test/test_sys.py

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

892892
@test.support.cpython_only
893893
def test_clear_type_cache(self):
894-
sys._clear_type_cache()
894+
with self.assertWarnsRegex(DeprecationWarning,
895+
r"sys\._clear_type_cache\(\) is deprecated.*"):
896+
sys._clear_type_cache()
895897

896898
@force_not_colorized
897899
@support.requires_subprocess()

Lib/test/test_type_cache.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
""" Tests for the internal type cache in CPython. """
2-
import unittest
32
import dis
3+
import unittest
4+
import warnings
45
from test import support
56
from test.support import import_helper, requires_specialization, requires_specialization_ft
67
try:
@@ -16,6 +17,10 @@
1617
type_assign_version = _testcapi.type_assign_version
1718
type_modified = _testcapi.type_modified
1819

20+
def clear_type_cache():
21+
with warnings.catch_warnings():
22+
warnings.simplefilter("ignore", DeprecationWarning)
23+
_clear_type_cache()
1924

2025
@support.cpython_only
2126
@unittest.skipIf(_clear_type_cache is None, "requires sys._clear_type_cache")
@@ -38,7 +43,7 @@ def test_tp_version_tag_unique(self):
3843
append_result = all_version_tags.append
3944
assertNotEqual = self.assertNotEqual
4045
for _ in range(30):
41-
_clear_type_cache()
46+
clear_type_cache()
4247
X = type('Y', (), {})
4348
X.x = 1
4449
X.x
@@ -78,7 +83,7 @@ class C:
7883
new_version = type_get_version(C)
7984
self.assertEqual(new_version, orig_version + 5)
8085

81-
_clear_type_cache()
86+
clear_type_cache()
8287

8388
def test_per_class_limit(self):
8489
class C:
@@ -112,7 +117,7 @@ class HolderSub(Holder):
112117
@support.cpython_only
113118
class TypeCacheWithSpecializationTests(unittest.TestCase):
114119
def tearDown(self):
115-
_clear_type_cache()
120+
clear_type_cache()
116121

117122
def _assign_valid_version_or_skip(self, type_):
118123
type_modified(type_)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Raise :exc:`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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2208,6 +2208,14 @@ static PyObject *
22082208
sys__clear_type_cache_impl(PyObject *module)
22092209
/*[clinic end generated code: output=20e48ca54a6f6971 input=127f3e04a8d9b555]*/
22102210
{
2211+
if (PyErr_WarnEx(PyExc_DeprecationWarning,
2212+
"sys._clear_type_cache() is deprecated and"
2213+
" scheduled for removal in a future version."
2214+
" Use sys._clear_internal_caches() instead.",
2215+
1) < 0)
2216+
{
2217+
return NULL;
2218+
}
22112219
PyType_ClearCache();
22122220
Py_RETURN_NONE;
22132221
}

0 commit comments

Comments
 (0)