mirror of
				https://github.com/django/django.git
				synced 2025-10-30 11:37:19 +00:00 
			
		
		
		
	Fixed #16383 -- Raised the AttributeError raised in property of an object when used in a template.
Thanks maraujop for the report and Hiroki and Tim Graham for review.
This commit is contained in:
		
							parent
							
								
									9d9f0acd7e
								
							
						
					
					
						commit
						0dd05c9e66
					
				
					 2 changed files with 13 additions and 3 deletions
				
			
		|  | @ -773,7 +773,10 @@ class Variable(object): | ||||||
|                         if isinstance(current, BaseContext) and getattr(type(current), bit): |                         if isinstance(current, BaseContext) and getattr(type(current), bit): | ||||||
|                             raise AttributeError |                             raise AttributeError | ||||||
|                         current = getattr(current, bit) |                         current = getattr(current, bit) | ||||||
|                     except (TypeError, AttributeError): |                     except (TypeError, AttributeError) as e: | ||||||
|  |                         # Reraise an AttributeError raised by a @property | ||||||
|  |                         if isinstance(e, AttributeError) and not isinstance(current, BaseContext) and bit in dir(current): | ||||||
|  |                             raise | ||||||
|                         try:  # list-index lookup |                         try:  # list-index lookup | ||||||
|                             current = current[int(bit)] |                             current = current[int(bit)] | ||||||
|                         except (IndexError,  # list index out of range |                         except (IndexError,  # list index out of range | ||||||
|  |  | ||||||
|  | @ -110,13 +110,17 @@ class SomeClass: | ||||||
|             raise SomeOtherException |             raise SomeOtherException | ||||||
|         raise KeyError |         raise KeyError | ||||||
| 
 | 
 | ||||||
|  |     @property | ||||||
|     def silent_fail_attribute(self): |     def silent_fail_attribute(self): | ||||||
|         raise SomeException |         raise SomeException | ||||||
|     silent_fail_attribute = property(silent_fail_attribute) |  | ||||||
| 
 | 
 | ||||||
|  |     @property | ||||||
|     def noisy_fail_attribute(self): |     def noisy_fail_attribute(self): | ||||||
|         raise SomeOtherException |         raise SomeOtherException | ||||||
|     noisy_fail_attribute = property(noisy_fail_attribute) | 
 | ||||||
|  |     @property | ||||||
|  |     def attribute_error_attribute(self): | ||||||
|  |         raise AttributeError | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| class OtherClass: | class OtherClass: | ||||||
|  | @ -820,6 +824,9 @@ class TemplateTests(TestCase): | ||||||
|             'filter-syntax23': (r'1{{ var.noisy_fail_key }}2', {"var": SomeClass()}, (SomeOtherException, SomeOtherException)), |             'filter-syntax23': (r'1{{ var.noisy_fail_key }}2', {"var": SomeClass()}, (SomeOtherException, SomeOtherException)), | ||||||
|             'filter-syntax24': (r'1{{ var.noisy_fail_attribute }}2', {"var": SomeClass()}, (SomeOtherException, SomeOtherException)), |             'filter-syntax24': (r'1{{ var.noisy_fail_attribute }}2', {"var": SomeClass()}, (SomeOtherException, SomeOtherException)), | ||||||
| 
 | 
 | ||||||
|  |             # #16383 - A @property that raises AttributeError should not fail loudly. | ||||||
|  |             'filter-syntax25': ('{{ var.attribute_error_attribute }}', {"var": SomeClass()}, (AttributeError)), | ||||||
|  | 
 | ||||||
|             ### COMMENT SYNTAX ######################################################## |             ### COMMENT SYNTAX ######################################################## | ||||||
|             'comment-syntax01': ("{# this is hidden #}hello", {}, "hello"), |             'comment-syntax01': ("{# this is hidden #}hello", {}, "hello"), | ||||||
|             'comment-syntax02': ("{# this is hidden #}hello{# foo #}", {}, "hello"), |             'comment-syntax02': ("{# this is hidden #}hello{# foo #}", {}, "hello"), | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Anubhav Joshi
						Anubhav Joshi