A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2004-July/046463.html below:

[Python-Dev] Fix import errors to have data

[Python-Dev] Fix import errors to have dataDelaney, Timothy C (Timothy) tdelaney at avaya.com
Wed Jul 28 02:27:01 CEST 2004
Delaney, Timothy C (Timothy) wrote:

> Perhaps the import machinery could keep a (weak?) mapping from module
> *object* to the ImportError it raised. If the module that would be
> returned by the import is in the mapping, raise the corresponding
> ImportError instead. The appropriate line, etc in the module would
> thus 
> be shown each time the module was imported.
> 
> I believe this would work properly for reload() as well.

Of course it won't ... (no name to reload). This would effectively
prevent ever loading that module in that session, even if it got fixed.

Instead, perhaps the following would be the right semantics:

    # import replacement

    import __builtin__
    import sys

    _builtin_import = __import__

    def __import__(name, *p):
        try:
            return _builtin_import(name, *p)
        except ImportError, e:
            module = sys.modules[name]

            for n, m in sys.modules.items():
                if m is module:
                    del sys.modules[n]

            raise e

    __builtin__.__import__ = __import__

    # a.py

    raise ImportError(__name__)

    # b.py

    try:
        import a
    except ImportError:
        import a

    print 'OK'

    >>> try:
    ...     import a
    ... except ImportError:
    ...     import a
    ...
    Traceback (most recent call last):
      File "<stdin>", line 4, in ?
      File "<stdin>", line 9, in __import__
    ImportError: a

Tim Delaney
More information about the Python-Dev mailing list

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4