[Python-Dev] Proposal for a modified import mechanism.

Prabhu Ramachandran prabhu at aero.iitm.ernet.in
Sat Nov 10 23:35:13 EST 2001


>>>>> "GMcM" == Gordon McMillan <gmcm at hypernet.com> writes:

    GMcM> [Prabhu works on knee.py]

    >> it also fixes a bug where the parent package is an extension
    >> module.

    GMcM> Python provides no support for an extension module being a
    GMcM> package parent module. More precisely, I think the fact that
    GMcM> an extension module can be made to behave like a package
    GMcM> parent module is an accident. There is special code in
    GMcM> import for modules named __init__, and the code is bypassed
    GMcM> for extension modules.

    GMcM> I suspect you'd have to provide a pretty strong
    GMcM> justification before this would become supported behavior.

I guess this was unclear.  My addition is extremely simple and does
not do anything new.  Here is an illustration

>>> import knee
>>> import Numeric.array
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/local/lib/python2.1/knee.py", line 17, in import_hook
    m = load_tail(q, tail)
  File "/usr/local/lib/python2.1/knee.py", line 68, in load_tail
    m = import_module(head, mname, m)
  File "/usr/local/lib/python2.1/knee.py", line 97, in import_module
    parent and parent.__path__)
AttributeError: 'Numeric' module has no attribute '__path__'
>>>

Point is, there is a line in knee.py (line 97 that assumes that there
is a __path__ attribute for the passed parent.  However, if parent is
an extension module there is none.  So I simply modified it.  Here is
the diff.

$ diff  knee.py  /usr/local/lib/python2.1/knee.py
98,101c98
<     except (ImportError, AttributeError):
<         # extension modules dont have a __path__ attribute.
<         # caching failures.
<         sys.modules[fqname] = None
---
>     except ImportError:

In fact that is all I changed in knee.py!  Which is why I said the
changes are very small.  Maybe I should have shown a patch but the
mail was already long.

prabhu





More information about the Python-list mailing list