A user points out that this code in reportlab uses the now deprecated imp module

def _fake_import(fn,name):
    if os.path.isfile(fn):
        import imp
        with open(fn,'rb') as f:
            imp.load_source(name,fn,f)

and suggests I use importlib SourceFileLoader. Is there anything wrong with this code (only for use in python 3.x)


def _fake_import(fn,name):
    from importlib import machinery
    m = machinery.SourceFileLoader(name,fn)
    try:
        return m.load_module(name)
    except FileNotFoundError:
        raise ImportError('file %s not found' % ascii(fn))

the newer import machinery seems particularly complex so I'm not at all sure this does what I intend even though it seems to work.

The original function is only used once like this

try:
    
_fake_import(os.path.expanduser(os.path.join('~','.reportlab_mods')),'reportlab_mods')
except (ImportError,KeyError):
    pass

and is intended to allow per user actions in a config file when reportlab is imported.

--
Robin Becker

--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to