[XML-SIG] Encoding argument for toxml and toprettyxml

Walter Dörwald walter@livinglogic.de
Thu, 04 Jul 2002 23:17:52 +0200


Martin v. Loewis wrote:

> Walter Dörwald <walter@livinglogic.de> writes:
> 
> 
>>Currently I can't imagine a stream writer where the state menagement
>>depends on the error handling name. Can you give an example? 
> 
> 
> No, I can't. I know that encoders have to be stateful, but I can't
> give a real-world example where the state needs to take into account
> error handling.

OK, so I hope we can ignore this potential problem.

>>But this only checks if the error handling API is present in
>>Python. It does not check whether a certain codec uses the API.
> 
> 
> Using that as a check for "Python 2.3" is good enough for me.

OK, but but using the new error handling name might fail for
a third party codecs, that hasn't been updated yet.

But I hope that Tamito Kajayama will update his JapanaesCodec,
when (and if) PEP 293 gets accepted.

>>How about the following code? This will raise the same exception
>>if assigning the errors attribute or using it doesn't work:
>>
>>try:
>>    try:
>>       writer.errors = errors
>>    except AttributeError:
>>       pass
>>     writer.write(data)
>>except ValueError:
>>     try:
>>       writer.errors = "strict"
>>    except AttributeError:
>>       pass
>>     writer.write(data)
> 
> 
> I don't think that can work. What if the first write was "partial",
> i.e. already wrote some of the data?

That's exactly one of the reasons for the PEP.

One workaround would be to do a test write to a throwaway StringIO
stream at the start of toprettyxml with characters that are guaranteed
to fail and check which exception is raised. The result of this test
could be stored in a dictionary with the encoding as the key, so that
the test has to be done only once per encoding.

Bye,
    Walter Dörwald