From adc0ab3386520874dbc96c2f905fd66769b8f16d Mon Sep 17 00:00:00 2001 From: Loic Bistuer Date: Thu, 5 Sep 2013 10:36:31 +0700 Subject: [PATCH] Fixed #21037 -- Made MigrationWriter raise a ValueError when serializing lambda functions. --- django/db/migrations/writer.py | 6 ++++-- tests/migrations/test_writer.py | 3 +++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/django/db/migrations/writer.py b/django/db/migrations/writer.py index e23f15dd77..fa4afbeabf 100644 --- a/django/db/migrations/writer.py +++ b/django/db/migrations/writer.py @@ -152,10 +152,12 @@ class MigrationWriter(object): klass = value.im_class module = klass.__module__ return "%s.%s.%s" % (module, klass.__name__, value.__name__), set(["import %s" % module]) + elif value.__name__ == '': + raise ValueError("Cannot serialize function: lambda") + elif value.__module__ is None: + raise ValueError("Cannot serialize function %r: No module" % value) else: module = value.__module__ - if module is None: - raise ValueError("Cannot serialize function %r: No module" % value) return "%s.%s" % (module, value.__name__), set(["import %s" % module]) # Classes elif isinstance(value, type): diff --git a/tests/migrations/test_writer.py b/tests/migrations/test_writer.py index ff6fc47737..022628803b 100644 --- a/tests/migrations/test_writer.py +++ b/tests/migrations/test_writer.py @@ -60,6 +60,9 @@ class WriterTests(TestCase): self.assertSerializedEqual(set([2, 3, "eighty"])) self.assertSerializedEqual({"lalalala": ["yeah", "no", "maybe"]}) self.assertSerializedEqual(_('Hello')) + # Functions + with six.assertRaisesRegex(self, ValueError, 'Cannot serialize function: lambda'): + self.assertSerializedEqual(lambda x: 42) # Datetime stuff self.assertSerializedEqual(datetime.datetime.utcnow()) self.assertSerializedEqual(datetime.datetime.utcnow)