Skip to content

Commit 18044ab

Browse files
committed
use interned str for builtin function names
1 parent 143036a commit 18044ab

File tree

8 files changed

+25
-25
lines changed

8 files changed

+25
-25
lines changed

derive-impl/src/pyclass.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ where
629629
quote_spanned! { ident.span() =>
630630
class.set_attr(
631631
ctx.names.#name_ident,
632-
ctx.make_func_def(#py_name, Self::#ident)
632+
ctx.make_func_def(ctx.intern_str(#py_name), Self::#ident)
633633
#doc
634634
#build_func
635635
.into(),
@@ -639,7 +639,7 @@ where
639639
quote_spanned! { ident.span() =>
640640
class.set_str_attr(
641641
#py_name,
642-
ctx.make_func_def(#py_name, Self::#ident)
642+
ctx.make_func_def(ctx.intern_str(#py_name), Self::#ident)
643643
#doc
644644
#build_func,
645645
ctx,

derive-impl/src/pymodule.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ impl ModuleItem for FunctionItem {
334334
};
335335
let doc = quote!(.with_doc(#doc.to_owned(), &vm.ctx));
336336
let new_func = quote_spanned!(ident.span()=>
337-
vm.ctx.make_func_def(#py_name, #ident)
337+
vm.ctx.make_func_def(vm.ctx.intern_str(#py_name), #ident)
338338
#doc
339339
.into_function()
340340
.with_module(vm.new_pyobj(#module.to_owned()))

vm/src/builtins/builtin_func.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{type_, PyClassMethod, PyStaticMethod, PyStr, PyStrRef, PyType};
1+
use super::{type_, PyClassMethod, PyStaticMethod, PyStr, PyStrInterned, PyStrRef, PyType};
22
use crate::{
33
builtins::PyBoundMethod,
44
class::PyClassImpl,
@@ -10,12 +10,12 @@ use std::fmt;
1010

1111
pub struct PyNativeFuncDef {
1212
pub func: PyNativeFunc,
13-
pub name: PyStrRef,
13+
pub name: &'static PyStrInterned,
1414
pub doc: Option<PyStrRef>,
1515
}
1616

1717
impl PyNativeFuncDef {
18-
pub fn new(func: PyNativeFunc, name: PyStrRef) -> Self {
18+
pub fn new(func: PyNativeFunc, name: &'static PyStrInterned) -> Self {
1919
Self {
2020
func,
2121
name,
@@ -122,7 +122,7 @@ impl PyBuiltinFunction {
122122
}
123123
#[pygetset(magic)]
124124
fn name(&self) -> PyStrRef {
125-
self.value.name.clone()
125+
self.value.name.to_owned()
126126
}
127127
#[pygetset(magic)]
128128
fn qualname(&self) -> PyStrRef {
@@ -217,7 +217,7 @@ impl Callable for PyBuiltinMethod {
217217

218218
impl PyBuiltinMethod {
219219
pub fn new_ref<F, FKind>(
220-
name: impl Into<PyStr>,
220+
name: &'static PyStrInterned,
221221
class: &'static Py<PyType>,
222222
f: F,
223223
ctx: &Context,
@@ -236,7 +236,7 @@ impl PyBuiltinMethod {
236236
impl PyBuiltinMethod {
237237
#[pygetset(magic)]
238238
fn name(&self) -> PyStrRef {
239-
self.value.name.clone()
239+
self.value.name.to_owned()
240240
}
241241
#[pygetset(magic)]
242242
fn qualname(&self) -> String {
@@ -260,7 +260,7 @@ impl PyBuiltinMethod {
260260
) -> (Option<PyObjectRef>, (Option<PyObjectRef>, PyStrRef)) {
261261
let builtins_getattr = vm.builtins.get_attr("getattr", vm).ok();
262262
let classname = vm.builtins.get_attr(&self.class.__name__(vm), vm).ok();
263-
(builtins_getattr, (classname, self.value.name.clone()))
263+
(builtins_getattr, (classname, self.value.name.to_owned()))
264264
}
265265
}
266266

vm/src/builtins/staticmethod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{PyStr, PyType, PyTypeRef};
1+
use super::{PyStr, PyStrInterned, PyType, PyTypeRef};
22
use crate::{
33
builtins::builtin_func::PyBuiltinMethod,
44
class::PyClassImpl,
@@ -75,7 +75,7 @@ impl PyStaticMethod {
7575

7676
impl PyStaticMethod {
7777
pub fn new_builtin_ref<F, FKind>(
78-
name: impl Into<PyStr>,
78+
name: &'static PyStrInterned,
7979
class: &'static Py<PyType>,
8080
f: F,
8181
ctx: &Context,

vm/src/exceptions.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,7 @@ impl ExceptionZoo {
753753
extend_exception!(PyLookupError, ctx, excs.lookup_error);
754754
extend_exception!(PyIndexError, ctx, excs.index_error);
755755
extend_exception!(PyKeyError, ctx, excs.key_error, {
756-
"__str__" => ctx.new_method("__str__", excs.key_error, key_error_str),
756+
"__str__" => ctx.new_method(identifier!(ctx, __str__), excs.key_error, key_error_str),
757757
});
758758

759759
extend_exception!(PyMemoryError, ctx, excs.memory_error);
@@ -786,8 +786,8 @@ impl ExceptionZoo {
786786
"filename" => ctx.none(),
787787
// second exception filename
788788
"filename2" => ctx.none(),
789-
"__str__" => ctx.new_method("__str__", excs.os_error, os_error_str),
790-
"__reduce__" => ctx.new_method("__reduce__", excs.os_error, os_error_reduce),
789+
"__str__" => ctx.new_method(identifier!(ctx, __str__), excs.os_error, os_error_str),
790+
"__reduce__" => ctx.new_method(identifier!(ctx, __reduce__), excs.os_error, os_error_reduce),
791791
});
792792
// TODO: this isn't really accurate
793793
#[cfg(windows)]

vm/src/macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ macro_rules! named_function {
219219
let ctx: &$crate::Context = &$ctx;
220220
$crate::__exports::paste::expr! {
221221
ctx.make_func_def(
222-
stringify!($func),
222+
ctx.intern_str(stringify!($func)),
223223
[<$module _ $func>],
224224
)
225225
.into_function()

vm/src/vm/context.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,7 @@ impl Context {
288288
let names = unsafe { ConstName::new(&string_pool, &types.str_type.to_owned()) };
289289

290290
let slot_new_wrapper = create_object(
291-
PyNativeFuncDef::new(PyType::__new__.into_func(), names.__new__.to_owned())
292-
.into_function(),
291+
PyNativeFuncDef::new(PyType::__new__.into_func(), names.__new__).into_function(),
293292
types.builtin_function_or_method_type,
294293
)
295294
.into();
@@ -491,11 +490,11 @@ impl Context {
491490
}
492491

493492
#[inline]
494-
pub fn make_func_def<F, FKind>(&self, name: impl Into<PyStr>, f: F) -> PyNativeFuncDef
493+
pub fn make_func_def<F, FKind>(&self, name: &'static PyStrInterned, f: F) -> PyNativeFuncDef
495494
where
496495
F: IntoPyNativeFunc<FKind>,
497496
{
498-
PyNativeFuncDef::new(f.into_func(), PyStr::new_ref(name, self))
497+
PyNativeFuncDef::new(f.into_func(), name)
499498
}
500499

501500
#[inline]
@@ -531,16 +530,17 @@ impl Context {
531530
}
532531

533532
// #[deprecated]
534-
pub fn new_function<F, FKind>(&self, name: impl Into<PyStr>, f: F) -> PyRef<PyBuiltinFunction>
533+
pub fn new_function<F, FKind>(&self, name: &str, f: F) -> PyRef<PyBuiltinFunction>
535534
where
536535
F: IntoPyNativeFunc<FKind>,
537536
{
538-
self.make_func_def(name, f).build_function(self)
537+
self.make_func_def(self.intern_str(name), f)
538+
.build_function(self)
539539
}
540540

541541
pub fn new_method<F, FKind>(
542542
&self,
543-
name: impl Into<PyStr>,
543+
name: &'static PyStrInterned,
544544
class: &'static Py<PyType>,
545545
f: F,
546546
) -> PyRef<PyBuiltinMethod>

wasm/lib/src/wasm_builtins.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ pub fn make_stdout_object(
3030
{}
3131
));
3232
let write_method = ctx.new_method(
33-
"write",
33+
ctx.intern_str("write"),
3434
cls,
3535
move |_self: PyObjectRef, data: PyStrRef, vm: &VirtualMachine| -> PyResult<()> {
3636
write_f(data.as_str(), vm)
3737
},
3838
);
39-
let flush_method = ctx.new_method("flush", cls, |_self: PyObjectRef| {});
39+
let flush_method = ctx.new_method(ctx.intern_str("flush"), cls, |_self: PyObjectRef| {});
4040
extend_class!(ctx, cls, {
4141
"write" => write_method,
4242
"flush" => flush_method,

0 commit comments

Comments
 (0)