Skip to content

Commit 9135d29

Browse files
committed
Replace non-implemented type conversions in each PyNumber_Int, PyNumber_Index, PyNumber_Float
Signed-off-by: snowapril <sinjihng@gmail.com>
1 parent 96af524 commit 9135d29

File tree

1 file changed

+75
-75
lines changed

1 file changed

+75
-75
lines changed

vm/src/protocol/number.rs

Lines changed: 75 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -460,101 +460,101 @@ impl PyNumber<'_> {
460460
pub fn int(self, vm: &VirtualMachine) -> Option<PyResult<PyIntRef>> {
461461
self.class().slots.as_number.int.load().map(|f| {
462462
let ret = f(self, vm)?;
463-
let value: PyRef<PyInt> = if !ret.class().is(PyInt::class(&vm.ctx)) {
464-
if !ret.class().fast_issubclass(vm.ctx.types.int_type) {
465-
return Err(vm.new_type_error(format!(
466-
"{}.__int__ returned non-int(type {})",
467-
self.class().name(),
468-
ret.class().name()
469-
)));
470-
}
471463

472-
warnings::warn(
473-
vm.ctx.exceptions.deprecation_warning,
474-
format!(
475-
"__int__ returned non-int (type {}). \
476-
The ability to return an instance of a strict subclass of int \
477-
is deprecated, and may be removed in a future version of Python.",
478-
ret.class()
479-
),
480-
1,
481-
vm,
482-
)?;
483-
// TODO(snowapril) : modify to proper conversion method
484-
unsafe { ret.downcast_unchecked() }
464+
if let Some(ret) = ret.downcast_ref_if_exact::<PyInt>(vm) {
465+
Ok(vm.ctx.new_pyref(ret.as_bigint().to_owned()))
485466
} else {
486-
// TODO(snowapril) : modify to proper conversion method
487-
unsafe { ret.downcast_unchecked() }
488-
};
489-
Ok(value)
467+
let ret_class = ret.clone().class().to_owned();
468+
if let Some(ret) = ret.payload_if_subclass::<PyInt>(vm) {
469+
warnings::warn(
470+
vm.ctx.exceptions.deprecation_warning,
471+
format!(
472+
"__int__ returned non-int (type {}). \
473+
The ability to return an instance of a strict subclass of int \
474+
is deprecated, and may be removed in a future version of Python.",
475+
ret_class
476+
),
477+
1,
478+
vm,
479+
)?;
480+
481+
Ok(vm.ctx.new_pyref(ret.as_bigint().to_owned()))
482+
} else {
483+
Err(vm.new_type_error(format!(
484+
"{}.__int__ returned non-int(type {})",
485+
self.class(),
486+
ret_class
487+
)))
488+
}
489+
}
490490
})
491491
}
492492

493493
#[inline]
494494
pub fn index(self, vm: &VirtualMachine) -> Option<PyResult<PyIntRef>> {
495495
self.class().slots.as_number.index.load().map(|f| {
496496
let ret = f(self, vm)?;
497-
let value: PyRef<PyInt> = if !ret.class().is(PyInt::class(&vm.ctx)) {
498-
if !ret.class().fast_issubclass(vm.ctx.types.int_type) {
499-
return Err(vm.new_type_error(format!(
500-
"{}.__index__ returned non-int(type {})",
501-
self.class().name(),
502-
ret.class().name()
503-
)));
504-
}
505497

506-
warnings::warn(
507-
vm.ctx.exceptions.deprecation_warning,
508-
format!(
509-
"__index__ returned non-int (type {}). \
510-
The ability to return an instance of a strict subclass of int \
511-
is deprecated, and may be removed in a future version of Python.",
512-
ret.class()
513-
),
514-
1,
515-
vm,
516-
)?;
517-
// TODO(snowapril) : modify to proper conversion method
518-
unsafe { ret.downcast_unchecked() }
498+
if let Some(ret) = ret.downcast_ref_if_exact::<PyInt>(vm) {
499+
Ok(vm.ctx.new_pyref(ret.as_bigint().to_owned()))
519500
} else {
520-
// TODO(snowapril) : modify to proper conversion method
521-
unsafe { ret.downcast_unchecked() }
522-
};
523-
Ok(value)
501+
let ret_class = ret.clone().class().to_owned();
502+
if let Some(ret) = ret.payload_if_subclass::<PyInt>(vm) {
503+
warnings::warn(
504+
vm.ctx.exceptions.deprecation_warning,
505+
format!(
506+
"__index__ returned non-int (type {}). \
507+
The ability to return an instance of a strict subclass of int \
508+
is deprecated, and may be removed in a future version of Python.",
509+
ret_class
510+
),
511+
1,
512+
vm,
513+
)?;
514+
515+
Ok(vm.ctx.new_pyref(ret.as_bigint().to_owned()))
516+
} else {
517+
Err(vm.new_type_error(format!(
518+
"{}.__index__ returned non-int(type {})",
519+
self.class(),
520+
ret_class
521+
)))
522+
}
523+
}
524524
})
525525
}
526526

527527
#[inline]
528528
pub fn float(self, vm: &VirtualMachine) -> Option<PyResult<PyRef<PyFloat>>> {
529529
self.class().slots.as_number.float.load().map(|f| {
530530
let ret = f(self, vm)?;
531-
let value: PyRef<PyFloat> = if !ret.class().is(PyFloat::class(&vm.ctx)) {
532-
if !ret.class().fast_issubclass(vm.ctx.types.float_type) {
533-
return Err(vm.new_type_error(format!(
534-
"{}.__float__ returned non-float(type {})",
535-
self.class().name(),
536-
ret.class().name()
537-
)));
538-
}
539531

540-
warnings::warn(
541-
vm.ctx.exceptions.deprecation_warning,
542-
format!(
543-
"__float__ returned non-float (type {}). \
544-
The ability to return an instance of a strict subclass of float \
545-
is deprecated, and may be removed in a future version of Python.",
546-
ret.class()
547-
),
548-
1,
549-
vm,
550-
)?;
551-
// TODO(snowapril) : modify to proper conversion method
552-
unsafe { ret.downcast_unchecked() }
532+
if let Some(ret) = ret.downcast_ref_if_exact::<PyFloat>(vm) {
533+
Ok(vm.ctx.new_pyref(ret.to_f64()))
553534
} else {
554-
// TODO(snowapril) : modify to proper conversion method
555-
unsafe { ret.downcast_unchecked() }
556-
};
557-
Ok(value)
535+
let ret_class = ret.clone().class().to_owned();
536+
if let Some(ret) = ret.payload_if_subclass::<PyFloat>(vm) {
537+
warnings::warn(
538+
vm.ctx.exceptions.deprecation_warning,
539+
format!(
540+
"__float__ returned non-float (type {}). \
541+
The ability to return an instance of a strict subclass of float \
542+
is deprecated, and may be removed in a future version of Python.",
543+
ret_class
544+
),
545+
1,
546+
vm,
547+
)?;
548+
549+
Ok(vm.ctx.new_pyref(ret.to_f64()))
550+
} else {
551+
Err(vm.new_type_error(format!(
552+
"{}.__float__ returned non-float(type {})",
553+
self.class(),
554+
ret_class
555+
)))
556+
}
557+
}
558558
})
559559
}
560560
}

0 commit comments

Comments
 (0)