diff --git a/django/db/migrations/writer.py b/django/db/migrations/writer.py index f6f01d943f..53ec414335 100644 --- a/django/db/migrations/writer.py +++ b/django/db/migrations/writer.py @@ -1,10 +1,11 @@ from __future__ import unicode_literals -import datetime -import inspect -import decimal import collections +import datetime +import decimal from importlib import import_module +import inspect +import math import os import re import sys @@ -303,7 +304,11 @@ class MigrationWriter(object): elif isinstance(value, SettingsReference): return "settings.%s" % value.setting_name, set(["from django.conf import settings"]) # 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() elif isinstance(value, six.binary_type): value_repr = repr(value) diff --git a/docs/releases/1.7.2.txt b/docs/releases/1.7.2.txt index eaaf15507a..3d9a74616b 100644 --- a/docs/releases/1.7.2.txt +++ b/docs/releases/1.7.2.txt @@ -49,3 +49,6 @@ Bugfixes * Allowed usage of ``DateTimeField()`` as ``Transform.output_field`` (:ticket:`23420`). + +* Fixed a migration serializing bug involving ``float("nan")`` and + ``float("inf")`` (:ticket:23770:). diff --git a/tests/migrations/test_writer.py b/tests/migrations/test_writer.py index 0503270ed2..cb9ed5d0d5 100644 --- a/tests/migrations/test_writer.py +++ b/tests/migrations/test_writer.py @@ -2,6 +2,7 @@ from __future__ import unicode_literals import datetime +import math import os import re import tokenize @@ -69,6 +70,10 @@ class WriterTests(TestCase): """ # Basic values 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(b"foobar") string, imports = MigrationWriter.serialize(b"foobar")