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)

stdlib/src/pyexpat.rs

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ macro_rules! create_property {
3232
#[pymodule(name = "pyexpat")]
3333
mod _pyexpat {
3434
use crate::vm::{
35-
builtins::{PyStr, PyStrRef, PyTypeRef},
35+
builtins::{PyStr, PyStrRef, PyType},
3636
function::ArgBytesLike,
3737
function::{IntoFuncArgs, OptionalArg},
38-
Context, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject, VirtualMachine,
38+
Context, Py, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject, VirtualMachine,
3939
};
4040
use rustpython_common::lock::PyRwLock;
4141
use std::io::Cursor;
@@ -76,38 +76,20 @@ mod _pyexpat {
7676
}
7777

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

82-
create_property!(
83-
ctx,
84-
attributes,
85-
"StartElementHandler",
86-
class.clone(),
87-
start_element
88-
);
89-
create_property!(
90-
ctx,
91-
attributes,
92-
"EndElementHandler",
93-
class.clone(),
94-
end_element
95-
);
82+
create_property!(ctx, attributes, "StartElementHandler", class, start_element);
83+
create_property!(ctx, attributes, "EndElementHandler", class, end_element);
9684
create_property!(
9785
ctx,
9886
attributes,
9987
"CharacterDataHandler",
100-
class.clone(),
88+
class,
10189
character_data
10290
);
103-
create_property!(
104-
ctx,
105-
attributes,
106-
"EntityDeclHandler",
107-
class.clone(),
108-
entity_decl
109-
);
110-
create_property!(ctx, attributes, "buffer_text", class.clone(), buffer_text);
91+
create_property!(ctx, attributes, "EntityDeclHandler", class, entity_decl);
92+
create_property!(ctx, attributes, "buffer_text", class, buffer_text);
11193
}
11294

11395
fn create_config(&self) -> xml::ParserConfig {

stdlib/src/select.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ mod decl {
165165

166166
#[pyattr]
167167
fn error(vm: &VirtualMachine) -> PyTypeRef {
168-
vm.ctx.exceptions.os_error.clone()
168+
vm.ctx.exceptions.os_error.to_owned()
169169
}
170170

171171
#[pyfunction]

stdlib/src/socket.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,31 +80,31 @@ mod _socket {
8080

8181
#[pyattr]
8282
fn error(vm: &VirtualMachine) -> PyTypeRef {
83-
vm.ctx.exceptions.os_error.clone()
83+
vm.ctx.exceptions.os_error.to_owned()
8484
}
8585

8686
#[pyattr(once)]
8787
fn timeout(vm: &VirtualMachine) -> PyTypeRef {
8888
vm.ctx.new_exception_type(
8989
"socket",
9090
"timeout",
91-
Some(vec![vm.ctx.exceptions.os_error.clone()]),
91+
Some(vec![vm.ctx.exceptions.os_error.to_owned()]),
9292
)
9393
}
9494
#[pyattr(once)]
9595
fn herror(vm: &VirtualMachine) -> PyTypeRef {
9696
vm.ctx.new_exception_type(
9797
"socket",
9898
"herror",
99-
Some(vec![vm.ctx.exceptions.os_error.clone()]),
99+
Some(vec![vm.ctx.exceptions.os_error.to_owned()]),
100100
)
101101
}
102102
#[pyattr(once)]
103103
fn gaierror(vm: &VirtualMachine) -> PyTypeRef {
104104
vm.ctx.new_exception_type(
105105
"socket",
106106
"gaierror",
107-
Some(vec![vm.ctx.exceptions.os_error.clone()]),
107+
Some(vec![vm.ctx.exceptions.os_error.to_owned()]),
108108
)
109109
}
110110

@@ -153,7 +153,7 @@ mod _socket {
153153
type CastFrom = libc::c_longlong;
154154

155155
// should really just be to_index() but test_socket tests the error messages explicitly
156-
if obj.fast_isinstance(&vm.ctx.types.float_type) {
156+
if obj.fast_isinstance(vm.ctx.types.float_type) {
157157
return Err(vm.new_type_error("integer argument expected, got float".to_owned()));
158158
}
159159
let int = vm

stdlib/src/ssl.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ mod _ssl {
180180
vm.ctx.new_exception_type(
181181
"ssl",
182182
"SSLError",
183-
Some(vec![vm.ctx.exceptions.os_error.clone()]),
183+
Some(vec![vm.ctx.exceptions.os_error.to_owned()]),
184184
)
185185
}
186186

@@ -190,7 +190,10 @@ mod _ssl {
190190
vm.ctx.new_exception_type(
191191
"ssl",
192192
"SSLCertVerificationError",
193-
Some(vec![ssl_error(vm), vm.ctx.exceptions.value_error.clone()]),
193+
Some(vec![
194+
ssl_error(vm),
195+
vm.ctx.exceptions.value_error.to_owned(),
196+
]),
194197
)
195198
}
196199

stdlib/src/termios.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ mod termios {
271271
vm.ctx.new_exception_type(
272272
"termios",
273273
"error",
274-
Some(vec![vm.ctx.exceptions.os_error.clone()]),
274+
Some(vec![vm.ctx.exceptions.os_error.to_owned()]),
275275
)
276276
}
277277
}

stdlib/src/zlib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ mod zlib {
5858
vm.ctx.new_exception_type(
5959
"zlib",
6060
"error",
61-
Some(vec![vm.ctx.exceptions.exception_type.clone()]),
61+
Some(vec![vm.ctx.exceptions.exception_type.to_owned()]),
6262
)
6363
}
6464

0 commit comments

Comments
 (0)