Fixed #23770 -- Changed serialization strategy for floats with respect to NaN and Inf

Thanks to w0rp for the report
This commit is contained in:
Markus Holtermann 2014-11-06 12:29:43 +01:00 committed by Baptiste Mispelon
parent b07aa52e8a
commit c7c098cf97
3 changed files with 17 additions and 4 deletions

View File

@ -1,10 +1,11 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import datetime
import inspect
import decimal
import collections import collections
import datetime
import decimal
from importlib import import_module from importlib import import_module
import inspect
import math
import os import os
import re import re
import sys import sys
@ -304,7 +305,11 @@ class MigrationWriter(object):
elif isinstance(value, SettingsReference): elif isinstance(value, SettingsReference):
return "settings.%s" % value.setting_name, {"from django.conf import settings"} return "settings.%s" % value.setting_name, {"from django.conf import settings"}
# Simple types # Simple types
elif isinstance(value, six.integer_types + (float, bool, type(None))): elif isinstance(value, float):
if math.isnan(value) or math.isinf(value):
return 'float("{}")'.format(value), set()
return repr(value), set()
elif isinstance(value, six.integer_types + (bool, type(None))):
return repr(value), set() return repr(value), set()
elif isinstance(value, six.binary_type): elif isinstance(value, six.binary_type):
value_repr = repr(value) value_repr = repr(value)

View File

@ -49,3 +49,6 @@ Bugfixes
* Allowed usage of ``DateTimeField()`` as ``Transform.output_field`` * Allowed usage of ``DateTimeField()`` as ``Transform.output_field``
(:ticket:`23420`). (:ticket:`23420`).
* Fixed a migration serializing bug involving ``float("nan")`` and
``float("inf")`` (:ticket:23770:).

View File

@ -2,6 +2,7 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import datetime import datetime
import math
import os import os
import re import re
import tokenize import tokenize
@ -69,6 +70,10 @@ class WriterTests(TestCase):
""" """
# Basic values # Basic values
self.assertSerializedEqual(1) self.assertSerializedEqual(1)
self.assertSerializedEqual(1.2)
self.assertTrue(math.isinf(self.serialize_round_trip(float("inf"))))
self.assertTrue(math.isinf(self.serialize_round_trip(float("-inf"))))
self.assertTrue(math.isnan(self.serialize_round_trip(float("nan"))))
self.assertSerializedEqual(None) self.assertSerializedEqual(None)
self.assertSerializedEqual(b"foobar") self.assertSerializedEqual(b"foobar")
string, imports = MigrationWriter.serialize(b"foobar") string, imports = MigrationWriter.serialize(b"foobar")