Issue1631

classification
Title: java.util.Map derived classes lack dict methods
Type: behaviour Severity: normal
Components: Core Versions: Jython 2.7, Jython 2.5
Milestone:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: zyasoft Nosy List: amak, doublep, fwierzbicki, santa4nt, zyasoft
Priority: high Keywords: patch

Created on 2010-07-09.14:47:44 by zyasoft, last changed 2015-01-04.17:03:52 by zyasoft.

Files
File name Uploaded Description Edit Remove
issue1631.patch santa4nt, 2014-05-23.00:52:15 Inject "iteritems()" built-in methods to Java objects derived from java.util.Map.
issue1631_with_failed_tests.patch santa4nt, 2014-05-29.00:43:01 Adapting DictTest from test_dict to use java.util.HashMap, with multiple failing tests.
Messages
msg5885 (view) Author: Jim Baker (zyasoft) Date: 2010-07-09.14:47:43
Workaround as discussed in my email on jython-users:

from java.util import LinkedHashMap

def monkeypatch_method_if_not_set(cls):
    def decorator(func):
        if not hasattr(cls, func.__name__):
            setattr(cls, func.__name__, func)
        return func
    return decorator

@monkeypatch_method_if_not_set(LinkedHashMap)
def iteritems(self):
    return ((entry.getKey(), entry.getValue()) for entry in self.entrySet())

# equivalent functionality - we will do a similar type of guard
try:
    LinkedHashMap.update
except AttributeError:
    LinkedHashMap.update = LinkedHashMap.putAll
    LinkedHashMap.iterkeys = LinkedHashMap.keySet
    LinkedHashMap.itervalues = LinkedHashMap.values

# etc

Implement something like this directly in the proxy generation.
msg5887 (view) Author: (doublep) Date: 2010-07-12.11:55:39
I don't see why this is a "bug" in Jython.
msg5888 (view) Author: Jim Baker (zyasoft) Date: 2010-07-12.14:50:45
This is a bug because a goal of Jython is to create a tight integration (among others) between standard Java collections (Map, List, Set) with equivalent Python collection types (dict, list, set).
msg5921 (view) Author: Jim Baker (zyasoft) Date: 2010-07-26.01:51:06
Another missing method is the keys method. But not __iter__ or values!

We should systematically test this, instead of the limited testing we have now. Maybe we can readily reuse code from test_dict or test_userdict?
msg5922 (view) Author: Jim Baker (zyasoft) Date: 2010-07-26.01:53:48
I should also point out that this inconsistency came up in this excellent blog post
http://blogs.mulesoft.org/easily-optimizing-python-extending-with-java/
msg8558 (view) Author: Jim Baker (zyasoft) Date: 2014-05-22.02:28:06
It would be really nice to fix this issue for beta 4
msg8569 (view) Author: Santoso Wijaya (santa4nt) Date: 2014-05-22.20:08:43
I'll have a patch by tonight.
msg8570 (view) Author: Santoso Wijaya (santa4nt) Date: 2014-05-23.00:52:14
Here you go.
msg8580 (view) Author: Jim Baker (zyasoft) Date: 2014-05-27.17:20:20
Santoso, thanks for the patch! This looks like a good start. I think the next step is to look at how we can re-use existing "generic" map testing, per msg5921
msg8581 (view) Author: Santoso Wijaya (santa4nt) Date: 2014-05-28.17:54:59
@zyasoft Agreed. I have a bitbucket pull request [1] I can put further improvements on.

[1] https://bitbucket.org/jython/jython/pull-request/42/fix-issue-1631-javautilmap-derived-classes/diff
msg8584 (view) Author: Santoso Wijaya (santa4nt) Date: 2014-05-29.00:43:00
Adapting DictTest from test_dict to use java.util.HashMap, with multiple failing tests.

Not sure if it's in the scope of this issue to try to address all of them.
msg8730 (view) Author: Jim Baker (zyasoft) Date: 2014-06-19.06:35:10
Santoso has pretty much completed this work, it's now just pending a detailed review.
msg8947 (view) Author: Jim Baker (zyasoft) Date: 2014-09-02.21:26:00
Just wanted to follow up here in this bug: Santoso's PR is great and complete, however it's unfortunately exposing bugs in certain modules (such as threading) in Jython's stdlib implementation. These bugs rely on buggy behavior in the current support of java.util.Map.

I plan to address by adding a new jythonlib module that will allow for building dict, list, or set with arbitrary backing collections supporting respectively java.util.Map, java.util.List, and java.util.Set. An example is using Guava's MapMaker as a builder for dicts. Example:

import jythonlib
try:
    # jarjar-ed version
	from org.python.google.common.collect import MapMaker
except ImportError:
    # dev version from extlibs
	from com.google.common.collect import MapMaker

# note the use of the bound method
builder = jythonlib.dict_builder(MapMaker().concurrencyLevel(4).weakKeys().weakValues().makeMap)
# initialize a new dict using the above ConcurrentMap definition
d1 = builder(x=1, y=2, z=3)
msg8952 (view) Author: Jim Baker (zyasoft) Date: 2014-09-05.16:26:07
Unblocked as of http://hg.python.org/jython/rev/7a96264b09ab
msg9021 (view) Author: Jim Baker (zyasoft) Date: 2014-09-24.03:19:00
The corresponding PR has been merged in (https://hg.python.org/jython/rev/864bbec6ddb5), so at this point we are only lacking comparison methods other than __eq__.
msg9262 (view) Author: Jim Baker (zyasoft) Date: 2014-12-23.16:43:56
Fixed as of https://hg.python.org/jython/rev/fc24f60fb32c
History
Date User Action Args
2015-01-04 17:03:52zyasoftsetstatus: pending -> closed
2014-12-23 16:43:56zyasoftsetstatus: open -> pending
resolution: remind -> fixed
messages: + msg9262
2014-10-06 03:32:51zyasoftsetpriority: normal -> high
2014-09-24 03:19:01zyasoftsetmessages: + msg9021
2014-09-05 16:26:07zyasoftsetmessages: + msg8952
2014-09-02 21:26:00zyasoftsetmessages: + msg8947
2014-06-19 06:35:10zyasoftsetmessages: + msg8730
2014-05-29 00:43:02santa4ntsetfiles: + issue1631_with_failed_tests.patch
messages: + msg8584
2014-05-28 17:54:59santa4ntsetmessages: + msg8581
2014-05-27 17:20:20zyasoftsetmessages: + msg8580
2014-05-23 00:52:15santa4ntsetfiles: + issue1631.patch
keywords: + patch
messages: + msg8570
2014-05-22 20:08:43santa4ntsetmessages: + msg8569
2014-05-22 04:23:22santa4ntsetnosy: + santa4nt
type: behaviour
components: + Core
versions: + Jython 2.7
2014-05-22 02:28:06zyasoftsetresolution: remind
messages: + msg8558
2013-02-25 18:48:36fwierzbickisetnosy: + fwierzbicki
versions: + Jython 2.5, - 2.5.1
2012-03-19 20:31:42amaksetnosy: + amak
2010-07-26 01:53:48zyasoftsetmessages: + msg5922
2010-07-26 01:51:07zyasoftsetmessages: + msg5921
title: java.util.Map derived classes lack iterXXX methods -> java.util.Map derived classes lack dict methods
2010-07-12 14:50:45zyasoftsetmessages: + msg5888
2010-07-12 11:55:39doublepsetnosy: + doublep
messages: + msg5887
2010-07-09 14:47:44zyasoftcreate