Skip to content

Commit 55f5bf3

Browse files
committed
TypeZoo uses &'static Py<PyType>
1 parent 15d8abf commit 55f5bf3

Some content is hidden

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

88 files changed

+891
-853
lines changed

ast/asdl_rs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ 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)
392392
self.emit(f'class.set_str_attr("_fields", ctx.new_list(vec![{fields}]));', depth + 2)
393393
attrs = ",".join(f"ctx.new_str(ascii!({json.dumps(attr.name)})).into()" for attr in attrs)
@@ -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:

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
@@ -237,7 +237,7 @@ fn generate_class_def(
237237
}
238238
.map(|typ| {
239239
quote! {
240-
fn static_baseclass() -> &'static ::rustpython_vm::builtins::PyTypeRef {
240+
fn static_baseclass() -> &'static ::rustpython_vm::Py<::rustpython_vm::builtins::PyType> {
241241
use rustpython_vm::class::StaticType;
242242
#typ::static_type()
243243
}
@@ -247,7 +247,7 @@ fn generate_class_def(
247247
let meta_class = metaclass.map(|typ| {
248248
let typ = Ident::new(&typ, ident.span());
249249
quote! {
250-
fn static_metaclass() -> &'static ::rustpython_vm::builtins::PyTypeRef {
250+
fn static_metaclass() -> &'static ::rustpython_vm::Py<::rustpython_vm::builtins::PyType> {
251251
use rustpython_vm::class::StaticType;
252252
#typ::static_type()
253253
}
@@ -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) -> &::rustpython_vm::builtins::PyTypeRef {
371-
&vm.ctx.exceptions.#ctx_name
370+
fn class(vm: &::rustpython_vm::VirtualMachine) -> &'static ::rustpython_vm::Py<::rustpython_vm::builtins::PyType> {
371+
vm.ctx.exceptions.#ctx_name
372372
}
373373
}
374374

@@ -490,9 +490,9 @@ where
490490
quote!(.with_doc(#doc.to_owned(), ctx))
491491
});
492492
let build_func = match self.inner.attr_name {
493-
AttrName::Method => quote!(.build_method(ctx, class.clone())),
494-
AttrName::ClassMethod => quote!(.build_classmethod(ctx, class.clone())),
495-
AttrName::StaticMethod => quote!(.build_staticmethod(ctx, class.clone())),
493+
AttrName::Method => quote!(.build_method(ctx, class)),
494+
AttrName::ClassMethod => quote!(.build_classmethod(ctx, class)),
495+
AttrName::StaticMethod => quote!(.build_staticmethod(ctx, class)),
496496
other => unreachable!(
497497
"Only 'method', 'classmethod' and 'staticmethod' are supported, got {:?}",
498498
other
@@ -733,10 +733,10 @@ impl ToTokens for GetSetNursery {
733733
class.set_str_attr(
734734
#name,
735735
::rustpython_vm::PyRef::new_ref(
736-
::rustpython_vm::builtins::PyGetSet::new(#name.into(), class.clone())
736+
::rustpython_vm::builtins::PyGetSet::new(#name.into(), class)
737737
.with_get(&Self::#getter)
738738
#setter #deleter,
739-
ctx.types.getset_type.clone(), None)
739+
ctx.types.getset_type.to_owned(), None)
740740
);
741741
}
742742
});

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: 1 addition & 1 deletion
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 => {

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/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
@@ -163,7 +163,7 @@ mod decl {
163163

164164
#[pyattr]
165165
fn error(vm: &VirtualMachine) -> PyTypeRef {
166-
vm.ctx.exceptions.os_error.clone()
166+
vm.ctx.exceptions.os_error.to_owned()
167167
}
168168

169169
#[pyfunction]

0 commit comments

Comments
 (0)