Skip to content

Preserve native Python collection methods on Java-defined subclasses - #476

Open
rakillen wants to merge 3 commits into
jython:masterfrom
rakillen:fix/native-python-collection-proxies
Open

rakillen wants to merge 3 commits into
jython:masterfrom
rakillen:fix/native-python-collection-proxies

Conversation

@rakillen

@rakillen rakillen commented Nov 18, 2026 •

Copy link
Copy Markdown

Fixes #475.

Java-defined subclasses of native Python collections can throw NullPointerException when calling methods such as dict.get() or list.sort(). PyJavaType installs collection adapters based on Java interface assignability even when the receiver is a native PyObject without the Java proxy those adapters require.

Guard the call to addCollectionProxies() so native PyObject subclasses retain their Python methods and Java-defined overrides. Wrapped Java collections continue to receive the adapters.

Define the Java subclasses in org.python.tests.CollectionProxyFixtures and exercise them through Lib/test/test_java_collection_proxies.py, following the Java-fixture/Python-test pattern used by test_java_visibility. The eight Python tests cover dictionary lookups and values, explicit getter overrides, list sorting/removal, set removal/deepcopy overrides, and wrapped Java collection controls. The former JUnit tests containing embedded Python scripts have been removed.

Without the fix, seven tests fail with adapter NPEs and the wrapped-collection control passes. With the fix, all eight pass.

Validation:

  • JDK 8: ant developer-build and ant javatest passed (259 Java tests).
  • JDK 8: dist/bin/jython -m test.regrtest test_java_collection_proxies test_dict test_dict_jy test_dict2java test_dictproxy_jy test_list test_list_jy test_set test_set_jy test_java_subclasses test_java_list_delegate passed all 11 modules.
  • JDK 11, 21, and 25: dist/bin/jython -m test.regrtest -v test_java_collection_proxies passed all 8 tests on each runtime using the JDK 8 build, without rebuilding.

Skip Java collection adapters for PyObject subclasses, whose native methods do not require a wrapped Java proxy. Add regressions for native dictionaries, lists, sets, and wrapped Java collection controls.

Fixes jython#475

Signed-off-by: Richard Killen <richard.killen@oracle.com>
@robertpatrick

Copy link
Copy Markdown
Contributor

@jeff5 It would be good to get this into the 2.7.5 release.

@jeff5 jeff5 added this to the 2.7.6 milestone Nov 21, 2026
@jeff5

jeff5 commented Nov 21, 2026

Copy link
Copy Markdown
Member

This looks like a good diagnosis in a difficult area of the core, thanks. And there are tests too.

As a behavioural change, we cannot add this to a release candidate, so I've assigned it to 2.7.6.

@jeff5 jeff5 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is based on reading only. If you agree about the approach to testing, I'll pull after that change.

We'll ask you to jump through this hoop too: https://github.com/jython/jython/blob/master/ProcessDocs/contributor_agreement.md

Comment thread src/org/python/core/PyJavaType.java Outdated
private void addCollectionProxies(Class<?> forClass) {
// Native Python objects implement their own collection protocols and do not
// have the Java proxy required by these adapters.
if (PyObject.class.isAssignableFrom(forClass)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the perspective of a human reader, I think it is clearer to guard the call, rather than to look like we're going to add proxies, then change our minds here.

interp.close();
}

public void testDictionaryGet() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My main misgiving about the PR is the approach to testing. (It's good there are tests however, so thanks.)

Clearly the test is about subclassing built-ins, so there has to be a Java element to it. But then embedding a Python script in each test feels like the wrong approach as it involves a lot of the interpreter, but cannot be single-stepped.

There are tests in the codebase that need a Java element and some Python operations. I think two approaches have been applied historically and could be taken as a pattern:

  1. Define the Java classes and create Python types (and maybe instances) from them. This invokes the logic being addressed. Look for the right kind of descriptors (and sample values in them) to be returned from an attribute lookup called in Java unit tests (javatest target). Tests in org.python.expose.generate have examples of this approach.
  2. Define the Java classes and refer to them from Python unit tests (regrtest target). test_java_visibility is an example, where classes defined in org.python.tests are imported.

Either of these gives you not only a pass-fail result (as you presumably have now) but the precision to identify what failed when it does.

@robertpatrick

Copy link
Copy Markdown
Contributor

This looks like a good diagnosis in a difficult area of the core, thanks. And there are tests too.

As a behavioural change, we cannot add this to a release candidate, so I've assigned it to 2.7.6.

How is this a behavioral change? This works in 2.2.1 but is broken in 2.7.x. This is a minor change to restore intended functionality.

If adding those few lines of code to fix this doesn't break any existing tests, then I would say it is not changing functionality.

@Stewori

Stewori commented Nov 22, 2026

Copy link
Copy Markdown
Member

From 2.2 to current 2.7 is actually a rather long time (well over decade, perhaps two). In such a long time, some user code may have become bug-compatible with the wrong behavior. Not making a case for bug compatibility, but they should get at least one proper beta phase of time to fix their stuff. I think that's why it's not making it into an rc.

Guard collection-adapter installation at the call site. Keep Java-defined native collection fixtures in org.python.tests and exercise their methods through regrtest instead of embedded Python scripts in JUnit.

Signed-off-by: Richard Killen <richard.killen@oracle.com>
Signed-off-by: Richard Killen <richard.killen@oracle.com>
@rakillen

Copy link
Copy Markdown
Author

Thanks for the review, @jeff5. I've addressed both suggestions:

  • The guard is now at the call to addCollectionProxies().
  • The tests follow your second suggested approach: Java fixtures in org.python.tests.CollectionProxyFixtures, imported by Python unit tests in test_java_collection_proxies. The JUnit tests containing embedded scripts have been removed.

The rewritten tests still reproduce seven adapter NPEs without the fix; all eight pass with it. Local validation passed: 259 Java tests, 11 Python regression modules, and the new test module on JDK 8, 11, 21, and 25 using the JDK 8 build.

I've also signed the PSF contributor agreement and added my entry to ACKNOWLEDGMENTS.

Ready for another look when you have time. Thanks!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Java-defined native collection subclasses receive incompatible Java collection adapters

4 participants