Skip to content

Commit 4416b2a

Browse files
committed
Refactor PyNumber methods based on review suggestions
Signed-off-by: snowapril <sinjihng@gmail.com>
1 parent c69ff17 commit 4416b2a

File tree

1 file changed

+63
-63
lines changed

1 file changed

+63
-63
lines changed

vm/src/protocol/number.rs

Lines changed: 63 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -461,30 +461,30 @@ impl PyNumber<'_> {
461461
let ret = f(self, vm)?;
462462

463463
if let Some(ret) = ret.downcast_ref_if_exact::<PyInt>(vm) {
464-
Ok(vm.ctx.new_pyref(ret.as_bigint().to_owned()))
465-
} else {
466-
let ret_class = ret.clone().class().to_owned();
467-
if let Some(ret) = ret.payload_if_subclass::<PyInt>(vm) {
468-
warnings::warn(
469-
vm.ctx.exceptions.deprecation_warning,
470-
format!(
471-
"__int__ returned non-int (type {}). \
464+
return Ok(ret.to_owned());
465+
}
466+
467+
let ret_class = ret.class().to_owned();
468+
if let Some(ret) = ret.downcast_ref::<PyInt>() {
469+
warnings::warn(
470+
vm.ctx.exceptions.deprecation_warning,
471+
format!(
472+
"__int__ returned non-int (type {}). \
472473
The ability to return an instance of a strict subclass of int \
473474
is deprecated, and may be removed in a future version of Python.",
474-
ret_class
475-
),
476-
1,
477-
vm,
478-
)?;
479-
480-
Ok(vm.ctx.new_pyref(ret.as_bigint().to_owned()))
481-
} else {
482-
Err(vm.new_type_error(format!(
483-
"{}.__int__ returned non-int(type {})",
484-
self.class(),
485475
ret_class
486-
)))
487-
}
476+
),
477+
1,
478+
vm,
479+
)?;
480+
481+
Ok(ret.to_owned())
482+
} else {
483+
Err(vm.new_type_error(format!(
484+
"{}.__int__ returned non-int(type {})",
485+
self.class(),
486+
ret_class
487+
)))
488488
}
489489
})
490490
}
@@ -495,30 +495,30 @@ impl PyNumber<'_> {
495495
let ret = f(self, vm)?;
496496

497497
if let Some(ret) = ret.downcast_ref_if_exact::<PyInt>(vm) {
498-
Ok(vm.ctx.new_pyref(ret.as_bigint().to_owned()))
499-
} else {
500-
let ret_class = ret.clone().class().to_owned();
501-
if let Some(ret) = ret.payload_if_subclass::<PyInt>(vm) {
502-
warnings::warn(
503-
vm.ctx.exceptions.deprecation_warning,
504-
format!(
505-
"__index__ returned non-int (type {}). \
498+
return Ok(ret.to_owned());
499+
}
500+
501+
let ret_class = ret.class().to_owned();
502+
if let Some(ret) = ret.downcast_ref::<PyInt>() {
503+
warnings::warn(
504+
vm.ctx.exceptions.deprecation_warning,
505+
format!(
506+
"__index__ returned non-int (type {}). \
506507
The ability to return an instance of a strict subclass of int \
507508
is deprecated, and may be removed in a future version of Python.",
508-
ret_class
509-
),
510-
1,
511-
vm,
512-
)?;
513-
514-
Ok(vm.ctx.new_pyref(ret.as_bigint().to_owned()))
515-
} else {
516-
Err(vm.new_type_error(format!(
517-
"{}.__index__ returned non-int(type {})",
518-
self.class(),
519509
ret_class
520-
)))
521-
}
510+
),
511+
1,
512+
vm,
513+
)?;
514+
515+
Ok(ret.to_owned())
516+
} else {
517+
Err(vm.new_type_error(format!(
518+
"{}.__index__ returned non-int(type {})",
519+
self.class(),
520+
ret_class
521+
)))
522522
}
523523
})
524524
}
@@ -529,30 +529,30 @@ impl PyNumber<'_> {
529529
let ret = f(self, vm)?;
530530

531531
if let Some(ret) = ret.downcast_ref_if_exact::<PyFloat>(vm) {
532-
Ok(vm.ctx.new_pyref(ret.to_f64()))
533-
} else {
534-
let ret_class = ret.clone().class().to_owned();
535-
if let Some(ret) = ret.payload_if_subclass::<PyFloat>(vm) {
536-
warnings::warn(
537-
vm.ctx.exceptions.deprecation_warning,
538-
format!(
539-
"__float__ returned non-float (type {}). \
532+
return Ok(ret.to_owned());
533+
}
534+
535+
let ret_class = ret.class().to_owned();
536+
if let Some(ret) = ret.downcast_ref::<PyFloat>() {
537+
warnings::warn(
538+
vm.ctx.exceptions.deprecation_warning,
539+
format!(
540+
"__float__ returned non-float (type {}). \
540541
The ability to return an instance of a strict subclass of float \
541542
is deprecated, and may be removed in a future version of Python.",
542-
ret_class
543-
),
544-
1,
545-
vm,
546-
)?;
547-
548-
Ok(vm.ctx.new_pyref(ret.to_f64()))
549-
} else {
550-
Err(vm.new_type_error(format!(
551-
"{}.__float__ returned non-float(type {})",
552-
self.class(),
553543
ret_class
554-
)))
555-
}
544+
),
545+
1,
546+
vm,
547+
)?;
548+
549+
Ok(ret.to_owned())
550+
} else {
551+
Err(vm.new_type_error(format!(
552+
"{}.__float__ returned non-float(type {})",
553+
self.class(),
554+
ret_class
555+
)))
556556
}
557557
})
558558
}

0 commit comments

Comments
 (0)