Repository: ninia/jep (language: python)

Issue:
PyJType for java.util.Map now extends collections.abc.Mapping
This allows Java maps to be used in python anywhere a python mapping is expected. Before this change java Maps are missing functionality such as item(), keys(), and values() which are now automatically added by extending collections.abc.Mapping.
PyJType for java.util.Map now extends collections.abc.Mapping
This allows Java maps to be used in python anywhere a python mapping is expected. Before this change java Maps are missing functionality such as item(), keys(), and values() which are now automatically added by extending collections.abc.Mapping.
PyJType for java.util.Map now extends collections.abc.Mapping
This allows Java maps to be used in python anywhere a python mapping is expected. Before this change java Maps are missing functionality such as item(), keys(), and values() which are now automatically added by extending collections.abc.Mapping.


Discussion / hints:

Fix patch:
diff --git a/release_notes/4.2-notes.rst b/release_notes/4.2-notes.rst
index 6cf9d6af..4ccd9917 100644
--- a/release_notes/4.2-notes.rst
+++ b/release_notes/4.2-notes.rst
@@ -21,13 +21,6 @@ from a Java object. For more details on how to register a custom converter see
 `the wiki <https://github.com/ninia/jep/wiki/Accessing-Java-Objects-in-Python#custom-conversion-functions>`_
 or exec ``help(jep.setJavaToPythonConverter)`` in a Jep interpreter.
 
-PyJType for java.util.Map now extends collections.abc.Mapping
-*************************************************************
-This allows Java Maps to be used in Python anywhere a Python mapping is
-expected. Before this change Java Maps are missing functionality such as
-``items()``, ``keys()``, and ``values()`` which are now automatically added by
-extending ``collections.abc.Mapping``.
-
 Additions of PyBuiltins
 ***********************
 The `PyBuiltins <http://ninia.github.io/jep/javadoc/4.2/jep/python/PyBuiltin.html>`_
diff --git a/src/main/c/Objects/pyjtype.c b/src/main/c/Objects/pyjtype.c
index 4a78525c..6e5dff46 100644
--- a/src/main/c/Objects/pyjtype.c
+++ b/src/main/c/Objects/pyjtype.c
@@ -120,25 +120,9 @@ static int populateCustomTypeDict(JNIEnv *env, PyObject* fqnToPyType)
                            pyjcollection)) {
         return -1;
     }
-    PyObject* collectionAbc = PyImport_ImportModule("collections.abc");
-    if (!collectionAbc) {
+    if (!addSpecToTypeDict(env, fqnToPyType, JMAP_TYPE, &PyJMap_Spec, NULL)) {
         return -1;
     }
-    PyObject* mapping = PyObject_GetAttrString(collectionAbc, "Mapping");
-    if (!mapping) {
-        Py_DECREF(collectionAbc);
-        return -1;
-    }
-    Py_DECREF(collectionAbc);
-    if (!PyType_Check(mapping)) {
-        Py_DECREF(mapping);
-        return -1;
-    }
-    if (!addSpecToTypeDict(env, fqnToPyType, JMAP_TYPE, &PyJMap_Spec, (PyTypeObject*) mapping)) {
-        Py_DECREF(mapping);
-        return -1;
-    }
-    Py_DECREF(mapping);
     if (!addSpecToTypeDict(env, fqnToPyType, JNUMBER_TYPE, &PyJNumber_Spec,
                            &PyJObject_Type)) {
         return -1;
diff --git a/src/main/java/jep/python/PyBuiltins.java b/src/main/java/jep/python/PyBuiltins.java
index 0cb327d4..d585484a 100644
--- a/src/main/java/jep/python/PyBuiltins.java
+++ b/src/main/java/jep/python/PyBuiltins.java
@@ -46,8 +46,6 @@ public interface PyBuiltins {
 
     public PyObject dict();
 
-    public PyObject dict(Map<?,?> mapping);
-
     public String[] dir(PyObject object);
 
     public Object eval(String expression, PyObject globals);


Test patch:
diff --git a/src/test/java/jep/test/TestPyBuiltins.java b/src/test/java/jep/test/TestPyBuiltins.java
index f3a29164..401b56c8 100644
--- a/src/test/java/jep/test/TestPyBuiltins.java
+++ b/src/test/java/jep/test/TestPyBuiltins.java
@@ -84,16 +84,6 @@ private boolean testDict() {
             failure = "dict builtin does not return an empty dict";
             return false;
         }
-        Map<String,Integer> map = new HashMap<>();
-        map.put("a", 1);
-        map.put("b", 2);
-        map.put("c", 3);
-        dictBuiltin = builtins.dict(map);
-        dictGetValue = interp.getValue("{'a':1, 'b':2, 'c':3}", PyObject.class);
-        if (!dictBuiltin.equals(dictGetValue)){
-            failure = "dict builtin does not return a dict from map";
-            return false;
-        }
         return true;
     }
 
diff --git a/src/test/python/test_maps.py b/src/test/python/test_maps.py
index dbcec8c0..27199be0 100644
--- a/src/test/python/test_maps.py
+++ b/src/test/python/test_maps.py
@@ -43,12 +43,6 @@ def test_len(self):
         pymap = makePythonDict()
         self.assertEqual(len(jmap), len(pymap))
 
-    def test_mapping(self):
-        jmap = makeJavaMap()
-        self.assertTrue(isinstance(jmap, Mapping))
-        pymap = makePythonDict()
-        self.assertEqual(dict(jmap), pymap)
-
     def test_del(self):
         jmap = makeJavaMap()
         pymap = makePythonDict()