Skip to content

Commit dde92d1

Browse files
authored
Merge pull request RustPython#4853 from youknowone/builtin-function-names
use interned str for builtin function names
2 parents 46455e8 + 798b3bc commit dde92d1

File tree

10 files changed

+35
-34
lines changed

10 files changed

+35
-34
lines changed

derive-impl/src/pyclass.rs

+2-2
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

+1-1
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

+7-7
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

+2-2
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/class.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use crate::{
44
builtins::{PyBaseObject, PyBoundMethod, PyType, PyTypeRef},
55
identifier,
6-
object::{Py, PyObjectRef},
6+
object::Py,
77
types::{hash_not_implemented, PyTypeFlags, PyTypeSlots},
88
vm::Context,
99
};
@@ -99,10 +99,12 @@ pub trait PyClassImpl: PyClassDef {
9999
);
100100
}
101101
if class.slots.new.load().is_some() {
102-
let bound: PyObjectRef =
103-
PyBoundMethod::new_ref(class.to_owned().into(), ctx.slot_new_wrapper.clone(), ctx)
104-
.into();
105-
class.set_attr(identifier!(ctx, __new__), bound);
102+
let bound = PyBoundMethod::new_ref(
103+
class.to_owned().into(),
104+
ctx.slot_new_wrapper.clone().into(),
105+
ctx,
106+
);
107+
class.set_attr(identifier!(ctx, __new__), bound.into());
106108
}
107109

108110
if class.slots.hash.load().map_or(0, |h| h as usize) == hash_not_implemented as usize {

vm/src/exceptions.rs

+3-3
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

+1-1
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

+9-10
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub struct Context {
4545
pub int_cache_pool: Vec<PyIntRef>,
4646
// there should only be exact objects of str in here, no non-str objects and no subclasses
4747
pub(crate) string_pool: StringPool,
48-
pub(crate) slot_new_wrapper: PyObjectRef,
48+
pub(crate) slot_new_wrapper: PyRef<PyBuiltinFunction>,
4949
pub names: ConstName,
5050
}
5151

@@ -288,11 +288,9 @@ 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,
294-
)
295-
.into();
293+
);
296294

297295
let empty_str = unsafe { string_pool.intern("", types.str_type.to_owned()) }.to_owned();
298296
let empty_bytes = create_object(PyBytes::from(Vec::new()), types.bytes_type);
@@ -491,11 +489,11 @@ impl Context {
491489
}
492490

493491
#[inline]
494-
pub fn make_func_def<F, FKind>(&self, name: impl Into<PyStr>, f: F) -> PyNativeFuncDef
492+
pub fn make_func_def<F, FKind>(&self, name: &'static PyStrInterned, f: F) -> PyNativeFuncDef
495493
where
496494
F: IntoPyNativeFunc<FKind>,
497495
{
498-
PyNativeFuncDef::new(f.into_func(), PyStr::new_ref(name, self))
496+
PyNativeFuncDef::new(f.into_func(), name)
499497
}
500498

501499
#[inline]
@@ -531,16 +529,17 @@ impl Context {
531529
}
532530

533531
// #[deprecated]
534-
pub fn new_function<F, FKind>(&self, name: impl Into<PyStr>, f: F) -> PyRef<PyBuiltinFunction>
532+
pub fn new_function<F, FKind>(&self, name: &str, f: F) -> PyRef<PyBuiltinFunction>
535533
where
536534
F: IntoPyNativeFunc<FKind>,
537535
{
538-
self.make_func_def(name, f).build_function(self)
536+
self.make_func_def(self.intern_str(name), f)
537+
.build_function(self)
539538
}
540539

541540
pub fn new_method<F, FKind>(
542541
&self,
543-
name: impl Into<PyStr>,
542+
name: &'static PyStrInterned,
544543
class: &'static Py<PyType>,
545544
f: F,
546545
) -> PyRef<PyBuiltinMethod>

wasm/lib/src/convert.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ pub fn js_to_py(vm: &VirtualMachine, js_val: JsValue) -> PyObjectRef {
216216
let func = js_sys::Function::from(js_val);
217217
vm.ctx
218218
.new_function(
219-
String::from(func.name()),
219+
String::from(func.name()).as_str(),
220220
move |args: FuncArgs, vm: &VirtualMachine| -> PyResult {
221221
let this = Object::new();
222222
for (k, v) in args.kwargs {

wasm/lib/src/wasm_builtins.rs

+2-2
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)