Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Include/internal/pycore_interp_structs.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -823,6 +823,17 @@ typedef _Py_CODEUNIT *(*_PyJitEntryFuncPtr)(struct _PyExecutorObject *exec, _PyI

#define _PyInterpreterGuard_GUARDS_NOT_ALLOWED UINTPTR_MAX

typedef struct {
PyTypeObject *async_gen_hooks_type;
PyTypeObject *flags_type;
#if defined(MS_WINDOWS)
PyTypeObject *windows_version_type;
#endif
#ifdef __EMSCRIPTEN__
PyTypeObject *emscripten_info_type;
#endif
} _PySys_State;

/* PyInterpreterState holds the global state for one of the runtime's
interpreters. Typically the initial (main) interpreter is the only one.

Expand DownExpand Up@@ -899,6 +910,7 @@ struct _is {

// Dictionary of the sys module
PyObject *sysdict;
_PySys_State sys_state;

// Dictionary of the builtins module
PyObject *builtins;
Expand Down
6 changes: 6 additions & 0 deletions Lib/test/support/__init__.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -3464,3 +3464,9 @@ def skip_on_low_desktop_heap_memory_subprocess(returncode):
ifreturncode==STATUS_DLL_INIT_FAILED:
raiseunittest.SkipTest('gh-150436: DLL init failed, likely because '
'of low desktop heap memory')


defcheck_immutable_type(testcase, type):
regex=r'cannot set .* attribute of immutable type'
withtestcase.assertRaisesRegex(TypeError, regex):
setattr(type, 'custom_attr', 123)
4 changes: 2 additions & 2 deletions Lib/test/test_decimal.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -32,6 +32,7 @@
import unittest
import numbers
import locale
from test import support
from test.support import (is_resource_enabled,
requires_IEEE_754, requires_docstrings,
check_disallow_instantiation)
Expand DownExpand Up@@ -5806,8 +5807,7 @@ def test_c_immutable_types(self):
)
for tp in types:
with self.subTest(tp=tp):
with self.assertRaisesRegex(TypeError, "immutable"):
tp.foo = 1
support.check_immutable_type(self, tp)

def test_c_disallow_instantiation(self):
ContextManager = type(C.localcontext())
Expand Down
3 changes: 1 addition & 2 deletions Lib/test/test_itertools.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -1541,8 +1541,7 @@ def test_immutable_types(self):
)
fortpindataset:
withself.subTest(tp=tp):
withself.assertRaisesRegex(TypeError, "immutable"):
tp.foobar=1
support.check_immutable_type(self, tp)


classTestExamples(unittest.TestCase):
Expand Down
10 changes: 8 additions & 2 deletions Lib/test/test_sys.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -425,6 +425,7 @@ def test_getwindowsversion(self):
self.assertEqual(v[2], v.build)
self.assertEqual(v[3], v.platform)
self.assertEqual(v[4], v.service_pack)
support.check_immutable_type(self, type(v))

# This is how platform.py calls it. Make sure tuple
# still has 5 elements
Expand DownExpand Up@@ -690,6 +691,7 @@ def test_attributes(self):
self.assertEqual(algo, 0)
self.assertGreaterEqual(sys.hash_info.cutoff, 0)
self.assertLess(sys.hash_info.cutoff, 8)
support.check_immutable_type(self, type(sys.hash_info))

self.assertIsInstance(sys.maxsize, int)
self.assertIsInstance(sys.maxunicode, int)
Expand DownExpand Up@@ -893,11 +895,13 @@ def assert_raise_on_new_sys_type(self, sys_attr):
# sys.flags, sys.version_info, and sys.getwindowsversion.
support.check_disallow_instantiation(self, type(sys_attr), sys_attr)

def test_sys_flags_no_instantiation(self):
def test_sys_flags_type(self):
self.assert_raise_on_new_sys_type(sys.flags)
support.check_immutable_type(self, type(sys.flags))

def test_sys_version_info_no_instantiation(self):
def test_sys_version_info_type(self):
self.assert_raise_on_new_sys_type(sys.version_info)
support.check_immutable_type(self, type(sys.version_info))

def test_sys_getwindowsversion_no_instantiation(self):
# Skip if not being run on Windows.
Expand DownExpand Up@@ -1954,6 +1958,7 @@ def test_asyncgen_hooks(self):
cur = sys.get_asyncgen_hooks()
self.assertIsNone(cur.firstiter)
self.assertIsNone(cur.finalizer)
support.check_immutable_type(self, type(cur))

# gh-118473
with self.assertRaises(TypeError):
Expand DownExpand Up@@ -1997,6 +2002,7 @@ def write(self, s):
self.assertEqual(out, b"")
self.assertEqual(err, b"")


@test.support.support_remote_exec_only
@test.support.cpython_only
class TestRemoteExec(unittest.TestCase):
Expand Down
3 changes: 1 addition & 2 deletions Lib/test/test_xml_etree_c.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -194,8 +194,7 @@ def test_immutable_types(self):
)
fortpindataset:
withself.subTest(tp=tp):
withself.assertRaisesRegex(TypeError, "immutable"):
tp.foo=1
support.check_immutable_type(self, tp)

@support.cpython_only
deftest_disallow_instantiation(self):
Expand Down
21 changes: 11 additions & 10 deletions Python/pylifecycle.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -2072,7 +2072,6 @@ finalize_interp_types(PyInterpreterState *interp)
{
_PyTypes_FiniExtTypes(interp);
_PyUnicode_FiniTypes(interp);
_PySys_FiniTypes(interp);
_PyXI_FiniTypes(interp);
_PyExc_Fini(interp);
_PyFloat_FiniType(interp);
Expand DownExpand Up@@ -2112,14 +2111,16 @@ finalize_interp_types(PyInterpreterState *interp)
staticvoid
finalize_interp_clear(PyThreadState*tstate)
{
intis_main_interp=_Py_IsMainInterpreter(tstate->interp);
PyInterpreterState*interp=tstate->interp;
intis_main_interp=_Py_IsMainInterpreter(interp);

_PyXI_Fini(tstate->interp);
_PyExc_ClearExceptionGroupType(tstate->interp);
_Py_clear_generic_types(tstate->interp);
_PyTypes_FiniCachedDescriptors(tstate->interp);
_PyXI_Fini(interp);
_PyExc_ClearExceptionGroupType(interp);
_Py_clear_generic_types(interp);
_PyTypes_FiniCachedDescriptors(interp);
_PySys_FiniTypes(interp);

/* Clear interpreter state and all thread states */
/* Clear interpreter state and all thread states: last GC collection! */
_PyInterpreterState_Clear(tstate);

/* Clear all loghooks */
Expand All@@ -2136,13 +2137,13 @@ finalize_interp_clear(PyThreadState *tstate)
_PyPerfTrampoline_Fini();
}

finalize_interp_types(tstate->interp);
finalize_interp_types(interp);

/* Finalize dtoa at last so that finalizers calling repr of float doesn't crash */
_PyDtoa_Fini(tstate->interp);
_PyDtoa_Fini(interp);

/* Free any delayed free requests immediately */
_PyMem_FiniDelayed(tstate->interp);
_PyMem_FiniDelayed(interp);

/* finalize_interp_types may allocate Python objects so we may need to
abandon mimalloc segments again */
Expand Down
Loading
Loading