clean up set

This commit is contained in:
Jeong YunWon
2021-10-11 03:03:49 +09:00
parent 5cd375aaa9
commit 5803439f84
3 changed files with 16 additions and 13 deletions

View File

@@ -28,7 +28,6 @@ pub type SetContentType = dictdatatype::Dict<()>;
pub struct PySet {
pub(super) inner: PySetInner,
}
pub type PySetRef = PyRef<PySet>;
/// frozenset() -> empty frozenset object
/// frozenset(iterable) -> frozenset object
@@ -403,6 +402,14 @@ macro_rules! multi_args_set {
}};
}
impl PySet {
pub fn new_ref(ctx: &PyContext) -> PyRef<Self> {
// Initialized empty, as calling __hash__ is required for adding each object to the set
// which requires a VM context - this is done in the set code itself.
PyRef::new_ref(Self::default(), ctx.types.set_type.clone(), None)
}
}
#[pyimpl(with(Hashable, Comparable, Iterable), flags(BASETYPE))]
impl PySet {
#[pyslot]
@@ -692,6 +699,7 @@ impl PyFrozenSet {
for elem in it {
inner.add(elem, vm)?;
}
// FIXME: empty set check
Ok(Self { inner })
}

View File

@@ -625,7 +625,7 @@ impl ExecutingFrame<'_> {
Ok(None)
}
bytecode::Instruction::BuildSet { size, unpack } => {
let set = vm.ctx.new_set();
let set = set::PySet::new_ref(&vm.ctx);
{
let elements = self.pop_multiple(*size as usize);
if *unpack {
@@ -662,7 +662,7 @@ impl ExecutingFrame<'_> {
bytecode::Instruction::SetAdd { i } => {
let set_obj = self.nth_value(*i);
let item = self.pop_value();
set::PySetRef::try_from_object(vm, set_obj)?.add(item, vm)?;
PyRef::<set::PySet>::try_from_object(vm, set_obj)?.add(item, vm)?;
Ok(None)
}
bytecode::Instruction::MapAdd { i } => {

View File

@@ -10,10 +10,9 @@ use crate::{
bytearray, bytes,
code::{self, PyCode},
getset::{IntoPyGetterFunc, IntoPySetterFunc, PyGetSet},
object, pystr,
set::{self, PyFrozenSet},
PyBaseExceptionRef, PyBoundMethod, PyDict, PyDictRef, PyEllipsis, PyFloat, PyInt, PyIntRef,
PyList, PyNone, PyNotImplemented, PyStr, PyTuple, PyTupleRef, PyType, PyTypeRef,
object, pystr, PyBaseExceptionRef, PyBoundMethod, PyDict, PyDictRef, PyEllipsis, PyFloat,
PyFrozenSet, PyInt, PyIntRef, PyList, PyNone, PyNotImplemented, PyStr, PyTuple, PyTupleRef,
PyType, PyTypeRef,
},
dictdatatype::Dict,
exceptions,
@@ -157,6 +156,8 @@ impl PyContext {
self.not_implemented.clone().into()
}
// shortcuts for common type
#[inline]
pub fn new_int<T: Into<BigInt> + ToPrimitive>(&self, i: T) -> PyIntRef {
if let Some(i) = i.to_i32() {
@@ -235,12 +236,6 @@ impl PyContext {
PyObject::new(PyList::from(elements), self.types.list_type.clone(), None)
}
pub fn new_set(&self) -> set::PySetRef {
// Initialized empty, as calling __hash__ is required for adding each object to the set
// which requires a VM context - this is done in the set code itself.
PyRef::new_ref(set::PySet::default(), self.types.set_type.clone(), None)
}
pub fn new_dict(&self) -> PyDictRef {
PyRef::new_ref(PyDict::default(), self.types.dict_type.clone(), None)
}