Code review comment for lp:~vila/bzr/712474-module-available

Revision history for this message
Martin Packman (gz) wrote :

I think checking sys.modules should do instead, something like:

    sentinel = object()
    module = sys.modules.get(self.module_name)
    if module is None:
        return False
    if module is sentinel:
        try:
            module = __import__(self.module_name, {}, {}, [''])
        except ImportError: # import can raise other things, but that's a bug
            return False
    return True

That should save me worrying needlessly about exec on an interpolated string.

« Back to merge proposal