An excerpt from the Python 2.0 docs for pyexpat: """ ParseFile (file) Parse XML data reading from the object file. file only needs to provide the read(nbytes) method, returning the empty string when there's no more data. [snip] The following attributes contain values relating to the most recent error encountered by an xmlparser object, and will only have correct values once a call to Parse() or ParseFile() has raised a xml.parsers.expat.error exception. ErrorByteIndex Byte index at which an error occurred. [etc.] """ The wrong, "xml.parsers.expat" is the first indicator that there might be a problem, yet I took the docs at their word and wrapped the call to ParseFile in a blanket try/except, only to find that no exception of any sort is ever raised by ParseFile. It turns out that ParseFile actually returns 0 on error, returning 1 otherwise. The first matter is that the code and the docs need to be reconciled. However, I would _much_ rather prefer that things were as in the docs. I think ParseFile should raise an exception rather than return an error flag. Interestingly enough, this is the same argument I had with a colleague just last week. -- Uche Ogbuji Principal Consultant uche.ogbuji@fourthought.com +1 303 583 9900 x 101 Fourthought, Inc. http://Fourthought.com 4735 East Walnut St, Ste. C, Boulder, CO 80301-2537, USA Software-engineering, knowledge-management, XML, CORBA, Linux, Python
The wrong, "xml.parsers.expat" is the first indicator that there might be a problem, yet I took the docs at their word and wrapped the call to ParseFile in a blanket try/except, only to find that no exception of any sort is ever raised by ParseFile.
It turns out that ParseFile actually returns 0 on error, returning 1 otherwise.
The first matter is that the code and the docs need to be reconciled. However, I would _much_ rather prefer that things were as in the docs.
Thanks for the report. What do you mean with 'the wrong, "xml.parsers.expat"'? There really is an exception xml.parsers.expat.error, and it really is raised from xml.parsers.expat (aka pyexpat). It turns out that it is only raised for Parse, not for ParseFile, which is a bug introduced when the return value of these functions was changed from a number to an exception. In the PyXML CVS, I have corrected that error. As a result, PyXML 0.6.2 will build its own pyexpat.c even for Python 2.0, which will implement the documentation. Regards, Martin
[Cleaning out some old email...] On 29 Oct 2000, uche.ogbuji@fourthought.com wrote:
It turns out that ParseFile actually returns 0 on error, returning 1 otherwise.
The first matter is that the code and the docs need to be reconciled. However, I would _much_ rather prefer that things were as in the docs. I think ParseFile should raise an exception rather than return an error flag. Interestingly enough, this is the same argument I had with a colleague just last week.
The return value for ParseFile has just recently been made to do the right thing in all cases, and now matches Parse. An exception is raised when there's an error in parsing, and the exception instance carries the "code", "offset", and "lineno" attributes. Just in case anyone thought this was still a problem. ;-) -Fred -- Fred L. Drake, Jr. <fdrake at acm.org> PythonLabs at Zope Corporation
participants (3)
-
Fred L. Drake, Jr. -
Martin v. Loewis -
uche.ogbuji@fourthought.com