Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PYBIND11_OBJECT_CVT should use namespace for error_already_set() #3797

Merged

Conversation

oleksandr-pavlyk
Copy link
Contributor

@oleksandr-pavlyk oleksandr-pavlyk commented Mar 11, 2022

This change makes the macro usable outside of pybind11 namespace.

Description

Change to PYBIND11_OBJECT_CVT macro to fully qualify error_already_set used in constructors. This change allows macro to be used outside of pybind11 namespace.

Suggested changelog entry:

``PYBIND11_OBJECT_CVT`` and ``PYBIND11_OBJECT_CVT_DEFAULT`` macro can be used to define classes in namespaces other than pybind11.

This change makes the macro usable outside of pybind11 namespace.
@rwgk
Copy link
Collaborator

rwgk commented Mar 11, 2022

How did you discover this?
Is there a chance you could add a test based on the situation you had? Otherwise it's likely that this will get broken again in refactoring work in the future.
Recently we had a similar PR, with added test: #3758

@oleksandr-pavlyk
Copy link
Contributor Author

I discovered it by building a toy pybind11 extensions a.cpp in https://github.com/oleksandr-pavlyk/cython_using_number_magic_methods.git

To test the change I can try building a simple class outside of pybind11 namespace which wraps PyFloatObject inside.
Let me try to whip something up and add it to this PR.

The added test defines a dummy function that takes a custom-defined class external::float_
that uses PYBIND11_OBJECT_CVT
@oleksandr-pavlyk
Copy link
Contributor Author

Test has been added in test/test_pytypes.cpp and companion .py file. Please review again.

Copy link
Collaborator

@rwgk rwgk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks!
Just waiting for the CI to finish.

@rwgk
Copy link
Collaborator

rwgk commented Mar 11, 2022

valgrind is detecting a leak and clang-tidy has two complaints:

/__w/pybind11/pybind11/tests/test_pytypes.cpp:25:7: error: do not use 'else' after 'return' [readability-else-after-return,-warnings-as-errors]
    } else {
      ^~~~~~
/__w/pybind11/pybind11/tests/test_pytypes.cpp:577:48: error: the parameter 'x' is copied for each invocation but only used as a const reference; consider making it a const reference [performance-unnecessary-value-param,-warnings-as-errors]
    m.def("square_float_", [](external::float_ x) -> double {
                                               ^

I missed the leak when I looked at the test code before. Looking again.

@oleksandr-pavlyk
Copy link
Contributor Author

oleksandr-pavlyk commented Mar 11, 2022

valgrind is detecting a leak and clang-tidy has two complaints:

/__w/pybind11/pybind11/tests/test_pytypes.cpp:25:7: error: do not use 'else' after 'return' [readability-else-after-return,-warnings-as-errors]
    } else {
      ^~~~~~
/__w/pybind11/pybind11/tests/test_pytypes.cpp:577:48: error: the parameter 'x' is copied for each invocation but only used as a const reference; consider making it a const reference [performance-unnecessary-value-param,-warnings-as-errors]
    m.def("square_float_", [](external::float_ x) -> double {
                                               ^

I missed the leak when I looked at the test code before. Looking again.

I am about to push a change to fix this.

@oleksandr-pavlyk
Copy link
Contributor Author

@rwgk Thanks for your help. The CI is now green.

@rwgk
Copy link
Collaborator

rwgk commented Mar 11, 2022

Thanks @oleksandr-pavlyk!

@rwgk rwgk merged commit 91a6e12 into pybind:master Mar 11, 2022
@github-actions github-actions bot added the needs changelog Possibly needs a changelog entry label Mar 11, 2022
@oleksandr-pavlyk oleksandr-pavlyk deleted the pybind11_object_cvt_use_namespace branch March 11, 2022 20:30
rwgk pushed a commit to rwgk/pybind11 that referenced this pull request Mar 18, 2022
…ind#3797)

* PYBIND11_OBJECT_CVT should use namespace for error_already_set()

This change makes the macro usable outside of pybind11 namespace.

* added test for use of PYBIND11_OBJECT_CVT for classes in external to pybind11 namespaces

* Extended test_pytypes.cpp and test_pytest.py

The added test defines a dummy function that takes a custom-defined class external::float_
that uses PYBIND11_OBJECT_CVT

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fixed issues pointed out by CI

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fixed memory leak in default constructor

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
henryiii pushed a commit to henryiii/pybind11 that referenced this pull request Mar 25, 2022
…ind#3797)

* PYBIND11_OBJECT_CVT should use namespace for error_already_set()

This change makes the macro usable outside of pybind11 namespace.

* added test for use of PYBIND11_OBJECT_CVT for classes in external to pybind11 namespaces

* Extended test_pytypes.cpp and test_pytest.py

The added test defines a dummy function that takes a custom-defined class external::float_
that uses PYBIND11_OBJECT_CVT

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fixed issues pointed out by CI

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fixed memory leak in default constructor

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
@henryiii henryiii removed the needs changelog Possibly needs a changelog entry label Mar 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants