Skip to content

Commit fb52694

Browse files
committed
PyAttributes key is PyStrInterned
1 parent f8b1c65 commit fb52694

Some content is hidden

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

50 files changed

+1494
-898
lines changed

ast/asdl_rs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,9 +389,9 @@ def gen_classdef(self, name, fields, attrs, depth, base="AstNode"):
389389
self.emit(f"#[extend_class]", depth + 1)
390390
self.emit("fn extend_class_with_fields(ctx: &Context, class: &PyTypeRef) {", 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_str_attr("_fields", ctx.new_list(vec![{fields}]));', depth + 2)
392+
self.emit(f'class.set_attr(interned!(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_str_attr("_attributes", ctx.new_list(vec![{attrs}]));', depth + 2)
394+
self.emit(f'class.set_attr(interned!(ctx, _attributes), ctx.new_list(vec![{attrs}]).into());', depth + 2)
395395
self.emit("}", depth + 1)
396396
self.emit("}", depth)
397397

benches/execution.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ pub fn criterion_benchmark(c: &mut Criterion) {
116116
.map(|entry| {
117117
let path = entry.unwrap().path();
118118
(
119-
path.file_name().unwrap().to_str().unwrap().to_owned(),
119+
path.file_name().unwrap().to_owned().unwrap().to_owned(),
120120
std::fs::read_to_string(path).unwrap(),
121121
)
122122
})

derive/src/pyclass.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,8 @@ where
503503
#py_name,
504504
ctx.make_funcdef(#py_name, Self::#ident)
505505
#doc
506-
#build_func
506+
#build_func,
507+
ctx,
507508
);
508509
}
509510
};
@@ -611,7 +612,7 @@ where
611612
ident,
612613
py_name.clone(),
613614
quote! {
614-
class.set_str_attr(#py_name, #value);
615+
class.set_str_attr(#py_name, #value, ctx);
615616
},
616617
)
617618
} else {
@@ -736,7 +737,8 @@ impl ToTokens for GetSetNursery {
736737
::rustpython_vm::builtins::PyGetSet::new(#name.into(), class.clone())
737738
.with_get(&Self::#getter)
738739
#setter #deleter,
739-
ctx.types.getset_type.clone(), None)
740+
ctx.types.getset_type.clone(), None),
741+
ctx
740742
);
741743
}
742744
});

derive/src/pymodule.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ impl ModuleItem for ClassItem {
433433
};
434434
let class_new = quote_spanned!(ident.span() =>
435435
let new_class = <#ident as ::rustpython_vm::class::PyClassImpl>::make_class(&vm.ctx);
436-
new_class.set_str_attr("__module__", vm.new_pyobj(#module_name));
436+
new_class.set_attr(rustpython_vm::identifier!(vm, __module__), vm.new_pyobj(#module_name));
437437
);
438438
(class_name, class_new)
439439
};

src/shell/helper.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
use rustpython_vm::{
33
builtins::{PyDictRef, PyStrRef},
44
function::ArgIterable,
5-
PyResult, TryFromObject, VirtualMachine,
5+
identifier, PyResult, TryFromObject, VirtualMachine,
66
};
77

88
pub struct ShellHelper<'vm> {
@@ -81,14 +81,19 @@ impl<'vm> ShellHelper<'vm> {
8181
current = current.get_attr(attr.as_str(), self.vm).ok()?;
8282
}
8383

84-
let current_iter = str_iter_method(current, "__dir__").ok()?;
84+
let current_iter = str_iter_method(current, identifier!(self.vm, __dir__)).ok()?;
8585

8686
(last, current_iter, None)
8787
} else {
8888
// we need to get a variable based off of globals/builtins
8989

90-
let globals = str_iter_method(self.globals.clone().into(), "keys").ok()?;
91-
let builtins = str_iter_method(self.vm.builtins.clone().into(), "__dir__").ok()?;
90+
let globals =
91+
str_iter_method(self.globals.clone().into(), identifier!(self.vm, keys)).ok()?;
92+
let builtins = str_iter_method(
93+
self.vm.builtins.clone().into(),
94+
identifier!(self.vm, __dir__),
95+
)
96+
.ok()?;
9297
(first, globals, Some(builtins))
9398
};
9499
Some((word_start, iter1.chain(iter2.into_iter().flatten())))

stdlib/src/math.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ pub(crate) use math::make_module;
33
#[pymodule]
44
mod math {
55
use crate::vm::{
6-
builtins::{try_bigint_to_f64, try_f64_to_bigint, PyFloat, PyInt, PyIntRef},
6+
builtins::{try_bigint_to_f64, try_f64_to_bigint, PyFloat, PyInt, PyIntRef, PyStrInterned},
77
function::{ArgIntoFloat, ArgIterable, Either, OptionalArg, PosArgs},
8-
AsObject, PyObject, PyObjectRef, PyRef, PyResult, VirtualMachine,
8+
identifier, AsObject, PyObject, PyObjectRef, PyRef, PyResult, VirtualMachine,
99
};
1010
use num_bigint::BigInt;
1111
use num_traits::{One, Signed, Zero};
@@ -430,25 +430,29 @@ mod math {
430430
}
431431
}
432432

433-
fn try_magic_method(func_name: &str, vm: &VirtualMachine, value: &PyObject) -> PyResult {
433+
fn try_magic_method(
434+
func_name: &'static PyStrInterned,
435+
vm: &VirtualMachine,
436+
value: &PyObject,
437+
) -> PyResult {
434438
let method = vm.get_method_or_type_error(value.to_owned(), func_name, || {
435439
format!(
436440
"type '{}' doesn't define '{}' method",
437441
value.class().name(),
438-
func_name,
442+
func_name.as_str(),
439443
)
440444
})?;
441445
vm.invoke(&method, ())
442446
}
443447

444448
#[pyfunction]
445449
fn trunc(x: PyObjectRef, vm: &VirtualMachine) -> PyResult {
446-
try_magic_method("__trunc__", vm, &x)
450+
try_magic_method(identifier!(vm, __trunc__), vm, &x)
447451
}
448452

449453
#[pyfunction]
450454
fn ceil(x: PyObjectRef, vm: &VirtualMachine) -> PyResult {
451-
let result_or_err = try_magic_method("__ceil__", vm, &x);
455+
let result_or_err = try_magic_method(identifier!(vm, __ceil__), vm, &x);
452456
if result_or_err.is_err() {
453457
if let Ok(Some(v)) = x.try_to_f64(vm) {
454458
let v = try_f64_to_bigint(v.ceil(), vm)?;
@@ -460,7 +464,7 @@ mod math {
460464

461465
#[pyfunction]
462466
fn floor(x: PyObjectRef, vm: &VirtualMachine) -> PyResult {
463-
let result_or_err = try_magic_method("__floor__", vm, &x);
467+
let result_or_err = try_magic_method(identifier!(vm, __floor__), vm, &x);
464468
if result_or_err.is_err() {
465469
if let Ok(Some(v)) = x.try_to_f64(vm) {
466470
let v = try_f64_to_bigint(v.floor(), vm)?;

stdlib/src/pyexpat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ macro_rules! create_property {
2525
move |this: &PyExpatLikeXmlParser, func: PyObjectRef| *this.$element.write() = func,
2626
);
2727

28-
$attributes.insert($name.to_owned(), attr.into());
28+
$attributes.insert($ctx.intern_str($name), attr.into());
2929
};
3030
}
3131

stdlib/src/select.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,11 @@ struct Selectable {
7575
impl TryFromObject for Selectable {
7676
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
7777
let fno = obj.try_to_value(vm).or_else(|_| {
78-
let meth = vm.get_method_or_type_error(obj.clone(), "fileno", || {
79-
"select arg must be an int or object with a fileno() method".to_owned()
80-
})?;
78+
let meth = vm.get_method_or_type_error(
79+
obj.clone(),
80+
vm.ctx.interned_str("fileno").unwrap(),
81+
|| "select arg must be an int or object with a fileno() method".to_owned(),
82+
)?;
8183
vm.invoke(&meth, ())?.try_into_value(vm)
8284
})?;
8385
Ok(Selectable { obj, fno })

vm/src/builtins/bool.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use super::{PyInt, PyStrRef, PyTypeRef};
22
use crate::{
3-
class::PyClassImpl, convert::ToPyObject, function::OptionalArg, types::Constructor, AsObject,
4-
Context, PyObject, PyObjectRef, PyPayload, PyResult, TryFromBorrowedObject, VirtualMachine,
3+
class::PyClassImpl, convert::ToPyObject, function::OptionalArg, identifier, types::Constructor,
4+
AsObject, Context, PyObject, PyObjectRef, PyPayload, PyResult, TryFromBorrowedObject,
5+
VirtualMachine,
56
};
67
use num_bigint::Sign;
78
use num_traits::Zero;
@@ -32,7 +33,7 @@ impl PyObjectRef {
3233
if self.is(&vm.ctx.false_value) {
3334
return Ok(false);
3435
}
35-
let rs_bool = match vm.get_method(self.clone(), "__bool__") {
36+
let rs_bool = match vm.get_method(self.clone(), identifier!(vm, __bool__)) {
3637
Some(method_or_err) => {
3738
// If descriptor returns Error, propagate it further
3839
let method = method_or_err?;
@@ -46,7 +47,7 @@ impl PyObjectRef {
4647

4748
get_value(&bool_obj)
4849
}
49-
None => match vm.get_method(self, "__len__") {
50+
None => match vm.get_method(self, identifier!(vm, __len__)) {
5051
Some(method_or_err) => {
5152
let method = method_or_err?;
5253
let bool_obj = vm.invoke(&method, ())?;
@@ -112,9 +113,9 @@ impl PyBool {
112113
#[pymethod(magic)]
113114
fn repr(zelf: bool, vm: &VirtualMachine) -> PyStrRef {
114115
if zelf {
115-
vm.ctx.true_str
116+
vm.ctx.names.True
116117
} else {
117-
vm.ctx.false_str
118+
vm.ctx.names.False
118119
}
119120
.to_owned()
120121
}

vm/src/builtins/complex.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::{
77
PyArithmeticValue::{self, *},
88
PyComparisonValue,
99
},
10+
identifier,
1011
types::{Comparable, Constructor, Hashable, PyComparisonOp},
1112
AsObject, Context, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
1213
};
@@ -48,7 +49,7 @@ impl PyObjectRef {
4849
if let Some(complex) = self.payload_if_exact::<PyComplex>(vm) {
4950
return Ok(Some((complex.value, true)));
5051
}
51-
if let Some(method) = vm.get_method(self.clone(), "__complex__") {
52+
if let Some(method) = vm.get_method(self.clone(), identifier!(vm, __complex__)) {
5253
let result = vm.invoke(&method?, ())?;
5354
// TODO: returning strict subclasses of complex in __complex__ is deprecated
5455
return match result.payload::<PyComplex>() {

0 commit comments

Comments
 (0)