-
Notifications
You must be signed in to change notification settings - Fork 787
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1440 from davidhewitt/fix-multiple-kw-only-arg
pyfunction: refactor argument extraction
- Loading branch information
Showing
16 changed files
with
608 additions
and
182 deletions.
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,16 @@ | ||
[package] | ||
authors = ["PyO3 Authors"] | ||
name = "pyo3-benchmarks" | ||
version = "0.1.0" | ||
description = "Python-based benchmarks for various PyO3 functionality" | ||
edition = "2018" | ||
|
||
[dependencies] | ||
|
||
[dependencies.pyo3] | ||
path = "../../" | ||
features = ["extension-module"] | ||
|
||
[lib] | ||
name = "_pyo3_benchmarks" | ||
crate-type = ["cdylib"] |
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,2 @@ | ||
include pyproject.toml Cargo.toml | ||
recursive-include src * |
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,17 @@ | ||
# rustapi_module | ||
|
||
A simple extension module built using PyO3. | ||
|
||
## Build | ||
|
||
```shell | ||
python setup.py install | ||
``` | ||
|
||
## Testing | ||
|
||
To test install tox globally and run | ||
|
||
```shell | ||
tox -e py | ||
``` |
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 @@ | ||
from ._pyo3_benchmarks import * |
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,6 @@ | ||
pip>=19.1 | ||
hypothesis>=3.55 | ||
pytest>=3.5.0 | ||
setuptools-rust>=0.10.2 | ||
psutil>=5.6 | ||
pytest-benchmark~=3.2 |
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,44 @@ | ||
import sys | ||
import platform | ||
|
||
from setuptools import setup | ||
from setuptools_rust import RustExtension | ||
|
||
|
||
def get_py_version_cfgs(): | ||
# For now each Cfg Py_3_X flag is interpreted as "at least 3.X" | ||
version = sys.version_info[0:2] | ||
py3_min = 6 | ||
out_cfg = [] | ||
for minor in range(py3_min, version[1] + 1): | ||
out_cfg.append("--cfg=Py_3_%d" % minor) | ||
|
||
if platform.python_implementation() == "PyPy": | ||
out_cfg.append("--cfg=PyPy") | ||
|
||
return out_cfg | ||
|
||
|
||
setup( | ||
name="pyo3-benchmarks", | ||
version="0.1.0", | ||
classifiers=[ | ||
"License :: OSI Approved :: MIT License", | ||
"Development Status :: 3 - Alpha", | ||
"Intended Audience :: Developers", | ||
"Programming Language :: Python", | ||
"Programming Language :: Rust", | ||
"Operating System :: POSIX", | ||
"Operating System :: MacOS :: MacOS X", | ||
], | ||
packages=["pyo3_benchmarks"], | ||
rust_extensions=[ | ||
RustExtension( | ||
"pyo3_benchmarks._pyo3_benchmarks", | ||
rustc_flags=get_py_version_cfgs(), | ||
debug=False, | ||
), | ||
], | ||
include_package_data=True, | ||
zip_safe=False, | ||
) |
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,33 @@ | ||
use pyo3::prelude::*; | ||
use pyo3::types::{PyDict, PyTuple}; | ||
use pyo3::wrap_pyfunction; | ||
|
||
#[pyfunction(args = "*", kwargs = "**")] | ||
fn args_and_kwargs<'a>( | ||
args: &'a PyTuple, | ||
kwargs: Option<&'a PyDict>, | ||
) -> (&'a PyTuple, Option<&'a PyDict>) { | ||
(args, kwargs) | ||
} | ||
|
||
#[pyfunction(a, b = 2, args = "*", c = 4, kwargs = "**")] | ||
fn mixed_args<'a>( | ||
a: i32, | ||
b: i32, | ||
args: &'a PyTuple, | ||
c: i32, | ||
kwargs: Option<&'a PyDict>, | ||
) -> (i32, i32, &'a PyTuple, i32, Option<&'a PyDict>) { | ||
(a, b, args, c, kwargs) | ||
} | ||
|
||
#[pyfunction] | ||
fn no_args() {} | ||
|
||
#[pymodule] | ||
fn _pyo3_benchmarks(_py: Python<'_>, m: &PyModule) -> PyResult<()> { | ||
m.add_function(wrap_pyfunction!(args_and_kwargs, m)?)?; | ||
m.add_function(wrap_pyfunction!(mixed_args, m)?)?; | ||
m.add_function(wrap_pyfunction!(no_args, m)?)?; | ||
Ok(()) | ||
} |
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,46 @@ | ||
import pyo3_benchmarks | ||
|
||
|
||
def test_args_and_kwargs(benchmark): | ||
benchmark(pyo3_benchmarks.args_and_kwargs, 1, 2, 3, a=4, foo=10) | ||
|
||
|
||
def args_and_kwargs_py(*args, **kwargs): | ||
return (args, kwargs) | ||
|
||
|
||
def test_args_and_kwargs_py(benchmark): | ||
rust = pyo3_benchmarks.args_and_kwargs(1, 2, 3, bar=4, foo=10) | ||
py = args_and_kwargs_py(1, 2, 3, bar=4, foo=10) | ||
assert rust == py | ||
benchmark(args_and_kwargs_py, 1, 2, 3, bar=4, foo=10) | ||
|
||
|
||
def test_mixed_args(benchmark): | ||
benchmark(pyo3_benchmarks.mixed_args, 1, 2, 3, bar=4, foo=10) | ||
|
||
|
||
def mixed_args_py(a, b=2, *args, c=4, **kwargs): | ||
return (a, b, args, c, kwargs) | ||
|
||
|
||
def test_mixed_args_py(benchmark): | ||
rust = pyo3_benchmarks.mixed_args(1, 2, 3, bar=4, foo=10) | ||
py = mixed_args_py(1, 2, 3, bar=4, foo=10) | ||
assert rust == py | ||
benchmark(mixed_args_py, 1, 2, 3, bar=4, foo=10) | ||
|
||
|
||
def test_no_args(benchmark): | ||
benchmark(pyo3_benchmarks.no_args) | ||
|
||
|
||
def no_args_py(): | ||
return None | ||
|
||
|
||
def test_no_args_py(benchmark): | ||
rust = pyo3_benchmarks.no_args() | ||
py = no_args_py() | ||
assert rust == py | ||
benchmark(no_args_py) |
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,10 @@ | ||
[tox] | ||
# can't install from sdist because local pyo3 repo can't be included in the sdist | ||
skipsdist = true | ||
|
||
[testenv] | ||
description = Run the unit tests under {basepython} | ||
deps = -rrequirements-dev.txt | ||
commands = | ||
python setup.py install | ||
pytest {posargs} |
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
Oops, something went wrong.