Skip to content

Java-defined native collection subclasses receive incompatible Java collection adapters #475

Open

Description

@rakillen

Summary

Java-defined subclasses of native Python collection types receive Java collection adapters that expect a wrapped Java proxy. These native Python objects have no such proxy, so normal operations throw NullPointerException.

Reproduced with a fresh, unmodified build of jython/jython at 0192c55a614b2046b3fb5759b3cb7a69c2786c29, reporting Jython 2.7.5rc2-DEV, built and run with JDK 8 (1.8.0_501). The runtime classpath contains only the upstream standalone jar and the Java reproducer below.

Minimal reproducer

Save as MinimalDict.java:

package repro;

import org.python.core.PyDictionary;
import org.python.core.PyType;

public class MinimalDict extends PyDictionary {
    public MinimalDict() {
        super(PyType.fromClass(MinimalDict.class));
    }
}

Save as test_get.py:

from repro import MinimalDict

d = MinimalDict()
d['key'] = 'value'
print d['key']
print d.get('key', 'fallback')

From the upstream checkout, after ant jar-standalone, compile and run with a JDK on the path:

mkdir -p repro-classes
javac -cp dist/jython-standalone.jar -d repro-classes MinimalDict.java
java -cp dist/jython-standalone.jar:repro-classes org.python.util.jython -S test_get.py

Expected: both lookups print value.

Actual: indexing succeeds, but .get() throws:

java.lang.NullPointerException
    at org.python.core.JavaProxyMap$11.__call__(JavaProxyMap.java:269)

Native dict and wrapped java.util.HashMap work. This specifically affects a Java-defined subclass registered reflectively through PyType.fromClass, rather than ordinary Python class D(dict) subclasses.

Cause and related operations

PyType correctly treats the class as a native PyObject, but PyJavaType.addCollectionProxies() subsequently injects adapters based only on Java collection-interface assignability. Since PyDictionary implements Map, its subclass receives the late-injected get and values adapters. JavaProxyMap.MapMethod.asMap() calls self.getJavaProxy(), which returns null for this native Python object; the injected get dereferences it at map.containsKey(k).

The same mechanism affects .values() on that dictionary subclass, sort()/remove() on Java-defined PyList subclasses, and remove()/a custom __deepcopy__ on Java-defined PySet subclasses. An explicit Java get(PyObject, PyObject) override is also bypassed.

Skipping Java collection-adapter injection for PyObject subclasses fixes these cases in local testing while preserving adapters on wrapped Java collections.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions