Skip to content

Commit dedaab3

Browse files
committed
PyPayload::into_ref
1 parent b9b1c85 commit dedaab3

File tree

14 files changed

+63
-102
lines changed

14 files changed

+63
-102
lines changed

‎examples/call_between_rust_and_python.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub fn main() {
3131
#[pymodule]
3232
mod rust_py_module {
3333
use super::*;
34-
use rustpython::vm::{PyObjectRef, builtins::PyList, convert::ToPyObject};
34+
use rustpython::vm::{PyObjectRef, convert::ToPyObject};
3535

3636
#[pyfunction]
3737
fn rust_function(
@@ -58,7 +58,7 @@ python_person.name: {}",
5858
impl ToPyObject for NumVec {
5959
fn to_pyobject(self, vm: &VirtualMachine) -> PyObjectRef {
6060
let list = self.0.into_iter().map(|e| vm.new_pyobj(e)).collect();
61-
PyList::new_ref(list, vm.as_ref()).to_pyobject(vm)
61+
vm.ctx.new_list(list).to_pyobject(vm)
6262
}
6363
}
6464

‎vm/src/builtins/bytearray.rs

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,6 @@ pub(crate) fn init(context: &Context) {
7373
}
7474

7575
impl PyByteArray {
76-
pub fn new_ref(data: Vec<u8>, ctx: &Context) -> PyRef<Self> {
77-
PyRef::new_ref(Self::from(data), ctx.types.bytearray_type.to_owned(), None)
78-
}
79-
8076
fn from_inner(inner: PyBytesInner) -> Self {
8177
PyByteArray {
8278
inner: PyRwLock::new(inner),
@@ -134,7 +130,7 @@ impl PyByteArray {
134130
SequenceIndex::Slice(slice) => self
135131
.borrow_buf()
136132
.getitem_by_slice(vm, slice)
137-
.map(|x| Self::new_ref(x, &vm.ctx).into()),
133+
.map(|x| vm.ctx.new_bytearray(x).into()),
138134
}
139135
}
140136

@@ -463,11 +459,8 @@ impl PyByteArray {
463459
options: ByteInnerSplitOptions,
464460
vm: &VirtualMachine,
465461
) -> PyResult<Vec<PyObjectRef>> {
466-
self.inner().split(
467-
options,
468-
|s, vm| Self::new_ref(s.to_vec(), &vm.ctx).into(),
469-
vm,
470-
)
462+
self.inner()
463+
.split(options, |s, vm| vm.ctx.new_bytearray(s.to_vec()).into(), vm)
471464
}
472465

473466
#[pymethod]
@@ -476,11 +469,8 @@ impl PyByteArray {
476469
options: ByteInnerSplitOptions,
477470
vm: &VirtualMachine,
478471
) -> PyResult<Vec<PyObjectRef>> {
479-
self.inner().rsplit(
480-
options,
481-
|s, vm| Self::new_ref(s.to_vec(), &vm.ctx).into(),
482-
vm,
483-
)
472+
self.inner()
473+
.rsplit(options, |s, vm| vm.ctx.new_bytearray(s.to_vec()).into(), vm)
484474
}
485475

486476
#[pymethod]
@@ -490,9 +480,10 @@ impl PyByteArray {
490480
let value = self.inner();
491481
let (front, has_mid, back) = value.partition(&sep, vm)?;
492482
Ok(vm.new_tuple((
493-
Self::new_ref(front.to_vec(), &vm.ctx),
494-
Self::new_ref(if has_mid { sep.elements } else { Vec::new() }, &vm.ctx),
495-
Self::new_ref(back.to_vec(), &vm.ctx),
483+
vm.ctx.new_bytearray(front.to_vec()),
484+
vm.ctx
485+
.new_bytearray(if has_mid { sep.elements } else { Vec::new() }),
486+
vm.ctx.new_bytearray(back.to_vec()),
496487
)))
497488
}
498489

@@ -501,9 +492,10 @@ impl PyByteArray {
501492
let value = self.inner();
502493
let (back, has_mid, front) = value.rpartition(&sep, vm)?;
503494
Ok(vm.new_tuple((
504-
Self::new_ref(front.to_vec(), &vm.ctx),
505-
Self::new_ref(if has_mid { sep.elements } else { Vec::new() }, &vm.ctx),
506-
Self::new_ref(back.to_vec(), &vm.ctx),
495+
vm.ctx.new_bytearray(front.to_vec()),
496+
vm.ctx
497+
.new_bytearray(if has_mid { sep.elements } else { Vec::new() }),
498+
vm.ctx.new_bytearray(back.to_vec()),
507499
)))
508500
}
509501

@@ -515,7 +507,7 @@ impl PyByteArray {
515507
#[pymethod]
516508
fn splitlines(&self, options: anystr::SplitLinesArgs, vm: &VirtualMachine) -> Vec<PyObjectRef> {
517509
self.inner()
518-
.splitlines(options, |x| Self::new_ref(x.to_vec(), &vm.ctx).into())
510+
.splitlines(options, |x| vm.ctx.new_bytearray(x.to_vec()).into())
519511
}
520512

521513
#[pymethod]
@@ -873,10 +865,6 @@ impl Representable for PyByteArray {
873865
}
874866
}
875867

876-
// fn set_value(obj: &PyObject, value: Vec<u8>) {
877-
// obj.borrow_mut().kind = PyObjectPayload::Bytes { value };
878-
// }
879-
880868
#[pyclass(module = false, name = "bytearray_iterator")]
881869
#[derive(Debug)]
882870
pub struct PyByteArrayIterator {

‎vm/src/builtins/bytes.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,6 @@ impl Constructor for PyBytes {
9898
}
9999

100100
impl PyBytes {
101-
pub fn new_ref(data: Vec<u8>, ctx: &Context) -> PyRef<Self> {
102-
PyRef::new_ref(Self::from(data), ctx.types.bytes_type.to_owned(), None)
103-
}
104-
105101
fn _getitem(&self, needle: &PyObject, vm: &VirtualMachine) -> PyResult {
106102
match SequenceIndex::try_from_borrowed_object(vm, needle, "byte")? {
107103
SequenceIndex::Int(i) => self

‎vm/src/builtins/classmethod.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ impl GetDescriptor for PyClassMethod {
5858
let cls = cls.unwrap_or_else(|| _obj.class().to_owned().into());
5959
let call_descr_get: PyResult<PyObjectRef> = zelf.callable.lock().get_attr("__get__", vm);
6060
match call_descr_get {
61-
Err(_) => Ok(PyBoundMethod::new_ref(cls, zelf.callable.lock().clone(), &vm.ctx).into()),
61+
Err(_) => Ok(PyBoundMethod::new(cls, zelf.callable.lock().clone())
62+
.into_ref(&vm.ctx)
63+
.into()),
6264
Ok(call_descr_get) => call_descr_get.call((cls.clone(), cls), vm),
6365
}
6466
}
@@ -108,18 +110,6 @@ impl Initializer for PyClassMethod {
108110
}
109111
}
110112

111-
impl PyClassMethod {
112-
pub fn new_ref(callable: PyObjectRef, ctx: &Context) -> PyRef<Self> {
113-
PyRef::new_ref(
114-
Self {
115-
callable: PyMutex::new(callable),
116-
},
117-
ctx.types.classmethod_type.to_owned(),
118-
None,
119-
)
120-
}
121-
}
122-
123113
#[pyclass(
124114
with(GetDescriptor, Constructor, Representable),
125115
flags(BASETYPE, HAS_DICT)

‎vm/src/builtins/complex.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl PyPayload for PyComplex {
4242

4343
impl ToPyObject for Complex64 {
4444
fn to_pyobject(self, vm: &VirtualMachine) -> PyObjectRef {
45-
PyComplex::new_ref(self, &vm.ctx).into()
45+
PyComplex::from(self).to_pyobject(vm)
4646
}
4747
}
4848

@@ -232,10 +232,6 @@ impl Constructor for PyComplex {
232232
}
233233

234234
impl PyComplex {
235-
pub fn new_ref(value: Complex64, ctx: &Context) -> PyRef<Self> {
236-
PyRef::new_ref(Self::from(value), ctx.types.complex_type.to_owned(), None)
237-
}
238-
239235
pub fn to_complex(&self) -> Complex64 {
240236
self.value
241237
}

‎vm/src/builtins/dict.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,13 @@ impl fmt::Debug for PyDict {
4444
}
4545

4646
impl PyPayload for PyDict {
47+
#[inline]
4748
fn class(ctx: &Context) -> &'static Py<PyType> {
4849
ctx.types.dict_type
4950
}
5051
}
5152

5253
impl PyDict {
53-
pub fn new_ref(ctx: &Context) -> PyRef<Self> {
54-
PyRef::new_ref(Self::default(), ctx.types.dict_type.to_owned(), None)
55-
}
56-
5754
/// escape hatch to access the underlying data structure directly. prefer adding a method on
5855
/// PyDict instead of using this
5956
pub(crate) fn _as_dict_inner(&self) -> &DictContentType {

‎vm/src/builtins/function.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ impl GetDescriptor for PyFunction {
551551
let obj = if vm.is_none(&obj) && !Self::_cls_is(&cls, obj.class()) {
552552
zelf
553553
} else {
554-
PyBoundMethod::new_ref(obj, zelf, &vm.ctx).into()
554+
PyBoundMethod::new(obj, zelf).into_ref(&vm.ctx).into()
555555
};
556556
Ok(obj)
557557
}
@@ -719,17 +719,9 @@ impl Constructor for PyBoundMethod {
719719
}
720720

721721
impl PyBoundMethod {
722-
fn new(object: PyObjectRef, function: PyObjectRef) -> Self {
722+
pub fn new(object: PyObjectRef, function: PyObjectRef) -> Self {
723723
PyBoundMethod { object, function }
724724
}
725-
726-
pub fn new_ref(object: PyObjectRef, function: PyObjectRef, ctx: &Context) -> PyRef<Self> {
727-
PyRef::new_ref(
728-
Self::new(object, function),
729-
ctx.types.bound_method_type.to_owned(),
730-
None,
731-
)
732-
}
733725
}
734726

735727
#[pyclass(

‎vm/src/builtins/list.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,11 @@ impl PyPayload for PyList {
5858

5959
impl ToPyObject for Vec<PyObjectRef> {
6060
fn to_pyobject(self, vm: &VirtualMachine) -> PyObjectRef {
61-
PyList::new_ref(self, &vm.ctx).into()
61+
PyList::from(self).into_ref(&vm.ctx).into()
6262
}
6363
}
6464

6565
impl PyList {
66-
pub fn new_ref(elements: Vec<PyObjectRef>, ctx: &Context) -> PyRef<Self> {
67-
PyRef::new_ref(Self::from(elements), ctx.types.list_type.to_owned(), None)
68-
}
69-
7066
pub fn borrow_vec(&self) -> PyMappedRwLockReadGuard<'_, [PyObjectRef]> {
7167
PyRwLockReadGuard::map(self.elements.read(), |v| &**v)
7268
}
@@ -78,7 +74,7 @@ impl PyList {
7874
fn repeat(&self, n: isize, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
7975
let elements = &*self.borrow_vec();
8076
let v = elements.mul(vm, n)?;
81-
Ok(Self::new_ref(v, &vm.ctx))
77+
Ok(Self::from(v).into_ref(&vm.ctx))
8278
}
8379

8480
fn irepeat(zelf: PyRef<Self>, n: isize, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
@@ -140,7 +136,7 @@ impl PyList {
140136
})?;
141137
let mut elements = self.borrow_vec().to_vec();
142138
elements.extend(other.borrow_vec().iter().cloned());
143-
Ok(Self::new_ref(elements, &vm.ctx))
139+
Ok(Self::from(elements).into_ref(&vm.ctx))
144140
}
145141

146142
#[pymethod]
@@ -176,7 +172,7 @@ impl PyList {
176172

177173
#[pymethod]
178174
fn copy(&self, vm: &VirtualMachine) -> PyRef<Self> {
179-
Self::new_ref(self.borrow_vec().to_vec(), &vm.ctx)
175+
Self::from(self.borrow_vec().to_vec()).into_ref(&vm.ctx)
180176
}
181177

182178
#[allow(clippy::len_without_is_empty)]

‎vm/src/builtins/staticmethod.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,10 @@ impl Constructor for PyStaticMethod {
6161
}
6262

6363
impl PyStaticMethod {
64-
pub fn new_ref(callable: PyObjectRef, ctx: &Context) -> PyRef<Self> {
65-
PyRef::new_ref(
66-
Self {
67-
callable: PyMutex::new(callable),
68-
},
69-
ctx.types.staticmethod_type.to_owned(),
70-
None,
71-
)
64+
pub fn new(callable: PyObjectRef) -> Self {
65+
Self {
66+
callable: PyMutex::new(callable),
67+
}
7268
}
7369
}
7470

‎vm/src/builtins/str.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -392,11 +392,6 @@ impl PyStr {
392392
unsafe { AsciiString::from_ascii_unchecked(bytes) }.into()
393393
}
394394

395-
pub fn new_ref(zelf: impl Into<Self>, ctx: &Context) -> PyRef<Self> {
396-
let zelf = zelf.into();
397-
PyRef::new_ref(zelf, ctx.types.str_type.to_owned(), None)
398-
}
399-
400395
fn new_substr(&self, s: Wtf8Buf) -> Self {
401396
let kind = if self.kind().is_ascii() || s.is_ascii() {
402397
StrKind::Ascii

‎vm/src/bytes_inner.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ impl ByteInnerNewOptions {
9696
(OptionalArg::Present(obj), OptionalArg::Missing, OptionalArg::Missing) => {
9797
let obj = obj.clone();
9898
// construct an exact bytes from an exact bytes do not clone
99-
let obj = if cls.is(PyBytes::class(&vm.ctx)) {
99+
let obj = if cls.is(vm.ctx.types.bytes_type) {
100100
match obj.downcast_exact::<PyBytes>(vm) {
101101
Ok(b) => return Ok(b.into_pyref()),
102102
Err(obj) => obj,
@@ -109,7 +109,7 @@ impl ByteInnerNewOptions {
109109
// construct an exact bytes from __bytes__ slot.
110110
// if __bytes__ return a bytes, use the bytes object except we are the subclass of the bytes
111111
let bytes = bytes_method?.call((), vm)?;
112-
let bytes = if cls.is(PyBytes::class(&vm.ctx)) {
112+
let bytes = if cls.is(vm.ctx.types.bytes_type) {
113113
match bytes.downcast::<PyBytes>() {
114114
Ok(b) => return Ok(b),
115115
Err(bytes) => bytes,

‎vm/src/class.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,23 @@ use rustpython_common::static_cell;
1313
pub trait StaticType {
1414
// Ideally, saving PyType is better than PyTypeRef
1515
fn static_cell() -> &'static static_cell::StaticCell<PyTypeRef>;
16+
#[inline]
1617
fn static_metaclass() -> &'static Py<PyType> {
1718
PyType::static_type()
1819
}
20+
#[inline]
1921
fn static_baseclass() -> &'static Py<PyType> {
2022
PyBaseObject::static_type()
2123
}
24+
#[inline]
2225
fn static_type() -> &'static Py<PyType> {
23-
Self::static_cell()
24-
.get()
25-
.expect("static type has not been initialized. e.g. the native types defined in different module may be used before importing library.")
26+
#[cold]
27+
fn fail() -> ! {
28+
panic!(
29+
"static type has not been initialized. e.g. the native types defined in different module may be used before importing library."
30+
);
31+
}
32+
Self::static_cell().get().unwrap_or_else(|| fail())
2633
}
2734
fn init_manually(typ: PyTypeRef) -> &'static Py<PyType> {
2835
let cell = Self::static_cell();

‎vm/src/vm/context.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use crate::{
22
PyResult, VirtualMachine,
33
builtins::{
4-
PyBaseException, PyBytes, PyComplex, PyDict, PyDictRef, PyEllipsis, PyFloat, PyFrozenSet,
5-
PyInt, PyIntRef, PyList, PyListRef, PyNone, PyNotImplemented, PyStr, PyStrInterned,
6-
PyTuple, PyTupleRef, PyType, PyTypeRef, bytes,
4+
PyBaseException, PyByteArray, PyBytes, PyComplex, PyDict, PyDictRef, PyEllipsis, PyFloat,
5+
PyFrozenSet, PyInt, PyIntRef, PyList, PyListRef, PyNone, PyNotImplemented, PyStr,
6+
PyStrInterned, PyTuple, PyTupleRef, PyType, PyTypeRef,
77
code::{self, PyCode},
88
descriptor::{
99
MemberGetter, MemberKind, MemberSetter, MemberSetterFunc, PyDescriptorOwned,
@@ -414,7 +414,7 @@ impl Context {
414414

415415
#[inline]
416416
pub fn new_str(&self, s: impl Into<pystr::PyStr>) -> PyRef<PyStr> {
417-
pystr::PyStr::new_ref(s, self)
417+
s.into().into_ref(self)
418418
}
419419

420420
pub fn interned_or_new_str<S, M>(&self, s: S) -> PyRef<PyStr>
@@ -429,8 +429,17 @@ impl Context {
429429
}
430430

431431
#[inline]
432-
pub fn new_bytes(&self, data: Vec<u8>) -> PyRef<bytes::PyBytes> {
433-
bytes::PyBytes::new_ref(data, self)
432+
pub fn new_bytes(&self, data: Vec<u8>) -> PyRef<PyBytes> {
433+
PyBytes::from(data).into_ref(self)
434+
}
435+
436+
#[inline]
437+
pub fn new_bytearray(&self, data: Vec<u8>) -> PyRef<PyByteArray> {
438+
PyRef::new_ref(
439+
PyByteArray::from(data),
440+
self.types.bytearray_type.to_owned(),
441+
None,
442+
)
434443
}
435444

436445
#[inline(always)]
@@ -450,12 +459,12 @@ impl Context {
450459

451460
#[inline(always)]
452461
pub fn new_list(&self, elements: Vec<PyObjectRef>) -> PyListRef {
453-
PyList::new_ref(elements, self)
462+
PyList::from(elements).into_ref(self)
454463
}
455464

456465
#[inline(always)]
457466
pub fn new_dict(&self) -> PyDictRef {
458-
PyDict::new_ref(self)
467+
PyDict::default().into_ref(self)
459468
}
460469

461470
pub fn new_class(

0 commit comments

Comments
 (0)