Skip to content

Commit c52f592

Browse files
committed
PyPayload::{new_ref, into_ref}
1 parent 55f5bf3 commit c52f592

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+437
-400
lines changed

derive/src/pyclass.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,8 +367,8 @@ pub(crate) fn impl_define_exception(exc_def: PyExceptionDef) -> Result<TokenStre
367367

368368
// We need this to make extend mechanism work:
369369
impl ::rustpython_vm::PyPayload for #class_name {
370-
fn class(vm: &::rustpython_vm::VirtualMachine) -> &'static ::rustpython_vm::Py<::rustpython_vm::builtins::PyType> {
371-
vm.ctx.exceptions.#ctx_name
370+
fn class(ctx: &::rustpython_vm::vm::Context) -> &'static ::rustpython_vm::Py<::rustpython_vm::builtins::PyType> {
371+
ctx.exceptions.#ctx_name
372372
}
373373
}
374374

derive/src/pymodule.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -332,11 +332,12 @@ impl ModuleItem for FunctionItem {
332332
};
333333
let doc = quote!(.with_doc(#doc.to_owned(), &vm.ctx));
334334
let new_func = quote_spanned!(ident.span()=>
335-
vm.ctx.make_funcdef(#py_name, #ident)
336-
#doc
337-
.into_function()
338-
.with_module(vm.new_pyobj(#module.to_owned()))
339-
.into_ref(&vm.ctx)
335+
::rustpython_vm::object::PyPayload::into_ref(
336+
vm.ctx.make_funcdef(#py_name, #ident)
337+
#doc
338+
.into_function()
339+
.with_module(vm.new_pyobj(#module.to_owned())),
340+
&vm.ctx)
340341
);
341342

342343
if self.pyattrs.is_empty() {

derive/src/pypayload.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ pub(crate) fn impl_pypayload(input: DeriveInput) -> Result<TokenStream> {
77

88
let ret = quote! {
99
impl ::rustpython_vm::PyPayload for #ty {
10-
fn class(_vm: &::rustpython_vm::VirtualMachine) -> &'static rustpython_vm::Py<::rustpython_vm::builtins::PyType> {
10+
#[inline]
11+
fn class(_ctx: &::rustpython_vm::vm::Context) -> &'static rustpython_vm::Py<::rustpython_vm::builtins::PyType> {
1112
<Self as ::rustpython_vm::class::StaticType>::static_type()
1213
}
1314
}

stdlib/src/array.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -897,7 +897,7 @@ mod array {
897897
let bytes = bytes.get_bytes();
898898

899899
for b in bytes.chunks(BLOCKSIZE) {
900-
let b = PyBytes::from(b.to_vec()).into_ref(vm);
900+
let b = PyBytes::from(b.to_vec()).into_ref(&vm.ctx);
901901
vm.call_method(&f, "write", (b,))?;
902902
}
903903
Ok(())
@@ -1017,7 +1017,7 @@ mod array {
10171017
if let Some(other) = other.payload::<PyArray>() {
10181018
self.read()
10191019
.add(&*other.read(), vm)
1020-
.map(|array| PyArray::from(array).into_ref(vm))
1020+
.map(|array| PyArray::from(array).into_ref(&vm.ctx))
10211021
} else {
10221022
Err(vm.new_type_error(format!(
10231023
"can only append array (not \"{}\") to array",
@@ -1050,7 +1050,7 @@ mod array {
10501050
fn mul(&self, value: isize, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
10511051
self.read()
10521052
.mul(value, vm)
1053-
.map(|x| Self::from(x).into_ref(vm))
1053+
.map(|x| Self::from(x).into_ref(&vm.ctx))
10541054
}
10551055

10561056
#[pymethod(magic)]
@@ -1444,7 +1444,7 @@ mod array {
14441444
}
14451445

14461446
fn check_array_type(typ: PyTypeRef, vm: &VirtualMachine) -> PyResult<PyTypeRef> {
1447-
if !typ.fast_issubclass(PyArray::class(vm)) {
1447+
if !typ.fast_issubclass(PyArray::class(&vm.ctx)) {
14481448
return Err(
14491449
vm.new_type_error(format!("{} is not a subtype of array.array", typ.name()))
14501450
);

stdlib/src/pyexpat.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ mod _pyexpat {
7272
entity_decl: MutableObject::new(vm.ctx.none()),
7373
buffer_text: MutableObject::new(vm.ctx.new_bool(false).into()),
7474
}
75-
.into_ref(vm))
75+
.into_ref(&vm.ctx))
7676
}
7777

7878
#[extend_class]
@@ -118,15 +118,15 @@ mod _pyexpat {
118118
.unwrap();
119119
}
120120

121-
let name_str = PyStr::from(name.local_name).into_ref(vm);
121+
let name_str = PyStr::from(name.local_name).into_ref(&vm.ctx);
122122
invoke_handler(vm, &self.start_element, (name_str, dict));
123123
}
124124
Ok(XmlEvent::EndElement { name, .. }) => {
125-
let name_str = PyStr::from(name.local_name).into_ref(vm);
125+
let name_str = PyStr::from(name.local_name).into_ref(&vm.ctx);
126126
invoke_handler(vm, &self.end_element, (name_str,));
127127
}
128128
Ok(XmlEvent::Characters(chars)) => {
129-
let str = PyStr::from(chars).into_ref(vm);
129+
let str = PyStr::from(chars).into_ref(&vm.ctx);
130130
invoke_handler(vm, &self.character_data, (str,));
131131
}
132132
_ => {}

stdlib/src/pystruct.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub(crate) mod _struct {
3636
b @ PyBytes => if b.is_ascii() {
3737
Some(unsafe {
3838
PyStr::new_ascii_unchecked(b.as_bytes().to_vec())
39-
}.into_ref(vm))
39+
}.into_ref(&vm.ctx))
4040
} else {
4141
None
4242
},

stdlib/src/ssl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1438,7 +1438,7 @@ mod windows {
14381438
oids.into_iter().map(|oid| vm.ctx.new_str(oid).into()),
14391439
)
14401440
.unwrap()
1441-
.into_ref(vm)
1441+
.into_ref(&vm.ctx)
14421442
.into(),
14431443
};
14441444
Ok(vm.new_tuple((cert, enc_type, usage)).into())

stdlib/src/syslog.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ mod syslog {
3131
Some(value) => &argv[value..],
3232
None => argv,
3333
})
34-
.into_ref(vm),
34+
.into_ref(&vm.ctx),
3535
);
3636
}
3737
}

stdlib/src/unicodedata.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
77
let module = unicodedata::make_module(vm);
88

99
let ucd: PyObjectRef = unicodedata::Ucd::new(unic_ucd_age::UNICODE_VERSION)
10-
.into_ref(vm)
10+
.into_ref(&vm.ctx)
1111
.into();
1212

1313
for attr in ["category", "lookup", "name", "bidirectional", "normalize"]
@@ -147,7 +147,7 @@ mod unicodedata {
147147
micro: 0,
148148
},
149149
}
150-
.into_ref(vm)
150+
.into_ref(&vm.ctx)
151151
}
152152

153153
#[pyattr]

stdlib/src/zlib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,8 @@ mod zlib {
275275
Ok(PyDecompress {
276276
decompress: PyMutex::new(decompress),
277277
eof: AtomicCell::new(false),
278-
unused_data: PyMutex::new(PyBytes::from(vec![]).into_ref(vm)),
279-
unconsumed_tail: PyMutex::new(PyBytes::from(vec![]).into_ref(vm)),
278+
unused_data: PyMutex::new(PyBytes::from(vec![]).into_ref(&vm.ctx)),
279+
unconsumed_tail: PyMutex::new(PyBytes::from(vec![]).into_ref(&vm.ctx)),
280280
})
281281
}
282282
#[pyattr]
@@ -357,7 +357,7 @@ mod zlib {
357357

358358
let mut unconsumed_tail = self.unconsumed_tail.lock();
359359
if !leftover.is_empty() || !unconsumed_tail.is_empty() {
360-
*unconsumed_tail = PyBytes::from(leftover.to_owned()).into_ref(vm);
360+
*unconsumed_tail = PyBytes::from(leftover.to_owned()).into_ref(&vm.ctx);
361361
}
362362

363363
ret
@@ -384,7 +384,7 @@ mod zlib {
384384
};
385385
self.save_unused_input(&mut d, &data, stream_end, orig_in, vm);
386386

387-
*data = PyBytes::from(Vec::new()).into_ref(vm);
387+
*data = PyBytes::from(Vec::new()).into_ref(&vm.ctx);
388388

389389
// TODO: drop the inner decompressor, somehow
390390
// if stream_end {

vm/src/builtins/asyncgenerator.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ pub struct PyAsyncGen {
2121
type PyAsyncGenRef = PyRef<PyAsyncGen>;
2222

2323
impl PyPayload for PyAsyncGen {
24-
fn class(vm: &VirtualMachine) -> &'static Py<PyType> {
25-
vm.ctx.types.async_generator
24+
#[inline]
25+
fn class(ctx: &Context) -> &'static Py<PyType> {
26+
ctx.types.async_generator
2627
}
2728
}
2829

@@ -135,8 +136,9 @@ impl Unconstructible for PyAsyncGen {}
135136
#[derive(Debug)]
136137
pub(crate) struct PyAsyncGenWrappedValue(pub PyObjectRef);
137138
impl PyPayload for PyAsyncGenWrappedValue {
138-
fn class(vm: &VirtualMachine) -> &'static Py<PyType> {
139-
vm.ctx.types.async_generator_wrapped_value
139+
#[inline]
140+
fn class(ctx: &Context) -> &'static Py<PyType> {
141+
ctx.types.async_generator_wrapped_value
140142
}
141143
}
142144

@@ -184,8 +186,9 @@ pub(crate) struct PyAsyncGenASend {
184186
}
185187

186188
impl PyPayload for PyAsyncGenASend {
187-
fn class(vm: &VirtualMachine) -> &'static Py<PyType> {
188-
vm.ctx.types.async_generator_asend
189+
#[inline]
190+
fn class(ctx: &Context) -> &'static Py<PyType> {
191+
ctx.types.async_generator_asend
189192
}
190193
}
191194

@@ -279,8 +282,9 @@ pub(crate) struct PyAsyncGenAThrow {
279282
}
280283

281284
impl PyPayload for PyAsyncGenAThrow {
282-
fn class(vm: &VirtualMachine) -> &'static Py<PyType> {
283-
vm.ctx.types.async_generator_athrow
285+
#[inline]
286+
fn class(ctx: &Context) -> &'static Py<PyType> {
287+
ctx.types.async_generator_athrow
284288
}
285289
}
286290

vm/src/builtins/bool.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,9 @@ impl PyObjectRef {
7979
pub struct PyBool;
8080

8181
impl PyPayload for PyBool {
82-
fn class(vm: &VirtualMachine) -> &'static Py<PyType> {
83-
vm.ctx.types.bool_type
82+
#[inline]
83+
fn class(ctx: &Context) -> &'static Py<PyType> {
84+
ctx.types.bool_type
8485
}
8586
}
8687

vm/src/builtins/builtinfunc.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ impl PyNativeFuncDef {
4747
class: &'static Py<PyType>,
4848
) -> PyRef<PyClassMethod> {
4949
// TODO: classmethod_descriptor
50-
let callable = self.build_method(ctx, class).into();
50+
let callable: PyObjectRef = self.build_method(ctx, class).into();
5151
PyClassMethod::new_ref(callable, ctx)
5252
}
5353
pub fn build_staticmethod(
@@ -71,8 +71,9 @@ pub struct PyBuiltinFunction {
7171
}
7272

7373
impl PyPayload for PyBuiltinFunction {
74-
fn class(vm: &VirtualMachine) -> &'static Py<PyType> {
75-
vm.ctx.types.builtin_function_or_method_type
74+
#[inline]
75+
fn class(ctx: &Context) -> &'static Py<PyType> {
76+
ctx.types.builtin_function_or_method_type
7677
}
7778
}
7879

@@ -97,14 +98,6 @@ impl PyBuiltinFunction {
9798
self
9899
}
99100

100-
pub fn into_ref(self, ctx: &Context) -> PyRef<Self> {
101-
PyRef::new_ref(
102-
self,
103-
ctx.types.builtin_function_or_method_type.to_owned(),
104-
None,
105-
)
106-
}
107-
108101
pub fn as_func(&self) -> &PyNativeFunc {
109102
&self.value.func
110103
}
@@ -176,8 +169,9 @@ pub struct PyBuiltinMethod {
176169
}
177170

178171
impl PyPayload for PyBuiltinMethod {
179-
fn class(vm: &VirtualMachine) -> &'static Py<PyType> {
180-
vm.ctx.types.method_descriptor_type
172+
#[inline]
173+
fn class(ctx: &Context) -> &'static Py<PyType> {
174+
ctx.types.method_descriptor_type
181175
}
182176
}
183177

@@ -201,7 +195,9 @@ impl GetDescriptor for PyBuiltinMethod {
201195
let r = if vm.is_none(&obj) && !Self::_cls_is(&cls, &obj.class()) {
202196
zelf.into()
203197
} else {
204-
PyBoundMethod::new_ref(obj, zelf.into(), &vm.ctx).into()
198+
PyBoundMethod::new(obj, zelf.into())
199+
.into_ref(&vm.ctx)
200+
.into()
205201
};
206202
Ok(r)
207203
}

0 commit comments

Comments
 (0)