-
Notifications
You must be signed in to change notification settings - Fork 4
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
Better error handling #4
Comments
Also a strong argument to have a pint unit check in this 😹 @TomNicholas |
I just read this from https://numpy.org/doc/stable/f2py/signature-file.html#f2py-statements
It's pretty cryptic, but basically I think we need to add this to the signature file. The way aerobulk handles errors is to call the This code calls the fortran STOP command. So the general problem we need to solve is how to pass a fortran STOP command up to python as an exception. This SO issue is relevant: https://stackoverflow.com/questions/18021081/stop-python-code-in-fortran-module-error-using-f2py I would start by writing a test for the expected exception. |
This page is also very helpful https://github.com/pearu/f2py/wiki/FAQ2ed. They suggest we will need to modify the fortran source code 😒 |
But what is solution 2, haha. |
I am going to try my best to summarize the short hack session with @rabernat and @TomNicholas today: The ultimate goal was to somehow enable us to call a python function from fortran (callback function) which then raises an error without a crashing python kernel. We mostly followed these instructions. We added this codeblock to
and the following code to
recompiled the code with from aerobulk.aerobulk.mod_aerobulk_wrap import mod_aerobulk_wrapper as w
class F2PYSTOP(Exception):
def __call__(self, status):
raise self.__class__(status)
w.foo2(11, F2PYSTOP()) Contrary to the example this resulted in a rather gnarly error:
We experimented a bit further and changing
to
in the first code block enabled us to run this without crashing the python kernel. @rabernat was hopeful that we might be able to do something with the return values, but we need to continue working on this. |
I found this, which I believe is the prequel to our source used above.
It seems like some more brute force approaches (e.g. replacing STOP statements in all fortran files) as part of the build could be considered if we find a way to use return? |
Notes from our hack on 2022/5/4:
the corresponding fortran file is :
This will raise an error like such:
for a test script: import pytest
from RANGE_CHECK import range_mod
errormsg = 0
def raise_error(msg):
global errormsg
print(f"raising error message {msg}")
errormsg = msg
def test_range_error():
# range_mod(12, raise_error)
range_mod(12)
assert errormsg == 12
def test_range_good():
# range_mod(12, raise_error)
range_mod(2)
assert 1 == 1 What we need to do next:
Steps to reproduce results with above files:
|
Epic session today. |
😎🤓 |
We just discovered that @brian-rose is an expert on f2py! (See https://github.com/brian-rose/minimalf2py). Brian, do you have any experience on error handling from Fortran. The above thread is very long and probably confusing, but basically we are trying to redirect Fortran STOP commands to raise Python exceptions (rather than crashing the entire session). Have you ever dealt with that situation before? |
... yikes. I am now about to be unmasked as a non-expert. Unfortunately no, I've never dealt with this. To my knowledge, none of the fortran code I've ever tried to wrapped used STOP commands. I'll follow your progress here though! If there's a good general solution (perhaps involving modifying the signature file, as you discussed above), then that might make a nice addition to minimalf2py |
So I just realized that omitting all the error checking in I think we should just bite the bullet and to the range_check in the numpy code (where we can raise useful errors). I have touched upon this in a bit more detail in #36 (comment) |
I now mark this as abandoned and close it due to the changes introduced in #43 |
During development on #3 I noticed that errors in the fortran code seem to lead in an immediate crash of the kernel. We should investigate how to catch and display these (I was not able to see the relevant message in a notebook, but did in an ipython session).
To reproduce I entered this (wildly wrong units) into ipython
Which gives:
Might be relevant to this : https://numpy.org/doc/stable/f2py/signature-file.html#f2py-statements
The text was updated successfully, but these errors were encountered: