-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
gh-121654: Add PyType_Freeze() function #122457
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
2d24aba
f7bd310
f7d75ac
1fcda68
beea5e3
0155362
1c1fa7e
e0cc4be
a9f1f01
0f98b64
2752daa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
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 | ||
|
||
class MyType: | ||
pass | ||
MyType.attr = "mutable" | ||
|
||
type_freeze(MyType) | ||
with self.assertRaises(TypeError): | ||
vstinner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
MyType.attr = "immutable" | ||
|
||
# PyType_Freeze() requires base classes to be immutable | ||
class Mutable: | ||
"mutable base class" | ||
pass | ||
class MyType2(Mutable): | ||
pass | ||
with self.assertRaises(TypeError): | ||
type_freeze(MyType2) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2510,3 +2510,5 @@ | |
added = '3.14' | ||
[function.PyIter_NextItem] | ||
added = '3.14' | ||
[function.PyType_Freeze] | ||
added = '3.14' |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4642,6 +4642,26 @@ check_basicsize_includes_size_and_offsets(PyTypeObject* type) | |
return 1; | ||
} | ||
|
||
static int | ||
check_immutable_bases(const char *type_name, PyObject *bases) | ||
{ | ||
for (Py_ssize_t i=0; i<PyTuple_GET_SIZE(bases); i++) { | ||
PyTypeObject *b = (PyTypeObject*)PyTuple_GET_ITEM(bases, i); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry for the necro-posting. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know, I just moved the code. The test was already there before. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got it, thanks! |
||
if (!b) { | ||
return -1; | ||
} | ||
if (!_PyType_HasFeature(b, Py_TPFLAGS_IMMUTABLETYPE)) { | ||
PyErr_Format( | ||
PyExc_TypeError, | ||
"Creating immutable type %s from mutable base %N", | ||
type_name, b | ||
); | ||
return -1; | ||
} | ||
} | ||
return 0; | ||
} | ||
|
||
static PyObject * | ||
_PyType_FromMetaclass_impl( | ||
PyTypeObject *metaclass, PyObject *module, | ||
|
@@ -4798,19 +4818,8 @@ _PyType_FromMetaclass_impl( | |
* and only heap types can be mutable.) | ||
*/ | ||
if (spec->flags & Py_TPFLAGS_IMMUTABLETYPE) { | ||
for (int i=0; i<PyTuple_GET_SIZE(bases); i++) { | ||
PyTypeObject *b = (PyTypeObject*)PyTuple_GET_ITEM(bases, i); | ||
if (!b) { | ||
goto finally; | ||
} | ||
if (!_PyType_HasFeature(b, Py_TPFLAGS_IMMUTABLETYPE)) { | ||
PyErr_Format( | ||
PyExc_TypeError, | ||
"Creating immutable type %s from mutable base %N", | ||
spec->name, b | ||
); | ||
goto finally; | ||
} | ||
if (check_immutable_bases(spec->name, bases) < 0) { | ||
goto finally; | ||
} | ||
} | ||
|
||
|
@@ -11178,6 +11187,18 @@ add_operators(PyTypeObject *type, PyTypeObject *def) | |
} | ||
|
||
|
||
int | ||
PyType_Freeze(PyTypeObject *type) | ||
{ | ||
PyObject *bases = type->tp_bases; | ||
if (check_immutable_bases(type->tp_name, bases) < 0) { | ||
vstinner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return -1; | ||
} | ||
type->tp_flags |= Py_TPFLAGS_IMMUTABLETYPE; | ||
return 0; | ||
} | ||
|
||
|
||
/* Cooperative 'super' */ | ||
|
||
typedef struct { | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.