Giter VIP home page Giter VIP logo

Comments (3)

andreww avatar andreww commented on May 26, 2024

I think I can explain why the exception is not working in a helpful way for routine1 in numpy/numpy#7855, but I don't think I can think of anything better than solution 2.

My mental model of python's exception handling (no doubt vastly simplified or wrong) is that there is a flag in the interpreter that is set when an exception is raised. This is checked by the interpreter each time a python function is called, and if the flag is set instead of the python function being called the call stack is examined and the exception handling machinery activated. To operate, the python interpreter calls a lots of different interpreter functions (written in C). Some of these functions will do the job of checking and setting python's exception flag. The C functions have their own call stack and frame pointer which works in the same way as for any other compiled code. An interpreter function calling another interpreter function does not result in the exception flag being examined. When f2py is used to compile a Fortran subroutine you end up with something that looks like a python function and a function that behaves like an interpreter function. When you call routine1 from python, it calls routine2 as an "interpreter" function, this calls f and sets the exception flag. However, the the state of that flag is not checked on return from routine2 (this is just one fortran subroutine calling another, it does not know that it is running inside the process running the python interpreter) but only when you get back to something that looks like a "python function" (routine1). One thing to note is that even for the routine1 call, by the time you get back to the python interpreter you do end up with an exception (no more python gets run and the exception is raised) it's just that all of routine2 has been run by this point. The callback machinery seems to know about the exception (I guess this is where the "Call-back cb_pystop_in_routine2__user__routines failed." line comes from) but does not seem to want to do anything about it. I don't know if it would be possible to fiddle with the interpreter call stack at this point to skip the rest of routine2, I suspect not. I've tried fiddling around with the callback function (e.g. making it call another python function which raises the exception, having a return value) but nothing seems to stop the rest of routine2 being run.

So, I think solution 2 is probably the best bet. The other advantage of this is that it would be possible to avoid stop in Fortran if people want to run SHTOOLS inside of MPI (mpi_abort() is probably better) or want to arrange for a core dump if they pass duff data into an SHTOOLS routine. We've got some fairly fancy (overly baroque?) implementations of exception handling in Fortran inside of FoX but these are probably overkill and are not designed for performance critical code.

from shtools.

MarkWieczorek avatar MarkWieczorek commented on May 26, 2024

That sound about right to me. Thanks!

from shtools.

MarkWieczorek avatar MarkWieczorek commented on May 26, 2024

see: #59

from shtools.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.