diff --git a/Lib/test/test_capi/test_immortal.py b/Lib/test/test_capi/test_immortal.py
new file mode 100644
index 00000000000..ef5d32b7f01
--- /dev/null
+++ b/Lib/test/test_capi/test_immortal.py
@@ -0,0 +1,16 @@
+import unittest
+from test.support import import_helper
+
+_testcapi = import_helper.import_module('_testcapi')
+
+
+class TestCAPI(unittest.TestCase):
+ def test_immortal_builtins(self):
+ _testcapi.test_immortal_builtins()
+
+ def test_immortal_small_ints(self):
+ _testcapi.test_immortal_small_ints()
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/Modules/Setup.stdlib.in b/Modules/Setup.stdlib.in
index fe1b9f8f538..a90c1e96ef0 100644
--- a/Modules/Setup.stdlib.in
+++ b/Modules/Setup.stdlib.in
@@ -169,7 +169,7 @@
@MODULE__XXTESTFUZZ_TRUE@_xxtestfuzz _xxtestfuzz/_xxtestfuzz.c _xxtestfuzz/fuzzer.c
@MODULE__TESTBUFFER_TRUE@_testbuffer _testbuffer.c
@MODULE__TESTINTERNALCAPI_TRUE@_testinternalcapi _testinternalcapi.c
-@MODULE__TESTCAPI_TRUE@_testcapi _testcapimodule.c _testcapi/vectorcall.c _testcapi/vectorcall_limited.c _testcapi/heaptype.c _testcapi/unicode.c _testcapi/getargs.c _testcapi/pytime.c _testcapi/datetime.c _testcapi/docstring.c _testcapi/mem.c _testcapi/watchers.c _testcapi/long.c _testcapi/float.c _testcapi/structmember.c _testcapi/exceptions.c _testcapi/code.c _testcapi/pyos.c
+@MODULE__TESTCAPI_TRUE@_testcapi _testcapimodule.c _testcapi/vectorcall.c _testcapi/vectorcall_limited.c _testcapi/heaptype.c _testcapi/unicode.c _testcapi/getargs.c _testcapi/pytime.c _testcapi/datetime.c _testcapi/docstring.c _testcapi/mem.c _testcapi/watchers.c _testcapi/long.c _testcapi/float.c _testcapi/structmember.c _testcapi/exceptions.c _testcapi/code.c _testcapi/pyos.c _testcapi/immortal.c
@MODULE__TESTCLINIC_TRUE@_testclinic _testclinic.c
# Some testing modules MUST be built as shared libraries.
diff --git a/Modules/_testcapi/immortal.c b/Modules/_testcapi/immortal.c
new file mode 100644
index 00000000000..10e1733d08a
--- /dev/null
+++ b/Modules/_testcapi/immortal.c
@@ -0,0 +1,47 @@
+#include "parts.h"
+
+int verify_immortality(PyObject *object)
+{
+ assert(_Py_IsImmortal(object));
+ Py_ssize_t old_count = Py_REFCNT(object);
+ for (int j = 0; j < 10000; j++) {
+ Py_DECREF(object);
+ }
+ Py_ssize_t current_count = Py_REFCNT(object);
+ return old_count == current_count;
+}
+
+static PyObject *
+test_immortal_builtins(PyObject *self, PyObject *Py_UNUSED(ignored))
+{
+ PyObject *objects[] = {Py_True, Py_False, Py_None, Py_Ellipsis};
+ Py_ssize_t n = Py_ARRAY_LENGTH(objects);
+ for (Py_ssize_t i = 0; i < n; i++) {
+ assert(verify_immortality(objects[i]));
+ }
+ Py_RETURN_NONE;
+}
+
+static PyObject *
+test_immortal_small_ints(PyObject *self, PyObject *Py_UNUSED(ignored))
+{
+ for (int i = -5; i <= 256; i++) {
+ assert(verify_immortality(PyLong_FromLong(i)));
+ }
+ Py_RETURN_NONE;
+}
+
+static PyMethodDef test_methods[] = {
+ {"test_immortal_builtins", test_immortal_builtins, METH_NOARGS},
+ {"test_immortal_small_ints", test_immortal_small_ints, METH_NOARGS},
+ {NULL},
+};
+
+int
+_PyTestCapi_Init_Immortal(PyObject *mod)
+{
+ if (PyModule_AddFunctions(mod, test_methods) < 0) {
+ return -1;
+ }
+ return 0;
+}
diff --git a/Modules/_testcapi/parts.h b/Modules/_testcapi/parts.h
index 60ec81dad2b..4d2d6832a82 100644
--- a/Modules/_testcapi/parts.h
+++ b/Modules/_testcapi/parts.h
@@ -39,6 +39,7 @@ int _PyTestCapi_Init_Structmember(PyObject *module);
int _PyTestCapi_Init_Exceptions(PyObject *module);
int _PyTestCapi_Init_Code(PyObject *module);
int _PyTestCapi_Init_PyOS(PyObject *module);
+int _PyTestCapi_Init_Immortal(PyObject *module);
#ifdef LIMITED_API_AVAILABLE
int _PyTestCapi_Init_VectorcallLimited(PyObject *module);
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c
index a5d23b1b3d5..30b8b6c6b3a 100644
--- a/Modules/_testcapimodule.c
+++ b/Modules/_testcapimodule.c
@@ -4313,6 +4313,9 @@ PyInit__testcapi(void)
if (_PyTestCapi_Init_PyOS(m) < 0) {
return NULL;
}
+ if (_PyTestCapi_Init_Immortal(m) < 0) {
+ return NULL;
+ }
#ifndef LIMITED_API_AVAILABLE
PyModule_AddObjectRef(m, "LIMITED_API_AVAILABLE", Py_False);
diff --git a/PCbuild/_testcapi.vcxproj b/PCbuild/_testcapi.vcxproj
index 439cd687fda..21941247eb9 100644
--- a/PCbuild/_testcapi.vcxproj
+++ b/PCbuild/_testcapi.vcxproj
@@ -110,6 +110,7 @@
+