quetz-mod_python-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From David Fraser <dav...@sjsoft.com>
Subject Re: more robust import
Date Mon, 22 Dec 2003 09:04:36 GMT
Brian Hawthorne wrote:

>hello, i have implemented a more robust core import function (replacing
>the builtin __import__) which handles a particular case of error in a way
>that makes debugging under mod_python (or any persistent interpreter)
>Specifically, when a runtime error occurs during the initial
>import of some module, an exception is raised (appropriately); but also,
>an incompletely instantiated module object for that module is added to
>sys.modules (*inappropriately*).  the result of this is that when the next
>attempt is made to import that module, it seems to import correctly (no
>exception, since it's just retrieved from sys.modules), but downstream
>code could fail with a cryptic error because the module object is missing
>stuff which is expected (like functions, classes, globals, etc.).
This is great. We have had exactly this problem, and wanted to code 
something like this - we'll give your code a spin...

>the new import function below *does not* add modules with runtime errors
>to sys.modules, resulting in an accurate exception every time an import
>attempt is made on a broken module.  i just added the following code to
>the end of the apache.py module, though there may be a better place for
># define new import function that *doesn't* add to sys modules if
># incompletely imported
>def new_import( name, globals={}, locals={}, fromlist=[] ):
>    GOOD = 0
>    try:
>        mod = builtin_mod.__orig_import__( name, globals, locals, fromlist
>        GOOD = 1
>    finally:
>        if not GOOD:
>            # determine the name of the offending module(s) and delete
>            # from the sys modules dictionary to prevent subsequent import
>            # attempts from succeeding artificialy
>            if fromlist:
>                if sys.modules.has_key( name ):
>                    fmod = sys.modules[name]
>                    if hasattr( fmod, "__path__" ):
>                    # must be a package
>                        for fr in fromlist:
>                            fullname = "%s.%s"%(name,fr)
>                            if sys.modules.has_key( fullname ):
>                                del sys.modules[fullname]
>                    else:
>                    # must be a module
>                        del sys.modules[name]
>            else:
>                if sys.modules.has_key( name ):
>                    del sys.modules[name]
>        else:
>            return mod
># save original builtin import function, then replace with new one
>builtin_mod = sys.modules["__builtin__"]
>if not hasattr( builtin_mod, "__orig_import__" ):
>    builtin_mod.__orig_import__ = builtin_mod.__import__
>builtin_mod.__import__ = new_import
Will this correctly replace the import function for all modules, or only 
modules that import mod_python.apache?
Thanks for posting this


View raw message