X Tutup
Skip to content

Commit fc1c278

Browse files
authored
Remove unnecessary to_{owned,string}() calls (RustPython#7367)
1 parent a27d812 commit fc1c278

Some content is hidden

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

71 files changed

+411
-552
lines changed

crates/stdlib/src/_asyncio.rs

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ pub(crate) mod _asyncio {
151151
fn init(zelf: PyRef<Self>, args: Self::Args, vm: &VirtualMachine) -> PyResult<()> {
152152
// Future does not accept positional arguments
153153
if !args.args.is_empty() {
154-
return Err(vm.new_type_error("Future() takes no positional arguments".to_string()));
154+
return Err(vm.new_type_error("Future() takes no positional arguments"));
155155
}
156156
// Extract only 'loop' keyword argument
157157
let loop_ = args.kwargs.get("loop").cloned();
@@ -265,7 +265,7 @@ pub(crate) mod _asyncio {
265265
#[pymethod]
266266
fn set_result(zelf: PyRef<Self>, result: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
267267
if zelf.fut_loop.read().is_none() {
268-
return Err(vm.new_runtime_error("Future object is not initialized.".to_string()));
268+
return Err(vm.new_runtime_error("Future object is not initialized."));
269269
}
270270
if zelf.fut_state.load() != FutureState::Pending {
271271
return Err(new_invalid_state_error(vm, "invalid state"));
@@ -283,7 +283,7 @@ pub(crate) mod _asyncio {
283283
vm: &VirtualMachine,
284284
) -> PyResult<()> {
285285
if zelf.fut_loop.read().is_none() {
286-
return Err(vm.new_runtime_error("Future object is not initialized.".to_string()));
286+
return Err(vm.new_runtime_error("Future object is not initialized."));
287287
}
288288
if zelf.fut_state.load() != FutureState::Pending {
289289
return Err(new_invalid_state_error(vm, "invalid state"));
@@ -336,7 +336,7 @@ pub(crate) mod _asyncio {
336336
vm: &VirtualMachine,
337337
) -> PyResult<()> {
338338
if zelf.fut_loop.read().is_none() {
339-
return Err(vm.new_runtime_error("Future object is not initialized.".to_string()));
339+
return Err(vm.new_runtime_error("Future object is not initialized."));
340340
}
341341
let ctx = match args.context.flatten() {
342342
Some(c) => c,
@@ -364,7 +364,7 @@ pub(crate) mod _asyncio {
364364
#[pymethod]
365365
fn remove_done_callback(&self, func: PyObjectRef, vm: &VirtualMachine) -> PyResult<usize> {
366366
if self.fut_loop.read().is_none() {
367-
return Err(vm.new_runtime_error("Future object is not initialized.".to_string()));
367+
return Err(vm.new_runtime_error("Future object is not initialized."));
368368
}
369369
let mut cleared_callback0 = 0usize;
370370

@@ -461,7 +461,7 @@ pub(crate) mod _asyncio {
461461
#[pymethod]
462462
fn cancel(zelf: PyRef<Self>, args: CancelArgs, vm: &VirtualMachine) -> PyResult<bool> {
463463
if zelf.fut_loop.read().is_none() {
464-
return Err(vm.new_runtime_error("Future object is not initialized.".to_string()));
464+
return Err(vm.new_runtime_error("Future object is not initialized."));
465465
}
466466
if zelf.fut_state.load() != FutureState::Pending {
467467
// Clear log_tb even when cancel fails
@@ -598,9 +598,7 @@ pub(crate) mod _asyncio {
598598
self.fut_blocking.store(v, Ordering::Relaxed);
599599
Ok(())
600600
}
601-
PySetterValue::Delete => {
602-
Err(vm.new_attribute_error("cannot delete attribute".to_string()))
603-
}
601+
PySetterValue::Delete => Err(vm.new_attribute_error("cannot delete attribute")),
604602
}
605603
}
606604

@@ -670,16 +668,12 @@ pub(crate) mod _asyncio {
670668
match value {
671669
PySetterValue::Assign(v) => {
672670
if v {
673-
return Err(vm.new_value_error(
674-
"_log_traceback can only be set to False".to_string(),
675-
));
671+
return Err(vm.new_value_error("_log_traceback can only be set to False"));
676672
}
677673
self.fut_log_tb.store(false, Ordering::Relaxed);
678674
Ok(())
679675
}
680-
PySetterValue::Delete => {
681-
Err(vm.new_attribute_error("cannot delete attribute".to_string()))
682-
}
676+
PySetterValue::Delete => Err(vm.new_attribute_error("cannot delete attribute")),
683677
}
684678
}
685679

@@ -1055,7 +1049,7 @@ pub(crate) mod _asyncio {
10551049
// Must be a subclass of BaseException
10561050
if !exc_class.fast_issubclass(vm.ctx.exceptions.base_exception_type) {
10571051
return Err(vm.new_type_error(
1058-
"exceptions must be classes or instances deriving from BaseException, not type".to_string()
1052+
"exceptions must be classes or instances deriving from BaseException, not type"
10591053
));
10601054
}
10611055

@@ -1072,9 +1066,9 @@ pub(crate) mod _asyncio {
10721066
if let OptionalArg::Present(ref val) = exc_val
10731067
&& !vm.is_none(val)
10741068
{
1075-
return Err(vm.new_type_error(
1076-
"instance exception may not have a separate value".to_string(),
1077-
));
1069+
return Err(
1070+
vm.new_type_error("instance exception may not have a separate value")
1071+
);
10781072
}
10791073
exc_type
10801074
} else {
@@ -1339,7 +1333,7 @@ pub(crate) mod _asyncio {
13391333
vm: &VirtualMachine,
13401334
) -> PyResult<()> {
13411335
if zelf.base.fut_loop.read().is_none() {
1342-
return Err(vm.new_runtime_error("Future object is not initialized.".to_string()));
1336+
return Err(vm.new_runtime_error("Future object is not initialized."));
13431337
}
13441338
let ctx = match args.context.flatten() {
13451339
Some(c) => c,
@@ -1367,7 +1361,7 @@ pub(crate) mod _asyncio {
13671361
#[pymethod]
13681362
fn remove_done_callback(&self, func: PyObjectRef, vm: &VirtualMachine) -> PyResult<usize> {
13691363
if self.base.fut_loop.read().is_none() {
1370-
return Err(vm.new_runtime_error("Future object is not initialized.".to_string()));
1364+
return Err(vm.new_runtime_error("Future object is not initialized."));
13711365
}
13721366
let mut cleared_callback0 = 0usize;
13731367

@@ -1686,9 +1680,7 @@ pub(crate) mod _asyncio {
16861680
self.base.fut_blocking.store(v, Ordering::Relaxed);
16871681
Ok(())
16881682
}
1689-
PySetterValue::Delete => {
1690-
Err(vm.new_attribute_error("cannot delete attribute".to_string()))
1691-
}
1683+
PySetterValue::Delete => Err(vm.new_attribute_error("cannot delete attribute")),
16921684
}
16931685
}
16941686

@@ -1718,7 +1710,7 @@ pub(crate) mod _asyncio {
17181710
Ok(())
17191711
}
17201712
PySetterValue::Delete => {
1721-
Err(vm.new_attribute_error("can't delete _log_destroy_pending".to_owned()))
1713+
Err(vm.new_attribute_error("can't delete _log_destroy_pending"))
17221714
}
17231715
}
17241716
}
@@ -1737,16 +1729,12 @@ pub(crate) mod _asyncio {
17371729
match value {
17381730
PySetterValue::Assign(v) => {
17391731
if v {
1740-
return Err(vm.new_value_error(
1741-
"_log_traceback can only be set to False".to_string(),
1742-
));
1732+
return Err(vm.new_value_error("_log_traceback can only be set to False"));
17431733
}
17441734
self.base.fut_log_tb.store(false, Ordering::Relaxed);
17451735
Ok(())
17461736
}
1747-
PySetterValue::Delete => {
1748-
Err(vm.new_attribute_error("cannot delete attribute".to_string()))
1749-
}
1737+
PySetterValue::Delete => Err(vm.new_attribute_error("cannot delete attribute")),
17501738
}
17511739
}
17521740

@@ -2532,14 +2520,10 @@ pub(crate) mod _asyncio {
25322520
let running_task = vm.asyncio_running_task.borrow();
25332521
match running_task.as_ref() {
25342522
None => {
2535-
return Err(vm.new_runtime_error(
2536-
"_leave_task: task is not the current task".to_owned(),
2537-
));
2523+
return Err(vm.new_runtime_error("_leave_task: task is not the current task"));
25382524
}
25392525
Some(current) if !current.is(&task) => {
2540-
return Err(vm.new_runtime_error(
2541-
"_leave_task: task is not the current task".to_owned(),
2542-
));
2526+
return Err(vm.new_runtime_error("_leave_task: task is not the current task"));
25432527
}
25442528
_ => {}
25452529
}
@@ -2777,7 +2761,7 @@ pub(crate) mod _asyncio {
27772761
.ok_or_else(|| vm.new_attribute_error("CancelledError not found"))?;
27782762
exc_type
27792763
.downcast()
2780-
.map_err(|_| vm.new_type_error("CancelledError is not a type".to_string()))
2764+
.map_err(|_| vm.new_type_error("CancelledError is not a type"))
27812765
}
27822766

27832767
fn is_cancelled_error(exc: &PyBaseExceptionRef, vm: &VirtualMachine) -> bool {

crates/stdlib/src/_remote_debugging.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ mod _remote_debugging {
9898
type Args = FuncArgs;
9999

100100
fn py_new(_cls: &Py<PyType>, _args: Self::Args, vm: &VirtualMachine) -> PyResult<Self> {
101-
Err(vm.new_not_implemented_error("_remote_debugging is not available".to_owned()))
101+
Err(vm.new_not_implemented_error("_remote_debugging is not available"))
102102
}
103103
}
104104

crates/stdlib/src/_sqlite3.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1512,9 +1512,9 @@ mod _sqlite3 {
15121512
let _ = unsafe { self.isolation_level.swap(value) };
15131513
Ok(())
15141514
}
1515-
PySetterValue::Delete => Err(vm.new_attribute_error(
1516-
"'isolation_level' attribute cannot be deleted".to_owned(),
1517-
)),
1515+
PySetterValue::Delete => {
1516+
Err(vm.new_attribute_error("'isolation_level' attribute cannot be deleted"))
1517+
}
15181518
}
15191519
}
15201520

crates/stdlib/src/faulthandler.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ mod decl {
463463

464464
// Install signal handlers
465465
if !faulthandler_enable_internal() {
466-
return Err(vm.new_runtime_error("Failed to enable faulthandler".to_owned()));
466+
return Err(vm.new_runtime_error("Failed to enable faulthandler"));
467467
}
468468

469469
Ok(())
@@ -802,19 +802,15 @@ mod decl {
802802
// Check if it's an integer (file descriptor)
803803
if let Ok(fd) = f.try_to_value::<i32>(vm) {
804804
if fd < 0 {
805-
return Err(
806-
vm.new_value_error("file is not a valid file descriptor".to_owned())
807-
);
805+
return Err(vm.new_value_error("file is not a valid file descriptor"));
808806
}
809807
return Ok(fd);
810808
}
811809
// Try to get fileno() from file object
812810
let fileno = vm.call_method(&f, "fileno", ())?;
813811
let fd: i32 = fileno.try_to_value(vm)?;
814812
if fd < 0 {
815-
return Err(
816-
vm.new_value_error("file is not a valid file descriptor".to_owned())
817-
);
813+
return Err(vm.new_value_error("file is not a valid file descriptor"));
818814
}
819815
// Try to flush the file
820816
let _ = vm.call_method(&f, "flush", ());
@@ -824,7 +820,7 @@ mod decl {
824820
// file=None or file not passed: fall back to sys.stderr
825821
let stderr = vm.sys_module.get_attr("stderr", vm)?;
826822
if vm.is_none(&stderr) {
827-
return Err(vm.new_runtime_error("sys.stderr is None".to_owned()));
823+
return Err(vm.new_runtime_error("sys.stderr is None"));
828824
}
829825
let fileno = vm.call_method(&stderr, "fileno", ())?;
830826
let fd: i32 = fileno.try_to_value(vm)?;
@@ -912,15 +908,15 @@ mod decl {
912908
let timeout: f64 = args.timeout.into_float();
913909

914910
if timeout <= 0.0 {
915-
return Err(vm.new_value_error("timeout must be greater than 0".to_owned()));
911+
return Err(vm.new_value_error("timeout must be greater than 0"));
916912
}
917913

918914
let fd = get_fd_from_file_opt(args.file, vm)?;
919915

920916
// Convert timeout to microseconds
921917
let timeout_us = (timeout * 1_000_000.0) as u64;
922918
if timeout_us == 0 {
923-
return Err(vm.new_value_error("timeout must be greater than 0".to_owned()));
919+
return Err(vm.new_value_error("timeout must be greater than 0"));
924920
}
925921

926922
let header = format_timeout(timeout_us);
@@ -1098,7 +1094,7 @@ mod decl {
10981094

10991095
// Check if signal is in valid range
11001096
if !(1..64).contains(&signum) {
1101-
return Err(vm.new_value_error("signal number out of range".to_owned()));
1097+
return Err(vm.new_value_error("signal number out of range"));
11021098
}
11031099

11041100
Ok(())

crates/stdlib/src/hashlib.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,7 @@ pub mod _hashlib {
210210
(Some(_), Some(_)) => Err(vm.new_type_error(
211211
"'data' and 'string' are mutually exclusive \
212212
and support for 'string' keyword parameter \
213-
is slated for removal in a future version."
214-
.to_owned(),
213+
is slated for removal in a future version.",
215214
)),
216215
}
217216
}
@@ -306,7 +305,7 @@ pub mod _hashlib {
306305
impl PyHmac {
307306
#[pyslot]
308307
fn slot_new(_cls: PyTypeRef, _args: FuncArgs, vm: &VirtualMachine) -> PyResult {
309-
Err(vm.new_type_error("cannot create '_hashlib.HMAC' instances".to_owned()))
308+
Err(vm.new_type_error("cannot create '_hashlib.HMAC' instances"))
310309
}
311310

312311
#[pygetset]
@@ -758,9 +757,10 @@ pub mod _hashlib {
758757

759758
#[pyfunction]
760759
fn hmac_new(args: NewHMACHashArgs, vm: &VirtualMachine) -> PyResult<PyHmac> {
761-
let digestmod = args.digestmod.into_option().ok_or_else(|| {
762-
vm.new_type_error("Missing required parameter 'digestmod'.".to_owned())
763-
})?;
760+
let digestmod = args
761+
.digestmod
762+
.into_option()
763+
.ok_or_else(|| vm.new_type_error("Missing required parameter 'digestmod'."))?;
764764
let name = resolve_digestmod(&digestmod, vm)?;
765765

766766
let key_buf = args.key.borrow_buf();
@@ -833,10 +833,10 @@ pub mod _hashlib {
833833
let name = args.hash_name.as_str().to_lowercase();
834834

835835
if args.iterations < 1 {
836-
return Err(vm.new_value_error("iteration value must be greater than 0.".to_owned()));
836+
return Err(vm.new_value_error("iteration value must be greater than 0."));
837837
}
838838
let rounds = u32::try_from(args.iterations)
839-
.map_err(|_| vm.new_overflow_error("iteration value is too great.".to_owned()))?;
839+
.map_err(|_| vm.new_overflow_error("iteration value is too great."))?;
840840

841841
let dklen: usize = match args.dklen.into_option() {
842842
Some(obj) if vm.is_none(&obj) => {
@@ -845,10 +845,10 @@ pub mod _hashlib {
845845
Some(obj) => {
846846
let len: i64 = obj.try_into_value(vm)?;
847847
if len < 1 {
848-
return Err(vm.new_value_error("key length must be greater than 0.".to_owned()));
848+
return Err(vm.new_value_error("key length must be greater than 0."));
849849
}
850850
usize::try_from(len)
851-
.map_err(|_| vm.new_overflow_error("key length is too great.".to_owned()))?
851+
.map_err(|_| vm.new_overflow_error("key length is too great."))?
852852
}
853853
None => hash_digest_size(&name).ok_or_else(|| unsupported_hash(&name, vm))?,
854854
};

crates/stdlib/src/math.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,13 @@ mod math {
102102
)));
103103
}
104104
if b == 1.0 {
105-
return Err(vm.new_value_error("math domain error".to_owned()));
105+
return Err(vm.new_value_error("math domain error"));
106106
}
107107
}
108108
// Handle BigInt specially for large values (only for actual int type, not float)
109109
if let Some(i) = x.downcast_ref::<PyInt>() {
110110
return pymath::math::log_bigint(i.as_bigint(), base).map_err(|err| match err {
111-
pymath::Error::EDOM => vm.new_value_error("expected a positive input".to_owned()),
111+
pymath::Error::EDOM => vm.new_value_error("expected a positive input"),
112112
_ => pymath_exception(err, vm),
113113
});
114114
}
@@ -132,7 +132,7 @@ mod math {
132132
// Handle BigInt specially for large values (only for actual int type, not float)
133133
if let Some(i) = x.downcast_ref::<PyInt>() {
134134
return pymath::math::log2_bigint(i.as_bigint()).map_err(|err| match err {
135-
pymath::Error::EDOM => vm.new_value_error("expected a positive input".to_owned()),
135+
pymath::Error::EDOM => vm.new_value_error("expected a positive input"),
136136
_ => pymath_exception(err, vm),
137137
});
138138
}
@@ -151,7 +151,7 @@ mod math {
151151
// Handle BigInt specially for large values (only for actual int type, not float)
152152
if let Some(i) = x.downcast_ref::<PyInt>() {
153153
return pymath::math::log10_bigint(i.as_bigint()).map_err(|err| match err {
154-
pymath::Error::EDOM => vm.new_value_error("expected a positive input".to_owned()),
154+
pymath::Error::EDOM => vm.new_value_error("expected a positive input"),
155155
_ => pymath_exception(err, vm),
156156
});
157157
}

crates/stdlib/src/mmap.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -557,13 +557,11 @@ mod mmap {
557557
// Parse tagname: None or a string
558558
let tag_str: Option<String> = match tagname {
559559
Some(ref obj) if !vm.is_none(obj) => {
560-
let s = obj.try_to_value::<String>(vm).map_err(|_| {
561-
vm.new_type_error("tagname must be a string or None".to_owned())
562-
})?;
560+
let s = obj
561+
.try_to_value::<String>(vm)
562+
.map_err(|_| vm.new_type_error("tagname must be a string or None"))?;
563563
if s.contains('\0') {
564-
return Err(vm.new_value_error(
565-
"tagname must not contain null characters".to_owned(),
566-
));
564+
return Err(vm.new_value_error("tagname must not contain null characters"));
567565
}
568566
Some(s)
569567
}

0 commit comments

Comments
 (0)
X Tutup