Issue2451
Created on 2016-01-04.15:36:06 by lsenta, last changed 2016-01-19.18:37:28 by zyasoft.
msg10585 (view) |
Author: Laurent Senta (lsenta) |
Date: 2016-01-04.15:36:06 |
|
I'm trying to use ByteArrays in a dict and I get duplicated keys,
It appears that in Jython bytearray can be hashed (not in regular python 2.7) but the hash is not consistent with the equality operator:
>>> from array import array
>>> xs = [
array('b', [99, 111, 117, 110, 116, 45, 105, 116, 101, 109, 115]),
array('b', [99, 111, 117, 110, 116, 45, 105, 116, 101, 109, 115]),
array('b', [98, 121, 116, 101, 45, 115, 105, 122, 101])
]
>>> xs[0] == xs[1]
True
>>> xs[0] == xs[2]
False
>>> map(hash, xs)
[1363822052, 1722768342, 576563150]
|
msg10586 (view) |
Author: Jim Baker (zyasoft) |
Date: 2016-01-05.04:43:54 |
|
Just missing the appropriate support so that Object#hashCode is not invoked (the hash code is defined for all objects in Java, unlike Python). Simply following the example of PyByteArray (which implements bytearray), we get the following patch:
+ @Override
+ public int hashCode()
+ return array___hash__();
+ }
+
+ @ExposedMethod
+ final int array___hash__() {
+ throw Py.TypeError(String.format("unhashable type: '%.200s'", getType().fastGetName()));
+ }
+
Just need to add a test to complete.
|
msg10587 (view) |
Author: Jim Baker (zyasoft) |
Date: 2016-01-05.04:44:51 |
|
Should have included the header as well - that patch is against org.python.core.PyArray
|
msg10596 (view) |
Author: Jim Baker (zyasoft) |
Date: 2016-01-06.21:46:53 |
|
Fixed as of https://hg.python.org/jython/rev/49d498968f34
Note this means you still cannot use array.array in a dict as keys, just like CPython.
|
msg10609 (view) |
Author: Laurent Senta (lsenta) |
Date: 2016-01-08.08:12:32 |
|
As long as it's explicit about it, that sounds fine to me,
thanks for fixing this.
|
|
Date |
User |
Action |
Args |
2016-01-19 18:37:28 | zyasoft | set | status: pending -> closed |
2016-01-08 08:12:33 | lsenta | set | messages:
+ msg10609 |
2016-01-06 21:46:53 | zyasoft | set | status: open -> pending resolution: accepted -> fixed title: hash and equality of bytearrays are not consistent -> array.array objects should not hash messages:
+ msg10596 milestone: Jython 2.7.1 |
2016-01-05 04:44:51 | zyasoft | set | messages:
+ msg10587 |
2016-01-05 04:43:55 | zyasoft | set | resolution: accepted messages:
+ msg10586 nosy:
+ zyasoft |
2016-01-04 15:36:06 | lsenta | create | |
|