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

[smart_holder] Change throw_if_uninitialized_or_disowned_holder() to also show C++ type name. #4977

Conversation

rwgk
Copy link
Collaborator

@rwgk rwgk commented Dec 15, 2023

Description

To aid debugging.

Suggested changelog entry:

@rwgk rwgk marked this pull request as ready for review December 15, 2023 08:48
@rwgk
Copy link
Collaborator Author

rwgk commented Dec 15, 2023

@karagog could you please help reviewing this PR?

if (!holder().is_populated) {
pybind11_fail("Missing value for wrapped C++ type:"
" Python instance is uninitialized.");
throw value_error(missing_value_msg + clean_type_id(typeid_name)
Copy link

Choose a reason for hiding this comment

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

I dont' suppose you can use f-strings?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Interesting idea, but no. We don't have f-strings in C++ (I never thought about that before, not sure how feasible that is), and compared to relying on std::string operator+, the overhead of using the Python C API is not something I'd want to have in an error handling code path like here.

@@ -9,23 +9,19 @@

@pytest.mark.xfail("env.PYPY", reason="gc after `del field` is apparently deferred")
@pytest.mark.parametrize("m_attr", ["m_valu_readonly", "m_valu_readwrite"])
def test_valu_getter(msg, m_attr):
def test_valu_getter(m_attr):
Copy link

Choose a reason for hiding this comment

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

nit: this function name is apparently misspelled

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's intentional, see comment at top of this file, which points here:

// The compact 4-character naming matches that in test_class_sh_basic.cpp
// Variable names are intentionally terse, to not distract from the more important C++ type names:
// valu(e), ref(erence), ptr or p (pointer), r = rvalue, m = mutable, c = const,
// sh = shared_ptr, uq = unique_ptr.

(The original idea was actually to make this code in test_class_sh_basic.cpp more readable.)

Copy link

@karagog karagog left a comment

Choose a reason for hiding this comment

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

LGTM

@rwgk
Copy link
Collaborator Author

rwgk commented Dec 16, 2023

Thanks for the review!

@rwgk rwgk merged commit d2ac3f5 into pybind:smart_holder Dec 16, 2023
152 checks passed
@rwgk rwgk deleted the sh_throw_if_uninitialized_or_disowned_holder_better_error branch December 16, 2023 03:29
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.

2 participants