Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #20278 -- ensured .get() exceptions do not recurse infinitely

A regression caused by d5b93d3 made .get() error
reporting recurse infinitely on certain rare conditions. Fixed this by
not trying to print the given lookup kwargs.
  • Loading branch information...
commit 266c0bb23e9d64c47ace4d162e582febd5a1e336 1 parent c9a9607
Anssi Kääriäinen authored May 20, 2013
10  django/db/models/query.py
@@ -388,13 +388,11 @@ def get(self, *args, **kwargs):
388 388
             return clone._result_cache[0]
389 389
         if not num:
390 390
             raise self.model.DoesNotExist(
391  
-                "%s matching query does not exist. "
392  
-                "Lookup parameters were %s" %
393  
-                (self.model._meta.object_name, kwargs))
  391
+                "%s matching query does not exist." %
  392
+                self.model._meta.object_name)
394 393
         raise self.model.MultipleObjectsReturned(
395  
-            "get() returned more than one %s -- it returned %s! "
396  
-            "Lookup parameters were %s" %
397  
-            (self.model._meta.object_name, num, kwargs))
  394
+            "get() returned more than one %s -- it returned %s!" %
  395
+            (self.model._meta.object_name, num))
398 396
 
399 397
     def create(self, **kwargs):
400 398
         """
8  tests/basic/models.py
@@ -18,3 +18,11 @@ class Meta:
18 18
 
19 19
     def __str__(self):
20 20
         return self.headline
  21
+
  22
+@python_2_unicode_compatible
  23
+class SelfRef(models.Model):
  24
+    selfref = models.ForeignKey('self', null=True, blank=True,
  25
+                                related_name='+')
  26
+
  27
+    def __str__(self):
  28
+        return SelfRef.objects.get(selfref=self).pk
24  tests/basic/tests.py
@@ -11,7 +11,7 @@
11 11
 from django.utils import six
12 12
 from django.utils.translation import ugettext_lazy
13 13
 
14  
-from .models import Article
  14
+from .models import Article, SelfRef
15 15
 
16 16
 
17 17
 class ModelTest(TestCase):
@@ -87,23 +87,14 @@ def test_lookup(self):
87 87
         # parameters don't match any object.
88 88
         six.assertRaisesRegex(self,
89 89
             ObjectDoesNotExist,
90  
-            "Article matching query does not exist. Lookup parameters were "
91  
-            "{'id__exact': 2000}",
  90
+            "Article matching query does not exist.",
92 91
             Article.objects.get,
93 92
             id__exact=2000,
94 93
         )
95 94
         # To avoid dict-ordering related errors check only one lookup
96 95
         # in single assert.
97  
-        six.assertRaisesRegex(self,
98  
-            ObjectDoesNotExist,
99  
-            ".*'pub_date__year': 2005.*",
100  
-            Article.objects.get,
101  
-            pub_date__year=2005,
102  
-            pub_date__month=8,
103  
-        )
104  
-        six.assertRaisesRegex(self,
  96
+        self.assertRaises(
105 97
             ObjectDoesNotExist,
106  
-            ".*'pub_date__month': 8.*",
107 98
             Article.objects.get,
108 99
             pub_date__year=2005,
109 100
             pub_date__month=8,
@@ -111,8 +102,7 @@ def test_lookup(self):
111 102
 
112 103
         six.assertRaisesRegex(self,
113 104
             ObjectDoesNotExist,
114  
-            "Article matching query does not exist. Lookup parameters were "
115  
-            "{'pub_date__week_day': 6}",
  105
+            "Article matching query does not exist.",
116 106
             Article.objects.get,
117 107
             pub_date__week_day=6,
118 108
         )
@@ -689,6 +679,12 @@ def test_emptyqs_distinct(self):
689 679
         with self.assertNumQueries(0):
690 680
             self.assertEqual(len(Article.objects.none().distinct('headline', 'pub_date')), 0)
691 681
 
  682
+    def test_ticket_20278(self):
  683
+        sr = SelfRef.objects.create()
  684
+        with self.assertRaises(ObjectDoesNotExist):
  685
+            SelfRef.objects.get(selfref=sr)
  686
+
  687
+
692 688
 class ConcurrentSaveTests(TransactionTestCase):
693 689
     @skipUnlessDBFeature('test_db_allows_multiple_connections')
694 690
     def test_concurrent_delete_with_save(self):

0 notes on commit 266c0bb

Please sign in to comment.
Something went wrong with that request. Please try again.