Strange xml.parsers.xml import problem

D

dwelch91

Hi, c.l.p.'ers-

I am having a problem with the import of xml.parsers.expat that has
gotten me completely stumped. I have two programs, one a PyQt program
and one a command line (text) program that both eventually call the
same code that imports xml.parsers.expat. Both give me different
results...

The code that gets called is (print statements have been added for
debugging):

# this is status.py
import xml.parsers.expat
print xml
print xml.parsers
print dir(xml.parsers)
print repr(xml.parsers.expat.ExpatError)
print xml.parsers.expat

if info_device_status:
try:
device_status =
utils.XMLToDictParser().parseXML(info_device_status)
log.debug_block("info_device_status", info_device_status)
log.debug(device_status)
except xml.parsers.expat.ExpatError:
log.error("Device Status XML parse error")
device_status = {}

if info_ssp:
try:
ssp = utils.XMLToDictParser().parseXML(info_ssp)
log.debug_block("info_spp", info_ssp)
log.debug(ssp)
except xml.parsers.expat.ExpatError:
log.error("SSP XML parse error")
ssp = {}

When I run this code from my console program, it prints out:

<module '_xmlplus' from
'/usr/lib/python2.4/site-packages/_xmlplus/__init__.pyc'>
<module 'xml.parsers' from
'/usr/lib/python2.4/site-packages/_xmlplus/parsers/__init__.pyc'>
['__all__', '__builtins__', '__doc__', '__file__', '__name__',
'__path__', 'expat', 'pyexpat']
<class xml.parsers.expat.ExpatError at 0x2aaaac6c6950>
<module 'xml.parsers.expat' from
'/usr/lib/python2.4/site-packages/_xmlplus/parsers/expat.pyc'>

and all is well...

However, from the PyQt program I get:

<module '_xmlplus' from
'/usr/lib/python2.4/site-packages/_xmlplus/__init__.pyc'>
<module '_xmlplus.parsers' from
'/usr/lib/python2.4/site-packages/_xmlplus/parsers/__init__.pyc'>
['__all__', '__builtins__', '__doc__', '__file__', '__name__',
'__path__', 'sgmlop']
Traceback (most recent call last):

[...snip...]

File
"/home/dwelch/tip/linux-imaging-and-printing/src/base/status.py", line
980, in StatusType6
print repr(xml.parsers.expat.ExpatError)
AttributeError: 'module' object has no attribute 'expat'

Somehow, the import has brought in "_xmlplus" rather than "xml", and
"expat" is no longer in the xml.parsers module. This is on the same
machine with the same user account, running each program "back to
back". Running each program as a different user made no difference.

Everything is also OK if I manually enter the import and print commands
into a Python >>> prompt.

Any help much appreciated!

-Don
 
D

dwelch91

I was able to fix (i.e., work around) this issue by using the import:

import xml.parsers.expat as expat

and then referring to:

expat.ExpatError

I have no idea why this makes it work, seems like a bug in Python to
me.

-Don


Sorry, that should have been "xml.parsers.expat"

Hi, c.l.p.'ers-

I am having a problem with the import of xml.parsers.expat that has
gotten me completely stumped. I have two programs, one a PyQt program
and one a command line (text) program that both eventually call the
same code that imports xml.parsers.expat. Both give me different
results...

The code that gets called is (print statements have been added for
debugging):

# this is status.py
import xml.parsers.expat
print xml
print xml.parsers
print dir(xml.parsers)
print repr(xml.parsers.expat.ExpatError)
print xml.parsers.expat

if info_device_status:
try:
device_status =
utils.XMLToDictParser().parseXML(info_device_status)
log.debug_block("info_device_status", info_device_status)
log.debug(device_status)
except xml.parsers.expat.ExpatError:
log.error("Device Status XML parse error")
device_status = {}

if info_ssp:
try:
ssp = utils.XMLToDictParser().parseXML(info_ssp)
log.debug_block("info_spp", info_ssp)
log.debug(ssp)
except xml.parsers.expat.ExpatError:
log.error("SSP XML parse error")
ssp = {}

When I run this code from my console program, it prints out:

<module '_xmlplus' from
'/usr/lib/python2.4/site-packages/_xmlplus/__init__.pyc'>
<module 'xml.parsers' from
'/usr/lib/python2.4/site-packages/_xmlplus/parsers/__init__.pyc'>
['__all__', '__builtins__', '__doc__', '__file__', '__name__',
'__path__', 'expat', 'pyexpat']
<class xml.parsers.expat.ExpatError at 0x2aaaac6c6950>
<module 'xml.parsers.expat' from
'/usr/lib/python2.4/site-packages/_xmlplus/parsers/expat.pyc'>

and all is well...

However, from the PyQt program I get:

<module '_xmlplus' from
'/usr/lib/python2.4/site-packages/_xmlplus/__init__.pyc'>
<module '_xmlplus.parsers' from
'/usr/lib/python2.4/site-packages/_xmlplus/parsers/__init__.pyc'>
['__all__', '__builtins__', '__doc__', '__file__', '__name__',
'__path__', 'sgmlop']
Traceback (most recent call last):

[...snip...]

File
"/home/dwelch/tip/linux-imaging-and-printing/src/base/status.py", line
980, in StatusType6
print repr(xml.parsers.expat.ExpatError)
AttributeError: 'module' object has no attribute 'expat'

Somehow, the import has brought in "_xmlplus" rather than "xml", and
"expat" is no longer in the xml.parsers module. This is on the same
machine with the same user account, running each program "back to
back". Running each program as a different user made no difference.

Everything is also OK if I manually enter the import and print commands
into a Python >>> prompt.

Any help much appreciated!

-Don
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top