Re: Overloaded Java method handing changes between 2.7.2 and 2.7.3?

John Hubbard <[email protected]> Mon, 10 Jun 2024 13:13:40 -0600
Newsgroups gmane.comp.lang.jython.user
Message-ID <CAO2imXx73xSGg9TYjev+4a9zLJ2wWDGn=9ArQ+wQmxKNvOQFjQ@mail.gmail.com>
--===============8807504134147793850==
Content-Type: multipart/alternative; boundary="000000000000939507061a8df468"

--000000000000939507061a8df468
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Jeff,

Thanks again for pointing me in the right direction.  For anyone else who
stumbles across this thread I tracked the issue down and have opened [1].

[1] https://github.com/jython/jython/issues/335

--
Cheers
-john


On Fri, Jun 7, 2024 at 4:52=E2=80=AFPM John Hubbard <[email protected]> wrot=
e:

> Thanks Jeff for the quick reply.
>
> The GH-221 tickets that the commit addressed sounds kind of like this
> because our AttributeTable class does in fact use varargs but there is a
> comment[1] in the ticket for what sounds exactly like my issue and
> tpoliaw's reply suggests that this isn't intended to fix it though.
>
> I went ahead and built the latest version of master locally and used that
> for testing and unfortunately it doesn't address my problem.  Now that I'=
ve
> actually built this myself, and armed with a pointer at where to look, I'=
ll
> see if I can identify where things changed and work on a solution.  Don't
> worry about holding off on a 2.7.4 on our account.
>
> Thanks again!
>
> [1] https://github.com/jython/jython/issues/221#issuecomment-1784005688
> --
> Cheers
> -john
>
>
> On Fri, Jun 7, 2024 at 4:10=E2=80=AFAM Jeff Allen <[email protected]> wr=
ote:
>
>> This is maybe better as an issue, although it sounds like one that
>> already defeated us.
>>
>> 1. The code by which a call is matched to a method always looked somewha=
t
>> heuristic to me. (I believe this comes from a Greek word meaning "luck".=
)
>> As I understand it, it takes the first match, a match ocurring where the
>> arguments say they can produce the Java type in the signature. The sort
>> order of overloaded methods is therefore important. I think "What Would
>> Java Do?" is the aspiration, but I suspect this cannot be achieved just =
by
>> sorting. (Have we changed the sort? I didn't think so.) The best express=
ion
>> of expected behaviour is probably in the tests `test_joverload.py` and
>> `javatests/Reflection.java`.
>>
>> 2. There has been some work in the matching to address variable numbers
>> of arguments, so some code change, late in 2.7.3 (I think) and in 2.7.4
>> (currently in beta). I might have blamed:
>> https://github.com/jython/jython/commit/dd271a5edaa06925d45ef2d1477e3ec1=
cf6adfc7
>> but I think it is later than you need to explain the change. However, I
>> think it is a good place to start. These are the source files right file=
s
>> (probably).  Another possible cause is in "normalisations" that can occu=
r
>> during assignment which I think we tweaked (to solve another problem, no=
t
>> just for the heck of it).
>>
>> 3. You're asking to hook the `__tojava__` methods, which I don't *think*
>> you can do. It would be on built-in types too.
>>
>> How is it with 2.7.4b2? I think I wouldn't delay 2.7.4 for this, but
>> there's always a small hope your problem has gone away.
>>
>> Bets wishes,
>> Jeff
>>
>>
>> On 06/06/2024 22:38, John Hubbard wrote:
>>
>> Hello,
>>
>> Has something changed between Jython 2.7.2 and Jython 2.7.2 with regards
>> to how calls from Jython to an overloaded Java method are resolved?
>>
>> *Background*
>> I am trying to update our application from Jython 2.7.2 to Jython 2.7.3
>> and I have run into what I think is a change in how Jython handles
>> overloaded Java methods.  The primary motivation for the update is prope=
r
>> handling of * imports under JDK 17 (and JUnit 5); see Jython GitHub issu=
es
>> 105, 304 and maybe 309.
>>
>> Our application is primarily Java based but uses Jython for high level
>> scripts which perform high level sequencing.  One of the most common thi=
ngs
>> those scripts do is construct 'bags of data' which are handled by our
>> AttributeTable class (full jdoc here
>> <https://share.nso.edu/shared/dkist/jhubbard/atst/atst/cs/interfaces/IAt=
tributeTable.html>).
>> That class is backed by a Map<String, String[]> and stores heterogenous
>> data via various via getters and setters like:
>>
>> insert(String name, String value)
>> insert(String name, int value)
>>
>> insert(String name, boolean value)
>>
>> ...
>>
>> String getString(String name)
>>
>> int getInt(String name)
>>
>>
>> With Jython 2.7.2 I could could write a script like:
>>
>> tbl =3D AttributeTable();
>> name =3D 'name'
>> value =3D 27
>> tbl.insert(name, value)
>>
>> and it would behave as expected.  Jython would resolve the insert call
>> to  the Java AttributeTable.insert(String, int) method, the Java side wo=
uld
>> then convert the integer 27 into the String "27" and store it in the map
>> for later use.
>>
>> *Problem*
>> With Jython 2.7.3 I think that the truthiness of the value is being
>> evaluated and AttributeTable.insert(String, boolean) is what is getting
>> triggered.  For example the following code:
>>
>> def savePropertyValue(appName, propertyName, propertyValue):
>>   print('savePropertyValue(' + str(appName) + ', ' + str(propertyName)
>>           + ', ' + str(propertyValue) + ')')
>>   at =3D AttributeTable()
>>   at.insert(propertyName, propertyValue)
>>   at.show('savePropertyValue() AttributeTable ')
>>
>> produces
>>
>> savePropertyValue(atst.ics.visp, atst.ics.visp.slitStepSz, 0.05)
>> savePropertyValue() AttributeTable atst.ics.visp.slitStepSz: true
>>
>>
>> So the value 0.05 was passed into the jython savePropertyValue function
>> but what ended up being inserted into the Java AttributeTable object was
>> the boolean true.
>>
>> *Questions*
>>
>>    1. What is the expected behavior when interacting with overloaded
>>    Java objects?  Were we just lucky before and this was never expected =
to
>>    work?
>>    2. Is anyone aware of changes on the Java side that may have
>>    triggered this?  I'd like some background on what might have caused t=
his so
>>    I can investigate what might be improved to fix this.
>>    3. Is it possible to inject custom PyObject --> Java Object coercion
>>    routines?  The interpreters are setup at a high level; if I could mas=
k the
>>    Java AttributeTable class with a Python class that better handled
>>    overloaded methods and then somehow coerce/convert it to an Java
>>    AttributeTable before sending it back into Java land I might be able =
to
>>    work around this.
>>
>> Thanks
>>
>> --
>> Cheers
>> -john
>>
>>
>> _______________________________________________
>> Jython-users mailing [email protected]://lists=
.sourceforge.net/lists/listinfo/jython-users
>>
>> --
>>
>> Jeff Allen
>>
>> _______________________________________________
>> Jython-users mailing list
>> [email protected]
>> https://lists.sourceforge.net/lists/listinfo/jython-users
>>
>

--000000000000939507061a8df468
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Jeff,=C2=A0<div><br></div><div>Thanks again for pointing m=
e in the=C2=A0right direction.=C2=A0 For anyone else who stumbles across th=
is thread I tracked=C2=A0the issue down and have opened [1].=C2=A0=C2=A0</d=
iv><div><br></div><div>[1] <a href=3D"https://github.com/jython/jython/issu=
es/335">https://github.com/jython/jython/issues/335</a><br><div><br clear=
=3D"all"><div><div dir=3D"ltr" class=3D"gmail_signature" data-smartmail=3D"=
gmail_signature"><div dir=3D"ltr">--<div>Cheers</div><div>-john</div></div>=
</div></div><br></div></div></div><br><div class=3D"gmail_quote"><div dir=
=3D"ltr" class=3D"gmail_attr">On Fri, Jun 7, 2024 at 4:52=E2=80=AFPM John H=
ubbard &lt;<a href=3D"mailto:[email protected]">[email protected]</a>&gt; wro=
te:<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px =
0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir=3D"=
ltr">Thanks Jeff for the quick reply.=C2=A0=C2=A0<div><br></div><div>The GH=
-221 tickets that the commit addressed sounds kind of like this because our=
 AttributeTable class does in fact use varargs but there is a comment[1] in=
 the ticket for what sounds exactly like my issue and tpoliaw&#39;s reply s=
uggests that this isn&#39;t intended to fix it though.=C2=A0=C2=A0<div><br>=
</div><div><div><div>I went ahead and built the latest version of master lo=
cally and used that for testing and unfortunately it doesn&#39;t address my=
 problem.=C2=A0 Now that I&#39;ve actually built this myself, and armed wit=
h a pointer at where to look, I&#39;ll see if I can identify where things c=
hanged and work on a solution.=C2=A0 Don&#39;t worry=C2=A0about holding off=
 on a 2.7.4 on our account.</div><div><br></div><div>Thanks again!</div><di=
v><br></div><div>[1]=C2=A0<a href=3D"https://github.com/jython/jython/issue=
s/221#issuecomment-1784005688" target=3D"_blank">https://github.com/jython/=
jython/issues/221#issuecomment-1784005688</a><br clear=3D"all"><div><div di=
r=3D"ltr" class=3D"gmail_signature"><div dir=3D"ltr">--<div>Cheers=C2=A0</d=
iv><div>-john</div></div></div></div><br></div></div></div></div></div><br>=
<div class=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail_attr">On Fri, Ju=
n 7, 2024 at 4:10=E2=80=AFAM Jeff Allen &lt;<a href=3D"mailto:ja.py@farowl.=
co.uk" target=3D"_blank">[email protected]</a>&gt; wrote:<br></div><blockq=
uote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1p=
x solid rgb(204,204,204);padding-left:1ex"><u></u>

 =20
   =20
 =20
  <div>
    <div>This is maybe better as an issue,
      although it sounds like one that already defeated us.<br>
    </div>
    <div><br>
    </div>
    <div>1. The code by which a call is matched
      to a method always looked somewhat heuristic to me. (I believe
      this comes from a Greek word meaning &quot;luck&quot;.) As I understa=
nd it,
      it takes the first match, a match ocurring where the arguments say
      they can produce the Java type in the signature. The sort order of
      overloaded methods is therefore important. I think &quot;What Would
      Java Do?&quot; is the aspiration, but I suspect this cannot be achiev=
ed
      just by sorting. (Have we changed the sort? I didn&#39;t think so.)
      The best expression of expected behaviour is probably in the tests
      `test_joverload.py` and `javatests/Reflection.java`.</div>
    <div><br>
    </div>
    <div>2. There has been some work in the
      matching to address variable numbers of arguments, so some code
      change, late in 2.7.3 (I think) and in 2.7.4 (currently in beta).
      I might have blamed:
<a href=3D"https://github.com/jython/jython/commit/dd271a5edaa06925d45ef2d1=
477e3ec1cf6adfc7" target=3D"_blank">https://github.com/jython/jython/commit=
/dd271a5edaa06925d45ef2d1477e3ec1cf6adfc7</a>
      but I think it is later than you need to explain the change.
      However, I think it is a good place to start. These are the source
      files right files (probably).=C2=A0 Another possible cause is in
      &quot;normalisations&quot; that can occur during assignment which I t=
hink we
      tweaked (to solve another problem, not just for the heck of it).<br>
    </div>
    <div><br>
    </div>
    <div>3. You&#39;re asking to hook the
      `__tojava__` methods, which I don&#39;t *think* you can do. It would
      be on built-in types too.<br>
    </div>
    <div><br>
    </div>
    <div>How is it with 2.7.4b2? I think I
      wouldn&#39;t delay 2.7.4 for this, but there&#39;s always a small hop=
e
      your problem has gone away.<br>
    </div>
    <div><br>
    </div>
    <div>Bets wishes,<br>
    </div>
    <div>Jeff<br>
    </div>
    <div><br>
    </div>
    <div><br>
    </div>
    <div>On 06/06/2024 22:38, John Hubbard
      wrote:<br>
    </div>
    <blockquote type=3D"cite">
     =20
      <div dir=3D"ltr">
        <div dir=3D"ltr">Hello,=C2=A0</div>
        <div dir=3D"ltr"><br>
        </div>
        <div dir=3D"ltr">Has something changed between Jython 2.7.2 and
          Jython 2.7.2 with regards to how calls from Jython to an
          overloaded Java method are resolved?=C2=A0=C2=A0<br>
          <div><br>
          </div>
          <div><b>Background</b><br>
          </div>
          <div>I am trying to update our application from Jython 2.7.2
            to Jython 2.7.3 and I have run into what I think is a change
            in how Jython handles overloaded Java methods.=C2=A0 The primar=
y
            motivation for the update is proper handling of * imports
            under JDK 17 (and JUnit 5); see Jython GitHub issues 105,
            304 and maybe 309.=C2=A0=C2=A0</div>
          <div><br>
          </div>
          <div>Our application is primarily Java based but uses Jython
            for high level scripts which perform high level sequencing.=C2=
=A0
            One of the most common things those scripts do is construct
            &#39;bags of data&#39; which are handled by our AttributeTable =
class
            (full jdoc <a href=3D"https://share.nso.edu/shared/dkist/jhubba=
rd/atst/atst/cs/interfaces/IAttributeTable.html" target=3D"_blank">here</a>=
).=C2=A0 That
            class is backed by a Map&lt;String, String[]&gt; and stores
            heterogenous data via various via getters and setters like:</di=
v>
          <blockquote style=3D"margin:0px 0px 0px 40px;border:none;padding:=
0px">
            <div><font face=3D"monospace">insert(String name, String
                value)</font></div>
            <div><font face=3D"monospace">insert(String name, int value)</f=
ont></div>
          </blockquote>
          <blockquote style=3D"margin:0px 0px 0px 40px;border:none;padding:=
0px">
            <div><font face=3D"monospace">insert(String name, boolean
                value)</font></div>
          </blockquote>
          <blockquote style=3D"margin:0px 0px 0px 40px;border:none;padding:=
0px">
            <div><font face=3D"monospace">...</font></div>
          </blockquote>
          <blockquote style=3D"margin:0px 0px 0px 40px;border:none;padding:=
0px">
            <div><font face=3D"monospace">String getString(String name)</fo=
nt></div>
          </blockquote>
          <blockquote style=3D"margin:0px 0px 0px 40px;border:none;padding:=
0px">
            <div><font face=3D"monospace">int getInt(String name)</font></d=
iv>
          </blockquote>
          <div><br>
          </div>
          <div>With Jython 2.7.2 I could could write a script like:</div>
          <blockquote style=3D"margin:0px 0px 0px 40px;border:none;padding:=
0px">
            <div><font face=3D"monospace">tbl =3D AttributeTable();</font><=
/div>
            <div><font face=3D"monospace">name =3D &#39;name&#39;</font></d=
iv>
            <div><font face=3D"monospace">value =3D 27</font></div>
            <div><font face=3D"monospace">tbl.insert(name, value)</font></d=
iv>
          </blockquote>
          <div>and it would behave as expected.=C2=A0 Jython would resolve
            the insert call to=C2=A0 the Java AttributeTable.insert(String,
            int) method, the Java side would then convert the integer 27
            into the String &quot;27&quot; and store it in the map for late=
r
            use.=C2=A0=C2=A0</div>
          <div><br>
          </div>
          <div><b>Problem</b></div>
          <div>With Jython 2.7.3 I think that the truthiness of the
            value is being evaluated and AttributeTable.insert(String,
            boolean)=C2=A0is what is getting triggered.=C2=A0 For example t=
he
            following code:</div>
        </div>
        <blockquote style=3D"margin:0px 0px 0px 40px;border:none;padding:0p=
x">
          <div>
            <div>
              <div><font face=3D"monospace">def savePropertyValue(appName,
                  propertyName, propertyValue):</font></div>
            </div>
          </div>
          <div>
            <div>
              <div><font face=3D"monospace">=C2=A0 print(&#39;savePropertyV=
alue(&#39; +
                  str(appName) + &#39;, &#39; + str(propertyName)</font></d=
iv>
            </div>
          </div>
          <div>
            <div>
              <div><font face=3D"monospace">=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 + &#39;, &#39; +
                  str(propertyValue) + &#39;)&#39;)</font></div>
            </div>
          </div>
          <div>
            <div>
              <div><font face=3D"monospace">=C2=A0 at =3D AttributeTable()<=
/font></div>
            </div>
          </div>
          <div>
            <div>
              <div><font face=3D"monospace">=C2=A0 at.insert(propertyName,
                  propertyValue)</font></div>
            </div>
          </div>
          <div>
            <div>
              <div><font face=3D"monospace">=C2=A0 at.show(&#39;savePropert=
yValue()
                  AttributeTable &#39;)</font></div>
            </div>
          </div>
        </blockquote>
        <div dir=3D"ltr">
          <div>produces=C2=A0</div>
        </div>
        <blockquote style=3D"margin:0px 0px 0px 40px;border:none;padding:0p=
x">
          <div dir=3D"ltr">
            <div><font face=3D"monospace">savePropertyValue(atst.ics.visp,
                atst.ics.visp.slitStepSz, 0.05)<br>
                savePropertyValue() AttributeTable
                atst.ics.visp.slitStepSz: true</font><br>
            </div>
          </div>
        </blockquote>
        <div dir=3D"ltr">
          <div><br>
          </div>
          <div>So the value 0.05 was passed into the jython
            savePropertyValue function but what ended up being inserted
            into the Java AttributeTable object was the boolean true.=C2=A0=
=C2=A0</div>
          <div><br>
          </div>
          <div><b>Questions</b></div>
          <div>
            <ol>
              <li>What is the expected behavior when interacting with
                overloaded Java objects?=C2=A0 Were we just lucky before an=
d
                this was never expected to work?</li>
              <li>Is anyone aware of changes on the Java side that may
                have triggered this?=C2=A0 I&#39;d like some background on =
what
                might have caused this so I can investigate what might
                be improved to fix this.</li>
              <li>Is it possible to inject custom PyObject --&gt; Java
                Object coercion routines?=C2=A0 The interpreters are setup =
at
                a high level; if I could mask the Java AttributeTable
                class with a Python class that better handled overloaded
                methods and then somehow coerce/convert it to an Java
                AttributeTable before sending it back into Java land I
                might be able to work around this.</li>
            </ol>
            <div>Thanks</div>
          </div>
          <div><br>
          </div>
          <div>
            <div>
              <div dir=3D"ltr" class=3D"gmail_signature">
                <div dir=3D"ltr">--
                  <div>Cheers</div>
                  <div>-john</div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <br>
      <fieldset></fieldset>
      <br>
      <fieldset></fieldset>
      <pre>_______________________________________________
Jython-users mailing list
<a href=3D"mailto:[email protected]" target=3D"_blank">Jyt=
[email protected]</a>
<a href=3D"https://lists.sourceforge.net/lists/listinfo/jython-users" targe=
t=3D"_blank">https://lists.sourceforge.net/lists/listinfo/jython-users</a>
</pre>
    </blockquote>
    <pre cols=3D"72">--=20

Jeff Allen
</pre>
  </div>

_______________________________________________<br>
Jython-users mailing list<br>
<a href=3D"mailto:[email protected]" target=3D"_blank">Jyt=
[email protected]</a><br>
<a href=3D"https://lists.sourceforge.net/lists/listinfo/jython-users" rel=
=3D"noreferrer" target=3D"_blank">https://lists.sourceforge.net/lists/listi=
nfo/jython-users</a><br>
</blockquote></div>
</blockquote></div>

--000000000000939507061a8df468--


--===============8807504134147793850==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline


--===============8807504134147793850==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
Jython-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jython-users

--===============8807504134147793850==--