mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-02 19:39:49 +09:00
use interned str for builtin function names
This commit is contained in:
@@ -629,7 +629,7 @@ where
|
||||
quote_spanned! { ident.span() =>
|
||||
class.set_attr(
|
||||
ctx.names.#name_ident,
|
||||
ctx.make_func_def(#py_name, Self::#ident)
|
||||
ctx.make_func_def(ctx.intern_str(#py_name), Self::#ident)
|
||||
#doc
|
||||
#build_func
|
||||
.into(),
|
||||
@@ -639,7 +639,7 @@ where
|
||||
quote_spanned! { ident.span() =>
|
||||
class.set_str_attr(
|
||||
#py_name,
|
||||
ctx.make_func_def(#py_name, Self::#ident)
|
||||
ctx.make_func_def(ctx.intern_str(#py_name), Self::#ident)
|
||||
#doc
|
||||
#build_func,
|
||||
ctx,
|
||||
|
||||
@@ -334,7 +334,7 @@ impl ModuleItem for FunctionItem {
|
||||
};
|
||||
let doc = quote!(.with_doc(#doc.to_owned(), &vm.ctx));
|
||||
let new_func = quote_spanned!(ident.span()=>
|
||||
vm.ctx.make_func_def(#py_name, #ident)
|
||||
vm.ctx.make_func_def(vm.ctx.intern_str(#py_name), #ident)
|
||||
#doc
|
||||
.into_function()
|
||||
.with_module(vm.new_pyobj(#module.to_owned()))
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use super::{type_, PyClassMethod, PyStaticMethod, PyStr, PyStrRef, PyType};
|
||||
use super::{type_, PyClassMethod, PyStaticMethod, PyStr, PyStrInterned, PyStrRef, PyType};
|
||||
use crate::{
|
||||
builtins::PyBoundMethod,
|
||||
class::PyClassImpl,
|
||||
@@ -10,12 +10,12 @@ use std::fmt;
|
||||
|
||||
pub struct PyNativeFuncDef {
|
||||
pub func: PyNativeFunc,
|
||||
pub name: PyStrRef,
|
||||
pub name: &'static PyStrInterned,
|
||||
pub doc: Option<PyStrRef>,
|
||||
}
|
||||
|
||||
impl PyNativeFuncDef {
|
||||
pub fn new(func: PyNativeFunc, name: PyStrRef) -> Self {
|
||||
pub fn new(func: PyNativeFunc, name: &'static PyStrInterned) -> Self {
|
||||
Self {
|
||||
func,
|
||||
name,
|
||||
@@ -122,7 +122,7 @@ impl PyBuiltinFunction {
|
||||
}
|
||||
#[pygetset(magic)]
|
||||
fn name(&self) -> PyStrRef {
|
||||
self.value.name.clone()
|
||||
self.value.name.to_owned()
|
||||
}
|
||||
#[pygetset(magic)]
|
||||
fn qualname(&self) -> PyStrRef {
|
||||
@@ -217,7 +217,7 @@ impl Callable for PyBuiltinMethod {
|
||||
|
||||
impl PyBuiltinMethod {
|
||||
pub fn new_ref<F, FKind>(
|
||||
name: impl Into<PyStr>,
|
||||
name: &'static PyStrInterned,
|
||||
class: &'static Py<PyType>,
|
||||
f: F,
|
||||
ctx: &Context,
|
||||
@@ -236,7 +236,7 @@ impl PyBuiltinMethod {
|
||||
impl PyBuiltinMethod {
|
||||
#[pygetset(magic)]
|
||||
fn name(&self) -> PyStrRef {
|
||||
self.value.name.clone()
|
||||
self.value.name.to_owned()
|
||||
}
|
||||
#[pygetset(magic)]
|
||||
fn qualname(&self) -> String {
|
||||
@@ -260,7 +260,7 @@ impl PyBuiltinMethod {
|
||||
) -> (Option<PyObjectRef>, (Option<PyObjectRef>, PyStrRef)) {
|
||||
let builtins_getattr = vm.builtins.get_attr("getattr", vm).ok();
|
||||
let classname = vm.builtins.get_attr(&self.class.__name__(vm), vm).ok();
|
||||
(builtins_getattr, (classname, self.value.name.clone()))
|
||||
(builtins_getattr, (classname, self.value.name.to_owned()))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use super::{PyStr, PyType, PyTypeRef};
|
||||
use super::{PyStr, PyStrInterned, PyType, PyTypeRef};
|
||||
use crate::{
|
||||
builtins::builtin_func::PyBuiltinMethod,
|
||||
class::PyClassImpl,
|
||||
@@ -75,7 +75,7 @@ impl PyStaticMethod {
|
||||
|
||||
impl PyStaticMethod {
|
||||
pub fn new_builtin_ref<F, FKind>(
|
||||
name: impl Into<PyStr>,
|
||||
name: &'static PyStrInterned,
|
||||
class: &'static Py<PyType>,
|
||||
f: F,
|
||||
ctx: &Context,
|
||||
|
||||
@@ -753,7 +753,7 @@ impl ExceptionZoo {
|
||||
extend_exception!(PyLookupError, ctx, excs.lookup_error);
|
||||
extend_exception!(PyIndexError, ctx, excs.index_error);
|
||||
extend_exception!(PyKeyError, ctx, excs.key_error, {
|
||||
"__str__" => ctx.new_method("__str__", excs.key_error, key_error_str),
|
||||
"__str__" => ctx.new_method(identifier!(ctx, __str__), excs.key_error, key_error_str),
|
||||
});
|
||||
|
||||
extend_exception!(PyMemoryError, ctx, excs.memory_error);
|
||||
@@ -786,8 +786,8 @@ impl ExceptionZoo {
|
||||
"filename" => ctx.none(),
|
||||
// second exception filename
|
||||
"filename2" => ctx.none(),
|
||||
"__str__" => ctx.new_method("__str__", excs.os_error, os_error_str),
|
||||
"__reduce__" => ctx.new_method("__reduce__", excs.os_error, os_error_reduce),
|
||||
"__str__" => ctx.new_method(identifier!(ctx, __str__), excs.os_error, os_error_str),
|
||||
"__reduce__" => ctx.new_method(identifier!(ctx, __reduce__), excs.os_error, os_error_reduce),
|
||||
});
|
||||
// TODO: this isn't really accurate
|
||||
#[cfg(windows)]
|
||||
|
||||
@@ -219,7 +219,7 @@ macro_rules! named_function {
|
||||
let ctx: &$crate::Context = &$ctx;
|
||||
$crate::__exports::paste::expr! {
|
||||
ctx.make_func_def(
|
||||
stringify!($func),
|
||||
ctx.intern_str(stringify!($func)),
|
||||
[<$module _ $func>],
|
||||
)
|
||||
.into_function()
|
||||
|
||||
@@ -288,8 +288,7 @@ impl Context {
|
||||
let names = unsafe { ConstName::new(&string_pool, &types.str_type.to_owned()) };
|
||||
|
||||
let slot_new_wrapper = create_object(
|
||||
PyNativeFuncDef::new(PyType::__new__.into_func(), names.__new__.to_owned())
|
||||
.into_function(),
|
||||
PyNativeFuncDef::new(PyType::__new__.into_func(), names.__new__).into_function(),
|
||||
types.builtin_function_or_method_type,
|
||||
)
|
||||
.into();
|
||||
@@ -491,11 +490,11 @@ impl Context {
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn make_func_def<F, FKind>(&self, name: impl Into<PyStr>, f: F) -> PyNativeFuncDef
|
||||
pub fn make_func_def<F, FKind>(&self, name: &'static PyStrInterned, f: F) -> PyNativeFuncDef
|
||||
where
|
||||
F: IntoPyNativeFunc<FKind>,
|
||||
{
|
||||
PyNativeFuncDef::new(f.into_func(), PyStr::new_ref(name, self))
|
||||
PyNativeFuncDef::new(f.into_func(), name)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
@@ -531,16 +530,17 @@ impl Context {
|
||||
}
|
||||
|
||||
// #[deprecated]
|
||||
pub fn new_function<F, FKind>(&self, name: impl Into<PyStr>, f: F) -> PyRef<PyBuiltinFunction>
|
||||
pub fn new_function<F, FKind>(&self, name: &str, f: F) -> PyRef<PyBuiltinFunction>
|
||||
where
|
||||
F: IntoPyNativeFunc<FKind>,
|
||||
{
|
||||
self.make_func_def(name, f).build_function(self)
|
||||
self.make_func_def(self.intern_str(name), f)
|
||||
.build_function(self)
|
||||
}
|
||||
|
||||
pub fn new_method<F, FKind>(
|
||||
&self,
|
||||
name: impl Into<PyStr>,
|
||||
name: &'static PyStrInterned,
|
||||
class: &'static Py<PyType>,
|
||||
f: F,
|
||||
) -> PyRef<PyBuiltinMethod>
|
||||
|
||||
@@ -30,13 +30,13 @@ pub fn make_stdout_object(
|
||||
{}
|
||||
));
|
||||
let write_method = ctx.new_method(
|
||||
"write",
|
||||
ctx.intern_str("write"),
|
||||
cls,
|
||||
move |_self: PyObjectRef, data: PyStrRef, vm: &VirtualMachine| -> PyResult<()> {
|
||||
write_f(data.as_str(), vm)
|
||||
},
|
||||
);
|
||||
let flush_method = ctx.new_method("flush", cls, |_self: PyObjectRef| {});
|
||||
let flush_method = ctx.new_method(ctx.intern_str("flush"), cls, |_self: PyObjectRef| {});
|
||||
extend_class!(ctx, cls, {
|
||||
"write" => write_method,
|
||||
"flush" => flush_method,
|
||||
|
||||
Reference in New Issue
Block a user