Skip to content

Commit ed01f87

Browse files
authored
Merge pull request #3144 from youknowone/pytype-new
pytype::new -> PyType::new
2 parents 81603b5 + e21c0af commit ed01f87

File tree

4 files changed

+63
-63
lines changed

4 files changed

+63
-63
lines changed

vm/src/builtins/pytype.rs

Lines changed: 58 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,61 @@ impl PyValue for PyType {
4949
}
5050

5151
impl PyType {
52+
pub fn new(
53+
metaclass: PyRef<Self>,
54+
name: &str,
55+
base: PyRef<Self>,
56+
bases: Vec<PyRef<Self>>,
57+
attrs: PyAttributes,
58+
mut slots: PyTypeSlots,
59+
) -> Result<PyRef<Self>, String> {
60+
// Check for duplicates in bases.
61+
let mut unique_bases = HashSet::new();
62+
for base in bases.iter() {
63+
if !unique_bases.insert(base.get_id()) {
64+
return Err(format!("duplicate base class {}", base.name()));
65+
}
66+
}
67+
68+
let mros = bases
69+
.iter()
70+
.map(|x| x.iter_mro().cloned().collect())
71+
.collect();
72+
let mro = linearise_mro(mros)?;
73+
74+
if base.slots.flags.has_feature(PyTpFlags::HAS_DICT) {
75+
slots.flags |= PyTpFlags::HAS_DICT
76+
}
77+
78+
*slots.name.write() = Some(String::from(name));
79+
80+
let new_type = PyRef::new_ref(
81+
PyType {
82+
base: Some(base),
83+
bases,
84+
mro,
85+
subclasses: PyRwLock::default(),
86+
attributes: PyRwLock::new(attrs),
87+
slots,
88+
},
89+
metaclass,
90+
None,
91+
);
92+
93+
for attr_name in new_type.attributes.read().keys() {
94+
if attr_name.starts_with("__") && attr_name.ends_with("__") {
95+
new_type.update_slot(attr_name, true);
96+
}
97+
}
98+
for base in &new_type.bases {
99+
base.subclasses
100+
.write()
101+
.push(PyWeak::downgrade(new_type.as_object()));
102+
}
103+
104+
Ok(new_type)
105+
}
106+
52107
pub fn tp_name(&self) -> String {
53108
self.slots.name.read().as_ref().unwrap().to_string()
54109
}
@@ -493,7 +548,7 @@ impl PyType {
493548
let flags = PyTpFlags::heap_type_flags() | PyTpFlags::HAS_DICT;
494549
let slots = PyTypeSlots::from_flags(flags);
495550

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

499554
// avoid deadlock
@@ -844,61 +899,6 @@ fn linearise_mro(mut bases: Vec<Vec<PyTypeRef>>) -> Result<Vec<PyTypeRef>, Strin
844899
Ok(result)
845900
}
846901

847-
pub fn new(
848-
typ: PyTypeRef,
849-
name: &str,
850-
base: PyTypeRef,
851-
bases: Vec<PyTypeRef>,
852-
attrs: PyAttributes,
853-
mut slots: PyTypeSlots,
854-
) -> Result<PyTypeRef, String> {
855-
// Check for duplicates in bases.
856-
let mut unique_bases = HashSet::new();
857-
for base in bases.iter() {
858-
if !unique_bases.insert(base.get_id()) {
859-
return Err(format!("duplicate base class {}", base.name()));
860-
}
861-
}
862-
863-
let mros = bases
864-
.iter()
865-
.map(|x| x.iter_mro().cloned().collect())
866-
.collect();
867-
let mro = linearise_mro(mros)?;
868-
869-
if base.slots.flags.has_feature(PyTpFlags::HAS_DICT) {
870-
slots.flags |= PyTpFlags::HAS_DICT
871-
}
872-
873-
*slots.name.write() = Some(String::from(name));
874-
875-
let new_type = PyRef::new_ref(
876-
PyType {
877-
base: Some(base),
878-
bases,
879-
mro,
880-
subclasses: PyRwLock::default(),
881-
attributes: PyRwLock::new(attrs),
882-
slots,
883-
},
884-
typ,
885-
None,
886-
);
887-
888-
for attr_name in new_type.attributes.read().keys() {
889-
if attr_name.starts_with("__") && attr_name.ends_with("__") {
890-
new_type.update_slot(attr_name, true);
891-
}
892-
}
893-
for base in &new_type.bases {
894-
base.subclasses
895-
.write()
896-
.push(PyWeak::downgrade(new_type.as_object()));
897-
}
898-
899-
Ok(new_type)
900-
}
901-
902902
fn calculate_meta_class(
903903
metatype: PyTypeRef,
904904
bases: &[PyTypeRef],
@@ -986,7 +986,7 @@ mod tests {
986986
let object = &context.types.object_type;
987987
let type_type = &context.types.type_type;
988988

989-
let a = new(
989+
let a = PyType::new(
990990
type_type.clone(),
991991
"A",
992992
object.clone(),
@@ -995,7 +995,7 @@ mod tests {
995995
Default::default(),
996996
)
997997
.unwrap();
998-
let b = new(
998+
let b = PyType::new(
999999
type_type.clone(),
10001000
"B",
10011001
object.clone(),

vm/src/stdlib/io.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ mod _io {
7878
};
7979
use crate::{
8080
builtins::{
81-
pytype, PyByteArray, PyBytes, PyBytesRef, PyMemoryView, PyStr, PyStrRef, PyTypeRef,
81+
PyByteArray, PyBytes, PyBytesRef, PyMemoryView, PyStr, PyStrRef, PyType, PyTypeRef,
8282
},
8383
byteslike::{ArgBytesLike, ArgMemoryBuffer},
8484
exceptions::{self, PyBaseExceptionRef},
@@ -3621,7 +3621,7 @@ mod _io {
36213621
}
36223622

36233623
pub(super) fn make_unsupportedop(ctx: &PyContext) -> PyTypeRef {
3624-
pytype::new(
3624+
PyType::new(
36253625
ctx.types.type_type.clone(),
36263626
"UnsupportedOperation",
36273627
ctx.exceptions.os_error.clone(),

vm/src/stdlib/ssl.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::socket::{self, PySocket};
22
use crate::common::lock::{PyRwLock, PyRwLockWriteGuard};
33
use crate::{
4-
builtins::{pytype, weakref::PyWeak, PyStrRef, PyTypeRef},
4+
builtins::{PyStrRef, PyType, PyTypeRef, PyWeak},
55
byteslike::{ArgBytesLike, ArgMemoryBuffer, ArgStrOrBytesLike},
66
exceptions::{create_exception_type, IntoPyException, PyBaseExceptionRef},
77
function::{ArgCallable, OptionalArg},
@@ -1114,7 +1114,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
11141114

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

1117-
let ssl_cert_verification_error = pytype::new(
1117+
let ssl_cert_verification_error = PyType::new(
11181118
ctx.types.type_type.clone(),
11191119
"SSLCertVerificationError",
11201120
ssl_error.clone(),

vm/src/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ pub fn create_type_with_slots(
227227
slots: PyTypeSlots,
228228
) -> PyTypeRef {
229229
let dict = PyAttributes::default();
230-
pytype::new(
230+
PyType::new(
231231
type_type.clone(),
232232
name,
233233
base.clone(),

0 commit comments

Comments
 (0)