classification
Title: xdrlib raises ConversionError in inconsistent way
Type: behavior Stage: patch review
Components: Library (Lib) Versions: Python 3.3, Python 3.2, Python 2.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: georg.brandl, gruszczy, petri.lehtinen
Priority: normal Keywords: patch

Created on 2011-03-27 11:12 by gruszczy, last changed 2012-06-05 19:24 by petri.lehtinen.

Files
File name Uploaded Description Edit
11694.patch gruszczy, 2011-09-17 10:59 review
Messages (6)
msg132309 - (view) Author: Filip Gruszczyński (gruszczy) Date: 2011-03-27 11:12
xdrlib defines ConversionError, but very seldom uses it. For example:

    def pack_float(self, x):
        try: self.__buf.write(struct.pack('>f', x))
        except struct.error as msg:
            raise ConversionError(msg)

But it doesn't do so here:

    def pack_uint(self, x):
        self.__buf.write(struct.pack('>L', x))

Shouldn't that be more consistent?

I am happy to write a patch, that will make xdrlib raise ConversionError, as well as write proper test (I believe xdrlib tests should get some love altogether, so I would add a separate test case for this).
msg138783 - (view) Author: Petri Lehtinen (petri.lehtinen) * (Python committer) Date: 2011-06-21 12:30
This seems like a bug worth fixing. The ConversionError exception has been documented, an there's an example in the docs that suggest that at least all packing fails with a ConversionError.
msg144180 - (view) Author: Filip Gruszczyński (gruszczy) Date: 2011-09-17 10:59
Patch with tests.
msg151291 - (view) Author: Filip Gruszczyński (gruszczy) Date: 2012-01-15 15:50
Bump! It's almost 3 months since I posted the patch, so I would like to remind about this bug.
msg151293 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2012-01-15 16:54
Looks good to me.
msg162367 - (view) Author: Petri Lehtinen (petri.lehtinen) * (Python committer) Date: 2012-06-05 19:24
I see one obvious issue with the patch: The ConversionErrors it creates are passed the struct.error or TypeError instance as a parameter. The first argument of these exceptions would be better, i.e.

try:
   ...
except struct.error as e:
   raise ConversionError(e.args[0])

Furthermore, my ear thinks that raises_conversion_error would be a better name for the decorator than raising_conversion_error.

Anyway, I think the whole ConversionError is a bit problematic, as either TypeError or ValueError would be the most appropriate exception, depending on the situation. For example:

p = Packer()
p.pack_int('foo')  # should raise a TypeError
p.pack_int(2**100) # should raise a ValueError

This would be slightly harder to implement, though, as struct.error has exactly the same problem.
History
Date User Action Args
2012-06-05 19:24:15petri.lehtinensetmessages: + msg162367
2012-01-15 16:54:32georg.brandlsetstage: test needed -> patch review
2012-01-15 16:54:21georg.brandlsetnosy: + georg.brandl
messages: + msg151293
2012-01-15 15:50:39gruszczysetmessages: + msg151291
2011-09-17 10:59:26gruszczysetfiles: + 11694.patch
keywords: + patch
messages: + msg144180
2011-07-24 18:26:12petri.lehtinensetstage: test needed
versions: + Python 2.7, Python 3.2
2011-06-21 12:30:42petri.lehtinensetnosy: + petri.lehtinen
messages: + msg138783
2011-03-27 11:12:16gruszczycreate