[f2py] Re: unloading/reloading compiled modules

Kevin Mitchell <[email protected]>
Newsgroups gmane.comp.python.f2py.user
Message-ID <[email protected]>
I tried sending this before I was a subscriber and I'm still waiting
for approval. So apologies if its out of line to be sending again, but
I wanted to get this out there in case anyone had any ideas.

There was a thread on the numpy-discussion list about this, but its really old,
http://www.mail-archive.com/numpy-discussion-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org/msg02661.html

Basically, I want to be able to reload a recompiled Fortran module
without restarting python. This is more or less essential for using
python as a development interface.

I understand that what really needs to happen is for the old version
of the module to first get UNloaded. To this end, I put together the
following script based on what I found at
http://stackoverflow.com/questions/359498/how-can-i-unload-a-dll-using-ctypes-in-python

-------------------------------------
#!/usr/bin/env python
import os
import ctypes
import numpy.f2py

def isloaded(lib):
   libp = os.path.abspath(lib)
   fd=open('/proc/%d/maps'%os.getpid())
   ret=fd.read().find(libp)
   fd.close()
   return (ret != -1)

def dlclose(handle):
   libdl = ctypes.CDLL("libdl.so")
   libdl.dlclose(handle)

def unload(fname):
   mydll = ctypes.CDLL(fname)
   handle = mydll._handle
   del mydll
   while isloaded(fname):
       dlclose(handle)

import yo

yo.yo()

unload('./yo.so')

print 'isloaded(\'./yo.so\') =', isloaded('./yo.so')

import yo

print 'good to here!'

yo.yo()

print 'you\'ll never see this'
----------------------------------------------------------

Where "yo.so" is compiled from yo.f:
---------------------------
     subroutine yo()
     write(*,*) 'yo'
     end
----------------------------
with

f2py -c yo.f -m yo

It segfaults at the second calling of yo.yo(). In fact, it segfaults
on exit even if I never reload the module. My guess would be that
something is expecting that library to still be linked, but I have no
idea where to look or how to change that expectation. If its not
already obvious from the above script, I should probably mention that
I'm using Linux and I could care less about platform portability.

Kevin
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.