mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 13:41:24 +00:00 
			
		
		
		
	Issue #5647: MutableSet.__iand__() no longer mutates self during iteration.
This commit is contained in:
		
							parent
							
								
									449b7d95d4
								
							
						
					
					
						commit
						66c4a6b51c
					
				
					 3 changed files with 30 additions and 4 deletions
				
			
		|  | @ -286,10 +286,9 @@ def __ior__(self, it): | ||||||
|             self.add(value) |             self.add(value) | ||||||
|         return self |         return self | ||||||
| 
 | 
 | ||||||
|     def __iand__(self, c): |     def __iand__(self, it): | ||||||
|         for value in self: |         for value in (self - it): | ||||||
|             if value not in c: |             self.discard(value) | ||||||
|                 self.discard(value) |  | ||||||
|         return self |         return self | ||||||
| 
 | 
 | ||||||
|     def __ixor__(self, it): |     def __ixor__(self, it): | ||||||
|  |  | ||||||
|  | @ -327,6 +327,25 @@ class C: | ||||||
|             B.register(C) |             B.register(C) | ||||||
|             self.failUnless(issubclass(C, B)) |             self.failUnless(issubclass(C, B)) | ||||||
| 
 | 
 | ||||||
|  | class WithSet(MutableSet): | ||||||
|  | 
 | ||||||
|  |     def __init__(self, it=()): | ||||||
|  |         self.data = set(it) | ||||||
|  | 
 | ||||||
|  |     def __len__(self): | ||||||
|  |         return len(self.data) | ||||||
|  | 
 | ||||||
|  |     def __iter__(self): | ||||||
|  |         return iter(self.data) | ||||||
|  | 
 | ||||||
|  |     def __contains__(self, item): | ||||||
|  |         return item in self.data | ||||||
|  | 
 | ||||||
|  |     def add(self, item): | ||||||
|  |         self.data.add(item) | ||||||
|  | 
 | ||||||
|  |     def discard(self, item): | ||||||
|  |         self.data.discard(item) | ||||||
| 
 | 
 | ||||||
| class TestCollectionABCs(ABCTestCase): | class TestCollectionABCs(ABCTestCase): | ||||||
| 
 | 
 | ||||||
|  | @ -363,6 +382,12 @@ def test_MutableSet(self): | ||||||
|         self.validate_abstract_methods(MutableSet, '__contains__', '__iter__', '__len__', |         self.validate_abstract_methods(MutableSet, '__contains__', '__iter__', '__len__', | ||||||
|             'add', 'discard') |             'add', 'discard') | ||||||
| 
 | 
 | ||||||
|  |     def test_issue_5647(self): | ||||||
|  |         # MutableSet.__iand__ mutated the set during iteration | ||||||
|  |         s = WithSet('abcd') | ||||||
|  |         s &= WithSet('cdef')            # This used to fail | ||||||
|  |         self.assertEqual(set(s), set('cd')) | ||||||
|  | 
 | ||||||
|     def test_issue_4920(self): |     def test_issue_4920(self): | ||||||
|         # MutableSet.pop() method did not work |         # MutableSet.pop() method did not work | ||||||
|         class MySet(collections.MutableSet): |         class MySet(collections.MutableSet): | ||||||
|  |  | ||||||
|  | @ -206,6 +206,8 @@ Library | ||||||
|   instead of performing them in functions. Helps prevent import deadlocking in |   instead of performing them in functions. Helps prevent import deadlocking in | ||||||
|   threads. |   threads. | ||||||
| 
 | 
 | ||||||
|  | - Issue #5647: MutableSet.__iand__() no longer mutates self during iteration. | ||||||
|  | 
 | ||||||
| - Actually make the SimpleXMLRPCServer CGI handler work. | - Actually make the SimpleXMLRPCServer CGI handler work. | ||||||
| 
 | 
 | ||||||
| - Issue #2522: locale.format now checks its first argument to ensure it has | - Issue #2522: locale.format now checks its first argument to ensure it has | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Raymond Hettinger
						Raymond Hettinger