Skip to content

Commit e253095

Browse files
committed
new_str allows &str and &String
1 parent eba638e commit e253095

File tree

20 files changed

+74
-97
lines changed

20 files changed

+74
-97
lines changed

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ fn run_module(vm: &VirtualMachine, module: &str) -> PyResult<()> {
409409
debug!("Running module {}", module);
410410
let runpy = vm.import("runpy", &[], 0)?;
411411
let run_module_as_main = vm.get_attribute(runpy, "_run_module_as_main")?;
412-
vm.invoke(&run_module_as_main, vec![vm.ctx.new_str(module.to_owned())])?;
412+
vm.invoke(&run_module_as_main, vec![vm.ctx.new_str(module)])?;
413413
Ok(())
414414
}
415415

vm/src/builtins.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,7 @@ mod decl {
784784
.split('.')
785785
.next_back()
786786
.unwrap();
787-
let name_obj = vm.ctx.new_str(name.to_owned());
787+
let name_obj = vm.ctx.new_str(name);
788788

789789
let mut metaclass = if let Some(metaclass) = kwargs.pop_kwarg("metaclass") {
790790
PyClassRef::try_from_object(vm, metaclass)?

vm/src/dictdatatype.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -492,12 +492,12 @@ mod tests {
492492
assert_eq!(0, dict.len());
493493

494494
let key1 = vm.ctx.new_bool(true);
495-
let value1 = vm.ctx.new_str("abc".to_owned());
495+
let value1 = vm.ctx.new_str("abc");
496496
dict.insert(&vm, key1.clone(), value1.clone()).unwrap();
497497
assert_eq!(1, dict.len());
498498

499-
let key2 = vm.ctx.new_str("x".to_owned());
500-
let value2 = vm.ctx.new_str("def".to_owned());
499+
let key2 = vm.ctx.new_str("x");
500+
let value2 = vm.ctx.new_str("def");
501501
dict.insert(&vm, key2.clone(), value2.clone()).unwrap();
502502
assert_eq!(2, dict.len());
503503

vm/src/frame.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,7 +1291,7 @@ impl ExecutingFrame<'_> {
12911291
.split('.')
12921292
.next_back()
12931293
.unwrap();
1294-
vm.set_attr(&func_obj, "__name__", vm.ctx.new_str(name.to_owned()))?;
1294+
vm.set_attr(&func_obj, "__name__", vm.ctx.new_str(name))?;
12951295
vm.set_attr(&func_obj, "__qualname__", qualified_name)?;
12961296
let module = self
12971297
.scope
@@ -1439,13 +1439,13 @@ impl ExecutingFrame<'_> {
14391439
fn store_attr(&mut self, vm: &VirtualMachine, attr_name: &str) -> FrameResult {
14401440
let parent = self.pop_value();
14411441
let value = self.pop_value();
1442-
vm.set_attr(&parent, vm.ctx.new_str(attr_name.to_owned()), value)?;
1442+
vm.set_attr(&parent, vm.ctx.new_str(attr_name), value)?;
14431443
Ok(None)
14441444
}
14451445

14461446
fn delete_attr(&mut self, vm: &VirtualMachine, attr_name: &str) -> FrameResult {
14471447
let parent = self.pop_value();
1448-
let name = vm.ctx.new_str(attr_name.to_owned());
1448+
let name = vm.ctx.new_str(attr_name);
14491449
vm.del_attr(&parent, name)?;
14501450
Ok(None)
14511451
}

vm/src/import.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,9 @@ pub fn import_codeobj(
106106
set_file_attr: bool,
107107
) -> PyResult {
108108
let attrs = vm.ctx.new_dict();
109-
attrs.set_item("__name__", vm.ctx.new_str(module_name.to_owned()), vm)?;
109+
attrs.set_item("__name__", vm.ctx.new_str(module_name), vm)?;
110110
if set_file_attr {
111-
attrs.set_item(
112-
"__file__",
113-
vm.ctx.new_str(code_obj.source_path.to_owned()),
114-
vm,
115-
)?;
111+
attrs.set_item("__file__", vm.ctx.new_str(&code_obj.source_path), vm)?;
116112
}
117113
let module = vm.new_module(module_name, attrs.clone());
118114

vm/src/obj/objcode.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,7 @@ impl PyCodeRef {
108108

109109
#[pyproperty]
110110
fn co_varnames(self, vm: &VirtualMachine) -> PyObjectRef {
111-
let varnames = self
112-
.code
113-
.varnames()
114-
.map(|s| vm.ctx.new_str(s.to_owned()))
115-
.collect();
111+
let varnames = self.code.varnames().map(|s| vm.ctx.new_str(s)).collect();
116112
vm.ctx.new_tuple(varnames)
117113
}
118114
}

vm/src/obj/objdict.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ impl PyDictRef {
392392
if let Some((key, value)) = self.entries.pop_front() {
393393
Ok(vm.ctx.new_tuple(vec![key, value]))
394394
} else {
395-
let err_msg = vm.ctx.new_str("popitem(): dictionary is empty".to_owned());
395+
let err_msg = vm.ctx.new_str("popitem(): dictionary is empty");
396396
Err(vm.new_key_error(err_msg))
397397
}
398398
}

vm/src/obj/objset.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ impl PySetInner {
281281
if let Some((key, _)) = self.content.pop_front() {
282282
Ok(key)
283283
} else {
284-
let err_msg = vm.ctx.new_str("pop from an empty set".to_owned());
284+
let err_msg = vm.ctx.new_str("pop from an empty set");
285285
Err(vm.new_key_error(err_msg))
286286
}
287287
}

vm/src/obj/objstr.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impl AsRef<str> for PyString {
6666

6767
impl<T> From<&T> for PyString
6868
where
69-
T: AsRef<T> + ?Sized,
69+
T: AsRef<str> + ?Sized,
7070
{
7171
fn from(s: &T) -> PyString {
7272
s.as_ref().to_owned().into()
@@ -745,10 +745,8 @@ impl PyString {
745745

746746
#[pymethod]
747747
fn splitlines(&self, args: pystr::SplitLinesArgs, vm: &VirtualMachine) -> PyObjectRef {
748-
vm.ctx.new_list(
749-
self.value
750-
.py_splitlines(args, |s| vm.ctx.new_str(s.to_owned())),
751-
)
748+
vm.ctx
749+
.new_list(self.value.py_splitlines(args, |s| vm.ctx.new_str(s)))
752750
}
753751

754752
#[pymethod]
@@ -1124,7 +1122,7 @@ impl IntoPyObject for String {
11241122

11251123
impl IntoPyObject for &str {
11261124
fn into_pyobject(self, vm: &VirtualMachine) -> PyObjectRef {
1127-
vm.ctx.new_str(self.to_owned())
1125+
vm.ctx.new_str(self)
11281126
}
11291127
}
11301128

@@ -1308,13 +1306,9 @@ mod tests {
13081306
let vm: VirtualMachine = Default::default();
13091307

13101308
let table = vm.context().new_dict();
1311-
table
1312-
.set_item("a", vm.ctx.new_str("🎅".to_owned()), &vm)
1313-
.unwrap();
1309+
table.set_item("a", vm.ctx.new_str("🎅"), &vm).unwrap();
13141310
table.set_item("b", vm.get_none(), &vm).unwrap();
1315-
table
1316-
.set_item("c", vm.ctx.new_str("xda".to_owned()), &vm)
1317-
.unwrap();
1311+
table.set_item("c", vm.ctx.new_str("xda"), &vm).unwrap();
13181312
let translated = PyString::maketrans(
13191313
table.into_object(),
13201314
OptionalArg::Missing,

vm/src/obj/objsuper.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,6 @@ pub fn init(context: &PyContext) {
200200
super().cmeth(arg)\n";
201201

202202
extend_class!(context, super_type, {
203-
"__doc__" => context.new_str(super_doc.to_owned()),
203+
"__doc__" => context.new_str(super_doc),
204204
});
205205
}

0 commit comments

Comments
 (0)