Code

Ticket #12152: 12152-3.diff

File 12152-3.diff, 4.9 KB (added by mattmcc, 4 years ago)

Remove unnecessary uses of getattr

Line 
1Index: django/db/models/base.py
2===================================================================
3--- django/db/models/base.py    (revision 12386)
4+++ django/db/models/base.py    (working copy)
5@@ -52,10 +52,14 @@
6 
7         new_class.add_to_class('_meta', Options(meta, **kwargs))
8         if not abstract:
9-            new_class.add_to_class('DoesNotExist',
10-                    subclass_exception('DoesNotExist', ObjectDoesNotExist, module))
11-            new_class.add_to_class('MultipleObjectsReturned',
12-                    subclass_exception('MultipleObjectsReturned', MultipleObjectsReturned, module))
13+            new_class.add_to_class('DoesNotExist', subclass_exception('DoesNotExist',
14+                    tuple(x.DoesNotExist
15+                            for x in parents if hasattr(x, '_meta') and not x._meta.abstract)
16+                                    or ObjectDoesNotExist, module))
17+            new_class.add_to_class('MultipleObjectsReturned', subclass_exception('MultipleObjectsReturned',
18+                    tuple(x.MultipleObjectsReturned
19+                            for x in parents if hasattr(x, '_meta') and not x._meta.abstract)
20+                                    or MultipleObjectsReturned, module))
21             if base_meta and not base_meta.abstract:
22                 # Non-abstract child classes inherit some attributes from their
23                 # non-abstract parent (unless an ABC comes before it in the
24@@ -920,7 +924,9 @@
25 if sys.version_info < (2, 5):
26     # Prior to Python 2.5, Exception was an old-style class
27     def subclass_exception(name, parent, unused):
28-        return types.ClassType(name, (parent,), {})
29+        return types.ClassType(name,
30+                    (parent,) if type(parent) == type else parent, {})
31 else:
32     def subclass_exception(name, parent, module):
33-        return type(name, (parent,), {'__module__': module})
34+        return type(name, (parent,) if type(parent) == type else parent,
35+                    {'__module__': module})
36Index: tests/modeltests/proxy_models/models.py
37===================================================================
38--- tests/modeltests/proxy_models/models.py     (revision 12386)
39+++ tests/modeltests/proxy_models/models.py     (working copy)
40@@ -206,6 +206,26 @@
41 >>> MyPersonProxy.objects.all()
42 [<MyPersonProxy: Bazza del Frob>, <MyPersonProxy: Foo McBar>, <MyPersonProxy: homer>]
43 
44+# Proxy models are included in the ancestors for a model's DoesNotExist and MultipleObjectsReturned
45+>>> try:
46+...     MyPersonProxy.objects.get(name='Zathras')
47+... except Person.DoesNotExist:
48+...     pass
49+>>> try:
50+...     MyPersonProxy.objects.get(id__lt=10)
51+... except Person.MultipleObjectsReturned:
52+...     pass
53+>>> try:
54+...     StatusPerson.objects.get(name='Zathras')
55+... except Person.DoesNotExist:
56+...     pass
57+>>> sp1 = StatusPerson.objects.create(name='Bazza Jr.')
58+>>> sp2 = StatusPerson.objects.create(name='Foo Jr.')
59+>>> try:
60+...     StatusPerson.objects.get(id__lt=10)
61+... except Person.MultipleObjectsReturned:
62+...     pass
63+
64 # And now for some things that shouldn't work...
65 #
66 # All base classes must be non-abstract
67Index: tests/modeltests/model_inheritance/models.py
68===================================================================
69--- tests/modeltests/model_inheritance/models.py        (revision 12386)
70+++ tests/modeltests/model_inheritance/models.py        (working copy)
71@@ -38,6 +38,9 @@
72     class Meta:
73         pass
74 
75+class StudentWorker(Student, Worker):
76+    pass
77+
78 #
79 # Abstract base classes with related models
80 #
81@@ -176,6 +179,32 @@
82     ...
83 AttributeError: type object 'CommonInfo' has no attribute 'objects'
84 
85+# A StudentWorker which does not exist is both a Student and Worker which does not exist.
86+>>> try:
87+...     StudentWorker.objects.get(id=1)
88+... except Student.DoesNotExist:
89+...     pass
90+>>> try:
91+...     StudentWorker.objects.get(id=1)
92+... except Worker.DoesNotExist:
93+...     pass
94+
95+# MultipleObjectsReturned is also inherited.
96+>>> sw1 = StudentWorker()
97+>>> sw1.name = 'Wilma'
98+>>> sw1.age = 35
99+>>> sw1.save()
100+>>> sw2 = StudentWorker()
101+>>> sw2.name = 'Betty'
102+>>> sw2.age = 34
103+>>> sw2.save()
104+>>> try:
105+...     StudentWorker.objects.get(id__lt=10)
106+... except Student.MultipleObjectsReturned:
107+...     pass
108+... except Worker.MultipleObjectsReturned:
109+...     pass
110+
111 # Create a Post
112 >>> post = Post(title='Lorem Ipsum')
113 >>> post.save()
114@@ -267,6 +296,18 @@
115     ...
116 DoesNotExist: ItalianRestaurant matching query does not exist.
117 
118+# An ItalianRestaurant which does not exist is also a Place which does not exist.
119+>>> try:
120+...     ItalianRestaurant.objects.get(name='The Noodle Void')
121+... except Place.DoesNotExist:
122+...     pass
123+
124+# MultipleObjectsReturned is also inherited.
125+>>> try:
126+...     Restaurant.objects.get(id__lt=10)
127+... except Place.MultipleObjectsReturned:
128+...     pass
129+
130 # Related objects work just as they normally do.
131 
132 >>> s1 = Supplier(name="Joe's Chickens", address='123 Sesame St')