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

Channel C++ warnings to Python's warning system #136

Closed
ischoegl opened this issue Feb 14, 2022 Discussed in #112 · 3 comments · Fixed by Cantera/cantera#1201
Closed

Channel C++ warnings to Python's warning system #136

ischoegl opened this issue Feb 14, 2022 Discussed in #112 · 3 comments · Fixed by Cantera/cantera#1201
Assignees
Labels
feature-request New feature request

Comments

@ischoegl
Copy link
Member

While exceptions raised in the C++ core are already captured and thrown by the Python API, the same is not true for the warning system. I had originally added this as a discussion topic, but it may be more appropriate to discuss this as an issue (feature request).

Discussed in #112

Originally posted by ischoegl August 18, 2021
At the moment, the C++ code base issues warning messages via warn_deprecated and warn_user (the latter introduced in Cantera/cantera#741). In this context, @bryanwweber commented on a new deprecation warning proposed in Cantera/cantera#1084:

One other comment, and I suppose this applies to all the warn_deprecated() calls in C++, I wonder if it's possible to catch those in Python and turn them into real Python DeprecationWarnings so they can be controlled by Python's warning interfaces?

(see here; the same presumably applies to warn_user).

@speth's response was:

I think this would require something a bit like how we interface with Python for writing messages to stdout, that is, registering a handler in the Application class and in that handler making whatever the correct calls are to the Python C API.

Additional context:

Other thoughts:

As there may be a relatively straight-forward route to this after all, it may make sense to convert this discussion to an enhancement request (i.e. issue).

@ischoegl ischoegl added the feature-request New feature request label Feb 14, 2022
@ischoegl ischoegl self-assigned this Feb 15, 2022
@ischoegl
Copy link
Member Author

@speth ... while I know how C++ outputs logging messages (fairly apparent in src/base/application.h) and have a hunch for how information flows (re-routed to wrappers.h and - I believe - captured with _logger in utils.pyx?), I'm not sure that I am following how those are actually caught and displayed in Python? It would be great if you could point me in the right direction.

@speth
Copy link
Member

speth commented Feb 18, 2022

Right, the lines

cdef CxxPythonLogger* _logger = new CxxPythonLogger()
CxxSetLogger(_logger)

in utils.pyx create a new instance of PythonLogger (defined in wrappers.h) and assign that as the logwriter member of the global Application object. That PythonLogger object is then used for all calls to writelog().

I think for this purpose, what you would want is an additional method for the Logger class that's used to handle calls to warn_user instead of having that just call writelog. The implementation of this method in PythonLogger would then need to call something like PyErr_WarnEx (see https://docs.python.org/3/c-api/exceptions.html#issuing-warnings). For the base Logger class, I guess you would probably want to direct the warning to std::cerr.

@ischoegl
Copy link
Member Author

🎉 Ha, that comment made all the difference! Thanks!! I guess the magic sauce in the current implementation is in PySys_WriteStdout and PyRun_SimpleString. This was hiding in plain sight! Exact same thing (i.e. PyRun_SimpleString) can be used for triggering warnings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request New feature request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants