Skip to content

Commit 947c1cc

Browse files
committed
Replace IntoPyRef with VirtualMachine::new_pyref
1 parent b54539c commit 947c1cc

File tree

6 files changed

+28
-34
lines changed

6 files changed

+28
-34
lines changed

stdlib/src/zlib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ mod zlib {
55
use crate::common::lock::PyMutex;
66
use crate::vm::{
77
builtins::{PyBaseExceptionRef, PyBytes, PyBytesRef, PyIntRef, PyTypeRef},
8-
function::{ArgBytesLike, IntoPyRef, OptionalArg, OptionalOption},
8+
function::{ArgBytesLike, OptionalArg, OptionalOption},
99
PyResult, PyValue, VirtualMachine,
1010
};
1111
use adler32::RollingAdler32 as Adler32;
@@ -321,7 +321,7 @@ mod zlib {
321321
.chain(leftover)
322322
.copied()
323323
.collect();
324-
*unused_data = unused.into_pyref(vm);
324+
*unused_data = vm.new_pyref(unused);
325325
}
326326
}
327327

vm/src/builtins/range.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::{PyInt, PyIntRef, PySlice, PyTupleRef, PyTypeRef};
22
use crate::common::hash::PyHash;
33
use crate::{
44
builtins::builtins_iter,
5-
function::{FuncArgs, IntoPyRef, OptionalArg, PyComparisonValue},
5+
function::{FuncArgs, OptionalArg, PyComparisonValue},
66
protocol::{PyIterReturn, PyMappingMethods, PySequenceMethods},
77
pyclass::PyClassImpl,
88
types::{
@@ -183,9 +183,9 @@ pub fn init(context: &PyContext) {
183183
impl PyRange {
184184
fn new(cls: PyTypeRef, stop: PyIntRef, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
185185
PyRange {
186-
start: (0).into_pyref(vm),
186+
start: vm.new_pyref(0),
187187
stop,
188-
step: (1).into_pyref(vm),
188+
step: vm.new_pyref(1),
189189
}
190190
.into_ref_with_type(vm, cls)
191191
}
@@ -197,7 +197,7 @@ impl PyRange {
197197
step: OptionalArg<PyIntRef>,
198198
vm: &VirtualMachine,
199199
) -> PyResult<PyRef<Self>> {
200-
let step = step.unwrap_or_else(|| (1).into_pyref(vm));
200+
let step = step.unwrap_or_else(|| vm.new_pyref(1));
201201
if step.as_bigint().is_zero() {
202202
return Err(vm.new_value_error("range() arg 3 must not be zero".to_owned()));
203203
}
@@ -350,9 +350,9 @@ impl PyRange {
350350
substop = (substop * range_step.as_bigint()) + range_start.as_bigint();
351351

352352
Ok(PyRange {
353-
start: substart.into_pyref(vm),
354-
stop: substop.into_pyref(vm),
355-
step: substep.into_pyref(vm),
353+
start: vm.new_pyref(substart),
354+
stop: vm.new_pyref(substop),
355+
step: vm.new_pyref(substep),
356356
}
357357
.into_ref(vm)
358358
.into())

vm/src/function.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ mod number;
66
use crate::{
77
builtins::{PyBaseExceptionRef, PyTupleRef, PyTypeRef},
88
convert::{ToPyObject, ToPyResult},
9-
pyobject::{PyObjectPayload, PyThreadingConstraint},
9+
pyobject::PyThreadingConstraint,
1010
AsPyObject, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, VirtualMachine,
1111
};
1212
use indexmap::IndexMap;
@@ -18,10 +18,6 @@ pub use arithmetic::{PyArithmeticValue, PyComparisonValue};
1818
pub use buffer::{ArgAsciiBuffer, ArgBytesLike, ArgMemoryBuffer, ArgStrOrBytesLike};
1919
pub use number::{ArgIntoBool, ArgIntoComplex, ArgIntoFloat};
2020

21-
pub trait IntoPyRef<T: PyObjectPayload> {
22-
fn into_pyref(self, vm: &VirtualMachine) -> PyRef<T>;
23-
}
24-
2521
pub trait IntoFuncArgs: Sized {
2622
fn into_args(self, vm: &VirtualMachine) -> FuncArgs;
2723
fn into_method_args(self, obj: PyObjectRef, vm: &VirtualMachine) -> FuncArgs {

vm/src/pyobject.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::{
1414
convert::{ToPyObject, ToPyResult},
1515
dictdatatype::Dict,
1616
exceptions,
17-
function::{IntoFuncArgs, IntoPyNativeFunc, IntoPyRef},
17+
function::{IntoFuncArgs, IntoPyNativeFunc},
1818
pyclass::{PyClassImpl, StaticType},
1919
types::{PyTypeFlags, PyTypeSlots, TypeZoo},
2020
VirtualMachine,
@@ -497,16 +497,6 @@ where
497497
}
498498
}
499499

500-
impl<T, P> IntoPyRef<P> for T
501-
where
502-
P: PyValue + ToPyObject + From<T>,
503-
{
504-
#[inline(always)]
505-
fn into_pyref(self, vm: &VirtualMachine) -> PyRef<P> {
506-
P::from(self).into_ref(vm)
507-
}
508-
}
509-
510500
impl<T: PyObjectPayload> ToPyObject for PyRef<T> {
511501
#[inline(always)]
512502
fn to_pyobject(self, _vm: &VirtualMachine) -> PyObjectRef {

vm/src/stdlib/os.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ pub(super) mod _os {
436436
},
437437
convert::{ToPyException, ToPyObject},
438438
crt_fd::{Fd, Offset},
439-
function::{ArgBytesLike, FuncArgs, IntoPyRef, OptionalArg},
439+
function::{ArgBytesLike, FuncArgs, OptionalArg},
440440
protocol::PyIterReturn,
441441
suppress_iph,
442442
types::{IterNext, IterNextIterable},
@@ -1033,13 +1033,13 @@ pub(super) mod _os {
10331033
let to_f64 = |(s, ns)| (s as f64) + (ns as f64) / (NANOS_PER_SEC as f64);
10341034
let to_ns = |(s, ns)| s as i128 * NANOS_PER_SEC as i128 + ns as i128;
10351035
StatResult {
1036-
st_mode: stat.st_mode.into_pyref(vm),
1037-
st_ino: stat.st_ino.into_pyref(vm),
1038-
st_dev: stat.st_dev.into_pyref(vm),
1039-
st_nlink: stat.st_nlink.into_pyref(vm),
1040-
st_uid: stat.st_uid.into_pyref(vm),
1041-
st_gid: stat.st_gid.into_pyref(vm),
1042-
st_size: stat.st_size.into_pyref(vm),
1036+
st_mode: vm.new_pyref(stat.st_mode),
1037+
st_ino: vm.new_pyref(stat.st_ino),
1038+
st_dev: vm.new_pyref(stat.st_dev),
1039+
st_nlink: vm.new_pyref(stat.st_nlink),
1040+
st_uid: vm.new_pyref(stat.st_uid),
1041+
st_gid: vm.new_pyref(stat.st_gid),
1042+
st_size: vm.new_pyref(stat.st_size),
10431043
__st_atime_int: atime.0,
10441044
__st_mtime_int: mtime.0,
10451045
__st_ctime_int: ctime.0,

vm/src/vm_new.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{
1010
convert::ToPyObject,
1111
scope::Scope,
1212
vm::VirtualMachine,
13-
AsPyObject, PyObject, PyObjectRef, PyObjectWrap, PyRef,
13+
AsPyObject, PyObject, PyObjectRef, PyObjectWrap, PyRef, PyValue,
1414
};
1515

1616
/// Collection of object creation helpers
@@ -20,6 +20,14 @@ impl VirtualMachine {
2020
value.to_pyobject(self)
2121
}
2222

23+
pub fn new_pyref<T, P>(&self, value: T) -> PyRef<P>
24+
where
25+
T: Into<P>,
26+
P: PyValue,
27+
{
28+
value.into().into_ref(self)
29+
}
30+
2331
pub fn new_tuple(&self, value: impl IntoPyTuple) -> PyTupleRef {
2432
value.into_pytuple(self)
2533
}

0 commit comments

Comments
 (0)