Skip to content
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

Implement DISALLOW_INSTANTIATION PyTypeFlags #5286

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion stdlib/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1403,7 +1403,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(magic)]
fn setstate(&self, state: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
Expand Down
4 changes: 2 additions & 2 deletions stdlib/src/csv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -921,7 +921,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 @@ -1075,7 +1075,7 @@ mod _csv {
}
}

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

#[pyclass(with(IterNext, Iterable))]
#[pyclass(with(IterNext, Iterable), flags(DISALLOW_INSTANTIATION))]
impl UnpackIterator {
#[pymethod(magic)]
fn length_hint(&self) -> usize {
Expand Down
2 changes: 1 addition & 1 deletion stdlib/src/unicodedata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,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 stdlib/src/zlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ mod zlib {
unused_data: PyMutex<PyBytesRef>,
unconsumed_tail: PyMutex<PyBytesRef>,
}
#[pyclass]
#[pyclass(flags(DISALLOW_INSTANTIATION))]
impl PyDecompress {
#[pygetset]
fn eof(&self) -> bool {
Expand Down Expand Up @@ -468,7 +468,7 @@ mod zlib {
inner: PyMutex<CompressInner>,
}

#[pyclass]
#[pyclass(flags(DISALLOW_INSTANTIATION))]
impl PyCompress {
#[pymethod]
fn compress(&self, data: ArgBytesLike, vm: &VirtualMachine) -> PyResult<Vec<u8>> {
Expand Down
38 changes: 34 additions & 4 deletions vm/src/builtins/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ impl PyType {
if base.slots.flags.has_feature(PyTypeFlags::HAS_DICT) {
slots.flags |= PyTypeFlags::HAS_DICT
}
let slots = Self::set_new(&base, slots);

let new_type = PyRef::new_ref(
PyType {
Expand Down Expand Up @@ -247,6 +248,7 @@ impl PyType {

let bases = vec![base.clone()];
let mro = base.iter_mro().map(|x| x.to_owned()).collect();
let slots = Self::set_new(&base, slots);

let new_type = PyRef::new_ref(
PyType {
Expand Down Expand Up @@ -397,6 +399,24 @@ impl PyType {
}
}

fn set_new(base: &PyRef<Self>, slots: PyTypeSlots) -> PyTypeSlots {
// TODO: must provide ctx to new_static
// if slots.new.load().is_none()
// && base.class().is(ctx.types.object_type)
// && slots.flags.contains(PyTypeFlags::HEAPTYPE)
// {
// 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.slots.new.load())
}

slots
}

pub fn slot_name(&self) -> BorrowedValue<str> {
self.name_inner(
|name| name.into(),
Expand Down Expand Up @@ -1094,13 +1114,23 @@ 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) {
let num_args = args.args.len();

if (zelf.is(vm.ctx.types.type_type) && args.kwargs.is_empty()) || !obj.fast_isinstance(zelf)
{
return Ok(obj);
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)));
};

let init = obj.class().mro_find_map(|cls| cls.slots.init.load());
if let Some(init_method) = init {
init_method(obj.clone(), args, vm)?;
Expand Down
1 change: 1 addition & 0 deletions vm/src/types/slot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ bitflags! {
#[derive(Copy, Clone, Debug, PartialEq)]
#[non_exhaustive]
pub struct PyTypeFlags: u64 {
const DISALLOW_INSTANTIATION = 1 << 7;
const IMMUTABLETYPE = 1 << 8;
const HEAPTYPE = 1 << 9;
const BASETYPE = 1 << 10;
Expand Down