Catching a SIGSEGV signal on an import

Ryan heniser at yahoo.com
Thu Sep 9 08:23:14 EDT 2010


Is there anyway to catch a SIGSEGV signal that results from an import?
I'd like to get a list of all modules on the sys.path. The module
pkgutil has a nice method, walk_packages, to do just that. But, there
is a third party extension that throws a SIGSEGV when imported. I
tried to create a signal handler with the signal module like:
#!/bin/env python
import pkgutil
import signal
import traceback
def handler(signal, stackframe):
 raise ImportError
# Handle seg faults that may occur during an import
signal.signal(signal.SIGSEGV, handler)
if __name__ == "__main__":
 goodMods = []
 def onerror(pkgName):
 sys.stdout.write('Unable to import package %s\n' % pkgName)
 traceback.print_exc(file=sys.stdout)
 sys.stdout.write('\n')
 #sys.stdout.flush()
 for importer, mod, ispkg in pkgutil.walk_packages(path=None,
onerror=onerror):
 goodMods.append(mod)
 for m in goodMods:
 sys.stdout.write(m + '\n')
 sys.stdout.flush()
This sometimes works. But, since SIGSEGV is asynchronous it is not
guaranteed to work all the time. In general, is there anyway to catch
a SIGSEGV on import? If so, is there a way to use that with
pkgutil.walk_packages to get all the modules on sys.path?
Thanks,
Ryan


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /