From: SourceForge.net <no...@so...> - 2007-06-19 07:13:45
|
Bugs item #1622207, was opened at 2006-12-25 22:45 Message generated for change (Comment added) made by cgroves You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=112867&aid=1622207&group_id=12867 Please note that this message will contain a full copy of the comment thread, including the initial issue submission, for this request, not just the latest update. Category: None Group: targeted for 2.2rc1 >Status: Closed >Resolution: Fixed Priority: 5 Private: No Submitted By: Leonardo Soto (leosoto) Assigned to: Nobody/Anonymous (nobody) Summary: _weakref.ref(o) only works if the argument is hasheable Initial Comment: Jython seems to impose a non standard restriction here, as CPython doesn't have any problem with this code: >>> import _weakref, UserDict >>> _weakref.ref(UserDict.UserDict()) But it raises TypeError on Jython (2.3 branch at least). I'm attaching a simple patch, but I don't know if that's the "right way" or not. ---------------------------------------------------------------------- >Comment By: Charles Groves (cgroves) Date: 2007-06-19 02:13 Message: Logged In: YES user_id=1174327 Originator: NO This is fixed in r3259. ---------------------------------------------------------------------- Comment By: Lars Heuer (lheuer) Date: 2007-05-11 05:06 Message: Logged In: YES user_id=399396 Originator: NO Even if the object defines a __hash__ method, the WeakValueDictionary does not work: >>> class X(object): def __init__(self, id): self.id = id def __hash__(self): return hash(self.id) >>> # Code taken from the CPython example: >>> import weakref >>> _id2obj_dict = weakref.WeakValueDictionary() >>> def remember(obj): oid = id(obj) _id2obj_dict[oid] = obj return oid >>> x = X(123) >>> remember(x) Traceback (innermost last): ... TypeError: unhashable instance ---------------------------------------------------------------------- Comment By: Charles Groves (cgroves) Date: 2007-04-25 03:08 Message: Logged In: YES user_id=1174327 Originator: NO The patch makes the hash value for all unhashable PyObjects 0. Combined with the way GlobalRefs are used in a hashtable, this means that creating refs for two equal but distinct objects will only make a single ref. This is undesirable as illustrated in the example I'm attaching: calling the second ref returns the first object. The only solution I can think of is to use System.identityHashCode(object) instead of object.hashCode(). As long as there isn't a case where Jython creates two separate Objects that refer to the same underlying Python object that should be fine. I can't think of anything like that off the top of my head. File Added: surprising_assignment.py ---------------------------------------------------------------------- Comment By: Mr. Codepage (codepage) Date: 2007-02-05 22:10 Message: Logged In: YES user_id=9229 Originator: NO The attached patch solved my problem with weakref. My repro case is below. from weakref import * wvd = WeakValueDictionary() try: wvd_hash = hash(wvd) except Exception,e: print e, "this was expected" try: wvd_ref = ref(wvd) print wvd_ref except Exception, e: print e, "this exception was NOT expected, running under jython" foo@foo-desktop:~/jython_dev$ python weakref_bug.py unhashable instance this was expected <weakref at 0xb7dbdfcc; to 'instance' at 0xb7dbbcec> foo@foo-desktop:~/jython_dev$ jython weakref_bug.py unhashable instance this was expected <weakref 1; to 'instance' object> If we could get any comments from a core jython dev on this bug that would be great. ---------------------------------------------------------------------- Comment By: leouser (leouserz) Date: 2007-01-15 19:20 Message: Logged In: YES user_id=1277399 Originator: NO ugh, it almost seems like a bug to me that something so basic as hashCode is throwing Exceptions. The patch is probably going to cover up something else that's more maniacal. ---------------------------------------------------------------------- You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=112867&aid=1622207&group_id=12867 |