xml.dom's weirdness?

Lie Lie.1296 at gmail.com
Sat Jul 26 06:23:52 EDT 2008


On Jul 26, 2:29 pm, Fredrik Lundh <fred... at pythonware.com> wrote:
> Lie wrote:
> > Why this generates AttributeError, then not?
>
> > Python 2.5.2 (r252:60911, Apr 21 2008, 11:17:30)
> > [GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
> > Type "help", "copyright", "credits" or "license" for more information.
> >>>> import xml
> >>>> xml.dom
> > Traceback (most recent call last):
> >   File "<stdin>", line 1, in <module>
> > AttributeError: 'module' object has no attribute 'dom'
> >>>> xml.dom
> > <module 'xml.dom' from '/usr/lib/python2.5/xml/dom/__init__.pyc'>
>
> this is what I get, on both Windows and Linux:
>
>  >>> import xml
>  >>> xml.dom
> Traceback (most recent call last):
>    File "<stdin>", line 1, in <module>
> AttributeError: 'module' object has no attribute 'dom'
>  >>> xml.dom
> Traceback (most recent call last):
>    File "<stdin>", line 1, in <module>
> AttributeError: 'module' object has no attribute 'dom'
>
> </F>

That was what I would have expected. The submodules shouldn't get
imported unless explicitly imported with import xml.dom, but what I
see right now in front of me... I wouldn't have believed such thing if
this isn't happening right under my eyes.

Doing several dir(xml) revealed that xml.dom and xml.parsers appeared
only after doing xml.dom and getting AttributeError

After further testing, I found that it also happens with xml.parsers,
but not xml.sax nor xml.etree, odd thing. And I tried PIL modules, the
oddities doesn't happen.

Then I tested the email module, the same thing happened:

Python 2.5.2 (r252:60911, Apr 21 2008, 11:17:30)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import email
>>> dir(email)
['Charset', 'Encoders', 'Errors', 'FeedParser', 'Generator', 'Header',
'Iterators', 'LazyImporter', 'MIMEAudio', 'MIMEBase', 'MIMEImage',
'MIMEMessage', 'MIMEMultipart', 'MIMENonMultipart', 'MIMEText',
'Message', 'Parser', 'Utils', '_LOWERNAMES', '_MIMENAMES', '__all__',
'__builtins__', '__doc__', '__file__', '__name__', '__path__',
'__version__', '_name', 'base64MIME', 'email', 'importer',
'message_from_file', 'message_from_string', 'mime', 'quopriMIME',
'sys']
>>> email.parser
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'parser'
>>> dir(email)
['Charset', 'Encoders', 'Errors', 'FeedParser', 'Generator', 'Header',
'Iterators', 'LazyImporter', 'MIMEAudio', 'MIMEBase', 'MIMEImage',
'MIMEMessage', 'MIMEMultipart', 'MIMENonMultipart', 'MIMEText',
'Message', 'Parser', 'Utils', '_LOWERNAMES', '_MIMENAMES', '__all__',
'__builtins__', '__doc__', '__file__', '__name__', '__path__',
'__version__', '_name', '_parseaddr', 'base64MIME', 'base64mime',
'charset', 'email', 'encoders', 'errors', 'importer', 'iterators',
'message', 'message_from_file', 'message_from_string', 'mime',
'quopriMIME', 'quoprimime', 'sys', 'utils']
>>>

It seems python (or my version of python, which is the one that
shipped with Ubuntu) mysteriously imported several modules's
submodules in a (seemingly) random, yet consistent manner after an
error occurred (as far as now, it seems ANY error would trigger the
mysterious submodule import).

I got even more head scratching when I see this:

Python 2.5.2 (r252:60911, Apr 21 2008, 11:17:30)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> ac
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'ac' is not defined
>>> import xml
>>> dir(xml)
['_MINIMUM_XMLPLUS_VERSION', '__all__', '__builtins__', '__doc__',
'__file__', '__name__', '__path__', '__version__', 'dom', 'parsers']
>>>

The triggering errors happened BEFORE I imported a module, yet later
module import would also import submodules

If you have any idea what black magic is happening in my computer
right now, I'd appreciate it.



More information about the Python-list mailing list