Skip to content

Commit 7d7ae74

Browse files
committed
check type immutable
1 parent fda9cee commit 7d7ae74

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

Lib/test/test_hashlib.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -994,8 +994,6 @@ def test_hash_disallow_instantiation(self):
994994
support.check_disallow_instantiation(self, HASH)
995995
support.check_disallow_instantiation(self, HASHXOF)
996996

997-
# TODO: RUSTPYTHON
998-
@unittest.expectedFailure
999997
def test_readonly_types(self):
1000998
for algorithm, constructors in self.constructors_to_test.items():
1001999
# all other types have DISALLOW_INSTANTIATION

stdlib/src/hashlib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ pub mod _hashlib {
9797
}
9898
}
9999

100-
#[pyclass(with(Representable))]
100+
#[pyclass(flags(IMMUTABLETYPE), with(Representable))]
101101
impl PyHasher {
102102
fn new(name: &str, d: HashWrapper) -> Self {
103103
Self {
@@ -170,7 +170,7 @@ pub mod _hashlib {
170170
}
171171
}
172172

173-
#[pyclass]
173+
#[pyclass(flags(IMMUTABLETYPE))]
174174
impl PyHasherXof {
175175
fn new(name: &str, d: HashXofWrapper) -> Self {
176176
Self {

vm/src/builtins/type.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,6 +1455,15 @@ impl SetAttr for PyType {
14551455
value: PySetterValue,
14561456
vm: &VirtualMachine,
14571457
) -> PyResult<()> {
1458+
// Check if type is immutable
1459+
if zelf.slots.flags.has_feature(PyTypeFlags::IMMUTABLETYPE) {
1460+
return Err(vm.new_type_error(format!(
1461+
"cannot set '{}' attribute of immutable type '{}'",
1462+
attr_name.as_str(),
1463+
zelf.name()
1464+
)));
1465+
}
1466+
14581467
// TODO: pass PyRefExact instead of &str
14591468
let attr_name = vm.ctx.intern_str(attr_name.as_str());
14601469
if let Some(attr) = zelf.get_class_attr(attr_name) {

0 commit comments

Comments
 (0)