Skip to content

Commit dd4539b

Browse files
committed
Change to use PyClassImpl::extend_class
1 parent c5f4eab commit dd4539b

File tree

1 file changed

+18
-62
lines changed

1 file changed

+18
-62
lines changed

vm/src/obj/objint.rs

Lines changed: 18 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ use num_traits::{Pow, Signed, ToPrimitive, Zero};
88
use crate::format::FormatSpec;
99
use crate::function::OptionalArg;
1010
use crate::pyobject::{
11-
IntoPyObject, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, TypeProtocol,
11+
IntoPyObject, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject,
12+
TypeProtocol,
1213
};
1314
use crate::vm::VirtualMachine;
1415

@@ -17,6 +18,20 @@ use super::objstr::{PyString, PyStringRef};
1718
use super::objtype;
1819
use crate::obj::objtype::PyClassRef;
1920

21+
/// int(x=0) -> integer
22+
/// int(x, base=10) -> integer
23+
///
24+
/// Convert a number or string to an integer, or return 0 if no arguments
25+
/// are given. If x is a number, return x.__int__(). For floating point
26+
/// numbers, this truncates towards zero.
27+
///
28+
/// If x is not a number or if base is given, then x must be a string,
29+
/// bytes, or bytearray instance representing an integer literal in the
30+
/// given base. The literal can be preceded by '+' or '-' and be surrounded
31+
/// by whitespace. The base defaults to 10. Valid bases are 0 and 2-36.
32+
/// Base 0 means to interpret the base from the string as an integer literal.
33+
/// >>> int('0b100', base=0)
34+
/// 4
2035
#[pyclass(__inside_vm)]
2136
#[derive(Debug)]
2237
pub struct PyInt {
@@ -543,68 +558,9 @@ fn div_ints(vm: &VirtualMachine, i1: &BigInt, i2: &BigInt) -> PyResult {
543558
}
544559
}
545560

546-
#[rustfmt::skip] // to avoid line splitting
547561
pub fn init(context: &PyContext) {
548-
let int_doc = "int(x=0) -> integer
549-
int(x, base=10) -> integer
550-
551-
Convert a number or string to an integer, or return 0 if no arguments
552-
are given. If x is a number, return x.__int__(). For floating point
553-
numbers, this truncates towards zero.
554-
555-
If x is not a number or if base is given, then x must be a string,
556-
bytes, or bytearray instance representing an integer literal in the
557-
given base. The literal can be preceded by '+' or '-' and be surrounded
558-
by whitespace. The base defaults to 10. Valid bases are 0 and 2-36.
559-
Base 0 means to interpret the base from the string as an integer literal.
560-
>>> int('0b100', base=0)
561-
4";
562-
let int_type = &context.int_type;
563-
extend_class!(context, int_type, {
564-
"__doc__" => context.new_str(int_doc.to_string()),
565-
"__eq__" => context.new_rustfunc(PyInt::eq),
566-
"__ne__" => context.new_rustfunc(PyInt::ne),
567-
"__lt__" => context.new_rustfunc(PyInt::lt),
568-
"__le__" => context.new_rustfunc(PyInt::le),
569-
"__gt__" => context.new_rustfunc(PyInt::gt),
570-
"__ge__" => context.new_rustfunc(PyInt::ge),
571-
"__abs__" => context.new_rustfunc(PyInt::abs),
572-
"__add__" => context.new_rustfunc(PyInt::add),
573-
"__radd__" => context.new_rustfunc(PyInt::add),
574-
"__and__" => context.new_rustfunc(PyInt::and),
575-
"__divmod__" => context.new_rustfunc(PyInt::divmod),
576-
"__float__" => context.new_rustfunc(PyInt::float),
577-
"__round__" => context.new_rustfunc(PyInt::round),
578-
"__ceil__" => context.new_rustfunc(PyInt::ceil),
579-
"__floor__" => context.new_rustfunc(PyInt::floor),
580-
"__index__" => context.new_rustfunc(PyInt::index),
581-
"__trunc__" => context.new_rustfunc(PyInt::trunc),
582-
"__int__" => context.new_rustfunc(PyInt::int),
583-
"__floordiv__" => context.new_rustfunc(PyInt::floordiv),
584-
"__hash__" => context.new_rustfunc(PyInt::hash),
585-
"__lshift__" => context.new_rustfunc(PyInt::lshift),
586-
"__rshift__" => context.new_rustfunc(PyInt::rshift),
562+
PyInt::extend_class(context, &context.int_type);
563+
extend_class!(context, &context.int_type, {
587564
"__new__" => context.new_rustfunc(int_new),
588-
"__mod__" => context.new_rustfunc(PyInt::mod_),
589-
"__mul__" => context.new_rustfunc(PyInt::mul),
590-
"__rmul__" => context.new_rustfunc(PyInt::mul),
591-
"__or__" => context.new_rustfunc(PyInt::or),
592-
"__neg__" => context.new_rustfunc(PyInt::neg),
593-
"__pos__" => context.new_rustfunc(PyInt::pos),
594-
"__pow__" => context.new_rustfunc(PyInt::pow),
595-
"__repr__" => context.new_rustfunc(PyInt::repr),
596-
"__sub__" => context.new_rustfunc(PyInt::sub),
597-
"__rsub__" => context.new_rustfunc(PyInt::rsub),
598-
"__format__" => context.new_rustfunc(PyInt::format),
599-
"__truediv__" => context.new_rustfunc(PyInt::truediv),
600-
"__rtruediv__" => context.new_rustfunc(PyInt::rtruediv),
601-
"__xor__" => context.new_rustfunc(PyInt::xor),
602-
"__rxor__" => context.new_rustfunc(PyInt::rxor),
603-
"__bool__" => context.new_rustfunc(PyInt::bool),
604-
"__invert__" => context.new_rustfunc(PyInt::invert),
605-
"bit_length" => context.new_rustfunc(PyInt::bit_length),
606-
"conjugate" => context.new_rustfunc(PyInt::conjugate),
607-
"real" => context.new_property(PyInt::real),
608-
"imag" => context.new_property(PyInt::imag)
609565
});
610566
}

0 commit comments

Comments
 (0)