Skip to content

Commit 8ab4e77

Browse files
authored
Merge pull request RustPython#3717 from youknowone/exception-ref
ExceptionZoo holds static ref
2 parents b4486e6 + 9b2a9d2 commit 8ab4e77

Some content is hidden

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

103 files changed

+1435
-1417
lines changed

ast/asdl_rs.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -387,11 +387,11 @@ def gen_classdef(self, name, fields, attrs, depth, base="AstNode"):
387387
self.emit("#[pyimpl(flags(HAS_DICT, BASETYPE))]", depth)
388388
self.emit(f"impl {structname} {{", depth)
389389
self.emit(f"#[extend_class]", depth + 1)
390-
self.emit("fn extend_class_with_fields(ctx: &Context, class: &PyTypeRef) {", depth + 1)
390+
self.emit("fn extend_class_with_fields(ctx: &Context, class: &'static Py<PyType>) {", depth + 1)
391391
fields = ",".join(f"ctx.new_str(ascii!({json.dumps(f.name)})).into()" for f in fields)
392-
self.emit(f'class.set_attr(interned!(ctx, _fields), ctx.new_list(vec![{fields}]).into());', depth + 2)
392+
self.emit(f'class.set_attr(identifier!(ctx, _fields), ctx.new_list(vec![{fields}]).into());', depth + 2)
393393
attrs = ",".join(f"ctx.new_str(ascii!({json.dumps(attr.name)})).into()" for attr in attrs)
394-
self.emit(f'class.set_attr(interned!(ctx, _attributes), ctx.new_list(vec![{attrs}]).into());', depth + 2)
394+
self.emit(f'class.set_attr(identifier!(ctx, _attributes), ctx.new_list(vec![{attrs}]).into());', depth + 2)
395395
self.emit("}", depth + 1)
396396
self.emit("}", depth)
397397

@@ -481,7 +481,7 @@ def visitProduct(self, product, name, depth):
481481

482482
def make_node(self, variant, fields, depth):
483483
lines = []
484-
self.emit(f"let _node = AstNode.into_ref_with_type(_vm, Node{variant}::static_type().clone()).unwrap();", depth)
484+
self.emit(f"let _node = AstNode.into_ref_with_type(_vm, Node{variant}::static_type().to_owned()).unwrap();", depth)
485485
if fields:
486486
self.emit("let _dict = _node.as_object().dict().unwrap();", depth)
487487
for f in fields:

benches/microbenchmarks.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use criterion::{
33
Criterion, Throughput,
44
};
55
use rustpython_compiler::Mode;
6-
use rustpython_vm::{common::ascii, Interpreter, PyResult, Settings};
6+
use rustpython_vm::{common::ascii, AsObject, Interpreter, PyResult, Settings};
77
use std::{
88
ffi, fs, io,
99
path::{Path, PathBuf},

derive/src/pyclass.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ pub(crate) fn impl_pyimpl(attr: AttributeArgs, item: Item) -> Result<TokenStream
117117

118118
fn impl_extend_class(
119119
ctx: &::rustpython_vm::Context,
120-
class: &::rustpython_vm::builtins::PyTypeRef,
120+
class: &'static ::rustpython_vm::Py<::rustpython_vm::builtins::PyType>,
121121
) {
122122
#getset_impl
123123
#extend_impl
@@ -150,7 +150,7 @@ pub(crate) fn impl_pyimpl(attr: AttributeArgs, item: Item) -> Result<TokenStream
150150
parse_quote! {
151151
fn __extend_py_class(
152152
ctx: &::rustpython_vm::Context,
153-
class: &::rustpython_vm::builtins::PyTypeRef,
153+
class: &'static ::rustpython_vm::Py<::rustpython_vm::builtins::PyType>,
154154
) {
155155
#getset_impl
156156
#extend_impl
@@ -238,7 +238,7 @@ fn generate_class_def(
238238
}
239239
.map(|typ| {
240240
quote! {
241-
fn static_baseclass() -> &'static ::rustpython_vm::builtins::PyTypeRef {
241+
fn static_baseclass() -> &'static ::rustpython_vm::Py<::rustpython_vm::builtins::PyType> {
242242
use rustpython_vm::class::StaticType;
243243
#typ::static_type()
244244
}
@@ -248,7 +248,7 @@ fn generate_class_def(
248248
let meta_class = metaclass.map(|typ| {
249249
let typ = Ident::new(&typ, ident.span());
250250
quote! {
251-
fn static_metaclass() -> &'static ::rustpython_vm::builtins::PyTypeRef {
251+
fn static_metaclass() -> &'static ::rustpython_vm::Py<::rustpython_vm::builtins::PyType> {
252252
use rustpython_vm::class::StaticType;
253253
#typ::static_type()
254254
}
@@ -368,8 +368,8 @@ pub(crate) fn impl_define_exception(exc_def: PyExceptionDef) -> Result<TokenStre
368368

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

@@ -491,9 +491,9 @@ where
491491
quote!(.with_doc(#doc.to_owned(), ctx))
492492
});
493493
let build_func = match self.inner.attr_name {
494-
AttrName::Method => quote!(.build_method(ctx, class.clone())),
495-
AttrName::ClassMethod => quote!(.build_classmethod(ctx, class.clone())),
496-
AttrName::StaticMethod => quote!(.build_staticmethod(ctx, class.clone())),
494+
AttrName::Method => quote!(.build_method(ctx, class)),
495+
AttrName::ClassMethod => quote!(.build_classmethod(ctx, class)),
496+
AttrName::StaticMethod => quote!(.build_staticmethod(ctx, class)),
497497
other => unreachable!(
498498
"Only 'method', 'classmethod' and 'staticmethod' are supported, got {:?}",
499499
other
@@ -735,10 +735,10 @@ impl ToTokens for GetSetNursery {
735735
class.set_str_attr(
736736
#name,
737737
::rustpython_vm::PyRef::new_ref(
738-
::rustpython_vm::builtins::PyGetSet::new(#name.into(), class.clone())
738+
::rustpython_vm::builtins::PyGetSet::new(#name.into(), class)
739739
.with_get(&Self::#getter)
740740
#setter #deleter,
741-
ctx.types.getset_type.clone(), None),
741+
ctx.types.getset_type.to_owned(), None),
742742
ctx
743743
);
744744
}

derive/src/pypayload.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ 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) -> &rustpython_vm::builtins::PyTypeRef {
10+
fn class(_vm: &::rustpython_vm::VirtualMachine) -> &'static rustpython_vm::Py<::rustpython_vm::builtins::PyType> {
1111
<Self as ::rustpython_vm::class::StaticType>::static_type()
1212
}
1313
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ __import__("io").TextIOWrapper(
517517
#[cfg(not(feature = "ssl"))]
518518
fn install_pip(_: Scope, vm: &VirtualMachine) -> PyResult {
519519
Err(vm.new_exception_msg(
520-
vm.ctx.exceptions.system_error.clone(),
520+
vm.ctx.exceptions.system_error.to_owned(),
521521
"install-pip requires rustpython be build with the 'ssl' feature enabled.".to_owned(),
522522
))
523523
}

src/shell.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ pub fn run_shell(vm: &VirtualMachine, scope: Scope) -> PyResult<()> {
106106
continuing = false;
107107
full_input.clear();
108108
let keyboard_interrupt =
109-
vm.new_exception_empty(vm.ctx.exceptions.keyboard_interrupt.clone());
109+
vm.new_exception_empty(vm.ctx.exceptions.keyboard_interrupt.to_owned());
110110
Err(keyboard_interrupt)
111111
}
112112
ReadlineResult::Eof => {
@@ -127,7 +127,7 @@ pub fn run_shell(vm: &VirtualMachine, scope: Scope) -> PyResult<()> {
127127
};
128128

129129
if let Err(exc) = result {
130-
if exc.fast_isinstance(&vm.ctx.exceptions.system_exit) {
130+
if exc.fast_isinstance(vm.ctx.exceptions.system_exit) {
131131
repl.save_history(&repl_history_path).unwrap();
132132
return Err(exc);
133133
}

stdlib/src/array.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -843,7 +843,7 @@ mod array {
843843

844844
if not_enough_bytes {
845845
Err(vm.new_exception_msg(
846-
vm.ctx.exceptions.eof_error.clone(),
846+
vm.ctx.exceptions.eof_error.to_owned(),
847847
"read() didn't return enough bytes".to_owned(),
848848
))
849849
} else {

stdlib/src/binascii.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ mod decl {
1616
vm.ctx.new_exception_type(
1717
"binascii",
1818
"Error",
19-
Some(vec![vm.ctx.exceptions.value_error.clone()]),
19+
Some(vec![vm.ctx.exceptions.value_error.to_owned()]),
2020
)
2121
}
2222

stdlib/src/csv.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ mod _csv {
2828
vm.ctx.new_exception_type(
2929
"_csv",
3030
"Error",
31-
Some(vec![vm.ctx.exceptions.exception_type.clone()]),
31+
Some(vec![vm.ctx.exceptions.exception_type.to_owned()]),
3232
)
3333
}
3434

stdlib/src/json.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ mod _json {
3636
let object_hook = vm.option_if_none(ctx.get_attr("object_hook", vm)?);
3737
let object_pairs_hook = vm.option_if_none(ctx.get_attr("object_pairs_hook", vm)?);
3838
let parse_float = ctx.get_attr("parse_float", vm)?;
39-
let parse_float =
40-
if vm.is_none(&parse_float) || parse_float.is(&vm.ctx.types.float_type) {
41-
None
42-
} else {
43-
Some(parse_float)
44-
};
39+
let parse_float = if vm.is_none(&parse_float) || parse_float.is(vm.ctx.types.float_type)
40+
{
41+
None
42+
} else {
43+
Some(parse_float)
44+
};
4545
let parse_int = ctx.get_attr("parse_int", vm)?;
46-
let parse_int = if vm.is_none(&parse_int) || parse_int.is(&vm.ctx.types.int_type) {
46+
let parse_int = if vm.is_none(&parse_int) || parse_int.is(vm.ctx.types.int_type) {
4747
None
4848
} else {
4949
Some(parse_int)

0 commit comments

Comments
 (0)