Skip to content

Commit 9b2a9d2

Browse files
committed
TypeZoo uses &'static Py<PyType>
1 parent 9db69d6 commit 9b2a9d2

Some content is hidden

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

89 files changed

+916
-882
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: 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 {

0 commit comments

Comments
 (0)