Skip to content

Improve boxing of arguments to vararg methods#101

Merged
jeff5 merged 4 commits into
jython:masterfrom
tpoliaw:varargs
Apr 17, 2022
Merged

Improve boxing of arguments to vararg methods#101
jeff5 merged 4 commits into
jython:masterfrom
tpoliaw:varargs

Conversation

@tpoliaw
Copy link
Copy Markdown
Contributor

@tpoliaw tpoliaw commented Jun 24, 2021

Potential fix for #100.

Only check final argument for a sequence if the correct number of arguments have been passed.

Comment thread tests/java/org/python/core/ReflectedArgsTest.java Outdated
If a Java method that accepts varargs is called from Jython, the
arguments are boxed into a PyList first. This should be skipped only if
there are already the correct number of arguments and the final
argument is a sequence type. If only the final argument is checked for
being a sequence it can block valid calls from matching the underlying
method.

eg in Java
    void foo(Object... args) {}
should be callable from Jython as
    foo(1, 2, [3, 4])
where the list and the numbers are all objects and passed as a single
array to the varargs method.

Similarly, if there are too few arguments, a java method such as
    void bar(int a, int[] b, int... c) {}
should be callable from Jython as
    bar(1, [2, 3])
where the vararg argument is empty and [2, 3] is passed as b.

The only remaining ambiguity is where a vararg parameter can take a
sequence type as an element, eg
    void fizz(Object... args) {}
calling from Jython as
    fizz([1,2,3])
would be valid as either args being [1,2,3] or [[1, 2, 3]]. This
ambiguity is also present in Java but behaves as if it was the former.
@tpoliaw
Copy link
Copy Markdown
Contributor Author

tpoliaw commented Dec 9, 2021

Any chance of getting some feedback on this? It would be good to get this fixed without having to maintain the patched version locally.
Happy to rework the PR if required.

@jeff5
Copy link
Copy Markdown
Member

jeff5 commented Mar 14, 2022

@tpoliaw Sorry, I've been having too much fun with Jython 3, but I'm having a push on Jython 2 issues now. I hope to accept one soon. It looks well-reasoned, and to have Java tests is reassuring.

Single-stepping through some of those will improve my understanding of argument matching. Then my opinion will be worth something!

I don't understand the "force push" on this PR. When I pull it to a branch on my repo I only see one change set (9fb81f4). However, if the difference is only white space I won't worry.

Experiment. Intend squash commit.
@tpoliaw
Copy link
Copy Markdown
Contributor Author

tpoliaw commented Mar 14, 2022

The force push was to update the commit with the whitespace changes @Stewori requested rather than adding a new reformatting commit. The diff for before/after the update is here

@jeff5
Copy link
Copy Markdown
Member

jeff5 commented Mar 19, 2022

That's what I guessed.

This looks good functionally, thanks. I'm merging in the tip of master for test, and will push that to your PR branch. Then there are some non-code changes (e.g. line length, and making your helpful commit message a comment) but I'll make and push those rather than ask for them.

I'll squash-merge at the end which should make a single change set in which all the extra change will have evaporated.

At least, I think that will work.

Tweak code to match intent expressed in comments and examine the
argument array actually formed.
@jeff5
Copy link
Copy Markdown
Member

jeff5 commented Mar 20, 2022

@tpoliaw : I set out to give each test a Javadoc comment based on the comment in the body, but on close inspection, a couple of the the tests didn't actually do what they claim, so I adjusted the code to match the comment. That led me to want stronger tests of what actually comes back from match in callData , and to have both bar(int i, int[] j, int... rest) and baz(Integer i, Integer[] j, Integer... rest).

So the test is a lot longer now, and I understand what the code actually does. :)

Have I got this right and do they now cover the range of your addition to ReflectedArgs?

@jeff5
Copy link
Copy Markdown
Member

jeff5 commented Mar 20, 2022

And in a further confirmation, if Demo is made public, this all delivers the arguments one would hope:

Jython 2.7.3a1-DEV (heads/pr-101-merge:7b138dcaa, Mar 20 2022, 11:10:07)
[Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation)] on java1.8.0_321
Type "help", "copyright", "credits" or "license" for more information.
>>> from org.python.core.ReflectedArgsTest import Demo
>>> Demo.foo(3, 4, ["bar"])
>>> Demo.foo([1,2,3])
>>> Demo.foo(1,2,3)
>>> Demo.foo("foo", "bar")
>>> Demo.bar(1, [2, 3])
>>> Demo.bar(1, [2, 3], 4, 5)
>>> Demo.bar(1, [2, 3], [4, 5])
>>> Demo.baz(1, [2, 3])
>>> Demo.baz(1, [2, 3], 4, 5)
>>> Demo.baz(1, [2, 3], [4, 5])

Copy link
Copy Markdown
Contributor Author

@tpoliaw tpoliaw left a comment

Choose a reason for hiding this comment

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

Thanks for looking at this and improving the docs for the tests. I think everything looks ok to me.
A couple of nitpicking comments on the javadoc but nothing that should block the PR.

Comment thread tests/java/org/python/core/ReflectedArgsTest.java
Comment thread tests/java/org/python/core/ReflectedArgsTest.java Outdated
@jeff5
Copy link
Copy Markdown
Member

jeff5 commented Apr 17, 2022

Sigh ... it fails irrelevantly (test_select), then succeeds on the rebound.

@jeff5 jeff5 merged commit d0467e7 into jython:master Apr 17, 2022
@tpoliaw tpoliaw deleted the varargs branch April 18, 2022 08:47
jeff5 pushed a commit that referenced this pull request Aug 27, 2022
The natural sequel to #101 supporting the same pattern in constructors.
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.

3 participants