Skip to content

Commit 57bf7af

Browse files
committed
every PyStructSequence is StaticType
1 parent 3b78c60 commit 57bf7af

File tree

6 files changed

+30
-50
lines changed

6 files changed

+30
-50
lines changed

vm/src/pyobject.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,13 +1333,14 @@ pub trait PyClassImpl: PyClassDef {
13331333
}
13341334

13351335
#[pyimpl]
1336-
pub trait PyStructSequence: PyClassImpl + Sized + 'static {
1336+
pub trait PyStructSequence: StaticType + PyClassImpl + Sized + 'static {
13371337
const FIELD_NAMES: &'static [&'static str];
13381338

13391339
fn into_tuple(self, vm: &VirtualMachine) -> PyTuple;
13401340

1341-
fn into_struct_sequence(self, vm: &VirtualMachine, cls: PyTypeRef) -> PyResult<PyTupleRef> {
1342-
self.into_tuple(vm).into_ref_with_type(vm, cls)
1341+
fn into_struct_sequence(self, vm: &VirtualMachine) -> PyResult<PyTupleRef> {
1342+
self.into_tuple(vm)
1343+
.into_ref_with_type(vm, Self::static_type().clone())
13431344
}
13441345

13451346
#[pymethod(magic)]

vm/src/stdlib/io.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ mod _io {
730730
size: OptionalOption<PyObjectRef>,
731731
vm: &VirtualMachine,
732732
) -> PyResult<String> {
733-
let buffered_reader_class = vm.try_class("_io", "BufferedReader")?;
733+
let buffered_reader_class = BufferedReader::static_type();
734734
let raw = vm.get_attribute(instance, "buffer").unwrap();
735735

736736
if !raw.isinstance(&buffered_reader_class) {
@@ -754,7 +754,7 @@ mod _io {
754754
fn write(instance: PyObjectRef, obj: PyStrRef, vm: &VirtualMachine) -> PyResult<usize> {
755755
use std::str::from_utf8;
756756

757-
let buffered_writer_class = vm.try_class("_io", "BufferedWriter")?;
757+
let buffered_writer_class = BufferedWriter::static_type();
758758
let raw = vm.get_attribute(instance, "buffer").unwrap();
759759

760760
if !raw.isinstance(&buffered_writer_class) {
@@ -781,7 +781,7 @@ mod _io {
781781
size: OptionalOption<PyObjectRef>,
782782
vm: &VirtualMachine,
783783
) -> PyResult<String> {
784-
let buffered_reader_class = vm.try_class("_io", "BufferedReader")?;
784+
let buffered_reader_class = BufferedReader::static_type();
785785
let raw = vm.get_attribute(instance, "buffer").unwrap();
786786

787787
if !raw.isinstance(&buffered_reader_class) {

vm/src/stdlib/os.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -636,9 +636,7 @@ mod _os {
636636
#[pyimpl(with(PyStructSequence))]
637637
impl StatResult {
638638
pub(super) fn into_obj(self, vm: &VirtualMachine) -> PyObjectRef {
639-
self.into_struct_sequence(vm, StatResult::static_type().clone())
640-
.unwrap()
641-
.into_object()
639+
self.into_struct_sequence(vm).unwrap().into_object()
642640
}
643641
}
644642

@@ -1745,9 +1743,7 @@ mod posix {
17451743
#[pyimpl(with(PyStructSequence))]
17461744
impl UnameResult {
17471745
fn into_obj(self, vm: &VirtualMachine) -> PyObjectRef {
1748-
self.into_struct_sequence(vm, UnameResult::static_type().clone())
1749-
.unwrap()
1750-
.into_object()
1746+
self.into_struct_sequence(vm).unwrap().into_object()
17511747
}
17521748
}
17531749

@@ -2143,8 +2139,7 @@ mod posix {
21432139
(w.ws_col.into(), w.ws_row.into())
21442140
}
21452141
};
2146-
super::_os::PyTerminalSize { columns, lines }
2147-
.into_struct_sequence(vm, vm.try_class(super::MODULE_NAME, "terminal_size")?)
2142+
super::_os::PyTerminalSize { columns, lines }.into_struct_sequence(vm)
21482143
}
21492144

21502145
// from libstd:
@@ -2466,8 +2461,7 @@ mod nt {
24662461
)
24672462
}
24682463
};
2469-
super::_os::PyTerminalSize { columns, lines }
2470-
.into_struct_sequence(vm, vm.try_class(super::MODULE_NAME, "terminal_size")?)
2464+
super::_os::PyTerminalSize { columns, lines }.into_struct_sequence(vm)
24712465
}
24722466

24732467
#[cfg(target_env = "msvc")]

vm/src/stdlib/pwd.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ impl From<User> for Passwd {
4848

4949
fn pwd_getpwnam(name: PyStrRef, vm: &VirtualMachine) -> PyResult {
5050
match User::from_name(name.borrow_value()).map_err(|err| err.into_pyexception(vm))? {
51-
Some(user) => Ok(Passwd::from(user)
52-
.into_struct_sequence(vm, vm.try_class("pwd", "struct_passwd")?)?
53-
.into_object()),
51+
Some(user) => Ok(Passwd::from(user).into_struct_sequence(vm)?.into_object()),
5452
None => {
5553
let name_repr = vm.to_repr(name.as_object())?;
5654
let message = vm
@@ -68,9 +66,7 @@ fn pwd_getpwuid(uid: PyIntRef, vm: &VirtualMachine) -> PyResult {
6866
Err(_) => None,
6967
};
7068
match user {
71-
Some(user) => Ok(Passwd::from(user)
72-
.into_struct_sequence(vm, vm.try_class("pwd", "struct_passwd")?)?
73-
.into_object()),
69+
Some(user) => Ok(Passwd::from(user).into_struct_sequence(vm)?.into_object()),
7470
None => {
7571
let message = vm
7672
.ctx
@@ -86,14 +82,11 @@ fn pwd_getpwall(vm: &VirtualMachine) -> PyResult {
8682
static GETPWALL: parking_lot::Mutex<()> = parking_lot::const_mutex(());
8783
let _guard = GETPWALL.lock();
8884
let mut list = Vec::new();
89-
let cls = vm.try_class("pwd", "struct_passwd")?;
9085

9186
unsafe { libc::setpwent() };
9287
while let Some(ptr) = NonNull::new(unsafe { libc::getpwent() }) {
9388
let user = User::from(unsafe { ptr.as_ref() });
94-
let passwd = Passwd::from(user)
95-
.into_struct_sequence(vm, cls.clone())?
96-
.into_object();
89+
let passwd = Passwd::from(user).into_struct_sequence(vm)?.into_object();
9790
list.push(passwd);
9891
}
9992
unsafe { libc::endpwent() };

vm/src/stdlib/time_module.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ use crate::builtins::pytype::PyTypeRef;
1212
use crate::builtins::tuple::PyTupleRef;
1313
use crate::function::OptionalArg;
1414
use crate::pyobject::{
15-
BorrowValue, Either, PyClassImpl, PyObjectRef, PyResult, PyStructSequence, StaticType,
16-
TryFromObject,
15+
BorrowValue, Either, PyClassImpl, PyObjectRef, PyResult, PyStructSequence, TryFromObject,
1716
};
1817
use crate::vm::VirtualMachine;
1918

@@ -219,14 +218,13 @@ impl PyStructTime {
219218
}
220219

221220
fn into_obj(self, vm: &VirtualMachine) -> PyObjectRef {
222-
self.into_struct_sequence(vm, PyStructTime::static_type().clone())
223-
.unwrap()
224-
.into_object()
221+
self.into_struct_sequence(vm).unwrap().into_object()
225222
}
226223

227224
#[pyslot]
228-
fn tp_new(cls: PyTypeRef, seq: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyTupleRef> {
229-
Self::try_from_object(vm, seq)?.into_struct_sequence(vm, cls)
225+
fn tp_new(_cls: PyTypeRef, seq: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyTupleRef> {
226+
// cls is ignorable because this is not a basetype
227+
Self::try_from_object(vm, seq)?.into_struct_sequence(vm)
230228
}
231229
}
232230

vm/src/sysmodule.rs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ fn sys_getwindowsversion(vm: &VirtualMachine) -> PyResult<crate::builtins::tuple
323323
version.dwBuildNumber,
324324
), // TODO Provide accurate version, like CPython impl
325325
}
326-
.into_struct_sequence(vm, vm.try_class("sys", "_getwindowsversion_type")?)
326+
.into_struct_sequence(vm)
327327
}
328328
}
329329

@@ -485,30 +485,24 @@ impl PyIntInfo {
485485
pub fn make_module(vm: &VirtualMachine, module: PyObjectRef, builtins: PyObjectRef) {
486486
let ctx = &vm.ctx;
487487

488-
let flags_type = SysFlags::make_class(ctx);
488+
let _flags_type = SysFlags::make_class(ctx);
489489
let flags = SysFlags::from_settings(&vm.state.settings)
490-
.into_struct_sequence(vm, flags_type)
490+
.into_struct_sequence(vm)
491491
.unwrap();
492492

493-
let version_info_type = version::VersionInfo::make_class(ctx);
493+
let _version_info_type = version::VersionInfo::make_class(ctx);
494494
let version_info = version::VersionInfo::VERSION
495-
.into_struct_sequence(vm, version_info_type)
495+
.into_struct_sequence(vm)
496496
.unwrap();
497497

498-
let hash_info_type = PyHashInfo::make_class(ctx);
499-
let hash_info = PyHashInfo::INFO
500-
.into_struct_sequence(vm, hash_info_type)
501-
.unwrap();
498+
let _hash_info_type = PyHashInfo::make_class(ctx);
499+
let hash_info = PyHashInfo::INFO.into_struct_sequence(vm).unwrap();
502500

503-
let float_info_type = PyFloatInfo::make_class(ctx);
504-
let float_info = PyFloatInfo::INFO
505-
.into_struct_sequence(vm, float_info_type)
506-
.unwrap();
501+
let _float_info_type = PyFloatInfo::make_class(ctx);
502+
let float_info = PyFloatInfo::INFO.into_struct_sequence(vm).unwrap();
507503

508-
let int_info_type = PyIntInfo::make_class(ctx);
509-
let int_info = PyIntInfo::INFO
510-
.into_struct_sequence(vm, int_info_type)
511-
.unwrap();
504+
let _int_info_type = PyIntInfo::make_class(ctx);
505+
let int_info = PyIntInfo::INFO.into_struct_sequence(vm).unwrap();
512506

513507
// TODO Add crate version to this namespace
514508
let implementation = py_namespace!(vm, {

0 commit comments

Comments
 (0)