Skip to content

Commit 5d196a5

Browse files
committed
type.__dict__
1 parent e21ec55 commit 5d196a5

File tree

1 file changed

+22
-24
lines changed

1 file changed

+22
-24
lines changed

vm/src/builtins/type.rs

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,21 +1018,6 @@ impl Constructor for PyType {
10181018
attributes.insert(identifier!(vm, __hash__), vm.ctx.none.clone().into());
10191019
}
10201020

1021-
// All *classes* should have a dict. Exceptions are *instances* of
1022-
// classes that define __slots__ and instances of built-in classes
1023-
// (with exceptions, e.g function)
1024-
let __dict__ = identifier!(vm, __dict__);
1025-
attributes.entry(__dict__).or_insert_with(|| {
1026-
vm.ctx
1027-
.new_static_getset(
1028-
"__dict__",
1029-
vm.ctx.types.type_type,
1030-
subtype_get_dict,
1031-
subtype_set_dict,
1032-
)
1033-
.into()
1034-
});
1035-
10361021
// TODO: Flags is currently initialized with HAS_DICT. Should be
10371022
// updated when __slots__ are supported (toggling the flag off if
10381023
// a class has __slots__ defined).
@@ -1124,15 +1109,28 @@ impl Constructor for PyType {
11241109
}
11251110
}
11261111

1127-
if let Some(cell) = typ.attributes.write().get(identifier!(vm, __classcell__)) {
1128-
let cell = PyCellRef::try_from_object(vm, cell.clone()).map_err(|_| {
1129-
vm.new_type_error(format!(
1130-
"__classcell__ must be a nonlocal cell, not {}",
1131-
cell.class().name()
1132-
))
1133-
})?;
1134-
cell.set(Some(typ.clone().into()));
1135-
};
1112+
{
1113+
let mut attributes = typ.attributes.write();
1114+
// All *classes* should have a dict. Exceptions are *instances* of
1115+
// classes that define __slots__ and instances of built-in classes
1116+
// (with exceptions, e.g function)
1117+
let __dict__ = identifier!(vm, __dict__);
1118+
attributes.entry(__dict__).or_insert_with(|| unsafe {
1119+
vm.ctx
1120+
.new_getset("__dict__", &typ, subtype_get_dict, subtype_set_dict)
1121+
.into()
1122+
});
1123+
1124+
if let Some(cell) = attributes.get(identifier!(vm, __classcell__)) {
1125+
let cell = PyCellRef::try_from_object(vm, cell.clone()).map_err(|_| {
1126+
vm.new_type_error(format!(
1127+
"__classcell__ must be a nonlocal cell, not {}",
1128+
cell.class().name()
1129+
))
1130+
})?;
1131+
cell.set(Some(typ.clone().into()));
1132+
};
1133+
}
11361134

11371135
// avoid deadlock
11381136
let attributes = typ

0 commit comments

Comments
 (0)