You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
xml.sax.xmlreader does not support the InputSource protocol #46427
bpo-17089: Expat parser parses strings only when XML encoding is UTF-8
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee='https://github.com/freddrake'closed_at=<Date2015-04-06.19:18:26.667>created_at=<Date2008-02-24.13:52:31.314>labels= ['expert-XML', 'type-bug', 'library']
title='xml.sax.xmlreader does not support the InputSource protocol'updated_at=<Date2015-04-06.19:27:13.797>user='https://bugs.python.org/ygale'
In the documentation for xml.sax.xmlreader.InputSource objects
(section 8.12.4 of the Library Reference) we find that
users of InputSource objects should use the following
sequence to get their input data:
If the InputSource has a character stream, use that.
Otherwise, if the InputSource has a byte stream, use that.
Otherwise, open a URI connection to the system ID.
The parse() method of IncrementalParser skips step 1.
In addition, we need to add a method
getSourceEncoding() to the XMLReader interface;
if non-null, it will indicate to the parser that
the input is a byte stream in the given encoding.
The documentation should indicate what the parser
should do if the XML itself announces that its
encoding is something else. I propose that the parser should
be required to raise an error in that case.
Hmm. When getSourceEncoding() is None, there needs to be some
way for the parser to distinguish between the cases where it
is getting pre-decoded Unicode through a character stream,
or where it is getting a byte stream with an unspecified
encoding. In the latter case, it will have to look in the
XML for an encoding declaration, or use UTF-8 by default).
Note that expat only can handle the latter case.
ygale commented on Feb 24, 2008
ygalemannequin
MannequinAuthor
More actions
So I think there are two possibilities:
Use a special value for getSourceEnconding(),
like "unicode", to indicate that this is a
unicode character stream and not a byte stream.
Provide yet another method in the XMLReader
interface: sourceIsCharacterStream(), returning
a bool.
There is a more drastic option:
Since expat doesn't support this stuff
anyway, and perhaps not too many people
have written parsers that do support it,
dumb down the InputSource interface.
Specifically, deprecate setCharacterStream(),
getCharacterStream(), setEncoding() and
getEncoding(), none of which are used by
expat. Parsers should read the XML from
the byte stream and use that to determine
the encoding.
That may upset some implementors of XML
libraries though. They would each have to go
to some trouble to provide their own
proprietary and possibly incompatible
mechanisms for this, if they need it.
Perhaps a compromise fourth path would
be to have subclasses of InputSource for
the two cases of character stream and
byte stream.
ygale commented on Feb 24, 2008
ygalemannequin
MannequinAuthor
More actions
Subclass of XMLReader would be needed, not InputStream.
It's certainly arguable that the current behavior is a bug, though I
suspect it shouldn't be considered major since I've not seen any prior
complaints about this.
It should be easy to fix the bug you describe by taking the character
stream and encoding it before feeding it to the XML parser; Expat can
certainly be forced to take a known encoding, ignoring what's in the XML
declaration.
On the other hand, it's not at all clear that changing this is
worthwhile. This API borrows quite literally from the Java SAX APIs;
perhaps this separation of the character stream from the byte stream
makes sense for some of the Java XML parsers, but I don't know that
there are any Python parsers that benefit from that separation.
bpo-2175 has a patch that covers all three issues: bpo-1483, bpo-2174 and bpo-2175. I hesitate what parts of the patch are worth to be applied to maintained releases.
Activity
ygale commentedon Feb 24, 2008
In the documentation for xml.sax.xmlreader.InputSource objects
(section 8.12.4 of the Library Reference) we find that
users of InputSource objects should use the following
sequence to get their input data:
The parse() method of IncrementalParser skips step 1.
In addition, we need to add a method
getSourceEncoding() to the XMLReader interface;
if non-null, it will indicate to the parser that
the input is a byte stream in the given encoding.
The documentation should indicate what the parser
should do if the XML itself announces that its
encoding is something else. I propose that the parser should
be required to raise an error in that case.
See also bpo-1483.
ygale commentedon Feb 24, 2008
See also: bpo-1483 and bpo-2175.
ygale commentedon Feb 24, 2008
Hmm. When getSourceEncoding() is None, there needs to be some
way for the parser to distinguish between the cases where it
is getting pre-decoded Unicode through a character stream,
or where it is getting a byte stream with an unspecified
encoding. In the latter case, it will have to look in the
XML for an encoding declaration, or use UTF-8 by default).
Note that expat only can handle the latter case.
ygale commentedon Feb 24, 2008
So I think there are two possibilities:
Use a special value for getSourceEnconding(),
like "unicode", to indicate that this is a
unicode character stream and not a byte stream.
Provide yet another method in the XMLReader
interface: sourceIsCharacterStream(), returning
a bool.
There is a more drastic option:
anyway, and perhaps not too many people
have written parsers that do support it,
dumb down the InputSource interface.
Specifically, deprecate setCharacterStream(),
getCharacterStream(), setEncoding() and
getEncoding(), none of which are used by
expat. Parsers should read the XML from
the byte stream and use that to determine
the encoding.
That may upset some implementors of XML
libraries though. They would each have to go
to some trouble to provide their own
proprietary and possibly incompatible
mechanisms for this, if they need it.
Perhaps a compromise fourth path would
be to have subclasses of InputSource for
the two cases of character stream and
byte stream.
ygale commentedon Feb 24, 2008
Subclass of XMLReader would be needed, not InputStream.
freddrake commentedon Mar 28, 2008
It's certainly arguable that the current behavior is a bug, though I
suspect it shouldn't be considered major since I've not seen any prior
complaints about this.
It should be easy to fix the bug you describe by taking the character
stream and encoding it before feeding it to the XML parser; Expat can
certainly be forced to take a known encoding, ignoring what's in the XML
declaration.
On the other hand, it's not at all clear that changing this is
worthwhile. This API borrows quite literally from the Java SAX APIs;
perhaps this separation of the character stream from the byte stream
makes sense for some of the Java XML parsers, but I don't know that
there are any Python parsers that benefit from that separation.
serhiy-storchaka commentedon Mar 26, 2015
bpo-2175 has a patch that covers all three issues: bpo-1483, bpo-2174 and bpo-2175. I hesitate what parts of the patch are worth to be applied to maintained releases.
serhiy-storchaka commentedon Apr 2, 2015
Fixed in bpo-2175 (in 3.5 only).
freddrake commentedon Apr 6, 2015
Given that this has languished this long, patching historical releases seems pointless.