Skip to content

Commit 7a7a6bd

Browse files
authored
Merge pull request #3106 from youknowone/posargs
vm::function::{Args -> PosArgs}
2 parents be74fb9 + d9c17f2 commit 7a7a6bd

File tree

12 files changed

+76
-68
lines changed

12 files changed

+76
-68
lines changed

vm/src/builtins/make_module.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ mod decl {
2626
#[cfg(feature = "rustpython-compiler")]
2727
use crate::compile;
2828
use crate::function::{
29-
ArgCallable, ArgIterable, Args, FuncArgs, KwArgs, OptionalArg, OptionalOption,
29+
ArgCallable, ArgIterable, FuncArgs, KwArgs, OptionalArg, OptionalOption, PosArgs,
3030
};
3131
use crate::iterator;
3232
use crate::readline::{Readline, ReadlineResult};
@@ -652,7 +652,7 @@ mod decl {
652652
}
653653

654654
#[pyfunction]
655-
pub fn print(objects: Args, options: PrintOptions, vm: &VirtualMachine) -> PyResult<()> {
655+
pub fn print(objects: PosArgs, options: PrintOptions, vm: &VirtualMachine) -> PyResult<()> {
656656
let file = match options.file {
657657
Some(f) => f,
658658
None => sysmodule::get_stdout(vm)?,
@@ -809,7 +809,7 @@ mod decl {
809809
pub fn __build_class__(
810810
function: PyFunctionRef,
811811
qualified_name: PyStrRef,
812-
bases: Args,
812+
bases: PosArgs,
813813
mut kwargs: KwArgs,
814814
vm: &VirtualMachine,
815815
) -> PyResult {

vm/src/builtins/map.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::pytype::PyTypeRef;
2-
use crate::function::Args;
2+
use crate::function::PosArgs;
33
use crate::iterator;
44
use crate::slots::{PyIter, SlotConstructor};
55
use crate::vm::VirtualMachine;
@@ -23,7 +23,7 @@ impl PyValue for PyMap {
2323
}
2424

2525
impl SlotConstructor for PyMap {
26-
type Args = (PyObjectRef, Args<PyObjectRef>);
26+
type Args = (PyObjectRef, PosArgs<PyObjectRef>);
2727

2828
fn py_new(cls: PyTypeRef, (function, iterables): Self::Args, vm: &VirtualMachine) -> PyResult {
2929
let iterators = iterables

vm/src/builtins/set.rs

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::common::hash::PyHash;
66
use crate::common::rc::PyRc;
77
use crate::dictdatatype;
88
use crate::dictdatatype::DictSize;
9-
use crate::function::{ArgIterable, Args, FuncArgs, OptionalArg};
9+
use crate::function::{ArgIterable, FuncArgs, OptionalArg, PosArgs};
1010
use crate::slots::{
1111
Comparable, Hashable, Iterable, PyComparisonOp, PyIter, SlotConstructor, Unhashable,
1212
};
@@ -243,7 +243,7 @@ impl PySetInner {
243243
}
244244
}
245245

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

255-
fn intersection_update(&self, others: Args<ArgIterable>, vm: &VirtualMachine) -> PyResult<()> {
255+
fn intersection_update(
256+
&self,
257+
others: PosArgs<ArgIterable>,
258+
vm: &VirtualMachine,
259+
) -> PyResult<()> {
256260
let mut temp_inner = self.copy();
257261
self.clear();
258262
for iterable in others {
@@ -267,7 +271,7 @@ impl PySetInner {
267271
Ok(())
268272
}
269273

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

279283
fn symmetric_difference_update(
280284
&self,
281-
others: Args<ArgIterable>,
285+
others: PosArgs<ArgIterable>,
282286
vm: &VirtualMachine,
283287
) -> PyResult<()> {
284288
for iterable in others {
@@ -378,7 +382,7 @@ impl PySet {
378382
self.clear();
379383
}
380384
if let OptionalArg::Present(it) = iterable {
381-
self.update(Args::new(vec![it]), vm)?;
385+
self.update(PosArgs::new(vec![it]), vm)?;
382386
}
383387
Ok(())
384388
}
@@ -406,24 +410,24 @@ impl PySet {
406410
}
407411

408412
#[pymethod]
409-
fn union(&self, others: Args<ArgIterable>, vm: &VirtualMachine) -> PyResult<Self> {
413+
fn union(&self, others: PosArgs<ArgIterable>, vm: &VirtualMachine) -> PyResult<Self> {
410414
multi_args_set!(vm, others, self, union)
411415
}
412416

413417
#[pymethod]
414-
fn intersection(&self, others: Args<ArgIterable>, vm: &VirtualMachine) -> PyResult<Self> {
418+
fn intersection(&self, others: PosArgs<ArgIterable>, vm: &VirtualMachine) -> PyResult<Self> {
415419
multi_args_set!(vm, others, self, intersection)
416420
}
417421

418422
#[pymethod]
419-
fn difference(&self, others: Args<ArgIterable>, vm: &VirtualMachine) -> PyResult<Self> {
423+
fn difference(&self, others: PosArgs<ArgIterable>, vm: &VirtualMachine) -> PyResult<Self> {
420424
multi_args_set!(vm, others, self, difference)
421425
}
422426

423427
#[pymethod]
424428
fn symmetric_difference(
425429
&self,
426-
others: Args<ArgIterable>,
430+
others: PosArgs<ArgIterable>,
427431
vm: &VirtualMachine,
428432
) -> PyResult<Self> {
429433
multi_args_set!(vm, others, self, symmetric_difference)
@@ -518,13 +522,17 @@ impl PySet {
518522
}
519523

520524
#[pymethod]
521-
fn update(&self, others: Args<ArgIterable>, vm: &VirtualMachine) -> PyResult<()> {
525+
fn update(&self, others: PosArgs<ArgIterable>, vm: &VirtualMachine) -> PyResult<()> {
522526
self.inner.update(others, vm)?;
523527
Ok(())
524528
}
525529

526530
#[pymethod]
527-
fn intersection_update(&self, others: Args<ArgIterable>, vm: &VirtualMachine) -> PyResult<()> {
531+
fn intersection_update(
532+
&self,
533+
others: PosArgs<ArgIterable>,
534+
vm: &VirtualMachine,
535+
) -> PyResult<()> {
528536
self.inner.intersection_update(others, vm)?;
529537
Ok(())
530538
}
@@ -536,7 +544,7 @@ impl PySet {
536544
}
537545

538546
#[pymethod]
539-
fn difference_update(&self, others: Args<ArgIterable>, vm: &VirtualMachine) -> PyResult<()> {
547+
fn difference_update(&self, others: PosArgs<ArgIterable>, vm: &VirtualMachine) -> PyResult<()> {
540548
self.inner.difference_update(others, vm)?;
541549
Ok(())
542550
}
@@ -550,7 +558,7 @@ impl PySet {
550558
#[pymethod]
551559
fn symmetric_difference_update(
552560
&self,
553-
others: Args<ArgIterable>,
561+
others: PosArgs<ArgIterable>,
554562
vm: &VirtualMachine,
555563
) -> PyResult<()> {
556564
self.inner.symmetric_difference_update(others, vm)?;
@@ -673,24 +681,24 @@ impl PyFrozenSet {
673681
}
674682

675683
#[pymethod]
676-
fn union(&self, others: Args<ArgIterable>, vm: &VirtualMachine) -> PyResult<Self> {
684+
fn union(&self, others: PosArgs<ArgIterable>, vm: &VirtualMachine) -> PyResult<Self> {
677685
multi_args_frozenset!(vm, others, self, union)
678686
}
679687

680688
#[pymethod]
681-
fn intersection(&self, others: Args<ArgIterable>, vm: &VirtualMachine) -> PyResult<Self> {
689+
fn intersection(&self, others: PosArgs<ArgIterable>, vm: &VirtualMachine) -> PyResult<Self> {
682690
multi_args_frozenset!(vm, others, self, intersection)
683691
}
684692

685693
#[pymethod]
686-
fn difference(&self, others: Args<ArgIterable>, vm: &VirtualMachine) -> PyResult<Self> {
694+
fn difference(&self, others: PosArgs<ArgIterable>, vm: &VirtualMachine) -> PyResult<Self> {
687695
multi_args_frozenset!(vm, others, self, difference)
688696
}
689697

690698
#[pymethod]
691699
fn symmetric_difference(
692700
&self,
693-
others: Args<ArgIterable>,
701+
others: PosArgs<ArgIterable>,
694702
vm: &VirtualMachine,
695703
) -> PyResult<Self> {
696704
multi_args_frozenset!(vm, others, self, symmetric_difference)
@@ -787,7 +795,7 @@ impl Iterable for PyFrozenSet {
787795
}
788796

789797
struct SetIterable {
790-
iterable: Args<ArgIterable>,
798+
iterable: PosArgs<ArgIterable>,
791799
}
792800

793801
impl TryFromObject for SetIterable {
@@ -799,7 +807,7 @@ impl TryFromObject for SetIterable {
799807
// the class lease needs to be drop to be able to return the object
800808
drop(class);
801809
Ok(SetIterable {
802-
iterable: Args::new(vec![ArgIterable::try_from_object(vm, obj)?]),
810+
iterable: PosArgs::new(vec![ArgIterable::try_from_object(vm, obj)?]),
803811
})
804812
} else {
805813
Err(vm.new_type_error(format!("{} is not a subtype of set or frozenset", class)))

vm/src/builtins/zip.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::pytype::PyTypeRef;
2-
use crate::function::Args;
2+
use crate::function::PosArgs;
33
use crate::iterator;
44
use crate::slots::{PyIter, SlotConstructor};
55
use crate::vm::VirtualMachine;
@@ -18,7 +18,7 @@ impl PyValue for PyZip {
1818
}
1919

2020
impl SlotConstructor for PyZip {
21-
type Args = Args;
21+
type Args = PosArgs;
2222

2323
fn py_new(cls: PyTypeRef, iterables: Self::Args, vm: &VirtualMachine) -> PyResult {
2424
let iterators = iterables

vm/src/function.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ pub struct FuncArgs {
7777
/// Conversion from vector of python objects to function arguments.
7878
impl<A> From<A> for FuncArgs
7979
where
80-
A: Into<Args>,
80+
A: Into<PosArgs>,
8181
{
8282
fn from(args: A) -> Self {
8383
FuncArgs {
@@ -105,10 +105,10 @@ impl FromArgs for FuncArgs {
105105
impl FuncArgs {
106106
pub fn new<A, K>(args: A, kwargs: K) -> Self
107107
where
108-
A: Into<Args>,
108+
A: Into<PosArgs>,
109109
K: Into<KwArgs>,
110110
{
111-
let Args(args) = args.into();
111+
let PosArgs(args) = args.into();
112112
let KwArgs(kwargs) = kwargs.into();
113113
Self { args, kwargs }
114114
}
@@ -374,18 +374,18 @@ impl<T> IntoIterator for KwArgs<T> {
374374

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

386-
impl<T> Args<T> {
386+
impl<T> PosArgs<T> {
387387
pub fn new(args: Vec<T>) -> Self {
388-
Args(args)
388+
Self(args)
389389
}
390390

391391
pub fn into_vec(self) -> Vec<T> {
@@ -397,32 +397,32 @@ impl<T> Args<T> {
397397
}
398398
}
399399

400-
impl<T> From<Vec<T>> for Args<T> {
400+
impl<T> From<Vec<T>> for PosArgs<T> {
401401
fn from(v: Vec<T>) -> Self {
402-
Args(v)
402+
Self(v)
403403
}
404404
}
405405

406-
impl From<()> for Args<PyObjectRef> {
406+
impl From<()> for PosArgs<PyObjectRef> {
407407
fn from(_args: ()) -> Self {
408-
Args(Vec::new())
408+
Self(Vec::new())
409409
}
410410
}
411411

412-
impl<T> AsRef<[T]> for Args<T> {
412+
impl<T> AsRef<[T]> for PosArgs<T> {
413413
fn as_ref(&self) -> &[T] {
414414
&self.0
415415
}
416416
}
417417

418-
impl<T: PyValue> Args<PyRef<T>> {
418+
impl<T: PyValue> PosArgs<PyRef<T>> {
419419
pub fn into_tuple(self, vm: &VirtualMachine) -> PyObjectRef {
420420
vm.ctx
421421
.new_tuple(self.0.into_iter().map(PyRef::into_object).collect())
422422
}
423423
}
424424

425-
impl<T> FromArgs for Args<T>
425+
impl<T> FromArgs for PosArgs<T>
426426
where
427427
T: TryFromObject,
428428
{
@@ -431,11 +431,11 @@ where
431431
while let Some(value) = args.take_positional() {
432432
varargs.push(T::try_from_object(vm, value)?);
433433
}
434-
Ok(Args(varargs))
434+
Ok(PosArgs(varargs))
435435
}
436436
}
437437

438-
impl<T> IntoIterator for Args<T> {
438+
impl<T> IntoIterator for PosArgs<T> {
439439
type Item = T;
440440
type IntoIter = std::vec::IntoIter<T>;
441441

@@ -546,7 +546,7 @@ macro_rules! tuple_from_py_func_args {
546546
}
547547

548548
// Implement `FromArgs` for up to 7-tuples, allowing built-in functions to bind
549-
// up to 7 top-level parameters (note that `Args`, `KwArgs`, nested tuples, etc.
549+
// up to 7 top-level parameters (note that `PosArgs`, `KwArgs`, nested tuples, etc.
550550
// count as 1, so this should actually be more than enough).
551551
tuple_from_py_func_args!(A);
552552
tuple_from_py_func_args!(A, B);

vm/src/stdlib/itertools.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ mod decl {
88
};
99
use crate::{
1010
builtins::{int, PyInt, PyIntRef, PyTupleRef, PyTypeRef},
11-
function::{ArgCallable, Args, FuncArgs, OptionalArg, OptionalOption},
11+
function::{ArgCallable, FuncArgs, OptionalArg, OptionalOption, PosArgs},
1212
iterator::{call_next, get_iter, get_next_object},
1313
slots::{PyIter, SlotConstructor},
1414
IdProtocol, IntoPyObject, PyObjectRef, PyRef, PyResult, PyValue, PyWeakRef, StaticType,
@@ -1026,7 +1026,7 @@ mod decl {
10261026
}
10271027

10281028
impl SlotConstructor for PyItertoolsProduct {
1029-
type Args = (Args<PyObjectRef>, ProductArgs);
1029+
type Args = (PosArgs<PyObjectRef>, ProductArgs);
10301030

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

14271427
impl SlotConstructor for PyItertoolsZipLongest {
1428-
type Args = (Args<PyObjectRef>, ZipLongestArgs);
1428+
type Args = (PosArgs<PyObjectRef>, ZipLongestArgs);
14291429

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

0 commit comments

Comments
 (0)