From: Balaji S. <bal...@gm...> - 2007-09-18 21:50:56
|
Thanks Jeff. But what is imp? I tried interp.addModule and that doesnt compile.Thanks Balaji On 9/18/07, Jeff Emanuel <jem...@fr...> wrote: > > Add this after creating your interpreter: > > PyModule mod = imp.addModule("__main__"); > interp.setLocals(mod.__dict__); > > > Balaji Srinivasan wrote: > > Hi EveryoneI found a difference between the way jython works when it is > > called from the command line and when it is called from a java program. > Ive > > tried my best to explain it and get a workaround but all attempts have > > failed. I will appreciate any help. > > > > > > What I want to do is given a script string I want to find all the > functions > > defined in the script. > > Lets say the script fragment is as follows: > > def foobar(): > > print("foobar") > > > > > > If I try this via the jython program, I get the following: > > > > > > bash$ jython > > Jython 2.2 on java1.5.0_11 > > Type "copyright", "credits" or "license" for more information. > >>>> def foobar(): > > ... print("foobar") > > ... > >>>> this_module = __import__(__name__) > > *>>> print __name__* > > *__main__* > >>>> for e in dir(): > > ... if (callable(getattr(this_module, e))): > > ... print(e + " is callable") > > ... > > *foobar is callable* > > > > > > Notice that the __name__ shows up as __main__ and that foobar shows up > as > > callable. So far so good. > > > > > > Now if I try to do the same thing via java: > > > > > > bash$ java Test2 > > Traceback (innermost last): > > File "<string>", line 4, in ? > > *ImportError: no module named main* > > *Name was main* > > * > > * > > Now why is the __name__ set to main? I even tried hardcoding the > __import__ > > to call __import__('__main__'). It couldnt find that module either. > > Any ideas on how I can get this to work? Any help would be greatly > > appreciated. > > > > > > Here is the java code: > > import org.python.util.PythonInterpreter ; > > import org.python.core.*; > > > > > > public class Test2 { > > > > > > public static void main(String[] args) throws Exception { > > PythonInterpreter interp = new PythonInterpreter(); > > interp.setErr(System.err); > > interp.setOut(System.out); > > String s = "def foobar():\n" + > > " print(\"foobar\")\n" + > > "name=__name__\n" + > > "this_module = __import__(__name__)\n" + > > "for e in dir():\n" + > > " if (callable(getattr(this_module, > e))):\n" + > > " print(e + \" is callable\")\n"; > > > > > > try { > > interp.exec(s); > > } catch (PyException e) { > > e.printStackTrace (); > > } > > System.out.println("Name was " + interp.get("name")); > > } > > } > > > > > > Thanks a lot in advance and apologies if I missed an FAQ that covers > this. I > > did try to do my research before posting this. > > Thanks > > Balaji > > > > > > > > ------------------------------------------------------------------------ > > > > ------------------------------------------------------------ > ------------- > > This SF.net email is sponsored by: Microsoft > > Defy all challenges. Microsoft(R) Visual Studio 2005. > > http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ > > > > > > ------------------------------------------------------------------------ > > > > _______________________________________________ > > Jython-users mailing list > > Jyt...@li... > > https://lists.sourceforge.net/lists/listinfo/jython-users > |