I think both use cases are valid - any code using the factory could have its own instance of PySystemState, configured as desired, or a PySystemState could potentially be shared for simplicity. However, we should not rely on lookup from the current thread.

On Tue, Jul 21, 2009 at 2:18 PM, Charlie Groves <charlie.groves@gmail.com> wrote:
On Tue, Jul 21, 2009 at 9:41 AM, Jim Baker<jbaker@zyasoft.com> wrote:
> The object factory presented here
> http://kenai.com/projects/plyjy/sources/mercurial/content/src/org/plyjy/factory/PySystemObjectFactory.java?rev=2
> is quite usable. However, Tobias and I discussed this further, and we do
> need to refine it further (use a static factory method instead of a public
> constructor, genericize, change its over lengthy name, cache a private
> instance of PySystemState).

Neat!

Making it generic is pretty much already done by Py.tojava:

public class PyObjectInstantiator {
   public static <T> T create(Class<T> javaType, PyObject
pythonClass, Object... args) {
       PyObject[] pyArgs = new PyObject[args.length];
       for (int i = 0; i < pyArgs.length; i++) {
           pyArgs[i] = Py.java2py(args[i]);
       }
       return Py.tojava(pythonClass.__call__(pyArgs), javaType);
   }

   public static void main(String[] args) {
       PythonInterpreter interp = new PythonInterpreter();
       interp.exec("from java.lang import Runnable");
       interp.exec("class Runner(Runnable):\n" +
                   "    def run(self):\n" +
                   "        print 'Ran!'");
       Runnable runner = create(Runnable.class, interp.get("Runner"));
       runner.run();
   }
}

Also, I don't understand why this needs a private instance of
PySystemState.  Shouldn't that the current thread has its own
PySystemState allow callers to isolate their states however they like?

Charlie



--
Jim Baker
jbaker@zyasoft.com