[ZODB-Dev] ImportError results in AttributeError later

Thomas Güttler hv at tbz-pariv.de
Wed Mar 15 05:30:52 EST 2006


Hi,

If importing Foo.py results in an ImportError, you get this Traceback
in ZODB 3.2.10:

  File "/home/modarch/python/lib/python2.2/site-packages/ZODB/Connection.py", line 564, in setstate
    self._set_ghost_state(obj, p)
  File "/home/modarch/python/lib/python2.2/site-packages/ZODB/Connection.py", line 603, in _set_ghost_state
    state = unpickler.load()
  File "/home/modarch/python/lib/python2.2/site-packages/ZODB/Connection.py", line 200, in _persistent_load
    return self[oid]
  File "/home/modarch/python/lib/python2.2/site-packages/ZODB/Connection.py", line 162, in __getitem__
    klass=self._db._classFactory(self, module, name)
  File "/home/modarch/python/lib/python2.2/site-packages/ZODB/DB.py", line 128, in _classFactory
    name)
AttributeError: 'module' object has no attribute 'Foo'

I don't understand why the __import__ in the following code of ZODB/DB.py
does not raise the ImportError:

"""
    def _classFactory(self, connection, location, name,
                      _silly=('__doc__',), _globals={}):
        return getattr(__import__(location, _globals, _globals, _silly),
                       name)
"""

If a SyntaxError is in the code, it gets raised:
"""
  File "/home/modarch/python/lib/python2.3/site-packages/ZODB/DB.py", line 127, in _classFactory
    return getattr(__import__(location, _globals, _globals, _silly),
  File "/home/modarch/lib/modarch/Foo.py", line 39
    import my syntax error
                    ^
SyntaxError: invalid syntax
"""

It is not a big problem for me. Now I know it. But I would like to know,
why the __import__ does not raise an ImportError, but instead returns a
half imported module.

The same code in a small script raises an ImportError:
    location="modarch.Foo"
    name="Foo"
    module=__import__(location, globals(), globals(), [name])
    print getattr(module, name)


 Thomas Güttler

-- 
Thomas Güttler, TBZ-PARIV GmbH, Bernsdorfer Str. 210-212, 09126 Chemnitz
Tel.: 0371/5221-217 Fax:  0371/5221-216




More information about the ZODB-Dev mailing list