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

Replace error printing code gated by NDEBUG with a new flag: PYBIND11_DETAILED_ERROR_MESSAGES #3913

Merged
merged 7 commits into from
May 2, 2022

Conversation

voznesenskym
Copy link
Contributor

@voznesenskym voznesenskym commented Apr 28, 2022

Description

Does what it says in the title. We have some internal use cases of pybind where we would love to get richer logging from within pybind, but are not running a debug build.

Suggested changelog entry:

* Error printing code now uses ``PYBIND11_DETAILED_ERROR_MESSAGES`` instead of requiring ``NDEBUG``.

@voznesenskym
Copy link
Contributor Author

Apologies upfront if this is not desired. I am not 100% sure how to test it, I did run all unit tests and nothing exploded.

@voznesenskym
Copy link
Contributor Author

voznesenskym commented Apr 28, 2022

Only those with write access to this repository can merge pull requests.

@Skylion007 Thanks for the stamp, I do not have write access :)

@Skylion007
Copy link
Collaborator

Any objections @henryiii or @rwgk ? If not, let's merge it.

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.

The general idea is great, but the implementation needs more attention.

The prefix for macros is universally PYBIND11_ (11 is currently missing).

LOG isn't very fitting, let's think carefully about naming. I think ERROR definitely needs to be in the name, maybe PYBIND11_DETAILED_ERROR_MESSAGES?

"compile in" appears in these files:

cast.h
attr.h
pybind11.h
detail/type_caster_base.h

After a quick pass looking at those files, I'm thinking it is best to replace NDEBUG with the new macro for all "compile in" matches. With that comes the idea to handle the definition of the new macro in detail/common.h.

The old phrase "compile in debug mode for ..." is best updated, e.g. "compile with PYBIND11_DETAILED_ERROR_MESSAGES defined for ..." will be more to the point and helpful.

The current #if logic definitely needs work:

#if !defined(PYBIND11_DETAILED_ERROR_MESSAGES) && !defined(NDEBUG)
#    define PYBIND11_DETAILED_ERROR_MESSAGES
#endif

Optional, or follow-on PR: define the macro for at least one non-debug CI job (very easy) and verify that some error is complete with details (needs some thought, unless someone already has a good idea how to implement that).

@voznesenskym
Copy link
Contributor Author

voznesenskym commented Apr 29, 2022 via email

@voznesenskym
Copy link
Contributor Author

@rwgk thanks for the great feedback. Applied.

w/r/t

Optional, or follow-on PR: define the macro for at least one non-debug CI job (very easy) and verify that some error is complete with details (needs some thought, unless someone already has a good idea how to implement that).

There are tests in tests/test_methods_and_attributes.py and tests/test_pytypes.py that test that the correct mode prints the correct value. It's not perfect, as it relies on debug_enabled being set, which is set from reading the right #define, but can of course be set by anyone, anywhere.

@voznesenskym voznesenskym changed the title Add Alternative #define, PYBIND_LOG_DETAIL, to enable logging more detailed casting errors in prod builds / when in NDEBUG mode Replace error printing code gated by NDEBUG with a new flag: PYBIND11_DETAILED_ERROR_MESSAGES Apr 30, 2022
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.

Looks great to me!

@Skylion007, do you want to take another look?

@Skylion007 Skylion007 merged commit f0b9f75 into pybind:master May 2, 2022
@github-actions github-actions bot added the needs changelog Possibly needs a changelog entry label May 2, 2022
rwgk pushed a commit to rwgk/pybind11 that referenced this pull request May 5, 2022
@henryiii henryiii removed the needs changelog Possibly needs a changelog entry label Jul 7, 2022
rwgk pushed a commit to rwgk/pybind11 that referenced this pull request Jul 12, 2022
rwgk pushed a commit to rwgk/pybind11 that referenced this pull request Jul 12, 2022
rwgk pushed a commit that referenced this pull request Jul 12, 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