Dotted name resolving fails

I have a strange effect with both zope.dottedname.resolve or Products.GenericSetup.utils._resolveDottedName.

(Pdb++) from zope.dottedname.resolve import resolve
(Pdb++) resolve('pas.plugins.ldap')
*** AttributeError: module 'pas.plugins' has no attribute 'ldap'
Traceback (most recent call last):
  File "/home/jensens/ws/pro/akbild/portal/venv/lib/python3.9/site-packages/zope/dottedname/resolve.py", line 43, in resolve
    found = getattr(found, n)
AttributeError: module 'pas.plugins' has no attribute 'ldap'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/jensens/ws/pro/akbild/portal/venv/lib/python3.9/site-packages/zope/dottedname/resolve.py", line 46, in resolve
    found = getattr(found, n)
>>> from Products.GenericSetup.utils import _resolveDottedName
>>> _resolveDottedName('pas.plugins.ldap.plonecontrolpanel.exportimport.import_settings') is None
True

Looking at the code it tries to do a getattr on the package/module:

(Pdb++) import pas.plugins.ldap
(Pdb++) from pas.plugins import ldap
(Pdb++) import pas.plugins
(Pdb++) getattr(pas.plugins, 'ldap')
*** AttributeError: module 'pas.plugins' has no attribute 'ldap'

But this fails on the second level.
There is nothing special, except this time it is installed with pip.

Any idea whats going on here?

The code with this effect is here:

Effect is, on install the own import handler is not found.

EDIT/UPDATE:

Working code is in zope.configuration.name.resolve, which does not rely on modules as attributes (which is probably wrong and does not work) but uses __import__.

A workaround i this patch:

from Products.GenericSetup import utils
from zope.configuration.name import resolve

def patched_resolveDottedName(dotted):
    __traceback_info__ = dotted

    try:
        return resolve(dotted)
    except ModuleNotFoundError:
        return

utils._resolveDottedName = patched_resolveDottedName

UPDATE 2:
Patch depends on loading order and may fail.

A draft PR here https://github.com/zopefoundation/Products.GenericSetup/pull/117

1 Like