pybind / pybind/pybind11

[BUG]: 6 tests fail

Open
#5,240 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

triage
Dominant language
C++
Stars
18k
Forks
2.3k
Avg merge
5d 17h
Merged PRs (30d)
10

Description

Required prerequisites
What version (or hash if on master) of pybind11 are you using?

2.13.1

Problem description
========================================================================================= FAILURES ==========================================================================================
____________________________________________________________________________________ test_class_refcount ____________________________________________________________________________________

    @pytest.mark.xfail("env.PYPY")
    def test_class_refcount():
        """Instances must correctly increase/decrease the reference count of their types (#1029)"""
        from sys import getrefcount
    
        class PyDog(m.Dog):
            pass
    
        for cls in m.Dog, PyDog:
            refcount_1 = getrefcount(cls)
            molly = [cls("Molly") for _ in range(10)]
            refcount_2 = getrefcount(cls)
    
            del molly
            pytest.gc_collect()
            refcount_3 = getrefcount(cls)
    
>           assert refcount_1 == refcount_3
E           assert 10 == 9

PyDog      = <class 'test_class.test_class_refcount.<locals>.PyDog'>
cls        = <class 'pybind11_tests.class_.Dog'>
getrefcount = <built-in function getrefcount>
refcount_1 = 10
refcount_2 = 20
refcount_3 = 9

../../pybind11-2.13.1/tests/test_class.py:381: AssertionError
__________________________________________________________________________ test_cross_module_exception_translator ___________________________________________________________________________

    @pytest.mark.xfail(
        "env.MACOS and (env.PYPY or pybind11_tests.compiler_info.startswith('Homebrew Clang'))",
        raises=RuntimeError,
        reason="See Issue #2847, PR #2999, PR #4324",
    )
    def test_cross_module_exception_translator():
        with pytest.raises(KeyError):
            # translator registered in cross_module_tests
>           m.throw_should_be_translated_to_key_error()
E           RuntimeError


../../pybind11-2.13.1/tests/test_exceptions.py:85: RuntimeError
______________________________________________________________________________________ test_to_python _______________________________________________________________________________________

    @pytest.mark.xfail(
        env.PYPY, reason="PyPy 7.3.7 doesn't clear this anymore", strict=False
    )
    def test_to_python():
        mat = m.Matrix(5, 4)
        assert memoryview(mat).shape == (5, 4)
    
        assert mat[2, 3] == 0
        mat[2, 3] = 4.0
        mat[3, 2] = 7.0
        assert mat[2, 3] == 4
        assert mat[3, 2] == 7
        assert struct.unpack_from("f", mat, (3 * 4 + 2) * 4) == (7,)
        assert struct.unpack_from("f", mat, (2 * 4 + 3) * 4) == (4,)
    
        mat2 = np.array(mat, copy=False)
        assert mat2.shape == (5, 4)
        assert abs(mat2).sum() == 11
        assert mat2[2, 3] == 4
        assert mat2[3, 2] == 7
        mat2[2, 3] = 5
        assert mat2[2, 3] == 5
    
        cstats = ConstructorStats.get(m.Matrix)
        assert cstats.alive() == 1
        del mat
        pytest.gc_collect()
        assert cstats.alive() == 1
        del mat2  # holds a mat reference
        pytest.gc_collect()
        assert cstats.alive() == 0
>       assert cstats.values() == ["5x4 matrix"]
E       AssertionError: assert ['5x5 matrix', '5x4 matrix'] == ['5x4 matrix']
E         
E         At index 0 diff: '5x5 matrix' != '5x4 matrix'
E         Left contains one more item: '5x4 matrix'
E         Use -v to get more diff

cstats     = <pybind11_tests.ConstructorStats object at 0x3ac0d50eb230>

../../pybind11-2.13.1/tests/test_buffers.py:129: AssertionError
----------------------------------------------------------------------------------- Captured stdout call ------------------------------------------------------------------------------------
### test_submodule_buffers(module_&)::Matrix @ 0x3ac0d47f71a0 created 5x4 matrix
### test_submodule_buffers(module_&)::Matrix @ 0x3ac0d47f71a0 destroyed 5x4 matrix
__________________________________________________________________________________ test_init_factory_basic __________________________________________________________________________________

    def test_init_factory_basic():
        """Tests py::init_factory() wrapper around various ways of returning the object"""
    
        cstats = [
            ConstructorStats.get(c)
            for c in [m.TestFactory1, m.TestFactory2, m.TestFactory3]
        ]
        cstats[0].alive()  # force gc
        n_inst = ConstructorStats.detail_reg_inst()
    
        x1 = m.TestFactory1(tag.unique_ptr, 3)
        assert x1.value == "3"
        y1 = m.TestFactory1(tag.pointer)
        assert y1.value == "(empty)"
        z1 = m.TestFactory1("hi!")
        assert z1.value == "hi!"
    
        assert ConstructorStats.detail_reg_inst() == n_inst + 3
    
        x2 = m.TestFactory2(tag.move)
        assert x2.value == "(empty2)"
        y2 = m.TestFactory2(tag.pointer, 7)
        assert y2.value == "7"
        z2 = m.TestFactory2(tag.unique_ptr, "hi again")
        assert z2.value == "hi again"
    
        assert ConstructorStats.detail_reg_inst() == n_inst + 6
    
        x3 = m.TestFactory3(tag.shared_ptr)
        assert x3.value == "(empty3)"
        y3 = m.TestFactory3(tag.pointer, 42)
        assert y3.value == "42"
        z3 = m.TestFactory3("bye")
        assert z3.value == "bye"
    
        for null_ptr_kind in [tag.null_ptr, tag.null_unique_ptr, tag.null_shared_ptr]:
            with pytest.raises(TypeError) as excinfo:
                m.TestFactory3(null_ptr_kind)
            assert (
                str(excinfo.value) == "pybind11::init(): factory function returned nullptr"
            )
    
        assert [i.alive() for i in cstats] == [3, 3, 3]
        assert ConstructorStats.detail_reg_inst() == n_inst + 9
    
        del x1, y2, y3, z3
        assert [i.alive() for i in cstats] == [2, 2, 1]
        assert ConstructorStats.detail_reg_inst() == n_inst + 5
        del x2, x3, y1, z1, z2
        assert [i.alive() for i in cstats] == [0, 0, 0]
        assert ConstructorStats.detail_reg_inst() == n_inst
    
>       assert [i.values() for i in cstats] == [
            ["3", "hi!"],
            ["7", "hi again"],
            ["42", "bye"],
        ]
E       AssertionError: assert [['3', 'hi!']...['42', 'bye']] == [['3', 'hi!']...['42', 'bye']]
E         
E         At index 1 diff: ['1', '1', '1', '7', 'hi again'] != ['7', 'hi again']
E         Use -v to get more diff

cstats     = [<pybind11_tests.ConstructorStats object at 0x3ac0d4ba17b0>, <pybind11_tests.ConstructorStats object at 0x3ac0d4ba01f0>, <pybind11_tests.ConstructorStats object at 0x3ac0d4ba3430>]
excinfo    = <ExceptionInfo TypeError('pybind11::init(): factory function returned nullptr') tblen=1>
n_inst     = 67
null_ptr_kind = <pybind11_tests.factory_constructors.tag.null_shared_ptr_tag object at 0x3ac0d3e1a1f0>

../../pybind11-2.13.1/tests/test_factory_constructors.py:64: AssertionError
----------------------------------------------------------------------------------- Captured stdout call ------------------------------------------------------------------------------------
### TestFactory1 @ 0x3ac0d447bf60 created 3
### TestFactory1 @ 0x3ac0d447bfa0 created via default constructor
### TestFactory1 @ 0x3ac0d47f74c0 created hi!
### TestFactory2 @ 0x3ac0d447bf00 created via default constructor
### TestFactory2 @ 0x3ac0d47f76a0 created 7
### TestFactory2 @ 0x4c3be870 created hi again
### TestFactory2 @ 0x3ac0d47f7080 created via move constructor
### TestFactory2 @ 0x4c3be870 destroyed
### TestFactory3 @ 0x3ac0d47f75a0 created via default constructor
### TestFactory3 @ 0x3ac0d47f77c0 created 42
### TestFactory3 @ 0x3ac0d47f77a0 created bye
### TestFactory1 @ 0x3ac0d447bf60 destroyed
### TestFactory2 @ 0x3ac0d47f76a0 destroyed
### TestFactory3 @ 0x3ac0d47f77c0 destroyed
### TestFactory3 @ 0x3ac0d47f77a0 destroyed
### TestFactory2 @ 0x3ac0d447bf00 destroyed
### TestFactory3 @ 0x3ac0d47f75a0 destroyed
### TestFactory1 @ 0x3ac0d447bfa0 destroyed
### TestFactory1 @ 0x3ac0d47f74c0 destroyed
### TestFactory2 @ 0x3ac0d47f7080 destroyed
_____________________________________________________________________________________ test_cpp_casting ______________________________________________________________________________________

    def test_cpp_casting():
        assert m.cpp_copy(m.fixed_r()) == 22.0
        assert m.cpp_copy(m.fixed_c()) == 22.0
        z = np.array([[5.0, 6], [7, 8]])
        assert m.cpp_copy(z) == 7.0
>       assert m.cpp_copy(m.get_cm_ref()) == 21.0
E       assert 31.0 == 21.0
E        +  where 31.0 = <built-in method cpp_copy of PyCapsule object at 0x3ac0d3d02370>(array([[ 0., 22., 20.],\n       [31., 37., 33.],\n       [41., 42., 38.]]))
E        +    where <built-in method cpp_copy of PyCapsule object at 0x3ac0d3d02370> = m.cpp_copy
E        +    and   array([[ 0., 22., 20.],\n       [31., 37., 33.],\n       [41., 42., 38.]]) = <built-in method get_cm_ref of PyCapsule object at 0x3ac0d3d029d0>()
E        +      where <built-in method get_cm_ref of PyCapsule object at 0x3ac0d3d029d0> = m.get_cm_ref

z          = array([[5., 6.],
       [7., 8.]])

../../pybind11-2.13.1/tests/test_eigen_matrix.py:123: AssertionError
______________________________________________________________________________________ test_smart_ptr _______________________________________________________________________________________

capture = <conftest.Capture object at 0x3ac0db1b34d0>

    def test_smart_ptr(capture):
        # Object1
        for i, o in enumerate(
            [m.make_object_1(), m.make_object_2(), m.MyObject1(3)], start=1
        ):
            assert o.getRefCount() == 1
            with capture:
                m.print_object_1(o)
                m.print_object_2(o)
                m.print_object_3(o)
                m.print_object_4(o)
            assert capture == f"MyObject1[{i}]\n" * 4
    
        for i, o in enumerate(
            [m.make_myobject1_1(), m.make_myobject1_2(), m.MyObject1(6), 7], start=4
        ):
            print(o)
            with capture:
                if not isinstance(o, int):
                    m.print_object_1(o)
                    m.print_object_2(o)
                    m.print_object_3(o)
                    m.print_object_4(o)
                m.print_myobject1_1(o)
                m.print_myobject1_2(o)
                m.print_myobject1_3(o)
                m.print_myobject1_4(o)
    
            times = 4 if isinstance(o, int) else 8
            assert capture == f"MyObject1[{i}]\n" * times
    
        cstats = ConstructorStats.get(m.MyObject1)
        assert cstats.alive() == 0
        expected_values = [f"MyObject1[{i}]" for i in range(1, 7)] + ["MyObject1[7]"] * 4
>       assert cstats.values() == expected_values
E       AssertionError: assert ['MyObject1[0...ect1[5]', ...] == ['MyObject1[1...ect1[6]', ...]
E         
E         At index 0 diff: 'MyObject1[0]' != 'MyObject1[1]'
E         Left contains one more item: 'MyObject1[7]'
E         Use -v to get more diff

capture    = <conftest.Capture object at 0x3ac0db1b34d0>
cstats     = <pybind11_tests.ConstructorStats object at 0x3ac0d3e7bdb0>
expected_values = ['MyObject1[1]', 'MyObject1[2]', 'MyObject1[3]', 'MyObject1[4]', 'MyObject1[5]', 'MyObject1[6]', ...]
i          = 7
o          = 7
times      = 4

../../pybind11-2.13.1/tests/test_smart_ptr.py:43: AssertionError
----------------------------------------------------------------------------------- Captured stdout call ------------------------------------------------------------------------------------
### (anonymous namespace)::MyObject1 @ 0x3ac0d4748af0 destroyed
### Object @ 0x3ac0d4748af0 destroyed
### ref<(anonymous namespace)::MyObject1> @ 0x3ac0db20d8c8 destroyed
### (anonymous namespace)::MyObject1 @ 0x3ac0d4748b20 destroyed
### Object @ 0x3ac0d4748b20 destroyed
### ref<(anonymous namespace)::MyObject1> @ 0x3ac0d5034dc8 destroyed
### (anonymous namespace)::MyObject1 @ 0x3ac0d4748070 destroyed
### Object @ 0x3ac0d4748070 destroyed
### ref<(anonymous namespace)::MyObject1> @ 0x3ac0d3e7bdc8 destroyed
========================================================================================= XFAILURES =========================================================================================
______________________________________________________________________________ test_have_both_class_any_struct ______________________________________________________________________________

    @pytest.mark.xfail(XFAIL_CONDITION, reason=XFAIL_REASON, strict=True)
    def test_have_both_class_any_struct():
        assert m.unnamed_namespace_a_any_struct is not None
>       assert mb.unnamed_namespace_b_any_struct is not None
E       assert None is not None
E        +  where None = mb.unnamed_namespace_b_any_struct


../../pybind11-2.13.1/tests/test_unnamed_namespace_a.py:36: AssertionError
_____________________________________________________________________________ test_have_class_any_struct[None] ______________________________________________________________________________

any_struct = None

    @pytest.mark.xfail(XFAIL_CONDITION, reason=XFAIL_REASON, strict=False)
    @pytest.mark.parametrize(
        "any_struct", [m.unnamed_namespace_a_any_struct, mb.unnamed_namespace_b_any_struct]
    )
    def test_have_class_any_struct(any_struct):
>       assert any_struct is not None
E       assert None is not None

any_struct = None

../../pybind11-2.13.1/tests/test_unnamed_namespace_a.py:23: AssertionError
===================================================================================== warnings summary ======================================================================================
test_async.py::test_await_missing
test_async.py::test_await
  /usr/local/lib/python3.11/site-packages/pytest_asyncio/plugin.py:757: DeprecationWarning: The event_loop fixture provided by pytest-asyncio has been redefined in
  /usr/ports/devel/pybind11/work/pybind11-2.13.1/tests/test_async.py:9
  Replacing the event_loop fixture with a custom implementation is deprecated
  and will lead to errors in the future.
  If you want to request an asyncio event loop with a scope other than function
  scope, use the "scope" argument to the asyncio mark when marking the tests.
  If you want to return different types of event loops, use the event_loop_policy
  fixture.
  
    warnings.warn(

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
========================================================================================== XPASSES ==========================================================================================
================================================================================== short test summary info ==================================================================================
SKIPPED [1] ../../pybind11-2.13.1/tests/test_stl.py:149: no <experimental/optional>
SKIPPED [1] ../../pybind11-2.13.1/tests/test_callbacks.py:215: Current PYBIND11_INTERNALS_VERSION too low
SKIPPED [1] ../../pybind11-2.13.1/tests/test_pytypes.py:1038: C++20 feature not available.
SKIPPED [1] ../../pybind11-2.13.1/tests/test_pytypes.py:420: Not defined: PYBIND11_HANDLE_REF_DEBUG
SKIPPED [1] ../../pybind11-2.13.1/tests/test_pytypes.py:1027: C++20 feature not available.
XFAIL ../../pybind11-2.13.1/tests/test_unnamed_namespace_a.py::test_have_both_class_any_struct - Known issues: https://github.com/pybind/pybind11/pull/4319
XFAIL ../../pybind11-2.13.1/tests/test_unnamed_namespace_a.py::test_have_class_any_struct[None] - Known issues: https://github.com/pybind/pybind11/pull/4319
XPASS ../../pybind11-2.13.1/tests/test_unnamed_namespace_a.py::test_have_class_any_struct[unnamed_namespace_a_any_struct] - Known issues: https://github.com/pybind/pybind11/pull/4319
FAILED ../../pybind11-2.13.1/tests/test_class.py::test_class_refcount - assert 10 == 9
FAILED ../../pybind11-2.13.1/tests/test_exceptions.py::test_cross_module_exception_translator - RuntimeError
FAILED ../../pybind11-2.13.1/tests/test_buffers.py::test_to_python - AssertionError: assert ['5x5 matrix', '5x4 matrix'] == ['5x4 matrix']
FAILED ../../pybind11-2.13.1/tests/test_factory_constructors.py::test_init_factory_basic - AssertionError: assert [['3', 'hi!']...['42', 'bye']] == [['3', 'hi!']...['42', 'bye']]
FAILED ../../pybind11-2.13.1/tests/test_eigen_matrix.py::test_cpp_casting - assert 31.0 == 21.0
FAILED ../../pybind11-2.13.1/tests/test_smart_ptr.py::test_smart_ptr - AssertionError: assert ['MyObject1[0...ect1[5]', ...] == ['MyObject1[1...ect1[6]', ...]
======================================================== 6 failed, 869 passed, 5 skipped, 2 xfailed, 1 xpassed, 2 warnings in 54.99s ========================================================

FreeBSD 14.1
python-3.11

Reproducible example code
n/a
Is this a regression? Put the last known working version here if it is.

Not sure.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the six failures reported in tests/test_class.py, tests/test_exceptions.py, tests/test_buffers.py, tests/test_factory_constructors.py, tests/test_eigen_matrix.py, and the smart-pointer test. Compare the observed reference counts, constructor statistics, exception translation, casting result, and object values with each assertion. Done means identifying the relevant cause and making all six reported tests pass.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
backend-api-design, testing-qa
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.