This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: It is possible to observe a mutating frozenset
Type: behavior Stage:
Components: Interpreter Core Versions: Python 3.1
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: abacabadabacaba, ezio.melotti, ggeal, rhettinger
Priority: low Keywords:

Created on 2010-04-17 19:26 by abacabadabacaba, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (4)
msg103426 - (view) Author: Evgeny Kapun (abacabadabacaba) Date: 2010-04-17 19:26
This code shows that frozensets aren't really immutable. The same frozenset is printed twice, with different content. Buggy functions are set_contains, set_remove and set_discard, all in Objects/setobject.c

class bad:
	def __eq__(self, other):
		global f2
		f2 = other
		print_f2()
		s1.add("querty")
		return self is other
	def __hash__(self):
		return hash(f1)
def print_f2():
	print(id(f2), repr(f2))
f1 = frozenset((1, 2, 3))
s1 = set(f1)
s1 in {bad()}
print_f2()
msg103451 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2010-04-17 23:19
I'm not sure that I care about this one.  The only way to pull it off it is to intentionally try to mutate the frozenset.  Will look at it along with the others though.
msg103502 - (view) Author: Gath-Gealaich (ggeal) Date: 2010-04-18 15:55
I have found this in Python 3.1.2 documentation:

"Note, the elem argument to the __contains__(), remove(), and discard() methods may be a set. To support searching for an equivalent frozenset, the elem set is temporarily mutated during the search and then restored. During the search, the elem set should not be read or mutated since it does not have a meaningful value."

I seems to me that this is precisely what happens in this code. Now we might argue whether this is an acceptable state of affairs, but at least it's a documented behavior (and people shouldn't write equality predicates like this anyway. ;)).
msg103535 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2010-04-18 21:58
Thanks ggeal.  Closing as won't fix.  The code is functioning as designed and documented.
History
Date User Action Args
2022-04-11 14:57:00adminsetgithub: 52682
2010-08-04 23:45:20ezio.melottisetnosy: + ezio.melotti
2010-04-18 21:58:23rhettingersetstatus: open -> closed
resolution: wont fix
messages: + msg103535
2010-04-18 15:55:45ggealsetnosy: + ggeal
messages: + msg103502
2010-04-17 23:19:28rhettingersetpriority: low

messages: + msg103451
2010-04-17 19:39:02benjamin.petersonsetassignee: rhettinger

nosy: + rhettinger
2010-04-17 19:26:17abacabadabacabacreate