Skip to content

PyPayload::{new_ref, into_ref} #3744

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ast/asdl_rs.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ def gen_classdef(self, name, fields, attrs, depth, base="AstNode"):
self.emit("#[pyimpl(flags(HAS_DICT, BASETYPE))]", depth)
self.emit(f"impl {structname} {{", depth)
self.emit(f"#[extend_class]", depth + 1)
self.emit("fn extend_class_with_fields(ctx: &Context, class: &PyTypeRef) {", depth + 1)
self.emit("fn extend_class_with_fields(ctx: &Context, class: &'static Py<PyType>) {", depth + 1)
fields = ",".join(f"ctx.new_str(ascii!({json.dumps(f.name)})).into()" for f in fields)
self.emit(f'class.set_str_attr("_fields", ctx.new_list(vec![{fields}]));', depth + 2)
attrs = ",".join(f"ctx.new_str(ascii!({json.dumps(attr.name)})).into()" for attr in attrs)
Expand Down Expand Up @@ -481,7 +481,7 @@ def visitProduct(self, product, name, depth):

def make_node(self, variant, fields, depth):
lines = []
self.emit(f"let _node = AstNode.into_ref_with_type(_vm, Node{variant}::static_type().clone()).unwrap();", depth)
self.emit(f"let _node = AstNode.into_ref_with_type(_vm, Node{variant}::static_type().to_owned()).unwrap();", depth)
if fields:
self.emit("let _dict = _node.as_object().dict().unwrap();", depth)
for f in fields:
Expand Down
22 changes: 20 additions & 2 deletions common/src/refcount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::atomic::{Ordering::*, PyAtomic, Radium};
///
/// Going above this limit will abort your program (although not
/// necessarily) at _exactly_ `MAX_REFCOUNT + 1` references.
const MAX_REFCOUNT: usize = (isize::MAX) as usize;
const MAX_REFCOUNT: usize = isize::MAX as usize;

pub struct RefCount {
strong: PyAtomic<usize>,
Expand All @@ -18,6 +18,8 @@ impl Default for RefCount {
}

impl RefCount {
const MASK: usize = MAX_REFCOUNT;

pub fn new() -> Self {
RefCount {
strong: Radium::new(1),
Expand All @@ -33,7 +35,7 @@ impl RefCount {
pub fn inc(&self) {
let old_size = self.strong.fetch_add(1, Relaxed);

if old_size > MAX_REFCOUNT {
if old_size & Self::MASK == Self::MASK {
std::process::abort();
}
}
Expand All @@ -58,3 +60,19 @@ impl RefCount {
true
}
}

impl RefCount {
// move these functions out and give separated type once type range is stabilized

pub fn leak(&self) {
debug_assert!(!self.is_leaked());
const BIT_MARKER: usize = (std::isize::MAX as usize) + 1;
debug_assert_eq!(BIT_MARKER.count_ones(), 1);
debug_assert_eq!(BIT_MARKER.leading_zeros(), 0);
self.strong.fetch_add(BIT_MARKER, Relaxed);
}

pub fn is_leaked(&self) -> bool {
(self.strong.load(Acquire) as isize) < 0
}
}
22 changes: 11 additions & 11 deletions derive/src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ pub(crate) fn impl_pyimpl(attr: AttributeArgs, item: Item) -> Result<TokenStream

fn impl_extend_class(
ctx: &::rustpython_vm::Context,
class: &::rustpython_vm::builtins::PyTypeRef,
class: &'static ::rustpython_vm::Py<::rustpython_vm::builtins::PyType>,
) {
#getset_impl
#extend_impl
Expand Down Expand Up @@ -150,7 +150,7 @@ pub(crate) fn impl_pyimpl(attr: AttributeArgs, item: Item) -> Result<TokenStream
parse_quote! {
fn __extend_py_class(
ctx: &::rustpython_vm::Context,
class: &::rustpython_vm::builtins::PyTypeRef,
class: &'static ::rustpython_vm::Py<::rustpython_vm::builtins::PyType>,
) {
#getset_impl
#extend_impl
Expand Down Expand Up @@ -237,7 +237,7 @@ fn generate_class_def(
}
.map(|typ| {
quote! {
fn static_baseclass() -> &'static ::rustpython_vm::builtins::PyTypeRef {
fn static_baseclass() -> &'static ::rustpython_vm::Py<::rustpython_vm::builtins::PyType> {
use rustpython_vm::class::StaticType;
#typ::static_type()
}
Expand All @@ -247,7 +247,7 @@ fn generate_class_def(
let meta_class = metaclass.map(|typ| {
let typ = Ident::new(&typ, ident.span());
quote! {
fn static_metaclass() -> &'static ::rustpython_vm::builtins::PyTypeRef {
fn static_metaclass() -> &'static ::rustpython_vm::Py<::rustpython_vm::builtins::PyType> {
use rustpython_vm::class::StaticType;
#typ::static_type()
}
Expand Down Expand Up @@ -367,8 +367,8 @@ pub(crate) fn impl_define_exception(exc_def: PyExceptionDef) -> Result<TokenStre

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

Expand Down Expand Up @@ -490,9 +490,9 @@ where
quote!(.with_doc(#doc.to_owned(), ctx))
});
let build_func = match self.inner.attr_name {
AttrName::Method => quote!(.build_method(ctx, class.clone())),
AttrName::ClassMethod => quote!(.build_classmethod(ctx, class.clone())),
AttrName::StaticMethod => quote!(.build_staticmethod(ctx, class.clone())),
AttrName::Method => quote!(.build_method(ctx, class)),
AttrName::ClassMethod => quote!(.build_classmethod(ctx, class)),
AttrName::StaticMethod => quote!(.build_staticmethod(ctx, class)),
other => unreachable!(
"Only 'method', 'classmethod' and 'staticmethod' are supported, got {:?}",
other
Expand Down Expand Up @@ -733,10 +733,10 @@ impl ToTokens for GetSetNursery {
class.set_str_attr(
#name,
::rustpython_vm::PyRef::new_ref(
::rustpython_vm::builtins::PyGetSet::new(#name.into(), class.clone())
::rustpython_vm::builtins::PyGetSet::new(#name.into(), class)
.with_get(&Self::#getter)
#setter #deleter,
ctx.types.getset_type.clone(), None)
ctx.types.getset_type.to_owned(), None)
);
}
});
Expand Down
11 changes: 6 additions & 5 deletions derive/src/pymodule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,11 +332,12 @@ impl ModuleItem for FunctionItem {
};
let doc = quote!(.with_doc(#doc.to_owned(), &vm.ctx));
let new_func = quote_spanned!(ident.span()=>
vm.ctx.make_funcdef(#py_name, #ident)
#doc
.into_function()
.with_module(vm.new_pyobj(#module.to_owned()))
.into_ref(&vm.ctx)
::rustpython_vm::object::PyPayload::into_ref(
vm.ctx.make_funcdef(#py_name, #ident)
#doc
.into_function()
.with_module(vm.new_pyobj(#module.to_owned())),
&vm.ctx)
);

if self.pyattrs.is_empty() {
Expand Down
3 changes: 2 additions & 1 deletion derive/src/pypayload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ pub(crate) fn impl_pypayload(input: DeriveInput) -> Result<TokenStream> {

let ret = quote! {
impl ::rustpython_vm::PyPayload for #ty {
fn class(_vm: &::rustpython_vm::VirtualMachine) -> &rustpython_vm::builtins::PyTypeRef {
#[inline]
fn class(_ctx: &::rustpython_vm::vm::Context) -> &'static rustpython_vm::Py<::rustpython_vm::builtins::PyType> {
<Self as ::rustpython_vm::class::StaticType>::static_type()
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ __import__("io").TextIOWrapper(
#[cfg(not(feature = "ssl"))]
fn install_pip(_: Scope, vm: &VirtualMachine) -> PyResult {
Err(vm.new_exception_msg(
vm.ctx.exceptions.system_error.clone(),
vm.ctx.exceptions.system_error.to_owned(),
"install-pip requires rustpython be build with the 'ssl' feature enabled.".to_owned(),
))
}
Expand Down
4 changes: 2 additions & 2 deletions src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ pub fn run_shell(vm: &VirtualMachine, scope: Scope) -> PyResult<()> {
continuing = false;
full_input.clear();
let keyboard_interrupt =
vm.new_exception_empty(vm.ctx.exceptions.keyboard_interrupt.clone());
vm.new_exception_empty(vm.ctx.exceptions.keyboard_interrupt.to_owned());
Err(keyboard_interrupt)
}
ReadlineResult::Eof => {
Expand All @@ -127,7 +127,7 @@ pub fn run_shell(vm: &VirtualMachine, scope: Scope) -> PyResult<()> {
};

if let Err(exc) = result {
if exc.fast_isinstance(&vm.ctx.exceptions.system_exit) {
if exc.fast_isinstance(vm.ctx.exceptions.system_exit) {
repl.save_history(&repl_history_path).unwrap();
return Err(exc);
}
Expand Down
12 changes: 6 additions & 6 deletions stdlib/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ mod array {
fn typecode(&self, vm: &VirtualMachine) -> PyStrRef {
vm.ctx
.intern_str(self.read().typecode().to_string())
.to_str()
.to_owned()
}

#[pyproperty]
Expand Down Expand Up @@ -843,7 +843,7 @@ mod array {

if not_enough_bytes {
Err(vm.new_exception_msg(
vm.ctx.exceptions.eof_error.clone(),
vm.ctx.exceptions.eof_error.to_owned(),
"read() didn't return enough bytes".to_owned(),
))
} else {
Expand Down Expand Up @@ -897,7 +897,7 @@ mod array {
let bytes = bytes.get_bytes();

for b in bytes.chunks(BLOCKSIZE) {
let b = PyBytes::from(b.to_vec()).into_ref(vm);
let b = PyBytes::from(b.to_vec()).into_ref(&vm.ctx);
vm.call_method(&f, "write", (b,))?;
}
Ok(())
Expand Down Expand Up @@ -1017,7 +1017,7 @@ mod array {
if let Some(other) = other.payload::<PyArray>() {
self.read()
.add(&*other.read(), vm)
.map(|array| PyArray::from(array).into_ref(vm))
.map(|array| PyArray::from(array).into_ref(&vm.ctx))
} else {
Err(vm.new_type_error(format!(
"can only append array (not \"{}\") to array",
Expand Down Expand Up @@ -1050,7 +1050,7 @@ mod array {
fn mul(&self, value: isize, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
self.read()
.mul(value, vm)
.map(|x| Self::from(x).into_ref(vm))
.map(|x| Self::from(x).into_ref(&vm.ctx))
}

#[pymethod(magic)]
Expand Down Expand Up @@ -1444,7 +1444,7 @@ mod array {
}

fn check_array_type(typ: PyTypeRef, vm: &VirtualMachine) -> PyResult<PyTypeRef> {
if !typ.fast_issubclass(PyArray::class(vm)) {
if !typ.fast_issubclass(PyArray::class(&vm.ctx)) {
return Err(
vm.new_type_error(format!("{} is not a subtype of array.array", typ.name()))
);
Expand Down
2 changes: 1 addition & 1 deletion stdlib/src/binascii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mod decl {
vm.ctx.new_exception_type(
"binascii",
"Error",
Some(vec![vm.ctx.exceptions.value_error.clone()]),
Some(vec![vm.ctx.exceptions.value_error.to_owned()]),
)
}

Expand Down
2 changes: 1 addition & 1 deletion stdlib/src/csv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ mod _csv {
vm.ctx.new_exception_type(
"_csv",
"Error",
Some(vec![vm.ctx.exceptions.exception_type.clone()]),
Some(vec![vm.ctx.exceptions.exception_type.to_owned()]),
)
}

Expand Down
14 changes: 7 additions & 7 deletions stdlib/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ mod _json {
let object_hook = vm.option_if_none(ctx.get_attr("object_hook", vm)?);
let object_pairs_hook = vm.option_if_none(ctx.get_attr("object_pairs_hook", vm)?);
let parse_float = ctx.get_attr("parse_float", vm)?;
let parse_float =
if vm.is_none(&parse_float) || parse_float.is(&vm.ctx.types.float_type) {
None
} else {
Some(parse_float)
};
let parse_float = if vm.is_none(&parse_float) || parse_float.is(vm.ctx.types.float_type)
{
None
} else {
Some(parse_float)
};
let parse_int = ctx.get_attr("parse_int", vm)?;
let parse_int = if vm.is_none(&parse_int) || parse_int.is(&vm.ctx.types.int_type) {
let parse_int = if vm.is_none(&parse_int) || parse_int.is(vm.ctx.types.int_type) {
None
} else {
Some(parse_int)
Expand Down
42 changes: 12 additions & 30 deletions stdlib/src/pyexpat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ macro_rules! create_property {
#[pymodule(name = "pyexpat")]
mod _pyexpat {
use crate::vm::{
builtins::{PyStr, PyStrRef, PyTypeRef},
builtins::{PyStr, PyStrRef, PyType},
function::ArgBytesLike,
function::{IntoFuncArgs, OptionalArg},
Context, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject, VirtualMachine,
Context, Py, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject, VirtualMachine,
};
use rustpython_common::lock::PyRwLock;
use std::io::Cursor;
Expand Down Expand Up @@ -72,42 +72,24 @@ mod _pyexpat {
entity_decl: MutableObject::new(vm.ctx.none()),
buffer_text: MutableObject::new(vm.ctx.new_bool(false).into()),
}
.into_ref(vm))
.into_ref(&vm.ctx))
}

#[extend_class]
fn extend_class_with_fields(ctx: &Context, class: &PyTypeRef) {
fn extend_class_with_fields(ctx: &Context, class: &'static Py<PyType>) {
let mut attributes = class.attributes.write();

create_property!(
ctx,
attributes,
"StartElementHandler",
class.clone(),
start_element
);
create_property!(
ctx,
attributes,
"EndElementHandler",
class.clone(),
end_element
);
create_property!(ctx, attributes, "StartElementHandler", class, start_element);
create_property!(ctx, attributes, "EndElementHandler", class, end_element);
create_property!(
ctx,
attributes,
"CharacterDataHandler",
class.clone(),
class,
character_data
);
create_property!(
ctx,
attributes,
"EntityDeclHandler",
class.clone(),
entity_decl
);
create_property!(ctx, attributes, "buffer_text", class.clone(), buffer_text);
create_property!(ctx, attributes, "EntityDeclHandler", class, entity_decl);
create_property!(ctx, attributes, "buffer_text", class, buffer_text);
}

fn create_config(&self) -> xml::ParserConfig {
Expand Down Expand Up @@ -136,15 +118,15 @@ mod _pyexpat {
.unwrap();
}

let name_str = PyStr::from(name.local_name).into_ref(vm);
let name_str = PyStr::from(name.local_name).into_ref(&vm.ctx);
invoke_handler(vm, &self.start_element, (name_str, dict));
}
Ok(XmlEvent::EndElement { name, .. }) => {
let name_str = PyStr::from(name.local_name).into_ref(vm);
let name_str = PyStr::from(name.local_name).into_ref(&vm.ctx);
invoke_handler(vm, &self.end_element, (name_str,));
}
Ok(XmlEvent::Characters(chars)) => {
let str = PyStr::from(chars).into_ref(vm);
let str = PyStr::from(chars).into_ref(&vm.ctx);
invoke_handler(vm, &self.character_data, (str,));
}
_ => {}
Expand Down
2 changes: 1 addition & 1 deletion stdlib/src/pystruct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub(crate) mod _struct {
b @ PyBytes => if b.is_ascii() {
Some(unsafe {
PyStr::new_ascii_unchecked(b.as_bytes().to_vec())
}.into_ref(vm))
}.into_ref(&vm.ctx))
} else {
None
},
Expand Down
2 changes: 1 addition & 1 deletion stdlib/src/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ mod decl {

#[pyattr]
fn error(vm: &VirtualMachine) -> PyTypeRef {
vm.ctx.exceptions.os_error.clone()
vm.ctx.exceptions.os_error.to_owned()
}

#[pyfunction]
Expand Down
Loading