开发者

jython2.2.1 AttributeError: 'javainstance' object has no attribute '__call__'

开发者 https://www.devze.com 2023-04-04 11:36 出处:网络
I\'m having trouble trying to run jython code embedded in a compiled groovy application. The same jython code works fine when it is embedded in a java application (The Grinder 3.1)

I'm having trouble trying to run jython code embedded in a compiled groovy application. The same jython code works fine when it is embedded in a java application (The Grinder 3.1)

In the groovy code I use the org.python.util.PythonInterpreter class (from jython 2.2.1) to create a callable instance of a class called TestRunner (this is a requirement from The Grinder).

Illustrative jython code example:

class TestRunner:
    def __init__(self):
        doinitstuff()
    def __call__():
        a = A()
        a.work()

class A:
    def __init__(self):
        self.b = B()

    def work(self):
        print "Calling methodcall"
        self.b.methodcall()

class B:
    def __init__(self):
        self.webservice = WebServiceStubImplementedInJava()
        print str(se开发者_如何转开发lf.webservice)

    def methodcall(self):
        print "In methodcall"
        try:
            return self.webservice.soapmethod()
        except:
            log_error()
            raise

Here is the output when I run the above code:

  1. The TestRunners __call__() method will invoke the work() method of a class A instance, and the webservice stub's toString output is printed.
  2. The "Calling methodcall" message is printed.
  3. The "In methodcall" message is never printed, and instead I get: AttributeError: 'javainstance' object has no attribute '__call__'. The stacktrace ends with self.b.methodcall()

Do you have any idea why the invocation of self.b.methodcall() should result in AttributeError: 'javainstance' object has no attribute __call__

Adding some context to the problem...

  1. I'm trying to use a Groovy class to execute the work that a Grinder worker thread would perform when we performance test our product.
    • I use groovy just for the sake of "less verbose code", but might have to switch over to plain old java if it's groovy that causes the problem.
  2. The reason for doing this is that I need to find out which files are actually used by Grinder for a given test scenario.
    • We have hundreds of *.py files and configuration files etc, but only a subset of them are used for one specific test scenario. All of them are used in some test scenario.
    • This makes it quite hard for a "beginner" to understand how to configure a test so I'm trying to build a "test configurator wizard" that set up a test scenario without forcing the user/tester to edit all config files manually.
    • This wizard will collect the relevant files from a "repository" and put them in a folder where the "Grinder Console" can present them to the user.

So, the way I use to find out which files are used by Grinder is to use AOP (AspectJ) to capture all calls to java.io.FileInputStream(java.io.File) from any code in the org.python.util and org.python.core packages. The "advice" I apply to these join-points is to print the file name to System.out. I use load-time weaving for this, so I can run the groovy/java/jython code with our without AOP enabled. The AttributeErrorproblem occurs regardless if I have AOP enabled or not.

I have a vague suspicion that the AttributeError problem could be caused by some classloader mismatch when a "groovy" class executes the PythonInterpreter methods, but I'm far from sure about this. I'm not sure if groovy is doing any kind of runtime bytecode editing when it loads classes and if that confuses the PythonInterpreter.

The groovy code itself is precompiled so I use the regular java.exe to launch the process.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号