Repository navigation
Conversation
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>
|
@jeff5 It would be good to get this into the 2.7.5 release. |
|
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
left a comment
There was a problem hiding this comment.
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
| 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)) { |
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
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:
- 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 (
javatesttarget). Tests inorg.python.expose.generatehave examples of this approach. - Define the Java classes and refer to them from Python unit tests (
regrtesttarget).test_java_visibilityis an example, where classes defined inorg.python.testsare 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.
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. |
|
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>
|
Thanks for the review, @jeff5. I've addressed both suggestions:
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 Ready for another look when you have time. Thanks! |
Fixes #475.
Java-defined subclasses of native Python collections can throw
NullPointerExceptionwhen calling methods such asdict.get()orlist.sort().PyJavaTypeinstalls collection adapters based on Java interface assignability even when the receiver is a nativePyObjectwithout the Java proxy those adapters require.Guard the call to
addCollectionProxies()so nativePyObjectsubclasses retain their Python methods and Java-defined overrides. Wrapped Java collections continue to receive the adapters.Define the Java subclasses in
org.python.tests.CollectionProxyFixturesand exercise them throughLib/test/test_java_collection_proxies.py, following the Java-fixture/Python-test pattern used bytest_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:
ant developer-buildandant javatestpassed (259 Java tests).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_delegatepassed all 11 modules.dist/bin/jython -m test.regrtest -v test_java_collection_proxiespassed all 8 tests on each runtime using the JDK 8 build, without rebuilding.