[issue7633] decimal.py: type conversion in context methods

Mark Dickinson report at bugs.python.org
Wed Jan 6 11:53:41 CET 2010


Mark Dickinson <dickinsm at gmail.com> added the comment:

Thanks for the patch!

Rather than using the Decimal constructor, I think you should convert use _convert_other(..., raiseit=True):  the Decimal constructor converts strings and tuples, as well as ints and longs, while _convert_other only converts ints and longs.  Note also that the conversion shouldn't depend on the current context;  only the operation itself needs that.

Maybe it would be worth adding some tests to ensure that e.g.,

MyContext.add('4.5', 123)

raises TypeError as expected?

I agree with the observation that it's usually only necessary to convert the first argument (since the Decimal method itself converts the second).

If you like, you could also usefully deal with the NotImplemented return value by turning it into a TypeError (i.e., in the context method, check for a NotImplemented return value, and raise TypeError there if necessary).  This is only needed for the double underscore methods __add__, __sub__, etc.  associated with Python's binary operators;  the other methods shouldn't ever return NotImplemented.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue7633>
_______________________________________


More information about the Python-bugs-list mailing list