Merge pull request #1468 from dralley/new-style

Adopt new style for several more object impls
This commit is contained in:
Jeong YunWon
2019-10-05 16:33:38 +09:00
committed by GitHub
4 changed files with 59 additions and 67 deletions

View File

@@ -26,27 +26,28 @@ impl PyValue for PyEnumerate {
}
}
fn enumerate_new(
cls: PyClassRef,
iterable: PyObjectRef,
start: OptionalArg<PyIntRef>,
vm: &VirtualMachine,
) -> PyResult<PyEnumerateRef> {
let counter = match start {
OptionalArg::Present(start) => start.as_bigint().clone(),
OptionalArg::Missing => BigInt::zero(),
};
let iterator = objiter::get_iter(vm, &iterable)?;
PyEnumerate {
counter: RefCell::new(counter.clone()),
iterator,
}
.into_ref_with_type(vm, cls)
}
#[pyimpl]
impl PyEnumerate {
#[pyslot(new)]
fn tp_new(
cls: PyClassRef,
iterable: PyObjectRef,
start: OptionalArg<PyIntRef>,
vm: &VirtualMachine,
) -> PyResult<PyEnumerateRef> {
let counter = match start {
OptionalArg::Present(start) => start.as_bigint().clone(),
OptionalArg::Missing => BigInt::zero(),
};
let iterator = objiter::get_iter(vm, &iterable)?;
PyEnumerate {
counter: RefCell::new(counter.clone()),
iterator,
}
.into_ref_with_type(vm, cls)
}
#[pymethod(name = "__next__")]
fn next(&self, vm: &VirtualMachine) -> PyResult {
let iterator = &self.iterator;
@@ -69,7 +70,4 @@ impl PyEnumerate {
pub fn init(context: &PyContext) {
PyEnumerate::extend_class(context, &context.types.enumerate_type);
extend_class!(context, &context.types.enumerate_type, {
(slot new) => enumerate_new,
});
}

View File

@@ -24,23 +24,24 @@ impl PyValue for PyFilter {
}
}
fn filter_new(
cls: PyClassRef,
function: PyObjectRef,
iterable: PyObjectRef,
vm: &VirtualMachine,
) -> PyResult<PyFilterRef> {
let iterator = objiter::get_iter(vm, &iterable)?;
PyFilter {
predicate: function.clone(),
iterator,
}
.into_ref_with_type(vm, cls)
}
#[pyimpl]
impl PyFilter {
#[pyslot(new)]
fn tp_new(
cls: PyClassRef,
function: PyObjectRef,
iterable: PyObjectRef,
vm: &VirtualMachine,
) -> PyResult<PyFilterRef> {
let iterator = objiter::get_iter(vm, &iterable)?;
PyFilter {
predicate: function.clone(),
iterator,
}
.into_ref_with_type(vm, cls)
}
#[pymethod(name = "__next__")]
fn next(&self, vm: &VirtualMachine) -> PyResult {
let predicate = &self.predicate;
@@ -68,7 +69,4 @@ impl PyFilter {
pub fn init(context: &PyContext) {
PyFilter::extend_class(context, &context.types.filter_type);
extend_class!(context, &context.types.filter_type, {
(slot new) => filter_new,
});
}

View File

@@ -181,6 +181,11 @@ fn inner_rshift(int1: &PyInt, int2: &PyInt, vm: &VirtualMachine) -> PyResult {
#[pyimpl]
impl PyInt {
#[pyslot(new)]
fn tp_new(cls: PyClassRef, options: IntOptions, vm: &VirtualMachine) -> PyResult<PyIntRef> {
PyInt::new(options.get_int_value(vm)?).into_ref_with_type(vm, cls)
}
#[pymethod(name = "__eq__")]
fn eq(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
if objtype::isinstance(&other, &vm.ctx.int_type()) {
@@ -720,10 +725,6 @@ impl IntOptions {
}
}
fn int_new(cls: PyClassRef, options: IntOptions, vm: &VirtualMachine) -> PyResult<PyIntRef> {
PyInt::new(options.get_int_value(vm)?).into_ref_with_type(vm, cls)
}
#[derive(FromArgs)]
struct IntFromByteArgs {
#[pyarg(positional_or_keyword)]
@@ -935,7 +936,4 @@ fn get_py_int(obj: &PyObjectRef) -> &PyInt {
pub fn init(context: &PyContext) {
PyInt::extend_class(context, &context.types.int_type);
extend_class!(context, &context.types.int_type, {
(slot new) => int_new,
});
}

View File

@@ -23,25 +23,26 @@ impl PyValue for PyMap {
}
}
fn map_new(
cls: PyClassRef,
function: PyObjectRef,
iterables: Args,
vm: &VirtualMachine,
) -> PyResult<PyMapRef> {
let iterators = iterables
.into_iter()
.map(|iterable| objiter::get_iter(vm, &iterable))
.collect::<Result<Vec<_>, _>>()?;
PyMap {
mapper: function.clone(),
iterators,
}
.into_ref_with_type(vm, cls.clone())
}
#[pyimpl]
impl PyMap {
#[pyslot(new)]
fn tp_new(
cls: PyClassRef,
function: PyObjectRef,
iterables: Args,
vm: &VirtualMachine,
) -> PyResult<PyMapRef> {
let iterators = iterables
.into_iter()
.map(|iterable| objiter::get_iter(vm, &iterable))
.collect::<Result<Vec<_>, _>>()?;
PyMap {
mapper: function.clone(),
iterators,
}
.into_ref_with_type(vm, cls.clone())
}
#[pymethod(name = "__next__")]
fn next(&self, vm: &VirtualMachine) -> PyResult {
let next_objs = self
@@ -62,7 +63,4 @@ impl PyMap {
pub fn init(context: &PyContext) {
PyMap::extend_class(context, &context.types.map_type);
extend_class!(context, &context.types.map_type, {
(slot new) => map_new,
});
}