Skip to content

ExceptionZoo holds static ref #3717

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

Merged
merged 2 commits into from
May 27, 2022
Merged
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
8 changes: 4 additions & 4 deletions ast/asdl_rs.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,11 +387,11 @@ 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_attr(interned!(ctx, _fields), ctx.new_list(vec![{fields}]).into());', depth + 2)
self.emit(f'class.set_attr(identifier!(ctx, _fields), ctx.new_list(vec![{fields}]).into());', depth + 2)
attrs = ",".join(f"ctx.new_str(ascii!({json.dumps(attr.name)})).into()" for attr in attrs)
self.emit(f'class.set_attr(interned!(ctx, _attributes), ctx.new_list(vec![{attrs}]).into());', depth + 2)
self.emit(f'class.set_attr(identifier!(ctx, _attributes), ctx.new_list(vec![{attrs}]).into());', depth + 2)
self.emit("}", depth + 1)
self.emit("}", depth)

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
2 changes: 1 addition & 1 deletion benches/microbenchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use criterion::{
Criterion, Throughput,
};
use rustpython_compiler::Mode;
use rustpython_vm::{common::ascii, Interpreter, PyResult, Settings};
use rustpython_vm::{common::ascii, AsObject, Interpreter, PyResult, Settings};
use std::{
ffi, fs, io,
path::{Path, PathBuf},
Expand Down
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 @@ -238,7 +238,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 @@ -248,7 +248,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 @@ -368,8 +368,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(vm: &::rustpython_vm::VirtualMachine) -> &'static ::rustpython_vm::Py<::rustpython_vm::builtins::PyType> {
vm.ctx.exceptions.#ctx_name
}
}

Expand Down Expand Up @@ -491,9 +491,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 @@ -735,10 +735,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),
ctx
);
}
Expand Down
2 changes: 1 addition & 1 deletion derive/src/pypayload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ 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 {
fn class(_vm: &::rustpython_vm::VirtualMachine) -> &'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
2 changes: 1 addition & 1 deletion stdlib/src/array.rs
Original file line number Diff line number Diff line change
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
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
34 changes: 8 additions & 26 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 @@ -76,38 +76,20 @@ mod _pyexpat {
}

#[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
2 changes: 1 addition & 1 deletion stdlib/src/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,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
10 changes: 5 additions & 5 deletions stdlib/src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,31 +80,31 @@ mod _socket {

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

#[pyattr(once)]
fn timeout(vm: &VirtualMachine) -> PyTypeRef {
vm.ctx.new_exception_type(
"socket",
"timeout",
Some(vec![vm.ctx.exceptions.os_error.clone()]),
Some(vec![vm.ctx.exceptions.os_error.to_owned()]),
)
}
#[pyattr(once)]
fn herror(vm: &VirtualMachine) -> PyTypeRef {
vm.ctx.new_exception_type(
"socket",
"herror",
Some(vec![vm.ctx.exceptions.os_error.clone()]),
Some(vec![vm.ctx.exceptions.os_error.to_owned()]),
)
}
#[pyattr(once)]
fn gaierror(vm: &VirtualMachine) -> PyTypeRef {
vm.ctx.new_exception_type(
"socket",
"gaierror",
Some(vec![vm.ctx.exceptions.os_error.clone()]),
Some(vec![vm.ctx.exceptions.os_error.to_owned()]),
)
}

Expand Down Expand Up @@ -153,7 +153,7 @@ mod _socket {
type CastFrom = libc::c_longlong;

// should really just be to_index() but test_socket tests the error messages explicitly
if obj.fast_isinstance(&vm.ctx.types.float_type) {
if obj.fast_isinstance(vm.ctx.types.float_type) {
return Err(vm.new_type_error("integer argument expected, got float".to_owned()));
}
let int = vm
Expand Down
7 changes: 5 additions & 2 deletions stdlib/src/ssl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ mod _ssl {
vm.ctx.new_exception_type(
"ssl",
"SSLError",
Some(vec![vm.ctx.exceptions.os_error.clone()]),
Some(vec![vm.ctx.exceptions.os_error.to_owned()]),
)
}

Expand All @@ -190,7 +190,10 @@ mod _ssl {
vm.ctx.new_exception_type(
"ssl",
"SSLCertVerificationError",
Some(vec![ssl_error(vm), vm.ctx.exceptions.value_error.clone()]),
Some(vec![
ssl_error(vm),
vm.ctx.exceptions.value_error.to_owned(),
]),
)
}

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

Expand Down
Loading