Skip to content

pytype::new -> PyType::new #3144

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

Merged
merged 1 commit into from
Sep 27, 2021
Merged
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
116 changes: 58 additions & 58 deletions vm/src/builtins/pytype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,61 @@ impl PyValue for PyType {
}

impl PyType {
pub fn new(
metaclass: PyRef<Self>,
name: &str,
base: PyRef<Self>,
bases: Vec<PyRef<Self>>,
attrs: PyAttributes,
mut slots: PyTypeSlots,
) -> Result<PyRef<Self>, String> {
// Check for duplicates in bases.
let mut unique_bases = HashSet::new();
for base in bases.iter() {
if !unique_bases.insert(base.get_id()) {
return Err(format!("duplicate base class {}", base.name()));
}
}

let mros = bases
.iter()
.map(|x| x.iter_mro().cloned().collect())
.collect();
let mro = linearise_mro(mros)?;

if base.slots.flags.has_feature(PyTpFlags::HAS_DICT) {
slots.flags |= PyTpFlags::HAS_DICT
}

*slots.name.write() = Some(String::from(name));

let new_type = PyRef::new_ref(
PyType {
base: Some(base),
bases,
mro,
subclasses: PyRwLock::default(),
attributes: PyRwLock::new(attrs),
slots,
},
metaclass,
None,
);

for attr_name in new_type.attributes.read().keys() {
if attr_name.starts_with("__") && attr_name.ends_with("__") {
new_type.update_slot(attr_name, true);
}
}
for base in &new_type.bases {
base.subclasses
.write()
.push(PyWeak::downgrade(new_type.as_object()));
}

Ok(new_type)
}

pub fn tp_name(&self) -> String {
self.slots.name.read().as_ref().unwrap().to_string()
}
Expand Down Expand Up @@ -493,7 +548,7 @@ impl PyType {
let flags = PyTpFlags::heap_type_flags() | PyTpFlags::HAS_DICT;
let slots = PyTypeSlots::from_flags(flags);

let typ = new(metatype, name.as_str(), base, bases, attributes, slots)
let typ = Self::new(metatype, name.as_str(), base, bases, attributes, slots)
.map_err(|e| vm.new_type_error(e))?;

// avoid deadlock
Expand Down Expand Up @@ -844,61 +899,6 @@ fn linearise_mro(mut bases: Vec<Vec<PyTypeRef>>) -> Result<Vec<PyTypeRef>, Strin
Ok(result)
}

pub fn new(
typ: PyTypeRef,
name: &str,
base: PyTypeRef,
bases: Vec<PyTypeRef>,
attrs: PyAttributes,
mut slots: PyTypeSlots,
) -> Result<PyTypeRef, String> {
// Check for duplicates in bases.
let mut unique_bases = HashSet::new();
for base in bases.iter() {
if !unique_bases.insert(base.get_id()) {
return Err(format!("duplicate base class {}", base.name()));
}
}

let mros = bases
.iter()
.map(|x| x.iter_mro().cloned().collect())
.collect();
let mro = linearise_mro(mros)?;

if base.slots.flags.has_feature(PyTpFlags::HAS_DICT) {
slots.flags |= PyTpFlags::HAS_DICT
}

*slots.name.write() = Some(String::from(name));

let new_type = PyRef::new_ref(
PyType {
base: Some(base),
bases,
mro,
subclasses: PyRwLock::default(),
attributes: PyRwLock::new(attrs),
slots,
},
typ,
None,
);

for attr_name in new_type.attributes.read().keys() {
if attr_name.starts_with("__") && attr_name.ends_with("__") {
new_type.update_slot(attr_name, true);
}
}
for base in &new_type.bases {
base.subclasses
.write()
.push(PyWeak::downgrade(new_type.as_object()));
}

Ok(new_type)
}

fn calculate_meta_class(
metatype: PyTypeRef,
bases: &[PyTypeRef],
Expand Down Expand Up @@ -986,7 +986,7 @@ mod tests {
let object = &context.types.object_type;
let type_type = &context.types.type_type;

let a = new(
let a = PyType::new(
type_type.clone(),
"A",
object.clone(),
Expand All @@ -995,7 +995,7 @@ mod tests {
Default::default(),
)
.unwrap();
let b = new(
let b = PyType::new(
type_type.clone(),
"B",
object.clone(),
Expand Down
4 changes: 2 additions & 2 deletions vm/src/stdlib/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ mod _io {
};
use crate::{
builtins::{
pytype, PyByteArray, PyBytes, PyBytesRef, PyMemoryView, PyStr, PyStrRef, PyTypeRef,
PyByteArray, PyBytes, PyBytesRef, PyMemoryView, PyStr, PyStrRef, PyType, PyTypeRef,
},
byteslike::{ArgBytesLike, ArgMemoryBuffer},
exceptions::{self, PyBaseExceptionRef},
Expand Down Expand Up @@ -3621,7 +3621,7 @@ mod _io {
}

pub(super) fn make_unsupportedop(ctx: &PyContext) -> PyTypeRef {
pytype::new(
PyType::new(
ctx.types.type_type.clone(),
"UnsupportedOperation",
ctx.exceptions.os_error.clone(),
Expand Down
4 changes: 2 additions & 2 deletions vm/src/stdlib/ssl.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::socket::{self, PySocketRef};
use crate::common::lock::{PyRwLock, PyRwLockWriteGuard};
use crate::{
builtins::{pytype, weakref::PyWeak, PyStrRef, PyTypeRef},
builtins::{PyStrRef, PyType, PyTypeRef, PyWeak},
byteslike::{ArgBytesLike, ArgMemoryBuffer, ArgStrOrBytesLike},
exceptions::{create_exception_type, IntoPyException, PyBaseExceptionRef},
function::{ArgCallable, OptionalArg},
Expand Down Expand Up @@ -1112,7 +1112,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {

let ssl_error = create_exception_type("SSLError", &vm.ctx.exceptions.os_error);

let ssl_cert_verification_error = pytype::new(
let ssl_cert_verification_error = PyType::new(
ctx.types.type_type.clone(),
"SSLCertVerificationError",
ssl_error.clone(),
Expand Down
2 changes: 1 addition & 1 deletion vm/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ pub fn create_type_with_slots(
slots: PyTypeSlots,
) -> PyTypeRef {
let dict = PyAttributes::default();
pytype::new(
PyType::new(
type_type.clone(),
name,
base.clone(),
Expand Down