Skip to content

merge SplitArgs + Generic support for FromArgs #1873

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 4 commits into from
Apr 24, 2020
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
23 changes: 14 additions & 9 deletions derive/src/from_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,11 @@ enum ParameterKind {

impl ParameterKind {
fn from_ident(ident: &Ident) -> Option<ParameterKind> {
if ident == "positional_only" {
Some(ParameterKind::PositionalOnly)
} else if ident == "positional_or_keyword" {
Some(ParameterKind::PositionalOrKeyword)
} else if ident == "keyword_only" {
Some(ParameterKind::KeywordOnly)
} else {
None
match ident.to_string().as_str() {
"positional_only" => Some(ParameterKind::PositionalOnly),
"positional_or_keyword" => Some(ParameterKind::PositionalOrKeyword),
"keyword_only" => Some(ParameterKind::KeywordOnly),
_ => None,
}
}
}
Expand Down Expand Up @@ -150,6 +147,13 @@ fn generate_field(field: &Field) -> Result<TokenStream2, Diagnostic> {
};

let name = &field.ident;
if let Some(name) = name {
if name.to_string().starts_with("_phantom") {
return Ok(quote! {
#name: std::marker::PhantomData,
});
}
}
let middle = quote! {
.map(|x| ::rustpython_vm::pyobject::TryFromObject::try_from_object(vm, x)).transpose()?
};
Expand Down Expand Up @@ -210,8 +214,9 @@ pub fn impl_from_args(input: DeriveInput) -> Result<TokenStream2, Diagnostic> {
};

let name = input.ident;
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
let output = quote! {
impl ::rustpython_vm::function::FromArgs for #name {
impl #impl_generics ::rustpython_vm::function::FromArgs for #name #ty_generics #where_clause {
fn from_args(
vm: &::rustpython_vm::VirtualMachine,
args: &mut ::rustpython_vm::function::PyFuncArgs
Expand Down
11 changes: 5 additions & 6 deletions vm/src/obj/objbytearray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ use std::convert::TryFrom;
use std::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard};

use super::objbyteinner::{
ByteInnerExpandtabsOptions, ByteInnerFindOptions, ByteInnerNewOptions, ByteInnerPaddingOptions,
ByteInnerSplitOptions, ByteInnerSplitlinesOptions, ByteInnerTranslateOptions, ByteOr,
PyByteInner,
ByteInnerFindOptions, ByteInnerNewOptions, ByteInnerPaddingOptions, ByteInnerSplitOptions,
ByteInnerTranslateOptions, ByteOr, PyByteInner,
};
use super::objint::PyIntRef;
use super::objiter;
use super::objslice::PySliceRef;
use super::objstr::{PyString, PyStringRef};
use super::objtype::PyClassRef;
use super::pystr::PyCommonString;
use super::pystr::{self, PyCommonString};
use crate::cformat::CFormatString;
use crate::function::{OptionalArg, OptionalOption};
use crate::obj::objstr::do_cformat_string;
Expand Down Expand Up @@ -440,12 +439,12 @@ impl PyByteArray {
}

#[pymethod(name = "expandtabs")]
fn expandtabs(&self, options: ByteInnerExpandtabsOptions) -> PyByteArray {
fn expandtabs(&self, options: pystr::ExpandTabsArgs) -> PyByteArray {
self.borrow_value().expandtabs(options).into()
}

#[pymethod(name = "splitlines")]
fn splitlines(&self, options: ByteInnerSplitlinesOptions, vm: &VirtualMachine) -> PyResult {
fn splitlines(&self, options: pystr::SplitLinesArgs, vm: &VirtualMachine) -> PyResult {
let as_bytes = self
.borrow_value()
.splitlines(options)
Expand Down
82 changes: 21 additions & 61 deletions vm/src/obj/objbyteinner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use super::objnone::PyNoneRef;
use super::objsequence::PySliceableSequence;
use super::objslice::PySliceRef;
use super::objstr::{self, PyString, PyStringRef};
use super::pystr::{self, PyCommonString, StringRange};
use super::pystr::{self, PyCommonString, PyCommonStringWrapper, StringRange};
use crate::function::{OptionalArg, OptionalOption};
use crate::pyhash;
use crate::pyobject::{
Expand Down Expand Up @@ -255,43 +255,7 @@ impl ByteInnerTranslateOptions {
}
}

#[derive(FromArgs)]
pub struct ByteInnerSplitOptions {
#[pyarg(positional_or_keyword, default = "None")]
sep: Option<PyByteInner>,
#[pyarg(positional_or_keyword, default = "-1")]
maxsplit: isize,
}

impl ByteInnerSplitOptions {
pub fn get_value(self, vm: &VirtualMachine) -> PyResult<(Option<Vec<u8>>, isize)> {
let sep = if let Some(s) = self.sep {
let sep = s.elements;
if sep.is_empty() {
return Err(vm.new_value_error("empty separator".to_owned()));
}
Some(sep)
} else {
None
};
Ok((sep, self.maxsplit))
}
}

#[derive(FromArgs)]
pub struct ByteInnerExpandtabsOptions {
#[pyarg(positional_or_keyword, optional = true)]
tabsize: OptionalArg<PyIntRef>,
}

impl ByteInnerExpandtabsOptions {
pub fn get_value(self) -> usize {
match self.tabsize.into_option() {
Some(int) => int.as_bigint().to_usize().unwrap_or(0),
None => 8,
}
}
}
pub type ByteInnerSplitOptions = pystr::SplitArgs<PyByteInner, [u8], u8>;

#[derive(FromArgs)]
pub struct ByteInnerSplitlinesOptions {
Expand Down Expand Up @@ -967,19 +931,13 @@ impl PyByteInner {
where
F: Fn(&[u8], &VirtualMachine) -> PyObjectRef,
{
let (sep, maxsplit) = options.get_value(vm)?;
let sep_ref = match sep {
Some(ref v) => Some(&v[..]),
None => None,
};
let elements = self.elements.py_split(
sep_ref,
maxsplit,
options,
vm,
|v, s, vm| v.split_str(s).map(|v| convert(v, vm)).collect(),
|v, s, n, vm| v.splitn_str(n, s).map(|v| convert(v, vm)).collect(),
|v, n, vm| v.py_split_whitespace(n, |v| convert(v, vm)),
);
)?;
Ok(vm.ctx.new_list(elements))
}

Expand All @@ -992,19 +950,13 @@ impl PyByteInner {
where
F: Fn(&[u8], &VirtualMachine) -> PyObjectRef,
{
let (sep, maxsplit) = options.get_value(vm)?;
let sep_ref = match sep {
Some(ref v) => Some(&v[..]),
None => None,
};
let mut elements = self.elements.py_split(
sep_ref,
maxsplit,
options,
vm,
|v, s, vm| v.rsplit_str(s).map(|v| convert(v, vm)).collect(),
|v, s, n, vm| v.rsplitn_str(n, s).map(|v| convert(v, vm)).collect(),
|v, n, vm| v.py_rsplit_whitespace(n, |v| convert(v, vm)),
);
)?;
elements.reverse();
Ok(vm.ctx.new_list(elements))
}
Expand Down Expand Up @@ -1047,8 +999,8 @@ impl PyByteInner {
Ok((front, has_mid, back))
}

pub fn expandtabs(&self, options: ByteInnerExpandtabsOptions) -> Vec<u8> {
let tabsize = options.get_value();
pub fn expandtabs(&self, options: pystr::ExpandTabsArgs) -> Vec<u8> {
let tabsize = options.tabsize();
let mut counter: usize = 0;
let mut res = vec![];

Expand Down Expand Up @@ -1079,9 +1031,7 @@ impl PyByteInner {
res
}

pub fn splitlines(&self, options: ByteInnerSplitlinesOptions) -> Vec<&[u8]> {
let keepends = options.get_value();

pub fn splitlines(&self, options: pystr::SplitLinesArgs) -> Vec<&[u8]> {
let mut res = vec![];

if self.elements.is_empty() {
Expand All @@ -1090,7 +1040,7 @@ impl PyByteInner {

let mut prev_index = 0;
let mut index = 0;
let keep = if keepends { 1 } else { 0 };
let keep = if options.keepends { 1 } else { 0 };
let slice = &self.elements;

while index < slice.len() {
Expand Down Expand Up @@ -1297,13 +1247,23 @@ pub fn bytes_zfill(bytes: &[u8], width: usize) -> Vec<u8> {
}
}

impl PyCommonStringWrapper<[u8]> for PyByteInner {
fn as_ref(&self) -> &[u8] {
&self.elements
}
}

const ASCII_WHITESPACES: [u8; 6] = [0x20, 0x09, 0x0a, 0x0c, 0x0d, 0x0b];

impl PyCommonString<'_, u8> for [u8] {
impl PyCommonString<u8> for [u8] {
fn get_slice(&self, range: std::ops::Range<usize>) -> &Self {
&self[range]
}

fn is_empty(&self) -> bool {
Self::is_empty(self)
}

fn len(&self) -> usize {
Self::len(self)
}
Expand Down
10 changes: 5 additions & 5 deletions vm/src/obj/objbytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ use std::ops::Deref;
use std::str::FromStr;

use super::objbyteinner::{
ByteInnerExpandtabsOptions, ByteInnerFindOptions, ByteInnerNewOptions, ByteInnerPaddingOptions,
ByteInnerSplitOptions, ByteInnerSplitlinesOptions, ByteInnerTranslateOptions, PyByteInner,
ByteInnerFindOptions, ByteInnerNewOptions, ByteInnerPaddingOptions, ByteInnerSplitOptions,
ByteInnerTranslateOptions, PyByteInner,
};
use super::objint::PyIntRef;
use super::objiter;
use super::objslice::PySliceRef;
use super::objstr::{PyString, PyStringRef};
use super::objtype::PyClassRef;
use super::pystr::PyCommonString;
use super::pystr::{self, PyCommonString};
use crate::cformat::CFormatString;
use crate::function::{OptionalArg, OptionalOption};
use crate::obj::objstr::do_cformat_string;
Expand Down Expand Up @@ -401,12 +401,12 @@ impl PyBytes {
}

#[pymethod(name = "expandtabs")]
fn expandtabs(&self, options: ByteInnerExpandtabsOptions) -> PyBytes {
fn expandtabs(&self, options: pystr::ExpandTabsArgs) -> PyBytes {
self.inner.expandtabs(options).into()
}

#[pymethod(name = "splitlines")]
fn splitlines(&self, options: ByteInnerSplitlinesOptions, vm: &VirtualMachine) -> PyResult {
fn splitlines(&self, options: pystr::SplitLinesArgs, vm: &VirtualMachine) -> PyResult {
let as_bytes = self
.inner
.splitlines(options)
Expand Down
Loading