8000 apply buffer · RustPython/RustPython@4448d7f · GitHub
[go: up one dir, main page]

Skip to content

Commit 4448d7f

Browse files
committed
apply buffer
1 parent 6e6df1e commit 4448d7f

File tree

2 files changed

+14
-26
lines changed

2 files changed

+14
-26
lines changed

crates/vm/src/builtins/type.rs

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,6 @@ impl<T> AsRef<T> for PointerSlot<T> {
9898
}
9999
}
100100

101-
impl<T> PointerSlot<T> {
102-
pub unsafe fn from_heaptype<F>(typ: &PyType, f: F) -> Option<Self>
103-
where
104-
F: FnOnce(&HeapTypeExt) -> &T,
105-
{
106-
typ.heaptype_ext
107-
.as_ref()
108-
.map(|ext| Self(NonNull::from(f(ext))))
109-
}
110-
}
111-
112101
pub type PyTypeRef = PyRef<PyType>;
113102

114103
cfg_if::cfg_if! {
@@ -327,6 +316,8 @@ impl PyType {
327316
slots.basicsize = base.slots.basicsize;
328317
}
329318

319+
Self::inherit_readonly_slots(&mut slots, &base);
320+
330321
if let Some(qualname) = attrs.get(identifier!(ctx, __qualname__))
331322
&& !qualname.fast_isinstance(ctx.types.str_type)
332323
{
@@ -383,6 +374,8 @@ impl PyType {
383374
slots.basicsize = base.slots.basicsize;
384375
}
385376

377+
Self::inherit_readonly_slots(&mut slots, &base);
378+
386379
let bases = PyRwLock::new(vec![base.clone()]);
387380
let mro = base.mro_map_collect(|x| x.to_owned());
388381

@@ -459,6 +452,14 @@ impl PyType {
459452
}
460453
}
< 8000 /td>
461454

455+
/// Inherit readonly slots from base type at creation time.
456+
/// These slots are not AtomicCell and must be set before the type is used.
457+
fn inherit_readonly_slots(slots: &mut PyTypeSlots, base: &Self) {
458+
if slots.as_buffer.is_none() {
459+
slots.as_buffer = base.slots.as_buffer;
460+
}
461+
}
462+
462463
/// Inherit slots from base type. typeobject.c: inherit_slots
463464
pub(crate) fn inherit_slots(&self, base: &Self) {
464465
macro_rules! copyslot {
@@ -488,6 +489,7 @@ impl PyType {
488489
// TODO: implement proper init inheritance with object_init check
489490
copyslot!(del);
490491
// new is handled by set_new()
492+
// as_buffer is inherited at type creation time (not AtomicCell)
491493

492494
// Sub-slots (number, sequence, mapping)
493495
self.inherit_number_slots(base);
@@ -817,19 +819,6 @@ impl Py<PyType> {
817819
.collect()
818820
}
819821

820-
pub(crate) fn mro_find_map<F, R>(&self, f: F) -> Option<R>
821-
where
822-
F: Fn(&Self) -> Option<R>,
823-
{
824-
// the hot path will be primitive types which usually hit the result from itself.
825-
// try std::intrinsics::likely once it is stabilized
826-
if let Some(r) = f(self) {
827-
Some(r)
828-
} else {
829-
self.mro.read().iter().find_map(|cls| f(cls))
830-
}
831-
}
832-
833822
pub fn iter_base_chain(&self) -> impl Iterator<Item = &Self> {
834823
std::iter::successors(Some(self), |cls| cls.base.as_deref())
835824
}

crates/vm/src/protocol/buffer.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,7 @@ impl PyBuffer {
143143
impl<'a> TryFromBorrowedObject<'a> for PyBuffer {
144144
fn try_from_borrowed_object(vm: &VirtualMachine, obj: &'a PyObject) -> PyResult<Self> {
145145
let cls = obj.class();
146-
let as_buffer = cls.mro_find_map(|cls| cls.slots.as_buffer);
147-
if let Some(f) = as_buffer {
146+
if let Some(f) = cls.slots.as_buffer {
148147
return f(obj, vm);
149148
}
150149
Err(vm.new_type_error(format!(

0 commit comments

Comments
 (0)
0