Skip to content
Merged
45 changes: 39 additions & 6 deletions crates/vm/src/builtins/bytearray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ use crate::{
PyMappedDetachingRwLockReadGuard, PyMappedDetachingRwLockWriteGuard, PyMutex,
},
convert::{ToPyObject, ToPyResult},
function::{ArgBytesLike, ArgIndex, FuncArgs, OptionalArg, PyComparisonValue, PySsize},
function::{
ArgBytesLike, ArgIndex, ArgumentError, FromArgs, FuncArgs, OptionalArg, Param,
PyComparisonValue, PySsize,
},
protocol::{
BufferDescriptor, BufferFlags, BufferMethods, BufferResizeGuard, PyBuffer, PyIterReturn,
PyMappingMethods, PyNumberMethods, PySequenceMethods,
Expand Down Expand Up @@ -493,10 +496,28 @@ impl PyByteArray {}

#[derive(FromArgs)]
struct ByteArrayReduceExArgs {
#[pyarg(positional, optional)]
#[pyarg(positional, optional, py_default = "0")]
proto: OptionalArg<ArgIndex>,
}

#[derive(FromArgs)]
struct TakeBytesArgs {
#[pyarg(positional, default = None)]
n: Option<PyObjectRef>,
}

// Expose the actual split parameters without converting them before the
// method acquires its export guard.
struct ByteArraySplitArgs(FuncArgs);

impl FromArgs for ByteArraySplitArgs {
const PARAMS: Option<&'static [Param]> = ByteInnerSplitOptions::PARAMS;

fn from_args(vm: &VirtualMachine, args: &mut FuncArgs) -> Result<Self, ArgumentError> {
FuncArgs::from_args(vm, args).map(Self)
}
}

#[derive(FromArgs)]
struct PopArgs {
#[pyarg(positional, default = -1)]
Expand Down Expand Up @@ -604,10 +625,14 @@ impl Py<PyByteArray> {
}

#[pymethod]
fn take_bytes(&self, n: OptionalArg, vm: &VirtualMachine) -> PyResult<PyBytes> {
fn take_bytes(
&self,
TakeBytesArgs { n }: TakeBytesArgs,
vm: &VirtualMachine,
) -> PyResult<PyBytes> {
// __index__ can resize self or acquire/release exports. Convert before
// observing either the payload or the export epoch.
let n = match n.into_option().filter(|n| !vm.is_none(n)) {
let n = match n {
None => None,
Some(n) => {
let index = n
Expand Down Expand Up @@ -980,7 +1005,11 @@ impl Py<PyByteArray> {
}

#[pymethod]
fn split(&self, args: FuncArgs, vm: &VirtualMachine) -> PyResult<Vec<PyObjectRef>> {
fn split(
&self,
ByteArraySplitArgs(args): ByteArraySplitArgs,
vm: &VirtualMachine,
) -> PyResult<Vec<PyObjectRef>> {
// Converting the separator runs Python, which must not resize this
// bytearray, so the arguments are bound with the buffer exported.
let _export = self.begin_export();
Expand All @@ -990,7 +1019,11 @@ impl Py<PyByteArray> {
}

#[pymethod]
fn rsplit(&self, args: FuncArgs, vm: &VirtualMachine) -> PyResult<Vec<PyObjectRef>> {
fn rsplit(
&self,
ByteArraySplitArgs(args): ByteArraySplitArgs,
vm: &VirtualMachine,
) -> PyResult<Vec<PyObjectRef>> {
let _export = self.begin_export();
let options: ByteInnerSplitOptions = args.bind_for(vm, "rsplit")?;
self.inner()
Expand Down
6 changes: 2 additions & 4 deletions crates/vm/src/builtins/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1486,8 +1486,7 @@ impl PyFrozenDict {
#[pymethod]
fn get(
zelf: &Py<Self>,
key: PyObjectRef,
default: OptionalArg<PyObjectRef>,
DictGetArgs { key, default }: DictGetArgs,
vm: &VirtualMachine,
) -> PyResult {
Ok(zelf
Expand All @@ -1503,8 +1502,7 @@ impl PyFrozenDict {
#[pyclassmethod]
fn fromkeys(
cls: PyTypeRef,
iterable: PyObjectRef,
value: OptionalArg<PyObjectRef>,
FromKeysArgs { iterable, value }: FromKeysArgs,
vm: &VirtualMachine,
) -> PyResult {
dict_fromkeys(cls, iterable, value.unwrap_or_else(|| vm.ctx.none()), vm)
Expand Down
4 changes: 2 additions & 2 deletions crates/vm/src/builtins/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -658,9 +658,9 @@ impl Py<PyRangeIterator> {
}

#[pymethod]
fn __setstate__(&self, object: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
fn __setstate__(&self, state: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
self.index.store(
range_state(&BigInt::from(self.length), &object, vm)?
range_state(&BigInt::from(self.length), &state, vm)?
.to_usize()
.unwrap(),
);
Expand Down
2 changes: 1 addition & 1 deletion crates/vm/src/builtins/sentinel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl PySentinel {
}

#[pymethod]
fn __deepcopy__(zelf: PyRef<Self>, _memo: PyObjectRef) -> PyRef<Self> {
fn __deepcopy__(zelf: PyRef<Self>, _object: PyObjectRef) -> PyRef<Self> {
zelf
}

Expand Down
4 changes: 2 additions & 2 deletions crates/vm/src/builtins/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,10 @@ impl Py<PySlice> {
#[pyclassmethod]
fn __class_getitem__(
cls: PyTypeRef,
args: PyObjectRef,
object: PyObjectRef,
vm: &VirtualMachine,
) -> PyResult<PyGenericAlias> {
PyGenericAlias::from_args(cls, args, vm)
PyGenericAlias::from_args(cls, object, vm)
}
#[pyslot]
fn slot_new(cls: PyTypeRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult {
Expand Down
1 change: 1 addition & 0 deletions crates/vm/src/function/argument.rs
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,7 @@ arg_name! {
NameKwds = "kwds",
NameKws = "kws",
NameObjs = "objs",
NameObjects = "objects",
NameIterables = "iterables",
NameKeywords = "keywords",
NameFields = "fields",
Expand Down
4 changes: 2 additions & 2 deletions crates/vm/src/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ mod time;
pub use argument::{
ArgumentError, Callee, FromArgOptional, FromArgs, FuncArgs, IntoFuncArgs, KwArgs, KwArgsMap,
NameArgs, NameChanges, NameCoordinates, NameExcInfo, NameFields, NameIntegers, NameIterables,
NameKeywords, NameKwargs, NameKwds, NameKws, NameObjs, NameOthers, NameValues, OptionalArg,
OptionalArgDefault, OptionalOption, PosArgs, PositionalIterable,
NameKeywords, NameKwargs, NameKwds, NameKws, NameObjects, NameObjs, NameOthers, NameValues,
OptionalArg, OptionalArgDefault, OptionalOption, PosArgs, PositionalIterable,
};
pub(crate) use argument::{
arity_message, given_by_name_and_position_message, unexpected_keyword_message,
Expand Down
19 changes: 12 additions & 7 deletions crates/vm/src/stdlib/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ mod builtins {
common::hash::PyHash,
function::{
ArgCallable, ArgIndex, ArgIntoBool, ArgIterable, ArgMapping, ArgStrOrBytesLike, Either,
FsPath, FuncArgs, KwArgs, NameKws, OptionalArg, PosArgs,
FsPath, FuncArgs, KwArgs, NameKws, NameObjects, OptionalArg, PosArgs,
},
protocol::{PyIter, PyIterReturn},
py_io,
Expand Down Expand Up @@ -806,8 +806,8 @@ mod builtins {
}

#[pyfunction]
fn hex(number: ArgIndex) -> String {
let number = number.into_int_ref();
fn hex(integer: ArgIndex) -> String {
let number = integer.into_int_ref();
let n = number.as_bigint();
format!("{n:#x}")
}
Expand Down Expand Up @@ -1074,8 +1074,8 @@ mod builtins {
}

#[pyfunction]
fn oct(number: ArgIndex, vm: &VirtualMachine) -> PyObjectRef {
let number = number.into_int_ref();
fn oct(integer: ArgIndex, vm: &VirtualMachine) -> PyObjectRef {
let number = integer.into_int_ref();
let n = number.as_bigint();
let s = if n.is_negative() {
format!("-0o{:o}", n.abs())
Expand Down Expand Up @@ -1160,7 +1160,11 @@ mod builtins {
}

#[pyfunction]
pub fn print(objects: PosArgs, options: PrintOptions, vm: &VirtualMachine) -> PyResult<()> {
pub fn print(
objects: PosArgs<PyObjectRef, NameObjects>,
options: PrintOptions,
vm: &VirtualMachine,
) -> PyResult<()> {
let file = match options.file {
Some(f) => f,
None => sys::get_stdout(vm)?,
Expand Down Expand Up @@ -1196,7 +1200,8 @@ mod builtins {
}

#[pyfunction]
pub fn reversed(sequence: PyObjectRef, vm: &VirtualMachine) -> PyResult {
pub fn reversed(object: PyObjectRef, vm: &VirtualMachine) -> PyResult {
let sequence = object;
if let Some(reversed_method) =
vm.get_method(sequence.clone(), identifier!(vm, __reversed__))
{
Expand Down
Loading