Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/stdlib/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1399,7 +1399,7 @@ mod array {
internal: PyMutex<PositionIterInternal<PyArrayRef>>,
}

#[pyclass(with(IterNext, Iterable), flags(HAS_DICT))]
#[pyclass(with(IterNext, Iterable), flags(HAS_DICT, DISALLOW_INSTANTIATION))]
impl PyArrayIter {
#[pymethod]
fn __setstate__(&self, state: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
Expand Down
4 changes: 2 additions & 2 deletions crates/stdlib/src/csv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,7 @@ mod _csv {
}
}

#[pyclass(with(IterNext, Iterable))]
#[pyclass(with(IterNext, Iterable), flags(DISALLOW_INSTANTIATION))]
impl Reader {
#[pygetset]
fn line_num(&self) -> u64 {
Expand Down Expand Up @@ -1059,7 +1059,7 @@ mod _csv {
}
}

#[pyclass]
#[pyclass(flags(DISALLOW_INSTANTIATION))]
impl Writer {
#[pygetset(name = "dialect")]
const fn get_dialect(&self, _vm: &VirtualMachine) -> PyDialect {
Expand Down
2 changes: 1 addition & 1 deletion crates/stdlib/src/pystruct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ pub(crate) mod _struct {
}
}

#[pyclass(with(Unconstructible, IterNext, Iterable))]
#[pyclass(with(IterNext, Iterable), flags(DISALLOW_INSTANTIATION))]
impl UnpackIterator {
#[pymethod]
fn __length_hint__(&self) -> usize {
Expand Down
2 changes: 1 addition & 1 deletion crates/stdlib/src/unicodedata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ mod unicodedata {
}
}

#[pyclass]
#[pyclass(flags(DISALLOW_INSTANTIATION))]
impl Ucd {
#[pymethod]
fn category(&self, character: PyStrRef, vm: &VirtualMachine) -> PyResult<String> {
Expand Down
4 changes: 2 additions & 2 deletions crates/stdlib/src/zlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ mod zlib {
inner: PyMutex<PyDecompressInner>,
}

#[pyclass]
#[pyclass(flags(DISALLOW_INSTANTIATION))]
impl PyDecompress {
#[pygetset]
fn eof(&self) -> bool {
Expand Down Expand Up @@ -383,7 +383,7 @@ mod zlib {
inner: PyMutex<CompressState<CompressInner>>,
}

#[pyclass]
#[pyclass(flags(DISALLOW_INSTANTIATION))]
impl PyCompress {
#[pymethod]
fn compress(&self, data: ArgBytesLike, vm: &VirtualMachine) -> PyResult<Vec<u8>> {
Expand Down
53 changes: 45 additions & 8 deletions crates/vm/src/builtins/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,8 @@ impl PyType {
None,
);

Self::set_new(&new_type.slots, &new_type.base);

let weakref_type = super::PyWeak::static_type();
for base in new_type.bases.read().iter() {
base.subclasses.write().push(
Expand All @@ -420,9 +422,6 @@ impl PyType {

for cls in self.mro.read().iter() {
for &name in cls.attributes.read().keys() {
if name == identifier!(ctx, __new__) {
continue;
}
if name.as_bytes().starts_with(b"__") && name.as_bytes().ends_with(b"__") {
slot_name_set.insert(name);
}
Expand All @@ -436,6 +435,31 @@ impl PyType {
for attr_name in slot_name_set {
self.update_slot::<true>(attr_name, ctx);
}

Self::set_new(&self.slots, &self.base);
}

fn set_new(slots: &PyTypeSlots, base: &Option<PyTypeRef>) {
// if self.slots.new.load().is_none()
// && self
// .base
// .as_ref()
// .map(|base| base.class().is(ctx.types.object_type))
// .unwrap_or(false)
// && self.slots.flags.contains(PyTypeFlags::HEAPTYPE)
// {
// self.slots.flags |= PyTypeFlags::DISALLOW_INSTANTIATION;
// }

if slots.flags.contains(PyTypeFlags::DISALLOW_INSTANTIATION) {
slots.new.store(None)
} else if slots.new.load().is_none() {
slots.new.store(
base.as_ref()
.map(|base| base.slots.new.load())
.unwrap_or(None),
)
}
}

// This is used for class initialization where the vm is not yet available.
Expand Down Expand Up @@ -1563,15 +1587,28 @@ impl Callable for PyType {
type Args = FuncArgs;
fn call(zelf: &Py<Self>, args: FuncArgs, vm: &VirtualMachine) -> PyResult {
vm_trace!("type_call: {:?}", zelf);
let obj = call_slot_new(zelf.to_owned(), zelf.to_owned(), args.clone(), vm)?;

if (zelf.is(vm.ctx.types.type_type) && args.kwargs.is_empty()) || !obj.fast_isinstance(zelf)
{
if zelf.is(vm.ctx.types.type_type) {
let num_args = args.args.len();
if num_args == 1 && args.kwargs.is_empty() {
return Ok(args.args[0].obj_type());
}
if num_args != 3 {
return Err(vm.new_type_error("type() takes 1 or 3 arguments".to_owned()));
}
}

let obj = if let Some(slot_new) = zelf.slots.new.load() {
slot_new(zelf.to_owned(), args.clone(), vm)?
} else {
return Err(vm.new_type_error(format!("cannot create '{}' instances", zelf.slots.name)));
};

if !obj.class().fast_issubclass(zelf) {
return Ok(obj);
}

let init = obj.class().mro_find_map(|cls| cls.slots.init.load());
if let Some(init_method) = init {
if let Some(init_method) = obj.class().slots.init.load() {
init_method(obj.clone(), args, vm)?;
}
Ok(obj)
Expand Down
Loading