how to handle occaisional segmentation fault in f2py module

白昼怎懂夜的黑 提交于 2020-05-17 07:45:08

问题


I have an f2py module that I am using. The core of the code I did not write myself, just the f2py wrapper to use it. Because of time constraints, and the knowledge that his code is short term need only, meaning I want to throw it away, not fix it, what I want to do is just catch the behavior when a segfault occurs and try again. The below code is an example:

sums.f90

subroutine bad(a, b, c)

  real, intent(in) :: a, b
  real, intent(out) :: c
  real, allocatable :: d(:)
  real :: r

  call random_number(r)

  if (r < 0.5) then
    print *, d(1)  ! segfault here
  end if

  c = a + b

end subroutine bad

compile with: f2py -c sums.f90 -m sums

and now if I just call it repeatedly, then sometimes it works and sometimes it doesn't.

>>> python -c "import sums; print(sums.bad(1,2))"
3.0
>>> python -c "import sums; print(sums.bad(1,2))"
3.0
>>> python -c "import sums; print(sums.bad(1,2))"
Segmentation fault: 11
>>> python -c "import sums; print(sums.bad(1,2))"
3.0
>>> python -c "import sums; print(sums.bad(1,2))"
3.0
>>> python -c "import sums; print(sums.bad(1,2))"
Segmentation fault: 11

Now I know the right thing to do is fix the segfault..., but I am hoping there is a way I can just handle it and move on from within the calling Python code. Is this possible?

来源:https://stackoverflow.com/questions/61831483/how-to-handle-occaisional-segmentation-fault-in-f2py-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!