Skip to content

Commit eea1a1f

Browse files
committed
Deprecate ::new_ref
1 parent 57029f6 commit eea1a1f

File tree

9 files changed

+38
-44
lines changed

9 files changed

+38
-44
lines changed

vm/src/builtins/bytearray.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,9 @@ pub(crate) fn init(context: &Context) {
7373
}
7474

7575
impl PyByteArray {
76+
#[deprecated(note = "use PyByteArray::from(...).into_ref() instead")]
7677
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+
Self::from(data).into_ref(ctx)
7879
}
7980

8081
const fn from_inner(inner: PyBytesInner) -> Self {

vm/src/builtins/bytes.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,9 @@ impl Constructor for PyBytes {
9999
}
100100

101101
impl PyBytes {
102+
#[deprecated(note = "use PyBytes::from(...).into_ref() instead")]
102103
pub fn new_ref(data: Vec<u8>, ctx: &Context) -> PyRef<Self> {
103-
PyRef::new_ref(Self::from(data), ctx.types.bytes_type.to_owned(), None)
104+
Self::from(data).into_ref(ctx)
104105
}
105106

106107
fn _getitem(&self, needle: &PyObject, vm: &VirtualMachine) -> PyResult {

vm/src/builtins/classmethod.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,9 @@ impl Initializer for PyClassMethod {
111111
}
112112

113113
impl PyClassMethod {
114+
#[deprecated(note = "use PyClassMethod::from(...).into_ref() instead")]
114115
pub fn new_ref(callable: PyObjectRef, ctx: &Context) -> PyRef<Self> {
115-
PyRef::new_ref(
116-
Self {
117-
callable: PyMutex::new(callable),
118-
},
119-
ctx.types.classmethod_type.to_owned(),
120-
None,
121-
)
116+
Self::from(callable).into_ref(ctx)
122117
}
123118
}
124119

vm/src/builtins/complex.rs

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,6 @@ pub struct PyComplex {
2929
value: Complex64,
3030
}
3131

32-
impl PyComplex {
33-
pub const fn to_complex64(self) -> Complex64 {
34-
self.value
35-
}
36-
}
37-
3832
impl PyPayload for PyComplex {
3933
#[inline]
4034
fn class(ctx: &Context) -> &'static Py<PyType> {
@@ -234,13 +228,30 @@ impl Constructor for PyComplex {
234228
}
235229

236230
impl PyComplex {
231+
#[deprecated(note = "use PyComplex::from(...).into_ref() instead")]
237232
pub fn new_ref(value: Complex64, ctx: &Context) -> PyRef<Self> {
238-
PyRef::new_ref(Self::from(value), ctx.types.complex_type.to_owned(), None)
233+
Self::from(value).into_ref(ctx)
234+
}
235+
236+
pub const fn to_complex64(self) -> Complex64 {
237+
self.value
239238
}
240239

241240
pub const fn to_complex(&self) -> Complex64 {
242241
self.value
243242
}
243+
244+
fn number_op<F, R>(a: &PyObject, b: &PyObject, op: F, vm: &VirtualMachine) -> PyResult
245+
where
246+
F: FnOnce(Complex64, Complex64, &VirtualMachine) -> R,
247+
R: ToPyResult,
248+
{
249+
if let (Some(a), Some(b)) = (to_op_complex(a, vm)?, to_op_complex(b, vm)?) {
250+
op(a, b, vm).to_pyresult(vm)
251+
} else {
252+
Ok(vm.ctx.not_implemented())
253+
}
254+
}
244255
}
245256

246257
#[pyclass(
@@ -503,20 +514,6 @@ impl Representable for PyComplex {
503514
}
504515
}
505516

506-
impl PyComplex {
507-
fn number_op<F, R>(a: &PyObject, b: &PyObject, op: F, vm: &VirtualMachine) -> PyResult
508-
where
509-
F: FnOnce(Complex64, Complex64, &VirtualMachine) -> R,
510-
R: ToPyResult,
511-
{
512-
if let (Some(a), Some(b)) = (to_op_complex(a, vm)?, to_op_complex(b, vm)?) {
513-
op(a, b, vm).to_pyresult(vm)
514-
} else {
515-
Ok(vm.ctx.not_implemented())
516-
}
517-
}
518-
}
519-
520517
#[derive(FromArgs)]
521518
pub struct ComplexArgs {
522519
#[pyarg(any, optional)]

vm/src/builtins/dict.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ impl PyPayload for PyDict {
5151
}
5252

5353
impl PyDict {
54+
#[deprecated(note = "use PyDict::default().into_ref() instead")]
5455
pub fn new_ref(ctx: &Context) -> PyRef<Self> {
55-
PyRef::new_ref(Self::default(), ctx.types.dict_type.to_owned(), None)
56+
Self::default().into_ref(ctx)
5657
}
5758

5859
/// escape hatch to access the underlying data structure directly. prefer adding a method on

vm/src/builtins/function.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -773,12 +773,9 @@ impl PyBoundMethod {
773773
Self { object, function }
774774
}
775775

776+
#[deprecated(note = "Use `Self::new(object, function).into_ref(ctx)` instead")]
776777
pub fn new_ref(object: PyObjectRef, function: PyObjectRef, ctx: &Context) -> PyRef<Self> {
777-
PyRef::new_ref(
778-
Self::new(object, function),
779-
ctx.types.bound_method_type.to_owned(),
780-
None,
781-
)
778+
Self::new(object, function).into_ref(ctx)
782779
}
783780
}
784781

vm/src/builtins/list.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ impl ToPyObject for Vec<PyObjectRef> {
6363
}
6464

6565
impl PyList {
66+
#[deprecated(note = "use PyList::from(...).into_ref() instead")]
6667
pub fn new_ref(elements: Vec<PyObjectRef>, ctx: &Context) -> PyRef<Self> {
6768
PyRef::new_ref(Self::from(elements), ctx.types.list_type.to_owned(), None)
6869
}

vm/src/builtins/staticmethod.rs

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

6363
impl PyStaticMethod {
64+
pub fn new(callable: PyObjectRef) -> Self {
65+
Self {
66+
callable: PyMutex::new(callable),
67+
}
68+
}
69+
#[deprecated(note = "use PyStaticMethod::new(...).into_ref() instead")]
6470
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-
)
71+
Self::new(callable).into_ref(ctx)
7272
}
7373
}
7474

vm/src/builtins/str.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,9 +418,10 @@ impl PyStr {
418418
unsafe { AsciiString::from_ascii_unchecked(bytes) }.into()
419419
}
420420

421+
#[deprecated(note = "use PyStr::from(...).into_ref() instead")]
421422
pub fn new_ref(zelf: impl Into<Self>, ctx: &Context) -> PyRef<Self> {
422423
let zelf = zelf.into();
423-
PyRef::new_ref(zelf, ctx.types.str_type.to_owned(), None)
424+
zelf.into_ref(ctx)
424425
}
425426

426427
fn new_substr(&self, s: Wtf8Buf) -> Self {

0 commit comments

Comments
 (0)