Skip to content

Commit

Permalink
small refactor in kwarg handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ariebovenberg committed Jul 13, 2024
1 parent bb53f87 commit bc99d48
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 26 deletions.
22 changes: 11 additions & 11 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,38 +171,38 @@ macro_rules! method_vararg(
pub(crate) struct KwargIter {
keys: *mut PyObject,
values: *const *mut PyObject,
len: isize,
i: isize,
size: isize,
pos: isize,
}

impl KwargIter {
pub(crate) unsafe fn new(keys: *mut PyObject, values: *const *mut PyObject) -> Self {
Self {
keys,
values,
len: if keys.is_null() {
size: if keys.is_null() {
0
} else {
PyTuple_GET_SIZE(keys) as isize
},
i: 0,
pos: 0,
}
}

pub(crate) fn length(&self) -> isize {
self.len
pub(crate) fn len(&self) -> isize {
self.size
}
}

impl Iterator for KwargIter {
type Item = (*mut PyObject, *mut PyObject);

fn next(&mut self) -> Option<Self::Item> {
if self.i >= self.len {
if self.pos >= self.size {
return None;
}
let i = self.i;
self.i = i + 1;
let i = self.pos;
self.pos = i + 1;
unsafe { Some((PyTuple_GET_ITEM(self.keys, i), *self.values.offset(i))) }
}
}
Expand Down Expand Up @@ -870,7 +870,7 @@ impl Disambiguate {
fname: &str,
) -> PyResult<Self> {
match kwargs.next() {
Some((name, value)) if kwargs.length() == 1 => {
Some((name, value)) if kwargs.len() == 1 => {
if name.kwarg_eq(str_disambiguate) {
Self::from_py(value)
} else {
Expand All @@ -884,7 +884,7 @@ impl Disambiguate {
Some(_) => Err(type_err!(
"{}() takes at most 1 keyword argument, got {}",
fname,
kwargs.length()
kwargs.len()
)),
None => Err(type_err!(
"{}() missing 1 required keyword-only argument: 'disambiguate'",
Expand Down
5 changes: 2 additions & 3 deletions src/local_datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ unsafe fn _shift_method(
negate: bool,
) -> PyReturn {
let fname = if negate { "subtract" } else { "add" };
// FUTURE: get fields all at once from State (this is faster)
let state = State::for_type(cls);
let mut months = 0;
let mut days = 0;
Expand All @@ -429,9 +430,7 @@ unsafe fn _shift_method(
match *args {
[arg] => {
match kwargs.next() {
Some((key, value))
if kwargs.length() == 1 && key.kwarg_eq(state.str_ignore_dst) =>
{
Some((key, value)) if kwargs.len() == 1 && key.kwarg_eq(state.str_ignore_dst) => {
ignore_dst = value == Py_True();
}
Some(_) => Err(type_err!(
Expand Down
6 changes: 2 additions & 4 deletions src/offset_datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ pub(crate) unsafe fn check_ignore_dst_kwarg(
) -> PyResult<()> {
match kwargs.next() {
Some((key, value))
if kwargs.length() == 1 && key.kwarg_eq(state.str_ignore_dst) && value == Py_True() =>
if kwargs.len() == 1 && key.kwarg_eq(state.str_ignore_dst) && value == Py_True() =>
{
Ok(())
}
Expand Down Expand Up @@ -751,9 +751,7 @@ unsafe fn _shift_method(
match *args {
[arg] => {
match kwargs.next() {
Some((key, value))
if kwargs.length() == 1 && key.kwarg_eq(state.str_ignore_dst) =>
{
Some((key, value)) if kwargs.len() == 1 && key.kwarg_eq(state.str_ignore_dst) => {
ignore_dst = value == Py_True();
}
None => {}
Expand Down
4 changes: 1 addition & 3 deletions src/system_datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -711,9 +711,7 @@ unsafe fn _shift_method(
match *args {
[arg] => {
match kwargs.next() {
Some((key, value))
if kwargs.length() == 1 && key.kwarg_eq(state.str_disambiguate) =>
{
Some((key, value)) if kwargs.len() == 1 && key.kwarg_eq(state.str_disambiguate) => {
dis = Some(Disambiguate::from_py(value)?)
}
Some(_) => Err(type_err!(
Expand Down
8 changes: 3 additions & 5 deletions src/zoned_datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,7 @@ unsafe fn check_from_timestamp_args_return_zoneinfo(
fname: &str,
) -> PyReturn {
match (args, kwargs.next()) {
(&[_], Some((key, value))) if kwargs.length() == 1 => {
(&[_], Some((key, value))) if kwargs.len() == 1 => {
if key.kwarg_eq(str_tz) {
call1(zoneinfo_type, value)
} else {
Expand All @@ -934,7 +934,7 @@ unsafe fn check_from_timestamp_args_return_zoneinfo(
_ => Err(type_err!(
"{}() expected 2 arguments, got {}",
fname,
args.len() + (kwargs.length() as usize)
args.len() + (kwargs.len() as usize)
)),
}
}
Expand Down Expand Up @@ -1139,9 +1139,7 @@ unsafe fn _shift_method(
match *args {
[arg] => {
match kwargs.next() {
Some((key, value))
if kwargs.length() == 1 && key.kwarg_eq(state.str_disambiguate) =>
{
Some((key, value)) if kwargs.len() == 1 && key.kwarg_eq(state.str_disambiguate) => {
dis = Some(Disambiguate::from_py(value)?)
}
None => {}
Expand Down

0 comments on commit bc99d48

Please sign in to comment.