Re: [Jython-users] Roundtrip Jython -> Java -> Jython

For anyone wondering or trying to do something similar, I did get this
to work, mostly thanks to Jeff Emanuel's advice. The key points to
make it work are:
- in Java, explicitly use the system class loader when loading the
class that has been modified from Jython
- in Jython, reset sys.classLoader when the callback is made, to use
the classloader from Eclipse.
/Geoff
On Mon, Nov 8, 2010 at 12:12 PM, Geoff Bache <geoff.bache@...> wrote:
> Hi again,
>
> It occurred to me that I could try to set a static member in my Java
> code that I could then call from Java. But unfortunately that didn't
> work either, because Eclipse uses its own class loaders so ends up
> using a completely different version of my Java class than the one I
> modify from Jython, with different static members.
>
> I guess the whole point of the classloader mechanism is to prevent
> people messing around with anything from the outside, so I guess this
> approach isn't going to work. (if only subclassing java.lang.String
> worked...)
>
> Anyway, to be clear, the call chain is currently
>
> 1. Set up Python framework code
> 2. Call Ecliipse "Equinox" launcher code, giving it a string
> identifying my Java code's class name
> 3. Eclipse then sets up its plugin test stuff, starts the application,
> and calls my Java code when it's ready to have tests run on it.
> 4. At this point (from my Java code) I'd like to call back into the
> original Python interpreter, with the framework
> already set up.
>
> Many thanks for any help.
> /Geoff
>
> On Fri, Nov 5, 2010 at 8:17 PM, Geoff Bache <geoff.bache@...> wrote:
>> Hi Jeff,
>>
>> The problem is that I have no control over the Java code. The Java
>> code is basically a standard Eclipse launcher, plus the Eclipse plugin
>> test framework. There's no way to pass it an arbitrary object
>> unfortunately.
>>
>> Regards,
>> Geoff
>>
>> On Fri, Nov 5, 2010 at 6:24 PM, Jeff Emanuel <jemanuel@...> wrote:
>>>
>>> It seems the simplest approach is for your code that calls
>>> Java to pass an object that the Java code can the callback
>>> on. It could be a java Runnable or a PyObject. That object
>>> would run your tests in Python.
>>>
>>> In Python:
>>>
>>>
>>> # Setup all your state.
>>>
>>> def runTests():
>>> # code to run your tests here
>>>
>>> import yourJavaCode
>>> yourJavaCode.startRCPAndRunTests(runTests)
>>>
>>>
>>>
>>> In java:
>>>
>>> public static void startRCPAndRunTests(Object runTests) {
>>> startRCP();
>>> ((PyObject)runTests).__call__();
>>> }
>>>
>>>
>>>
>>> Geoff Bache wrote:
>>>>
>>>> 2010/11/5 Alex Grönholm <alex.gronholm@...>:
>>>>
>>>>>
>>>>> 05.11.2010 16:06, Geoff Bache kirjoitti:
>>>>>
>>>>>>
>>>>>> Hi all,
>>>>>>
>>>>>> I have the situation where I would like to call a Jython script from
>>>>>> the command line, which will then call some Java code, which will
>>>>>> subsequently call back into Jython again. This is easy to do, but the
>>>>>> embedded Jython interpreter is entirely separate and cannot access
>>>>>> anything set up by the original one.
>>>>>>
>>>>>> Is there any way to set it up so that the embedded interpreter has
>>>>>> access to loaded modules and variables present in the command line
>>>>>> script?
>>>>>>
>>>>>
>>>>> Why do you have two interpreters set up? What do you need the second one
>>>>> for?
>>>>>
>>>>
>>>> I am trying to adapt my Python GUI test tool (PyUseCase) to work on
>>>> Java GUIs, specifically Eclipse RCP. PyUseCase already has a defined
>>>> command line and a load of standard setup which it would be useful to
>>>> make use of. The idea is to call that, launch the Eclipse RCP
>>>> application from it (which has to be done in Java) and then call back
>>>> into the Python code in order to actually carry out the tests (where I
>>>> will hopefully make use of plenty of utlities defined in Python along
>>>> the way).
>>>>
>>>> So basically: I need to do this because I don't want to rewrite any
>>>> existing Python code in Java, or any existing Java code in Python...
>>>>
>>>> /Geoff
>>>>
>>>>
>>>> ------------------------------------------------------------------------------
>>>> The Next 800 Companies to Lead America's Growth: New Video Whitepaper
>>>> David G. Thomson, author of the best-selling book "Blueprint to a Billion"
>>>> shares his insights and actions to help propel your business during the next
>>>> growth cycle. Listen Now!
>>>> http://p.sf.net/sfu/SAP-dev2dev
>>>> _______________________________________________
>>>> Jython-users mailing list
>>>> Jython-users@...
>>>> https://lists.sourceforge.net/lists/listinfo/jython-users
>>>>
>>>
>>
>

Thread view

Hi all,
I have the situation where I would like to call a Jython script from
the command line, which will then call some Java code, which will
subsequently call back into Jython again. This is easy to do, but the
embedded Jython interpreter is entirely separate and cannot access
anything set up by the original one.
Is there any way to set it up so that the embedded interpreter has
access to loaded modules and variables present in the command line
script?
Regards,
Geoff Bache

05.11.2010 16:06, Geoff Bache kirjoitti:
> Hi all,
>
> I have the situation where I would like to call a Jython script from
> the command line, which will then call some Java code, which will
> subsequently call back into Jython again. This is easy to do, but the
> embedded Jython interpreter is entirely separate and cannot access
> anything set up by the original one.
>
> Is there any way to set it up so that the embedded interpreter has
> access to loaded modules and variables present in the command line
> script?
Why do you have two interpreters set up? What do you need the second one
for?
> Regards,
> Geoff Bache
>
> ------------------------------------------------------------------------------
> The Next 800 Companies to Lead America's Growth: New Video Whitepaper
> David G. Thomson, author of the best-selling book "Blueprint to a
> Billion" shares his insights and actions to help propel your
> business during the next growth cycle. Listen Now!
> http://p.sf.net/sfu/SAP-dev2dev
> _______________________________________________
> Jython-users mailing list
> Jython-users@...
> https://lists.sourceforge.net/lists/listinfo/jython-users

2010/11/5 Alex Grönholm <alex.gronholm@...>:
> 05.11.2010 16:06, Geoff Bache kirjoitti:
>> Hi all,
>>
>> I have the situation where I would like to call a Jython script from
>> the command line, which will then call some Java code, which will
>> subsequently call back into Jython again. This is easy to do, but the
>> embedded Jython interpreter is entirely separate and cannot access
>> anything set up by the original one.
>>
>> Is there any way to set it up so that the embedded interpreter has
>> access to loaded modules and variables present in the command line
>> script?
> Why do you have two interpreters set up? What do you need the second one
> for?
I am trying to adapt my Python GUI test tool (PyUseCase) to work on
Java GUIs, specifically Eclipse RCP. PyUseCase already has a defined
command line and a load of standard setup which it would be useful to
make use of. The idea is to call that, launch the Eclipse RCP
application from it (which has to be done in Java) and then call back
into the Python code in order to actually carry out the tests (where I
will hopefully make use of plenty of utlities defined in Python along
the way).
So basically: I need to do this because I don't want to rewrite any
existing Python code in Java, or any existing Java code in Python...
/Geoff

It seems the simplest approach is for your code that calls
Java to pass an object that the Java code can the callback
on. It could be a java Runnable or a PyObject. That object
would run your tests in Python.
In Python:
# Setup all your state.
def runTests():
# code to run your tests here
import yourJavaCode
yourJavaCode.startRCPAndRunTests(runTests)
In java:
public static void startRCPAndRunTests(Object runTests) {
startRCP();
((PyObject)runTests).__call__();
}
Geoff Bache wrote:
> 2010/11/5 Alex Grönholm <alex.gronholm@...>:
>
>> 05.11.2010 16:06, Geoff Bache kirjoitti:
>>
>>> Hi all,
>>>
>>> I have the situation where I would like to call a Jython script from
>>> the command line, which will then call some Java code, which will
>>> subsequently call back into Jython again. This is easy to do, but the
>>> embedded Jython interpreter is entirely separate and cannot access
>>> anything set up by the original one.
>>>
>>> Is there any way to set it up so that the embedded interpreter has
>>> access to loaded modules and variables present in the command line
>>> script?
>>>
>> Why do you have two interpreters set up? What do you need the second one
>> for?
>>
>
> I am trying to adapt my Python GUI test tool (PyUseCase) to work on
> Java GUIs, specifically Eclipse RCP. PyUseCase already has a defined
> command line and a load of standard setup which it would be useful to
> make use of. The idea is to call that, launch the Eclipse RCP
> application from it (which has to be done in Java) and then call back
> into the Python code in order to actually carry out the tests (where I
> will hopefully make use of plenty of utlities defined in Python along
> the way).
>
> So basically: I need to do this because I don't want to rewrite any
> existing Python code in Java, or any existing Java code in Python...
>
> /Geoff
>
> ------------------------------------------------------------------------------
> The Next 800 Companies to Lead America's Growth: New Video Whitepaper
> David G. Thomson, author of the best-selling book "Blueprint to a
> Billion" shares his insights and actions to help propel your
> business during the next growth cycle. Listen Now!
> http://p.sf.net/sfu/SAP-dev2dev
> _______________________________________________
> Jython-users mailing list
> Jython-users@...
> https://lists.sourceforge.net/lists/listinfo/jython-users
>

Hi Jeff,
The problem is that I have no control over the Java code. The Java
code is basically a standard Eclipse launcher, plus the Eclipse plugin
test framework. There's no way to pass it an arbitrary object
unfortunately.
Regards,
Geoff
On Fri, Nov 5, 2010 at 6:24 PM, Jeff Emanuel <jemanuel@...> wrote:
>
> It seems the simplest approach is for your code that calls
> Java to pass an object that the Java code can the callback
> on. It could be a java Runnable or a PyObject. That object
> would run your tests in Python.
>
> In Python:
>
>
> # Setup all your state.
>
> def runTests():
> # code to run your tests here
>
> import yourJavaCode
> yourJavaCode.startRCPAndRunTests(runTests)
>
>
>
> In java:
>
> public static void startRCPAndRunTests(Object runTests) {
> startRCP();
> ((PyObject)runTests).__call__();
> }
>
>
>
> Geoff Bache wrote:
>>
>> 2010/11/5 Alex Grönholm <alex.gronholm@...>:
>>
>>>
>>> 05.11.2010 16:06, Geoff Bache kirjoitti:
>>>
>>>>
>>>> Hi all,
>>>>
>>>> I have the situation where I would like to call a Jython script from
>>>> the command line, which will then call some Java code, which will
>>>> subsequently call back into Jython again. This is easy to do, but the
>>>> embedded Jython interpreter is entirely separate and cannot access
>>>> anything set up by the original one.
>>>>
>>>> Is there any way to set it up so that the embedded interpreter has
>>>> access to loaded modules and variables present in the command line
>>>> script?
>>>>
>>>
>>> Why do you have two interpreters set up? What do you need the second one
>>> for?
>>>
>>
>> I am trying to adapt my Python GUI test tool (PyUseCase) to work on
>> Java GUIs, specifically Eclipse RCP. PyUseCase already has a defined
>> command line and a load of standard setup which it would be useful to
>> make use of. The idea is to call that, launch the Eclipse RCP
>> application from it (which has to be done in Java) and then call back
>> into the Python code in order to actually carry out the tests (where I
>> will hopefully make use of plenty of utlities defined in Python along
>> the way).
>>
>> So basically: I need to do this because I don't want to rewrite any
>> existing Python code in Java, or any existing Java code in Python...
>>
>> /Geoff
>>
>>
>> ------------------------------------------------------------------------------
>> The Next 800 Companies to Lead America's Growth: New Video Whitepaper
>> David G. Thomson, author of the best-selling book "Blueprint to a Billion"
>> shares his insights and actions to help propel your business during the next
>> growth cycle. Listen Now!
>> http://p.sf.net/sfu/SAP-dev2dev
>> _______________________________________________
>> Jython-users mailing list
>> Jython-users@...
>> https://lists.sourceforge.net/lists/listinfo/jython-users
>>
>

Hi again,
It occurred to me that I could try to set a static member in my Java
code that I could then call from Java. But unfortunately that didn't
work either, because Eclipse uses its own class loaders so ends up
using a completely different version of my Java class than the one I
modify from Jython, with different static members.
I guess the whole point of the classloader mechanism is to prevent
people messing around with anything from the outside, so I guess this
approach isn't going to work. (if only subclassing java.lang.String
worked...)
Anyway, to be clear, the call chain is currently
1. Set up Python framework code
2. Call Ecliipse "Equinox" launcher code, giving it a string
identifying my Java code's class name
3. Eclipse then sets up its plugin test stuff, starts the application,
and calls my Java code when it's ready to have tests run on it.
4. At this point (from my Java code) I'd like to call back into the
original Python interpreter, with the framework
already set up.
Many thanks for any help.
/Geoff
On Fri, Nov 5, 2010 at 8:17 PM, Geoff Bache <geoff.bache@...> wrote:
> Hi Jeff,
>
> The problem is that I have no control over the Java code. The Java
> code is basically a standard Eclipse launcher, plus the Eclipse plugin
> test framework. There's no way to pass it an arbitrary object
> unfortunately.
>
> Regards,
> Geoff
>
> On Fri, Nov 5, 2010 at 6:24 PM, Jeff Emanuel <jemanuel@...> wrote:
>>
>> It seems the simplest approach is for your code that calls
>> Java to pass an object that the Java code can the callback
>> on. It could be a java Runnable or a PyObject. That object
>> would run your tests in Python.
>>
>> In Python:
>>
>>
>> # Setup all your state.
>>
>> def runTests():
>> # code to run your tests here
>>
>> import yourJavaCode
>> yourJavaCode.startRCPAndRunTests(runTests)
>>
>>
>>
>> In java:
>>
>> public static void startRCPAndRunTests(Object runTests) {
>> startRCP();
>> ((PyObject)runTests).__call__();
>> }
>>
>>
>>
>> Geoff Bache wrote:
>>>
>>> 2010/11/5 Alex Grönholm <alex.gronholm@...>:
>>>
>>>>
>>>> 05.11.2010 16:06, Geoff Bache kirjoitti:
>>>>
>>>>>
>>>>> Hi all,
>>>>>
>>>>> I have the situation where I would like to call a Jython script from
>>>>> the command line, which will then call some Java code, which will
>>>>> subsequently call back into Jython again. This is easy to do, but the
>>>>> embedded Jython interpreter is entirely separate and cannot access
>>>>> anything set up by the original one.
>>>>>
>>>>> Is there any way to set it up so that the embedded interpreter has
>>>>> access to loaded modules and variables present in the command line
>>>>> script?
>>>>>
>>>>
>>>> Why do you have two interpreters set up? What do you need the second one
>>>> for?
>>>>
>>>
>>> I am trying to adapt my Python GUI test tool (PyUseCase) to work on
>>> Java GUIs, specifically Eclipse RCP. PyUseCase already has a defined
>>> command line and a load of standard setup which it would be useful to
>>> make use of. The idea is to call that, launch the Eclipse RCP
>>> application from it (which has to be done in Java) and then call back
>>> into the Python code in order to actually carry out the tests (where I
>>> will hopefully make use of plenty of utlities defined in Python along
>>> the way).
>>>
>>> So basically: I need to do this because I don't want to rewrite any
>>> existing Python code in Java, or any existing Java code in Python...
>>>
>>> /Geoff
>>>
>>>
>>> ------------------------------------------------------------------------------
>>> The Next 800 Companies to Lead America's Growth: New Video Whitepaper
>>> David G. Thomson, author of the best-selling book "Blueprint to a Billion"
>>> shares his insights and actions to help propel your business during the next
>>> growth cycle. Listen Now!
>>> http://p.sf.net/sfu/SAP-dev2dev
>>> _______________________________________________
>>> Jython-users mailing list
>>> Jython-users@...
>>> https://lists.sourceforge.net/lists/listinfo/jython-users
>>>
>>
>

For anyone wondering or trying to do something similar, I did get this
to work, mostly thanks to Jeff Emanuel's advice. The key points to
make it work are:
- in Java, explicitly use the system class loader when loading the
class that has been modified from Jython
- in Jython, reset sys.classLoader when the callback is made, to use
the classloader from Eclipse.
/Geoff
On Mon, Nov 8, 2010 at 12:12 PM, Geoff Bache <geoff.bache@...> wrote:
> Hi again,
>
> It occurred to me that I could try to set a static member in my Java
> code that I could then call from Java. But unfortunately that didn't
> work either, because Eclipse uses its own class loaders so ends up
> using a completely different version of my Java class than the one I
> modify from Jython, with different static members.
>
> I guess the whole point of the classloader mechanism is to prevent
> people messing around with anything from the outside, so I guess this
> approach isn't going to work. (if only subclassing java.lang.String
> worked...)
>
> Anyway, to be clear, the call chain is currently
>
> 1. Set up Python framework code
> 2. Call Ecliipse "Equinox" launcher code, giving it a string
> identifying my Java code's class name
> 3. Eclipse then sets up its plugin test stuff, starts the application,
> and calls my Java code when it's ready to have tests run on it.
> 4. At this point (from my Java code) I'd like to call back into the
> original Python interpreter, with the framework
> already set up.
>
> Many thanks for any help.
> /Geoff
>
> On Fri, Nov 5, 2010 at 8:17 PM, Geoff Bache <geoff.bache@...> wrote:
>> Hi Jeff,
>>
>> The problem is that I have no control over the Java code. The Java
>> code is basically a standard Eclipse launcher, plus the Eclipse plugin
>> test framework. There's no way to pass it an arbitrary object
>> unfortunately.
>>
>> Regards,
>> Geoff
>>
>> On Fri, Nov 5, 2010 at 6:24 PM, Jeff Emanuel <jemanuel@...> wrote:
>>>
>>> It seems the simplest approach is for your code that calls
>>> Java to pass an object that the Java code can the callback
>>> on. It could be a java Runnable or a PyObject. That object
>>> would run your tests in Python.
>>>
>>> In Python:
>>>
>>>
>>> # Setup all your state.
>>>
>>> def runTests():
>>> # code to run your tests here
>>>
>>> import yourJavaCode
>>> yourJavaCode.startRCPAndRunTests(runTests)
>>>
>>>
>>>
>>> In java:
>>>
>>> public static void startRCPAndRunTests(Object runTests) {
>>> startRCP();
>>> ((PyObject)runTests).__call__();
>>> }
>>>
>>>
>>>
>>> Geoff Bache wrote:
>>>>
>>>> 2010/11/5 Alex Grönholm <alex.gronholm@...>:
>>>>
>>>>>
>>>>> 05.11.2010 16:06, Geoff Bache kirjoitti:
>>>>>
>>>>>>
>>>>>> Hi all,
>>>>>>
>>>>>> I have the situation where I would like to call a Jython script from
>>>>>> the command line, which will then call some Java code, which will
>>>>>> subsequently call back into Jython again. This is easy to do, but the
>>>>>> embedded Jython interpreter is entirely separate and cannot access
>>>>>> anything set up by the original one.
>>>>>>
>>>>>> Is there any way to set it up so that the embedded interpreter has
>>>>>> access to loaded modules and variables present in the command line
>>>>>> script?
>>>>>>
>>>>>
>>>>> Why do you have two interpreters set up? What do you need the second one
>>>>> for?
>>>>>
>>>>
>>>> I am trying to adapt my Python GUI test tool (PyUseCase) to work on
>>>> Java GUIs, specifically Eclipse RCP. PyUseCase already has a defined
>>>> command line and a load of standard setup which it would be useful to
>>>> make use of. The idea is to call that, launch the Eclipse RCP
>>>> application from it (which has to be done in Java) and then call back
>>>> into the Python code in order to actually carry out the tests (where I
>>>> will hopefully make use of plenty of utlities defined in Python along
>>>> the way).
>>>>
>>>> So basically: I need to do this because I don't want to rewrite any
>>>> existing Python code in Java, or any existing Java code in Python...
>>>>
>>>> /Geoff
>>>>
>>>>
>>>> ------------------------------------------------------------------------------
>>>> The Next 800 Companies to Lead America's Growth: New Video Whitepaper
>>>> David G. Thomson, author of the best-selling book "Blueprint to a Billion"
>>>> shares his insights and actions to help propel your business during the next
>>>> growth cycle. Listen Now!
>>>> http://p.sf.net/sfu/SAP-dev2dev
>>>> _______________________________________________
>>>> Jython-users mailing list
>>>> Jython-users@...
>>>> https://lists.sourceforge.net/lists/listinfo/jython-users
>>>>
>>>
>>
>