forked from PyO3/pyo3
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
kwargs was broken by a check for the number of given arguments. Only apply this check if no arbitary number of keyword arguments are allowed by a "**" parameter of `#[args(...)`. Closes PyO3#318
- Loading branch information
1 parent
27ef337
commit fbd0126
Showing
2 changed files
with
53 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#![feature(custom_attribute)] | ||
#![feature(specialization)] | ||
|
||
extern crate pyo3; | ||
|
||
use pyo3::prelude::*; | ||
use pyo3::types::{PyDict, PyTuple}; | ||
|
||
#[macro_use] | ||
mod common; | ||
|
||
#[pyclass] | ||
struct MyClass {} | ||
|
||
#[pymethods] | ||
impl MyClass { | ||
#[staticmethod] | ||
#[args(args = "*")] | ||
fn test_args(args: &PyTuple) -> PyResult<&PyTuple> { | ||
Ok(args) | ||
} | ||
|
||
#[staticmethod] | ||
#[args(kwargs = "**")] | ||
fn test_kwargs(kwargs: Option<&PyDict>) -> PyResult<Option<&PyDict>> { | ||
Ok(kwargs) | ||
} | ||
} | ||
|
||
#[test] | ||
fn variable_args() { | ||
let gil = Python::acquire_gil(); | ||
let py = gil.python(); | ||
let my_obj = py.get_type::<MyClass>(); | ||
py_assert!(py, my_obj, "my_obj.test_args() == ()"); | ||
py_assert!(py, my_obj, "my_obj.test_args(1) == (1,)"); | ||
py_assert!(py, my_obj, "my_obj.test_args(1, 2) == (1, 2)"); | ||
} | ||
|
||
#[test] | ||
fn variable_kwargs() { | ||
let gil = Python::acquire_gil(); | ||
let py = gil.python(); | ||
let my_obj = py.get_type::<MyClass>(); | ||
py_assert!(py, my_obj, "my_obj.test_kwargs() == None"); | ||
py_assert!(py, my_obj, "my_obj.test_kwargs(test=1) == {'test': 1}"); | ||
py_assert!( | ||
py, | ||
my_obj, | ||
"my_obj.test_kwargs(test1=1, test2=2) == {'test1':1, 'test2':2}" | ||
); | ||
} |