Skip to content

vm::function::{Args -> PosArgs} #3106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions vm/src/builtins/make_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ mod decl {
#[cfg(feature = "rustpython-compiler")]
use crate::compile;
use crate::function::{
ArgCallable, ArgIterable, Args, FuncArgs, KwArgs, OptionalArg, OptionalOption,
ArgCallable, ArgIterable, FuncArgs, KwArgs, OptionalArg, OptionalOption, PosArgs,
};
use crate::iterator;
use crate::readline::{Readline, ReadlineResult};
Expand Down Expand Up @@ -652,7 +652,7 @@ mod decl {
}

#[pyfunction]
pub fn print(objects: Args, options: PrintOptions, vm: &VirtualMachine) -> PyResult<()> {
pub fn print(objects: PosArgs, options: PrintOptions, vm: &VirtualMachine) -> PyResult<()> {
let file = match options.file {
Some(f) => f,
None => sysmodule::get_stdout(vm)?,
Expand Down Expand Up @@ -809,7 +809,7 @@ mod decl {
pub fn __build_class__(
function: PyFunctionRef,
qualified_name: PyStrRef,
bases: Args,
bases: PosArgs,
mut kwargs: KwArgs,
vm: &VirtualMachine,
) -> PyResult {
Expand Down
4 changes: 2 additions & 2 deletions vm/src/builtins/map.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::pytype::PyTypeRef;
use crate::function::Args;
use crate::function::PosArgs;
use crate::iterator;
use crate::slots::{PyIter, SlotConstructor};
use crate::vm::VirtualMachine;
Expand All @@ -23,7 +23,7 @@ impl PyValue for PyMap {
}

impl SlotConstructor for PyMap {
type Args = (PyObjectRef, Args<PyObjectRef>);
type Args = (PyObjectRef, PosArgs<PyObjectRef>);

fn py_new(cls: PyTypeRef, (function, iterables): Self::Args, vm: &VirtualMachine) -> PyResult {
let iterators = iterables
Expand Down
48 changes: 28 additions & 20 deletions vm/src/builtins/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::common::hash::PyHash;
use crate::common::rc::PyRc;
use crate::dictdatatype;
use crate::dictdatatype::DictSize;
use crate::function::{ArgIterable, Args, FuncArgs, OptionalArg};
use crate::function::{ArgIterable, FuncArgs, OptionalArg, PosArgs};
use crate::slots::{
Comparable, Hashable, Iterable, PyComparisonOp, PyIter, SlotConstructor, Unhashable,
};
Expand Down Expand Up @@ -243,7 +243,7 @@ impl PySetInner {
}
}

fn update(&self, others: Args<ArgIterable>, vm: &VirtualMachine) -> PyResult<()> {
fn update(&self, others: PosArgs<ArgIterable>, vm: &VirtualMachine) -> PyResult<()> {
for iterable in others {
for item in iterable.iter(vm)? {
self.add(item?, vm)?;
Expand All @@ -252,7 +252,11 @@ impl PySetInner {
Ok(())
}

fn intersection_update(&self, others: Args<ArgIterable>, vm: &VirtualMachine) -> PyResult<()> {
fn intersection_update(
&self,
others: PosArgs<ArgIterable>,
vm: &VirtualMachine,
) -> PyResult<()> {
let mut temp_inner = self.copy();
self.clear();
for iterable in others {
Expand All @@ -267,7 +271,7 @@ impl PySetInner {
Ok(())
}

fn difference_update(&self, others: Args<ArgIterable>, vm: &VirtualMachine) -> PyResult<()> {
fn difference_update(&self, others: PosArgs<ArgIterable>, vm: &VirtualMachine) -> PyResult<()> {
for iterable in others {
for item in iterable.iter(vm)? {
self.content.delete_if_exists(vm, &item?)?;
Expand All @@ -278,7 +282,7 @@ impl PySetInner {

fn symmetric_difference_update(
&self,
others: Args<ArgIterable>,
others: PosArgs<ArgIterable>,
vm: &VirtualMachine,
) -> PyResult<()> {
for iterable in others {
Expand Down Expand Up @@ -378,7 +382,7 @@ impl PySet {
self.clear();
}
if let OptionalArg::Present(it) = iterable {
self.update(Args::new(vec![it]), vm)?;
self.update(PosArgs::new(vec![it]), vm)?;
}
Ok(())
}
Expand Down Expand Up @@ -406,24 +410,24 @@ impl PySet {
}

#[pymethod]
fn union(&self, others: Args<ArgIterable>, vm: &VirtualMachine) -> PyResult<Self> {
fn union(&self, others: PosArgs<ArgIterable>, vm: &VirtualMachine) -> PyResult<Self> {
multi_args_set!(vm, others, self, union)
}

#[pymethod]
fn intersection(&self, others: Args<ArgIterable>, vm: &VirtualMachine) -> PyResult<Self> {
fn intersection(&self, others: PosArgs<ArgIterable>, vm: &VirtualMachine) -> PyResult<Self> {
multi_args_set!(vm, others, self, intersection)
}

#[pymethod]
fn difference(&self, others: Args<ArgIterable>, vm: &VirtualMachine) -> PyResult<Self> {
fn difference(&self, others: PosArgs<ArgIterable>, vm: &VirtualMachine) -> PyResult<Self> {
multi_args_set!(vm, others, self, difference)
}

#[pymethod]
fn symmetric_difference(
&self,
others: Args<ArgIterable>,
others: PosArgs<ArgIterable>,
vm: &VirtualMachine,
) -> PyResult<Self> {
multi_args_set!(vm, others, self, symmetric_difference)
Expand Down Expand Up @@ -518,13 +522,17 @@ impl PySet {
}

#[pymethod]
fn update(&self, others: Args<ArgIterable>, vm: &VirtualMachine) -> PyResult<()> {
fn update(&self, others: PosArgs<ArgIterable>, vm: &VirtualMachine) -> PyResult<()> {
self.inner.update(others, vm)?;
Ok(())
}

#[pymethod]
fn intersection_update(&self, others: Args<ArgIterable>, vm: &VirtualMachine) -> PyResult<()> {
fn intersection_update(
&self,
others: PosArgs<ArgIterable>,
vm: &VirtualMachine,
) -> PyResult<()> {
self.inner.intersection_update(others, vm)?;
Ok(())
}
Expand All @@ -536,7 +544,7 @@ impl PySet {
}

#[pymethod]
fn difference_update(&self, others: Args<ArgIterable>, vm: &VirtualMachine) -> PyResult<()> {
fn difference_update(&self, others: PosArgs<ArgIterable>, vm: &VirtualMachine) -> PyResult<()> {
self.inner.difference_update(others, vm)?;
Ok(())
}
Expand All @@ -550,7 +558,7 @@ impl PySet {
#[pymethod]
fn symmetric_difference_update(
&self,
others: Args<ArgIterable>,
others: PosArgs<ArgIterable>,
vm: &VirtualMachine,
) -> PyResult<()> {
self.inner.symmetric_difference_update(others, vm)?;
Expand Down Expand Up @@ -673,24 +681,24 @@ impl PyFrozenSet {
}

#[pymethod]
fn union(&self, others: Args<ArgIterable>, vm: &VirtualMachine) -> PyResult<Self> {
fn union(&self, others: PosArgs<ArgIterable>, vm: &VirtualMachine) -> PyResult<Self> {
multi_args_frozenset!(vm, others, self, union)
}

#[pymethod]
fn intersection(&self, others: Args<ArgIterable>, vm: &VirtualMachine) -> PyResult<Self> {
fn intersection(&self, others: PosArgs<ArgIterable>, vm: &VirtualMachine) -> PyResult<Self> {
multi_args_frozenset!(vm, others, self, intersection)
}

#[pymethod]
fn difference(&self, others: Args<ArgIterable>, vm: &VirtualMachine) -> PyResult<Self> {
fn difference(&self, others: PosArgs<ArgIterable>, vm: &VirtualMachine) -> PyResult<Self> {
multi_args_frozenset!(vm, others, self, difference)
}

#[pymethod]
fn symmetric_difference(
&self,
others: Args<ArgIterable>,
others: PosArgs<ArgIterable>,
vm: &VirtualMachine,
) -> PyResult<Self> {
multi_args_frozenset!(vm, others, self, symmetric_difference)
Expand Down Expand Up @@ -787,7 +795,7 @@ impl Iterable for PyFrozenSet {
}

struct SetIterable {
iterable: Args<ArgIterable>,
iterable: PosArgs<ArgIterable>,
}

impl TryFromObject for SetIterable {
Expand All @@ -799,7 +807,7 @@ impl TryFromObject for SetIterable {
// the class lease needs to be drop to be able to return the object
drop(class);
Ok(SetIterable {
iterable: Args::new(vec![ArgIterable::try_from_object(vm, obj)?]),
iterable: PosArgs::new(vec![ArgIterable::try_from_object(vm, obj)?]),
})
} else {
Err(vm.new_type_error(format!("{} is not a subtype of set or frozenset", class)))
Expand Down
4 changes: 2 additions & 2 deletions vm/src/builtins/zip.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::pytype::PyTypeRef;
use crate::function::Args;
use crate::function::PosArgs;
use crate::iterator;
use crate::slots::{PyIter, SlotConstructor};
use crate::vm::VirtualMachine;
Expand All @@ -18,7 +18,7 @@ impl PyValue for PyZip {
}

impl SlotConstructor for PyZip {
type Args = Args;
type Args = PosArgs;

fn py_new(cls: PyTypeRef, iterables: Self::Args, vm: &VirtualMachine) -> PyResult {
let iterators = iterables
Expand Down
36 changes: 18 additions & 18 deletions vm/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub struct FuncArgs {
/// Conversion from vector of python objects to function arguments.
impl<A> From<A> for FuncArgs
where
A: Into<Args>,
A: Into<PosArgs>,
{
fn from(args: A) -> Self {
FuncArgs {
Expand Down Expand Up @@ -105,10 +105,10 @@ impl FromArgs for FuncArgs {
impl FuncArgs {
pub fn new<A, K>(args: A, kwargs: K) -> Self
where
A: Into<Args>,
A: Into<PosArgs>,
K: Into<KwArgs>,
{
let Args(args) = args.into();
let PosArgs(args) = args.into();
let KwArgs(kwargs) = kwargs.into();
Self { args, kwargs }
}
Expand Down Expand Up @@ -374,18 +374,18 @@ impl<T> IntoIterator for KwArgs<T> {

/// A list of positional argument values.
///
/// A built-in function with a `Args` parameter is analogous to a Python
/// A built-in function with a `PosArgs` parameter is analogous to a Python
/// function with `*args`. All remaining positional arguments are extracted
/// (and hence the function will permit an arbitrary number of them).
///
/// `Args` optionally accepts a generic type parameter to allow type checks
/// `PosArgs` optionally accepts a generic type parameter to allow type checks
/// or conversions of each argument.
#[derive(Clone)]
pub struct Args<T = PyObjectRef>(Vec<T>);
pub struct PosArgs<T = PyObjectRef>(Vec<T>);

impl<T> Args<T> {
impl<T> PosArgs<T> {
pub fn new(args: Vec<T>) -> Self {
Args(args)
Self(args)
}

pub fn into_vec(self) -> Vec<T> {
Expand All @@ -397,32 +397,32 @@ impl<T> Args<T> {
}
}

impl<T> From<Vec<T>> for Args<T> {
impl<T> From<Vec<T>> for PosArgs<T> {
fn from(v: Vec<T>) -> Self {
Args(v)
Self(v)
}
}

impl From<()> for Args<PyObjectRef> {
impl From<()> for PosArgs<PyObjectRef> {
fn from(_args: ()) -> Self {
Args(Vec::new())
Self(Vec::new())
}
}

impl<T> AsRef<[T]> for Args<T> {
impl<T> AsRef<[T]> for PosArgs<T> {
fn as_ref(&self) -> &[T] {
&self.0
}
}

impl<T: PyValue> Args<PyRef<T>> {
impl<T: PyValue> PosArgs<PyRef<T>> {
pub fn into_tuple(self, vm: &VirtualMachine) -> PyObjectRef {
vm.ctx
.new_tuple(self.0.into_iter().map(PyRef::into_object).collect())
}
}

impl<T> FromArgs for Args<T>
impl<T> FromArgs for PosArgs<T>
where
T: TryFromObject,
{
Expand All @@ -431,11 +431,11 @@ where
while let Some(value) = args.take_positional() {
varargs.push(T::try_from_object(vm, value)?);
}
Ok(Args(varargs))
Ok(PosArgs(varargs))
}
}

impl<T> IntoIterator for Args<T> {
impl<T> IntoIterator for PosArgs<T> {
type Item = T;
type IntoIter = std::vec::IntoIter<T>;

Expand Down Expand Up @@ -546,7 +546,7 @@ macro_rules! tuple_from_py_func_args {
}

// Implement `FromArgs` for up to 7-tuples, allowing built-in functions to bind
// up to 7 top-level parameters (note that `Args`, `KwArgs`, nested tuples, etc.
// up to 7 top-level parameters (note that `PosArgs`, `KwArgs`, nested tuples, etc.
// count as 1, so this should actually be more than enough).
tuple_from_py_func_args!(A);
tuple_from_py_func_args!(A, B);
Expand Down
6 changes: 3 additions & 3 deletions vm/src/stdlib/itertools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod decl {
};
use crate::{
builtins::{int, PyInt, PyIntRef, PyTupleRef, PyTypeRef},
function::{ArgCallable, Args, FuncArgs, OptionalArg, OptionalOption},
function::{ArgCallable, FuncArgs, OptionalArg, OptionalOption, PosArgs},
iterator::{call_next, get_iter, get_next_object},
slots::{PyIter, SlotConstructor},
IdProtocol, IntoPyObject, PyObjectRef, PyRef, PyResult, PyValue, PyWeakRef, StaticType,
Expand Down Expand Up @@ -1026,7 +1026,7 @@ mod decl {
}

impl SlotConstructor for PyItertoolsProduct {
type Args = (Args<PyObjectRef>, ProductArgs);
type Args = (PosArgs<PyObjectRef>, ProductArgs);

fn py_new(cls: PyTypeRef, (iterables, args): Self::Args, vm: &VirtualMachine) -> PyResult {
let repeat = args.repeat.unwrap_or(1);
Expand Down Expand Up @@ -1425,7 +1425,7 @@ mod decl {
}

impl SlotConstructor for PyItertoolsZipLongest {
type Args = (Args<PyObjectRef>, ZipLongestArgs);
type Args = (PosArgs<PyObjectRef>, ZipLongestArgs);

fn py_new(cls: PyTypeRef, (iterables, args): Self::Args, vm: &VirtualMachine) -> PyResult {
let fillvalue = args.fillvalue.unwrap_or_none(vm);
Expand Down
Loading