Django

Code

Ticket #7920 (closed: fixed)

Opened 1 month ago

Last modified 1 month ago

Adapt to Decimal repr change in Python 2.6

Reported by: Karen Tracey <kmtracey@gmail.com> Assigned to: kmtracey
Milestone: 1.0 beta Component: Uncategorized
Version: SVN Keywords: python26
Cc: Triage Stage: Accepted
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

In Python 2.6 (as of beta2) the repr for Decimal changed from using double to single quotes. Thus all of our tests that rely on Decimal repr fail like so:

Failed example:
    f.to_python(3)
Expected:
    Decimal("3")
Got:
    Decimal('3')

I asked about this over on the Python lists and feedback is the change is here to stay. Two suggestions for adapting the tests to be immune to the differences in repr across Python levels were:

1 - monkey-patch the Decimal module:

  >>> import decimal
  >>> decimal.Decimal.__repr__ = lambda s: 'Decimal("%s")' % str(s)

2 - adapt the tests to use a style like this:

>>> f.to_python(3) == Decimal('3')
True

instead of the style we have now. The first is easier but strikes me a bit hackish, the second is more work but might be a good style to move to generally if changing repr's are not seen as backwards-incompatible changes in Python.

Opinions on how we should adapt?

Attachments

decimal.diff (3.4 kB) - added by Karen Tracey <kmtracey@gmail.com> on 08/01/08 09:53:21.
decimal2.diff (3.5 kB) - added by kmtracey on 08/01/08 13:27:45.
Forgot about Python 2.3 compatibility

Change History

07/23/08 12:39:13 changed by mtredinnick

  • needs_better_patch changed.
  • stage changed from Unreviewed to Accepted.
  • needs_tests changed.
  • needs_docs changed.

I prefer the second one. The first looks a bit wrong to me, but it wouldn't be the end of the world if we went that way, either.

<editorial>Our tests are generally susceptible to subtle changes in output format like this. For example, if one of the VM-based pythons ever decided to use double-quotes instead of single quotes for default string printing, it'll be a real bunch of laughs. So it's not too surprising we have to change something like this now and again (although the Python guys could probably have managed to struggle on without making the change in the first place; it's hardly a requirement for functionality purposes. Still, it would be a Python release without some relatively pointless porting pain of this nature. :-( ).</editorial>

(Man, I wish people wouldn't put asides into comments like this. But I feel much better now that I've gotten that off my chest.)

07/23/08 13:04:35 changed by kmtracey

  • owner changed from nobody to kmtracey.
  • status changed from new to assigned.

Yeah, that's the feedback I got from the Python side. The tests as currently written are too dependent on output format, and the 2nd approach would be more future-proof if it were adopted in general throughout the tests. Not that I plan to do that, but just modifying the Decimal-dependent tests may result in people noticing/adopting a new idiom. (Or not, but one could hope.) I'll plan on taking the 2nd approach, then, unless I hear screams of protest from someone else.

08/01/08 09:53:21 changed by Karen Tracey <kmtracey@gmail.com>

  • attachment decimal.diff added.

08/01/08 09:55:07 changed by Karen Tracey <kmtracey@gmail.com>

  • has_patch set to 1.

Added patch for changed in Decimal repr in Python 2.6. Tested against Python 2.6b2 and Python 2.5.1.

08/01/08 13:27:45 changed by kmtracey

  • attachment decimal2.diff added.

Forgot about Python 2.3 compatibility

08/01/08 13:28:45 changed by kmtracey

Updated patch for Python 2.3 compatibility, and tested with Python 2.3.5.

08/01/08 23:48:53 changed by gwilson

  • status changed from assigned to closed.
  • resolution set to fixed.

fixed in [8190].


Add/Change #7920 (Adapt to Decimal repr change in Python 2.6)




Change Properties
Action