[XML-SIG] Encoding argument for toxml and toprettyxml

Martin v. Loewis martin@v.loewis.de
02 Jul 2002 00:28:14 +0200


Walter D=F6rwald <walter@livinglogic.de> writes:

>  > Right. Does that mean that the "errors" argument of the StreamWriter
>  > needs to be exposed,
>=20
> As long as the stream writer class is derived from codecs.StreamWriter
> the errors attribute is already exposed.

No, it's not: toxml reads

    def toprettyxml(self, indent=3D"\t", newl=3D"\n", encoding =3D None):
        writer =3D _get_StringIO()
        if encoding is not None:
            import codecs
            # Can't use codecs.getwriter to preserve 2.0 compatibility
            writer =3D codecs.lookup(encoding)[3](writer)
        if self.nodeType =3D=3D Node.DOCUMENT_NODE:
            # Can pass encoding only to document, to put it into XML header
            self.writexml(writer, "", indent, newl, encoding)
        else:
            self.writexml(writer, "", indent, newl)
        return writer.getvalue()

So the user never sees the stream writer, and can thus not adjust the
errors attribute.

> It just has to be documented that the errors attribute can be
> changed between calls to StreamWriter.write() to switch between
> different error handling modes.

Is that really something you can guarantee for all stream writers?
What if the stream writer needs to be stateful, and the state
interpretation is affected by the error handling?

> If we use "xmlcharrefreplace" now, Python 2.2 or codecs that haven't
> been updated will fail with:
>=20
> ValueError: foo encoding error; unknown error handling code:
> xmlcharrefreplace

Is this also the case for all third-party codecs?

> If we want escaping to work with Python 2.2 or old codecs, we could do
> something like this:

I was rather looking for code like

try:
  codecs.xmlcharrefreplace
  have_xmlcharrefreplace =3D 1
except AttributeError:
  have_xmlcharrefreplace =3D 0

> The same way we find out if it's a valid encoding: just try it.
> If it doesn't work, catch the ValueError exception (for Python 2.2).

That's not good enough. If the minidom implementation uses it, it
better be sure that it, if not supported, has the same meaning like
the "strict" error mode.

Regards,
Martin