-
-
Notifications
You must be signed in to change notification settings - Fork 30.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Co-authored-by: Petr Viktorin <[email protected]>
- Loading branch information
Showing
11 changed files
with
162 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
from test.support import import_helper | ||
import unittest | ||
|
||
_testcapi = import_helper.import_module('_testcapi') | ||
|
||
|
||
class TypeTests(unittest.TestCase): | ||
def test_freeze(self): | ||
# test PyType_Freeze() | ||
type_freeze = _testcapi.type_freeze | ||
|
||
# simple case, no inherante | ||
class MyType: | ||
pass | ||
MyType.attr = "mutable" | ||
|
||
type_freeze(MyType) | ||
err_msg = "cannot set 'attr' attribute of immutable type 'MyType'" | ||
with self.assertRaisesRegex(TypeError, err_msg): | ||
# the class is now immutable | ||
MyType.attr = "immutable" | ||
|
||
# test MRO: PyType_Freeze() requires base classes to be immutable | ||
class A: pass | ||
class B: pass | ||
class C(B): pass | ||
class D(A, C): pass | ||
|
||
self.assertEqual(D.mro(), [D, A, C, B, object]) | ||
with self.assertRaises(TypeError): | ||
type_freeze(D) | ||
|
||
type_freeze(A) | ||
type_freeze(B) | ||
type_freeze(C) | ||
# all parent classes are now immutable, so D can be made immutable | ||
# as well | ||
type_freeze(D) | ||
|
||
def test_freeze_meta(self): | ||
"""test PyType_Freeze() with overridden MRO""" | ||
type_freeze = _testcapi.type_freeze | ||
|
||
class Base: | ||
value = 1 | ||
|
||
class Meta(type): | ||
def mro(cls): | ||
return (cls, Base, object) | ||
|
||
class FreezeThis(metaclass=Meta): | ||
"""This has `Base` in the MRO, but not tp_bases""" | ||
|
||
self.assertEqual(FreezeThis.value, 1) | ||
|
||
with self.assertRaises(TypeError): | ||
type_freeze(FreezeThis) | ||
|
||
Base.value = 2 | ||
self.assertEqual(FreezeThis.value, 2) | ||
|
||
type_freeze(Base) | ||
with self.assertRaises(TypeError): | ||
Base.value = 3 | ||
type_freeze(FreezeThis) | ||
self.assertEqual(FreezeThis.value, 2) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
2 changes: 2 additions & 0 deletions
2
Misc/NEWS.d/next/C_API/2024-07-30-14-40-08.gh-issue-121654.tgGeAl.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Add :c:func:`PyType_Freeze` function to make a type immutable. Patch by | ||
Victor Stinner. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2538,3 +2538,5 @@ | |
added = '3.14' | ||
[function.PyUnicode_Equal] | ||
added = '3.14' | ||
[function.PyType_Freeze] | ||
added = '3.14' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.