mirror of
https://github.com/pybind/pybind11.git
synced 2026-03-14 20:27:47 +00:00
* Make argument_vector re-usable for other types. * Attempt to collect args into array for vectorcall * Revert "Attempt to collect args into array for vectorcall" This reverts commit418a034195. * Implement vectorcall args collector * pre-commit fixes * Checkpoint in moving to METH_FASTCALL * pre-commit fixes * Use the names tuple directly, cleaner code and less reference counting * Fix unit test, the code now holds more references It cannot re-use the incoming tuple as before, because it is no longer a tuple at all. So a new tuple must be created, which then holds references for each member. * Make clangtidy happy * Oops, _v is C++14 * style: pre-commit fixes * Minor code cleanup * Fix signed conversions * Fix args expansion This would be easier with `if constexpr` * style: pre-commit fixes * Code cleanup * fix(tests): Install multiple-interpreter test modules into wheel The `mod_per_interpreter_gil`, `mod_shared_interpreter_gil`, and `mod_per_interpreter_gil_with_singleton` modules were being built but not installed into the wheel when using scikit-build-core (SKBUILD=true). This caused iOS (and potentially Android) CIBW tests to fail with ModuleNotFoundError. Root cause analysis: - The main test targets have install() commands (line 531) - The PYBIND11_MULTIPLE_INTERPRETERS_TEST_MODULES were missing equivalent install() commands - For regular CMake builds, this wasn't a problem because LIBRARY_OUTPUT_DIRECTORY places the modules next to pybind11_tests - For wheel builds, only targets with explicit install() commands are included in the wheel This issue was latent until commitfee2527dchanged the test imports from `pytest.importorskip()` (graceful skip) to direct `import` statements (hard failure), which exposed the missing modules. Failing tests: - test_multiple_interpreters.py::test_independent_subinterpreters - test_multiple_interpreters.py::test_dependent_subinterpreters Error: ModuleNotFoundError: No module named 'mod_per_interpreter_gil' * tests: Pin numpy 2.4.0 for Python 3.14 CI tests Add numpy==2.4.0 requirement for Python 3.14 (both default and free-threaded builds). NumPy 2.4.0 is the first version to provide official PyPI wheels for Python 3.14: - numpy-2.4.0-cp314-cp314-manylinux_2_27_x86_64...whl (default) - numpy-2.4.0-cp314-cp314t-manylinux_2_27_x86_64...whl (free-threaded) Previously, CI was skipping all numpy-dependent tests for Python 3.14 because PIP_ONLY_BINARY was set and no wheels were available: SKIPPED [...] test_numpy_array.py:8: could not import 'numpy': No module named 'numpy' With this change, the full numpy test suite will run on Python 3.14, providing better test coverage for the newest Python version. Note: Using exact pin (==2.4.0) rather than compatible release (~=2.4.0) to ensure reproducible CI results with the first known-working version. * tests: Add verbose flag to CIBW pytest command Add `-v` to the pytest command in tests/pyproject.toml to help diagnose hanging tests in CIBW jobs (particularly iOS). This will show each test name as it runs, making it easier to identify which specific test is hanging. * tests: Skip subinterpreter tests on iOS, add pytest timeout - Add `IOS` platform constant to `tests/env.py` for consistency with existing `ANDROID`, `LINUX`, `MACOS`, `WIN`, `FREEBSD` constants. - Skip `test_multiple_interpreters.py` module on iOS. Subinterpreters are not supported in the iOS simulator environment. These tests were previously skipped implicitly because the modules weren't installed in the wheel; now that they are (commit6ed6d5a8), we need an explicit skip. - Change pytest timeout from 0 (disabled) to 120 seconds. This provides a safety net to catch hanging tests before the CI job times out after hours. Normal test runs complete in 33-55 seconds total (~1100 tests), so 120 seconds per test is very generous. - Add `-v` flag for verbose output to help diagnose any future issues. * More cleanups in argument vector, per comments. * Per Cursor, move all versions to Vectorcall since it has been supported since 3.8. This means getting rid of simple_collector, we can do the same with a constexpr if in the unpacking_collector. * Switch to a bool vec for the used_kwargs flag... This makes more sense and saves a sort, and the small_vector implementation means it will actually take less space than a vector of size_t elements. The most common case is that all kwargs are used. * Fix signedness for clang * Another signedness issue * tests: Disable pytest-timeout for Pyodide (no signal.setitimer) Pyodide runs in a WebAssembly sandbox without POSIX signals, so `signal.setitimer` is not available. This causes pytest-timeout to crash with `AttributeError: module 'signal' has no attribute 'setitimer'` when timeout > 0. Override the test-command for Pyodide to keep timeout=0 (disabled). * Combine temp storage and args into one vector It's a good bit faster at the cost of this one scary reinterpret_cast. * Phrasing * Delete incorrect comment At 6, the struct is 144 bytes (not 128 bytes as the comment said). * Fix push_back * Update push_back in argument_vector.h Co-authored-by: Aaron Gokaslan <aaronGokaslan@gmail.com> * style: pre-commit fixes * Use real types for these instead of object They can be null if you "steal" a null handle. * refactor: Replace small_vector<object> with ref_small_vector for explicit ownership Introduce `ref_small_vector` to manage PyObject* references in `unpacking_collector`, replacing the previous `small_vector<object>` approach. Primary goals: 1. **Maintainability**: The previous implementation relied on `sizeof(object) == sizeof(PyObject*)` and used a reinterpret_cast to pass the object array to vectorcall. This coupling to py::object's internal layout could break if someone adds a debug field or other member to py::handle/py::object in the future. 2. **Readability**: The new `push_back_steal()` vs `push_back_borrow()` API makes reference counting intent explicit at each call site, rather than relying on implicit py::object semantics. 3. **Intuitive code**: Storing `PyObject*` directly and passing it to `_PyObject_Vectorcall` without casts is straightforward and matches what the C API expects. No "scary" reinterpret_cast needed. Additional benefits: - `PyObject*` is trivially copyable, simplifying vector operations - Batch decref in destructor (tight loop vs N individual object destructors) - Self-documenting ownership semantics Design consideration: We considered folding the ref-counting functionality directly into `small_vector` via template specialization for `PyObject*`. We decided against this because: - It would give `small_vector<PyObject*, N>` a different interface than the generic `small_vector<T, N>` (steal/borrow vs push_back) - Someone might want a non-ref-counting `small_vector<PyObject*, N>` - The specialization behavior could surprise users expecting uniform semantics A separate `ref_small_vector` type makes the ref-counting behavior explicit and self-documenting, while keeping `small_vector` generic and predictable. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Ralf W. Grosse-Kunstleve <rgrossekunst@nvidia.com> Co-authored-by: Aaron Gokaslan <aaronGokaslan@gmail.com>
474 lines
18 KiB
Python
474 lines
18 KiB
Python
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
import env # noqa: F401
|
|
from pybind11_tests import kwargs_and_defaults as m
|
|
|
|
|
|
def test_function_signatures(doc):
|
|
assert (
|
|
doc(m.kw_func0)
|
|
== "kw_func0(arg0: typing.SupportsInt | typing.SupportsIndex, arg1: typing.SupportsInt | typing.SupportsIndex) -> str"
|
|
)
|
|
assert (
|
|
doc(m.kw_func1)
|
|
== "kw_func1(x: typing.SupportsInt | typing.SupportsIndex, y: typing.SupportsInt | typing.SupportsIndex) -> str"
|
|
)
|
|
assert (
|
|
doc(m.kw_func2)
|
|
== "kw_func2(x: typing.SupportsInt | typing.SupportsIndex = 100, y: typing.SupportsInt | typing.SupportsIndex = 200) -> str"
|
|
)
|
|
assert doc(m.kw_func3) == "kw_func3(data: str = 'Hello world!') -> None"
|
|
assert (
|
|
doc(m.kw_func4)
|
|
== "kw_func4(myList: collections.abc.Sequence[typing.SupportsInt | typing.SupportsIndex] = [13, 17]) -> str"
|
|
)
|
|
assert (
|
|
doc(m.kw_func_udl)
|
|
== "kw_func_udl(x: typing.SupportsInt | typing.SupportsIndex, y: typing.SupportsInt | typing.SupportsIndex = 300) -> str"
|
|
)
|
|
assert (
|
|
doc(m.kw_func_udl_z)
|
|
== "kw_func_udl_z(x: typing.SupportsInt | typing.SupportsIndex, y: typing.SupportsInt | typing.SupportsIndex = 0) -> str"
|
|
)
|
|
assert doc(m.args_function) == "args_function(*args) -> tuple"
|
|
assert (
|
|
doc(m.args_kwargs_function)
|
|
== "args_kwargs_function(*args, **kwargs) -> tuple[tuple, dict[str, typing.Any]]"
|
|
)
|
|
assert (
|
|
doc(m.args_kwargs_subclass_function)
|
|
== "args_kwargs_subclass_function(*args: str, **kwargs: str) -> tuple[tuple[str, ...], dict[str, str]]"
|
|
)
|
|
assert (
|
|
doc(m.KWClass.foo0)
|
|
== "foo0(self: m.kwargs_and_defaults.KWClass, arg0: typing.SupportsInt | typing.SupportsIndex, arg1: typing.SupportsFloat | typing.SupportsIndex) -> None"
|
|
)
|
|
assert (
|
|
doc(m.KWClass.foo1)
|
|
== "foo1(self: m.kwargs_and_defaults.KWClass, x: typing.SupportsInt | typing.SupportsIndex, y: typing.SupportsFloat | typing.SupportsIndex) -> None"
|
|
)
|
|
assert (
|
|
doc(m.kw_lb_func0)
|
|
== "kw_lb_func0(custom: m.kwargs_and_defaults.CustomRepr = array([[A, B], [C, D]])) -> None"
|
|
)
|
|
assert (
|
|
doc(m.kw_lb_func1)
|
|
== "kw_lb_func1(custom: m.kwargs_and_defaults.CustomRepr = array([[A, B], [C, D]])) -> None"
|
|
)
|
|
assert (
|
|
doc(m.kw_lb_func2)
|
|
== "kw_lb_func2(custom: m.kwargs_and_defaults.CustomRepr = array([[A, B], [C, D]])) -> None"
|
|
)
|
|
assert (
|
|
doc(m.kw_lb_func3)
|
|
== "kw_lb_func3(custom: m.kwargs_and_defaults.CustomRepr = array([[A, B], [C, D]])) -> None"
|
|
)
|
|
assert (
|
|
doc(m.kw_lb_func4)
|
|
== "kw_lb_func4(custom: m.kwargs_and_defaults.CustomRepr = array([[A, B], [C, D]])) -> None"
|
|
)
|
|
assert (
|
|
doc(m.kw_lb_func5)
|
|
== "kw_lb_func5(custom: m.kwargs_and_defaults.CustomRepr = array([[A, B], [C, D]])) -> None"
|
|
)
|
|
assert (
|
|
doc(m.kw_lb_func6)
|
|
== "kw_lb_func6(custom: m.kwargs_and_defaults.CustomRepr = ) -> None"
|
|
)
|
|
assert (
|
|
doc(m.kw_lb_func7)
|
|
== "kw_lb_func7(str_arg: str = 'First line.\\n Second line.') -> None"
|
|
)
|
|
assert (
|
|
doc(m.kw_lb_func8)
|
|
== "kw_lb_func8(custom: m.kwargs_and_defaults.CustomRepr = ) -> None"
|
|
)
|
|
|
|
|
|
def test_named_arguments():
|
|
assert m.kw_func0(5, 10) == "x=5, y=10"
|
|
|
|
assert m.kw_func1(5, 10) == "x=5, y=10"
|
|
assert m.kw_func1(5, y=10) == "x=5, y=10"
|
|
assert m.kw_func1(y=10, x=5) == "x=5, y=10"
|
|
|
|
assert m.kw_func2() == "x=100, y=200"
|
|
assert m.kw_func2(5) == "x=5, y=200"
|
|
assert m.kw_func2(x=5) == "x=5, y=200"
|
|
assert m.kw_func2(y=10) == "x=100, y=10"
|
|
assert m.kw_func2(5, 10) == "x=5, y=10"
|
|
assert m.kw_func2(x=5, y=10) == "x=5, y=10"
|
|
|
|
with pytest.raises(TypeError) as excinfo:
|
|
# noinspection PyArgumentList
|
|
m.kw_func2(x=5, y=10, z=12)
|
|
assert excinfo.match(
|
|
r"(?s)^kw_func2\(\): incompatible.*Invoked with: kwargs: ((x=5|y=10|z=12)(, |$)){3}$"
|
|
)
|
|
|
|
assert m.kw_func4() == "{13 17}"
|
|
assert m.kw_func4(myList=[1, 2, 3]) == "{1 2 3}"
|
|
|
|
assert m.kw_func_udl(x=5, y=10) == "x=5, y=10"
|
|
assert m.kw_func_udl_z(x=5) == "x=5, y=0"
|
|
|
|
|
|
def test_arg_and_kwargs():
|
|
args = "arg1_value", "arg2_value", 3
|
|
assert m.args_function(*args) == args
|
|
|
|
args = "a1", "a2"
|
|
kwargs = {"arg3": "a3", "arg4": 4}
|
|
assert m.args_kwargs_function(*args, **kwargs) == (args, kwargs)
|
|
assert m.args_kwargs_subclass_function(*args, **kwargs) == (args, kwargs)
|
|
|
|
|
|
def test_mixed_args_and_kwargs(msg):
|
|
mpa = m.mixed_plus_args
|
|
mpk = m.mixed_plus_kwargs
|
|
mpak = m.mixed_plus_args_kwargs
|
|
mpakd = m.mixed_plus_args_kwargs_defaults
|
|
|
|
assert mpa(1, 2.5, 4, 99.5, None) == (1, 2.5, (4, 99.5, None))
|
|
assert mpa(1, 2.5) == (1, 2.5, ())
|
|
with pytest.raises(TypeError) as excinfo:
|
|
assert mpa(1)
|
|
assert (
|
|
msg(excinfo.value)
|
|
== """
|
|
mixed_plus_args(): incompatible function arguments. The following argument types are supported:
|
|
1. (arg0: typing.SupportsInt | typing.SupportsIndex, arg1: typing.SupportsFloat | typing.SupportsIndex, *args) -> tuple[int, float, tuple]
|
|
|
|
Invoked with: 1
|
|
"""
|
|
)
|
|
with pytest.raises(TypeError) as excinfo:
|
|
assert mpa()
|
|
assert (
|
|
msg(excinfo.value)
|
|
== """
|
|
mixed_plus_args(): incompatible function arguments. The following argument types are supported:
|
|
1. (arg0: typing.SupportsInt | typing.SupportsIndex, arg1: typing.SupportsFloat | typing.SupportsIndex, *args) -> tuple[int, float, tuple]
|
|
|
|
Invoked with:
|
|
"""
|
|
)
|
|
|
|
assert mpk(-2, 3.5, pi=3.14159, e=2.71828) == (
|
|
-2,
|
|
3.5,
|
|
{"e": 2.71828, "pi": 3.14159},
|
|
)
|
|
assert mpak(7, 7.7, 7.77, 7.777, 7.7777, minusseven=-7) == (
|
|
7,
|
|
7.7,
|
|
(7.77, 7.777, 7.7777),
|
|
{"minusseven": -7},
|
|
)
|
|
assert mpakd() == (1, 3.14159, (), {})
|
|
assert mpakd(3) == (3, 3.14159, (), {})
|
|
assert mpakd(j=2.71828) == (1, 2.71828, (), {})
|
|
assert mpakd(k=42) == (1, 3.14159, (), {"k": 42})
|
|
assert mpakd(1, 1, 2, 3, 5, 8, then=13, followedby=21) == (
|
|
1,
|
|
1,
|
|
(2, 3, 5, 8),
|
|
{"then": 13, "followedby": 21},
|
|
)
|
|
# Arguments specified both positionally and via kwargs should fail:
|
|
with pytest.raises(TypeError) as excinfo:
|
|
assert mpakd(1, i=1)
|
|
assert (
|
|
msg(excinfo.value)
|
|
== """
|
|
mixed_plus_args_kwargs_defaults(): incompatible function arguments. The following argument types are supported:
|
|
1. (i: typing.SupportsInt | typing.SupportsIndex = 1, j: typing.SupportsFloat | typing.SupportsIndex = 3.14159, *args, **kwargs) -> tuple[int, float, tuple, dict[str, typing.Any]]
|
|
|
|
Invoked with: 1; kwargs: i=1
|
|
"""
|
|
)
|
|
with pytest.raises(TypeError) as excinfo:
|
|
assert mpakd(1, 2, j=1)
|
|
assert (
|
|
msg(excinfo.value)
|
|
== """
|
|
mixed_plus_args_kwargs_defaults(): incompatible function arguments. The following argument types are supported:
|
|
1. (i: typing.SupportsInt | typing.SupportsIndex = 1, j: typing.SupportsFloat | typing.SupportsIndex = 3.14159, *args, **kwargs) -> tuple[int, float, tuple, dict[str, typing.Any]]
|
|
|
|
Invoked with: 1, 2; kwargs: j=1
|
|
"""
|
|
)
|
|
|
|
# Arguments after a py::args are automatically keyword-only (pybind 2.9+)
|
|
assert m.args_kwonly(2, 2.5, z=22) == (2, 2.5, (), 22)
|
|
assert m.args_kwonly(2, 2.5, "a", "b", "c", z=22) == (2, 2.5, ("a", "b", "c"), 22)
|
|
assert m.args_kwonly(z=22, i=4, j=16) == (4, 16, (), 22)
|
|
|
|
with pytest.raises(TypeError) as excinfo:
|
|
assert m.args_kwonly(2, 2.5, 22) # missing z= keyword
|
|
assert (
|
|
msg(excinfo.value)
|
|
== """
|
|
args_kwonly(): incompatible function arguments. The following argument types are supported:
|
|
1. (i: typing.SupportsInt | typing.SupportsIndex, j: typing.SupportsFloat | typing.SupportsIndex, *args, z: typing.SupportsInt | typing.SupportsIndex) -> tuple[int, float, tuple, int]
|
|
|
|
Invoked with: 2, 2.5, 22
|
|
"""
|
|
)
|
|
|
|
assert m.args_kwonly_kwargs(i=1, k=4, j=10, z=-1, y=9) == (
|
|
1,
|
|
10,
|
|
(),
|
|
-1,
|
|
{"k": 4, "y": 9},
|
|
)
|
|
assert m.args_kwonly_kwargs(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, z=11, y=12) == (
|
|
1,
|
|
2,
|
|
(3, 4, 5, 6, 7, 8, 9, 10),
|
|
11,
|
|
{"y": 12},
|
|
)
|
|
assert (
|
|
m.args_kwonly_kwargs.__doc__
|
|
== "args_kwonly_kwargs(i: typing.SupportsInt | typing.SupportsIndex, j: typing.SupportsFloat | typing.SupportsIndex, *args, z: typing.SupportsInt | typing.SupportsIndex, **kwargs) -> tuple[int, float, tuple, int, dict[str, typing.Any]]\n"
|
|
)
|
|
|
|
assert (
|
|
m.args_kwonly_kwargs_defaults.__doc__
|
|
== "args_kwonly_kwargs_defaults(i: typing.SupportsInt | typing.SupportsIndex = 1, j: typing.SupportsFloat | typing.SupportsIndex = 3.14159, *args, z: typing.SupportsInt | typing.SupportsIndex = 42, **kwargs) -> tuple[int, float, tuple, int, dict[str, typing.Any]]\n"
|
|
)
|
|
assert m.args_kwonly_kwargs_defaults() == (1, 3.14159, (), 42, {})
|
|
assert m.args_kwonly_kwargs_defaults(2) == (2, 3.14159, (), 42, {})
|
|
assert m.args_kwonly_kwargs_defaults(z=-99) == (1, 3.14159, (), -99, {})
|
|
assert m.args_kwonly_kwargs_defaults(5, 6, 7, 8) == (5, 6, (7, 8), 42, {})
|
|
assert m.args_kwonly_kwargs_defaults(5, 6, 7, m=8) == (5, 6, (7,), 42, {"m": 8})
|
|
assert m.args_kwonly_kwargs_defaults(5, 6, 7, m=8, z=9) == (5, 6, (7,), 9, {"m": 8})
|
|
|
|
|
|
def test_keyword_only_args(msg):
|
|
assert m.kw_only_all(i=1, j=2) == (1, 2)
|
|
assert m.kw_only_all(j=1, i=2) == (2, 1)
|
|
|
|
with pytest.raises(TypeError) as excinfo:
|
|
assert m.kw_only_all(i=1) == (1,)
|
|
assert "incompatible function arguments" in str(excinfo.value)
|
|
|
|
with pytest.raises(TypeError) as excinfo:
|
|
assert m.kw_only_all(1, 2) == (1, 2)
|
|
assert "incompatible function arguments" in str(excinfo.value)
|
|
|
|
assert m.kw_only_some(1, k=3, j=2) == (1, 2, 3)
|
|
|
|
assert m.kw_only_with_defaults(z=8) == (3, 4, 5, 8)
|
|
assert m.kw_only_with_defaults(2, z=8) == (2, 4, 5, 8)
|
|
assert m.kw_only_with_defaults(2, j=7, k=8, z=9) == (2, 7, 8, 9)
|
|
assert m.kw_only_with_defaults(2, 7, z=9, k=8) == (2, 7, 8, 9)
|
|
|
|
assert m.kw_only_mixed(1, j=2) == (1, 2)
|
|
assert m.kw_only_mixed(j=2, i=3) == (3, 2)
|
|
assert m.kw_only_mixed(i=2, j=3) == (2, 3)
|
|
|
|
assert m.kw_only_plus_more(4, 5, k=6, extra=7) == (4, 5, 6, {"extra": 7})
|
|
assert m.kw_only_plus_more(3, k=5, j=4, extra=6) == (3, 4, 5, {"extra": 6})
|
|
assert m.kw_only_plus_more(2, k=3, extra=4) == (2, -1, 3, {"extra": 4})
|
|
|
|
with pytest.raises(TypeError) as excinfo:
|
|
assert m.kw_only_mixed(i=1) == (1,)
|
|
assert "incompatible function arguments" in str(excinfo.value)
|
|
|
|
with pytest.raises(RuntimeError) as excinfo:
|
|
m.register_invalid_kw_only(m)
|
|
assert (
|
|
msg(excinfo.value)
|
|
== """
|
|
arg(): cannot specify an unnamed argument after a kw_only() annotation or args() argument
|
|
"""
|
|
)
|
|
|
|
# https://github.com/pybind/pybind11/pull/3402#issuecomment-963341987
|
|
x = m.first_arg_kw_only(i=1)
|
|
x.method()
|
|
x.method(i=1, j=2)
|
|
assert (
|
|
m.first_arg_kw_only.__init__.__doc__
|
|
== "__init__(self: pybind11_tests.kwargs_and_defaults.first_arg_kw_only, *, i: typing.SupportsInt | typing.SupportsIndex = 0) -> None\n"
|
|
)
|
|
assert (
|
|
m.first_arg_kw_only.method.__doc__
|
|
== "method(self: pybind11_tests.kwargs_and_defaults.first_arg_kw_only, *, i: typing.SupportsInt | typing.SupportsIndex = 1, j: typing.SupportsInt | typing.SupportsIndex = 2) -> None\n"
|
|
)
|
|
|
|
|
|
def test_positional_only_args():
|
|
assert m.pos_only_all(1, 2) == (1, 2)
|
|
assert m.pos_only_all(2, 1) == (2, 1)
|
|
|
|
with pytest.raises(TypeError) as excinfo:
|
|
m.pos_only_all(i=1, j=2)
|
|
assert "incompatible function arguments" in str(excinfo.value)
|
|
|
|
assert m.pos_only_mix(1, 2) == (1, 2)
|
|
assert m.pos_only_mix(2, j=1) == (2, 1)
|
|
|
|
with pytest.raises(TypeError) as excinfo:
|
|
m.pos_only_mix(i=1, j=2)
|
|
assert "incompatible function arguments" in str(excinfo.value)
|
|
|
|
assert m.pos_kw_only_mix(1, 2, k=3) == (1, 2, 3)
|
|
assert m.pos_kw_only_mix(1, j=2, k=3) == (1, 2, 3)
|
|
|
|
with pytest.raises(TypeError) as excinfo:
|
|
m.pos_kw_only_mix(i=1, j=2, k=3)
|
|
assert "incompatible function arguments" in str(excinfo.value)
|
|
|
|
with pytest.raises(TypeError) as excinfo:
|
|
m.pos_kw_only_mix(1, 2, 3)
|
|
assert "incompatible function arguments" in str(excinfo.value)
|
|
|
|
with pytest.raises(TypeError) as excinfo:
|
|
m.pos_only_def_mix()
|
|
assert "incompatible function arguments" in str(excinfo.value)
|
|
|
|
assert m.pos_only_def_mix(1) == (1, 2, 3)
|
|
assert m.pos_only_def_mix(1, 4) == (1, 4, 3)
|
|
assert m.pos_only_def_mix(1, 4, 7) == (1, 4, 7)
|
|
assert m.pos_only_def_mix(1, 4, k=7) == (1, 4, 7)
|
|
|
|
with pytest.raises(TypeError) as excinfo:
|
|
m.pos_only_def_mix(1, j=4)
|
|
assert "incompatible function arguments" in str(excinfo.value)
|
|
|
|
# Mix it with args and kwargs:
|
|
assert (
|
|
m.args_kwonly_full_monty.__doc__
|
|
== "args_kwonly_full_monty(arg0: typing.SupportsInt | typing.SupportsIndex = 1, arg1: typing.SupportsInt | typing.SupportsIndex = 2, /, j: typing.SupportsFloat | typing.SupportsIndex = 3.14159, *args, z: typing.SupportsInt | typing.SupportsIndex = 42, **kwargs) -> tuple[int, int, float, tuple, int, dict[str, typing.Any]]\n"
|
|
)
|
|
assert m.args_kwonly_full_monty() == (1, 2, 3.14159, (), 42, {})
|
|
assert m.args_kwonly_full_monty(8) == (8, 2, 3.14159, (), 42, {})
|
|
assert m.args_kwonly_full_monty(8, 9) == (8, 9, 3.14159, (), 42, {})
|
|
assert m.args_kwonly_full_monty(8, 9, 10) == (8, 9, 10.0, (), 42, {})
|
|
assert m.args_kwonly_full_monty(3, 4, 5, 6, 7, m=8, z=9) == (
|
|
3,
|
|
4,
|
|
5.0,
|
|
(
|
|
6,
|
|
7,
|
|
),
|
|
9,
|
|
{"m": 8},
|
|
)
|
|
assert m.args_kwonly_full_monty(3, 4, 5, 6, 7, m=8, z=9) == (
|
|
3,
|
|
4,
|
|
5.0,
|
|
(
|
|
6,
|
|
7,
|
|
),
|
|
9,
|
|
{"m": 8},
|
|
)
|
|
assert m.args_kwonly_full_monty(5, j=7, m=8, z=9) == (5, 2, 7.0, (), 9, {"m": 8})
|
|
assert m.args_kwonly_full_monty(i=5, j=7, m=8, z=9) == (
|
|
1,
|
|
2,
|
|
7.0,
|
|
(),
|
|
9,
|
|
{"i": 5, "m": 8},
|
|
)
|
|
|
|
# pos_only at the beginning of the argument list was "broken" in how it was displayed (though
|
|
# this is fairly useless in practice). Related to:
|
|
# https://github.com/pybind/pybind11/pull/3402#issuecomment-963341987
|
|
assert (
|
|
m.first_arg_kw_only.pos_only.__doc__
|
|
== "pos_only(self: pybind11_tests.kwargs_and_defaults.first_arg_kw_only, /, i: typing.SupportsInt | typing.SupportsIndex, j: typing.SupportsInt | typing.SupportsIndex) -> None\n"
|
|
)
|
|
|
|
|
|
def test_signatures():
|
|
assert (
|
|
m.kw_only_all.__doc__
|
|
== "kw_only_all(*, i: typing.SupportsInt | typing.SupportsIndex, j: typing.SupportsInt | typing.SupportsIndex) -> tuple[int, int]\n"
|
|
)
|
|
assert (
|
|
m.kw_only_mixed.__doc__
|
|
== "kw_only_mixed(i: typing.SupportsInt | typing.SupportsIndex, *, j: typing.SupportsInt | typing.SupportsIndex) -> tuple[int, int]\n"
|
|
)
|
|
assert (
|
|
m.pos_only_all.__doc__
|
|
== "pos_only_all(i: typing.SupportsInt | typing.SupportsIndex, j: typing.SupportsInt | typing.SupportsIndex, /) -> tuple[int, int]\n"
|
|
)
|
|
assert (
|
|
m.pos_only_mix.__doc__
|
|
== "pos_only_mix(i: typing.SupportsInt | typing.SupportsIndex, /, j: typing.SupportsInt | typing.SupportsIndex) -> tuple[int, int]\n"
|
|
)
|
|
assert (
|
|
m.pos_kw_only_mix.__doc__
|
|
== "pos_kw_only_mix(i: typing.SupportsInt | typing.SupportsIndex, /, j: typing.SupportsInt | typing.SupportsIndex, *, k: typing.SupportsInt | typing.SupportsIndex) -> tuple[int, int, int]\n"
|
|
)
|
|
|
|
|
|
@pytest.mark.skipif("env.GRAALPY", reason="Different refcounting mechanism")
|
|
def test_args_refcount():
|
|
"""Issue/PR #1216 - py::args elements get double-inc_ref()ed when combined with regular
|
|
arguments"""
|
|
refcount = m.arg_refcount_h
|
|
|
|
myval = object()
|
|
expected = refcount(myval)
|
|
assert m.arg_refcount_h(myval) == expected
|
|
assert m.arg_refcount_o(myval) == expected + 1
|
|
assert m.arg_refcount_h(myval) == expected
|
|
assert refcount(myval) == expected
|
|
|
|
assert m.mixed_plus_args(1, 2.0, "a", myval) == (1, 2.0, ("a", myval))
|
|
assert refcount(myval) == expected
|
|
|
|
assert m.mixed_plus_kwargs(3, 4.0, a=1, b=myval) == (3, 4.0, {"a": 1, "b": myval})
|
|
assert refcount(myval) == expected
|
|
|
|
assert m.args_function(-1, myval) == (-1, myval)
|
|
assert refcount(myval) == expected
|
|
|
|
assert m.mixed_plus_args_kwargs(5, 6.0, myval, a=myval) == (
|
|
5,
|
|
6.0,
|
|
(myval,),
|
|
{"a": myval},
|
|
)
|
|
assert refcount(myval) == expected
|
|
|
|
assert m.args_kwargs_function(7, 8, myval, a=1, b=myval) == (
|
|
(7, 8, myval),
|
|
{"a": 1, "b": myval},
|
|
)
|
|
assert refcount(myval) == expected
|
|
|
|
assert m.args_kwargs_subclass_function(7, 8, myval, a=1, b=myval) == (
|
|
(7, 8, myval),
|
|
{"a": 1, "b": myval},
|
|
)
|
|
assert refcount(myval) == expected
|
|
|
|
exp3 = refcount(myval, myval, myval)
|
|
# if we have to create a new tuple internally, then it will hold an extra reference for each item in it.
|
|
assert m.args_refcount(myval, myval, myval) == (exp3 + 3, exp3 + 3, exp3 + 3)
|
|
assert refcount(myval) == expected
|
|
|
|
# This function takes the first arg as a `py::object` and the rest as a `py::args`. Unlike the
|
|
# previous case, when we have both positional and `py::args` we need to construct a new tuple
|
|
# for the `py::args`; in the previous case, we could simply inc_ref and pass on Python's input
|
|
# tuple without having to inc_ref the individual elements, but here we can't, hence the extra
|
|
# refs.
|
|
exp3_3 = exp3 + 3
|
|
assert m.mixed_args_refcount(myval, myval, myval) == (exp3_3, exp3_3, exp3_3)
|
|
|
|
assert m.class_default_argument() == "<class 'decimal.Decimal'>"
|