Skip to content

Commit 5f17d28

Browse files
authored
Convert Context::empty_str to &'static PyStrInterned (RustPython#4882)
The goal of this commit is to convert Context::empty_str to &'static PyStrInterned and to fix issue RustPython#4869
1 parent d918f7e commit 5f17d28

File tree

6 files changed

+9
-9
lines changed

6 files changed

+9
-9
lines changed

stdlib/src/sqlite.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ mod _sqlite {
297297
timeout: f64,
298298
#[pyarg(any, default = "0")]
299299
detect_types: c_int,
300-
#[pyarg(any, default = "Some(vm.ctx.empty_str.clone())")]
300+
#[pyarg(any, default = "Some(vm.ctx.empty_str.to_owned())")]
301301
isolation_level: Option<PyStrRef>,
302302
#[pyarg(any, default = "true")]
303303
check_same_thread: bool,

vm/src/builtins/str.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ impl PyStr {
352352
if value == 0 && zelf.class().is(vm.ctx.types.str_type) {
353353
// Special case: when some `str` is multiplied by `0`,
354354
// returns the empty `str`.
355-
return Ok(vm.ctx.empty_str.clone());
355+
return Ok(vm.ctx.empty_str.to_owned());
356356
}
357357
if (value == 1 || zelf.is_empty()) && zelf.class().is(vm.ctx.types.str_type) {
358358
// Special case: when some `str` is multiplied by `1` or is the empty `str`,

vm/src/exceptions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ impl PyBaseException {
496496
pub(super) fn str(&self, vm: &VirtualMachine) -> PyStrRef {
497497
let str_args = vm.exception_args_as_string(self.args(), true);
498498
match str_args.into_iter().exactly_one() {
499-
Err(i) if i.len() == 0 => vm.ctx.empty_str.clone(),
499+
Err(i) if i.len() == 0 => vm.ctx.empty_str.to_owned(),
500500
Ok(s) => s,
501501
Err(i) => PyStr::from(format!("({})", i.format(", "))).into_ref(&vm.ctx),
502502
}

vm/src/frame.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1130,7 +1130,7 @@ impl ExecutingFrame<'_> {
11301130

11311131
#[cfg_attr(feature = "flame-it", flame("Frame"))]
11321132
fn import(&mut self, vm: &VirtualMachine, module: Option<&Py<PyStr>>) -> PyResult<()> {
1133-
let module = module.unwrap_or(&vm.ctx.empty_str);
1133+
let module = module.unwrap_or(vm.ctx.empty_str);
11341134
let from_list = <Option<PyTupleTyped<PyStrRef>>>::try_from_object(vm, self.pop_value())?;
11351135
let level = usize::try_from_object(vm, self.pop_value())?;
11361136

vm/src/stdlib/io.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2609,7 +2609,7 @@ mod _io {
26092609
}
26102610
}
26112611
if chunks.is_empty() {
2612-
vm.ctx.empty_str.clone()
2612+
vm.ctx.empty_str.to_owned()
26132613
} else if chunks.len() == 1 {
26142614
chunks.pop().unwrap()
26152615
} else {
@@ -2871,7 +2871,7 @@ mod _io {
28712871
} else if let Some(cur_line) = cur_line {
28722872
cur_line.slice_pystr(vm)
28732873
} else {
2874-
vm.ctx.empty_str.clone()
2874+
vm.ctx.empty_str.to_owned()
28752875
};
28762876
Ok(line)
28772877
}
@@ -3030,7 +3030,7 @@ mod _io {
30303030
append: Option<PyStrRef>,
30313031
vm: &VirtualMachine,
30323032
) -> PyStrRef {
3033-
let empty_str = || vm.ctx.empty_str.clone();
3033+
let empty_str = || vm.ctx.empty_str.to_owned();
30343034
let chars_pos = std::mem::take(&mut self.decoded_chars_used).bytes;
30353035
let decoded_chars = match std::mem::take(&mut self.decoded_chars) {
30363036
None => return append.unwrap_or_else(empty_str),

vm/src/vm/context.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub struct Context {
3535
pub none: PyRef<PyNone>,
3636
pub empty_tuple: PyTupleRef,
3737
pub empty_frozenset: PyRef<PyFrozenSet>,
38-
pub empty_str: PyRef<PyStr>,
38+
pub empty_str: &'static PyStrInterned,
3939
pub empty_bytes: PyRef<PyBytes>,
4040
pub ellipsis: PyRef<PyEllipsis>,
4141
pub not_implemented: PyRef<PyNotImplemented>,
@@ -292,7 +292,7 @@ impl Context {
292292
types.builtin_function_or_method_type,
293293
);
294294

295-
let empty_str = unsafe { string_pool.intern("", types.str_type.to_owned()) }.to_owned();
295+
let empty_str = unsafe { string_pool.intern("", types.str_type.to_owned()) };
296296
let empty_bytes = create_object(PyBytes::from(Vec::new()), types.bytes_type);
297297
let context = Context {
298298
true_value,

0 commit comments

Comments
 (0)