From: Jayson S B. <js...@ci...> - 2001-02-12 07:33:54
|
Here is a crude python program for example purposes: for each i in <some list> print i with that, will a straight PythonInterpreter.exec(line) by a good enough shell interpreter for doing on the fly python interpreting? Or is there more higher level stuff to be done? Jay |
From: <bc...@wo...> - 2001-02-12 09:55:51
|
[Jayson S Baird] >Here is a crude python program for example purposes: > for each i in <some list> > print i > > with that, will a straight PythonInterpreter.exec(line) by a good enough >shell interpreter for doing on the fly python interpreting? Or is there >more higher level stuff to be done? The PythonInterpreter.exec(s) takes an entire script, not individual lines. The little example script can be embedded like this: import org.python.core.*; import org.python.util.*; public class si2 { public static void main(String[] args) { PythonInterpreter interp = new PythonInterpreter(); interp.exec("for i in (1,2,3):\n print i"); } } Note that the script contains newlines and indentation *exactly* as it would, if it came from a file: for i in (1,2,3): print i regards, finn |
From: Brian Z. <bri...@ya...> - 2001-02-12 20:08:13
|
These classes are accessible from java servlet and JSP pages, but not from jython servlet. The workaround would be to include those in CLASSPATH of tomcat startup script, but... you know. Anyway, enough complains. I take a brief look at tomcat 3.2 source code at :pserver:an...@ja...:/home/cvspublic/jakarta-tomcat /src/share/org/apache/tomcat context/LoaderInterceptor.java seems to be where it adds WEB-INF/classes and WEB-INF/lib/*.jar to the classloader class repositories. Can we do similar things in PyServlet.java ? Thanks, /Brian |
From: <bc...@wo...> - 2001-02-12 21:38:12
|
On Mon, 12 Feb 2001 12:08:30 -0800, you wrote: >These classes are accessible from java servlet and JSP pages, but not >from jython servlet. The workaround would be to include those in >CLASSPATH of tomcat startup script, but... you know. I have not yet look at this in depth, but I think the classes from lib & classes are available as classes, but not as jython package namespaces. You can test this by trying to load a class from withon the .py file with java.lang.Class.forName(). I guess this should work, but the normal jython import does not work. As a temporary workaround you can add the java packages to the jython package manager yourself with add_package. sys.add_package("testpackage") from testpackage import TestClass >Anyway, enough complains. > >I take a brief look at tomcat 3.2 source code at > > :pserver:an...@ja...:/home/cvspublic/jakarta-tomcat > /src/share/org/apache/tomcat > >context/LoaderInterceptor.java seems to be where it adds WEB-INF/classes >and WEB-INF/lib/*.jar to the classloader class repositories. > >Can we do similar things in PyServlet.java ? It should be possible to force the jython package manager to search these two wellknown places for java classes and packages. Perhaps by setting python.packages.paths and python.packages.directories. Perhaps by calling the package manager directly. More on later. regards, finn |
From: Brian Z. <bri...@ya...> - 2001-02-13 06:07:43
|
Hi Finn, ----- Original Message ----- From: "Finn Bock" <bc...@wo...> To: <jyt...@li...> Sent: Monday, February 12, 2001 1:37 PM Subject: Re: [Jython-dev] WEB-INF/classes & WEB-INF/lib/*.jar inaccessible in jython servlet > On Mon, 12 Feb 2001 12:08:30 -0800, you wrote: > > >These classes are accessible from java servlet and JSP pages, but not > >from jython servlet. The workaround would be to include those in > >CLASSPATH of tomcat startup script, but... you know. > > I have not yet look at this in depth, but I think the classes from lib & > classes are available as classes, but not as jython package namespaces. > You can test this by trying to load a class from withon the .py file > with java.lang.Class.forName(). I guess this should work, but the normal > jython import does not work. > Unfortunately this (try load a <context>/WEB-INF/classes class from jython servlet with java.lang.Class.forName()) does not work. What I'm trying to do actually is putting XMLC (see http://xmlc.enhydra.org) compiled pages inside <context>. From within a jython servlet, "java.class.path" System property value does not include <context>/WEB-INF/classes at all. So basically jython servlet has no idea where to get these classes. Two observations, if I have testpkg.TestClass under <context>/WEB-INF/classes: 1. I happen to place org.python.util.PyServlet under tomcat_home/classes, if I add try { Class.forName("testpkg.TestClass").newInstance(); } catch (Exception ex) { System.err.println(e.toString()); } to PyServlet.java, class loading fails. 2. If I add the above code to a servlet within the same <context>, it loads okay. Which is reasonable, but also suggests that servlet class loading does something special when within a <context>. PyServlet need to borrow the same logic. I didn't test JSP, but I believe it should be the same as servlet. /Brian > As a temporary workaround you can add the java packages to the jython > package manager yourself with add_package. > > sys.add_package("testpackage") > from testpackage import TestClass > > >Anyway, enough complains. > > > >I take a brief look at tomcat 3.2 source code at > > > > :pserver:an...@ja...:/home/cvspublic/jakarta-tomcat > > /src/share/org/apache/tomcat > > > >context/LoaderInterceptor.java seems to be where it adds WEB-INF/classes > >and WEB-INF/lib/*.jar to the classloader class repositories. > > > >Can we do similar things in PyServlet.java ? > > It should be possible to force the jython package manager to search > these two wellknown places for java classes and packages. Perhaps by > setting python.packages.paths and python.packages.directories. Perhaps > by calling the package manager directly. More on later. > > regards, > finn > > _______________________________________________ > Jython-dev mailing list > Jyt...@li... > http://lists.sourceforge.net/lists/listinfo/jython-dev > |
From: <bc...@wo...> - 2001-02-13 09:57:46
|
[finn] > I have not yet look at this in depth, but I think the classes from lib & > classes are available as classes, but not as jython package namespaces. > You can test this by trying to load a class from withon the .py file > with java.lang.Class.forName(). I guess this should work, but the normal > jython import does not work. [Brian Zhou] >Unfortunately this (try load a <context>/WEB-INF/classes class from jython >servlet with java.lang.Class.forName()) does not work. > >What I'm trying to do actually is putting XMLC (see http://xmlc.enhydra.org) >compiled pages inside <context>. > >From within a jython servlet, "java.class.path" System property value does >not include <context>/WEB-INF/classes at all. So basically jython servlet >has no idea where to get these classes. > >Two observations, if I have testpkg.TestClass under ><context>/WEB-INF/classes: > >1. I happen to place org.python.util.PyServlet under tomcat_home/classes, if >I add >try { > Class.forName("testpkg.TestClass").newInstance(); >} >catch (Exception ex) { > System.err.println(e.toString()); >} >to PyServlet.java, class loading fails. Right, but I'm not going to add any special support for this. The PyServlet class (and the rest of jython.jar) should be placed under the <context>/WEB-INF/lib. >2. If I add the above code to a servlet within the same <context>, it loads >okay. Right, and it will load ok from within a jython script too. >Which is reasonable, but also suggests that servlet class loading does >something special when within a <context>. PyServlet need to borrow the same >logic. PyServlet will get the logic for free from the classloader that loaded jython.jar (but only when jython.jar is loaded from <context>/WEB-INF/lib). What we need to add is only the inspection of classes and jars found on ../lib and ../classes. The actual classloading magic is already handled by tomcat's servlet context logic. > I didn't test JSP, but I believe it should be the same as servlet. I have added this to PyServlet: props.setProperty("python.packages.directories", "java.ext.dirs,pyservlet.lib"); props.setProperty("pyservlet.lib", rootPath + File.separator + "WEB-INF" + File.separator + "lib"); props.setProperty("python.packages.paths", "java.class.path,sun.boot.class.path,"+ "pyservlet.classes"); props.setProperty("pyservlet.classes", rootPath + File.separator + "WEB-INF" + File.separator + "classes"); It seems to work, but I think we should have some kind of direct API for this purpose. PySystemState.add_classpath(String directoryPath) PySystemState.add_extdirs(String directoryPath) Any thoughs, Samuele? regards, finn |
From: Brian Z. <bri...@ya...> - 2001-02-14 05:57:19
|
Hi Finn, Thanks for explaining to me that I need to put jython.jar under <context>/WEB-INF/lib. I did cvs update and rebuilt jython.jar. Now it's working with the help of: import sys sys.add_package("testpkg") from testpkg import TestClass But without the above, it still complains: File "C:\devel\servletContext\test\hello.py", line 3, in ? ImportError: no module named testpkg Is this the expected behavior so far? BTW, sys.registry.list(out) shows python.packages.paths=java.class.path, sun.boot.class.path python.packages.directories=java.ext.dirs ... No pyservlet.* in sight. Did I miss anything, again? Really appreciate all your help, /Brian ----- Original Message ----- From: "Finn Bock" <bc...@wo...> To: <jyt...@li...> Sent: Tuesday, February 13, 2001 1:56 AM Subject: Re: [Jython-dev] Re: WEB-INF/classes & WEB-INF/lib/*.jar inaccessible in jython servlet > > PyServlet will get the logic for free from the classloader that loaded > jython.jar (but only when jython.jar is loaded from > <context>/WEB-INF/lib). What we need to add is only the inspection of > classes and jars found on ../lib and ../classes. The actual classloading > magic is already handled by tomcat's servlet context logic. > > > I didn't test JSP, but I believe it should be the same as servlet. > > I have added this to PyServlet: > > props.setProperty("python.packages.directories", > "java.ext.dirs,pyservlet.lib"); > props.setProperty("pyservlet.lib", > rootPath + File.separator + > "WEB-INF" + File.separator + > "lib"); > > props.setProperty("python.packages.paths", > "java.class.path,sun.boot.class.path,"+ > "pyservlet.classes"); > props.setProperty("pyservlet.classes", > rootPath + File.separator + > "WEB-INF" + File.separator + > "classes"); > > It seems to work, but I think we should have some kind of direct API for > this purpose. > > PySystemState.add_classpath(String directoryPath) > PySystemState.add_extdirs(String directoryPath) > > Any thoughs, Samuele? > > regards, > finn > > > _______________________________________________ > Jython-dev mailing list > Jyt...@li... > http://lists.sourceforge.net/lists/listinfo/jython-dev > |
From: Brian Z. <bri...@ya...> - 2001-02-14 07:08:33
|
I figured it out. To answer myself: Currently in PyServlet.java, setting "python.home" in WEB-INF/web.xml and the following code are MUTUALLY EXCLUSIVE! """ props.setProperty("python.home", rootPath + File.separator + "WEB-INF" + File.separator + "lib"); props.setProperty("python.packages.directories", "java.ext.dirs,pyservlet.lib"); props.setProperty("pyservlet.lib", rootPath + File.separator + "WEB-INF" + File.separator + "lib"); props.setProperty("python.packages.paths", "java.class.path,sun.boot.class.path,"+ "pyservlet.classes"); props.setProperty("pyservlet.classes", rootPath + File.separator + "WEB-INF" + File.separator + "classes"); """ I kind of understand the reasoning behind this: make deployment self-contained. But setting "python.home" to <context>/WEB-INF/lib really makes development inconvenient (any module except sys need to be copied into WEB-INF/lib). Will it do any harm if we set pyservlet properties independent of "python.home"? /Brian ----- Original Message ----- From: "Brian Zhou" <bri...@ya...> To: <jyt...@li...> Sent: Tuesday, February 13, 2001 9:58 PM Subject: Re: [Jython-dev] Re: WEB-INF/classes & WEB-INF/lib/*.jar inaccessible in jython servlet > Hi Finn, > > Thanks for explaining to me that I need to put jython.jar under > <context>/WEB-INF/lib. I did cvs update and rebuilt jython.jar. Now it's > working with the help of: > > import sys > sys.add_package("testpkg") > from testpkg import TestClass > > But without the above, it still complains: > > File "C:\devel\servletContext\test\hello.py", line 3, in ? > ImportError: no module named testpkg > > Is this the expected behavior so far? BTW, sys.registry.list(out) shows > > python.packages.paths=java.class.path, sun.boot.class.path > python.packages.directories=java.ext.dirs > ... > > No pyservlet.* in sight. Did I miss anything, again? > > Really appreciate all your help, > > /Brian > > ----- Original Message ----- > From: "Finn Bock" <bc...@wo...> > To: <jyt...@li...> > Sent: Tuesday, February 13, 2001 1:56 AM > Subject: Re: [Jython-dev] Re: WEB-INF/classes & WEB-INF/lib/*.jar > inaccessible in jython servlet > > > > > > PyServlet will get the logic for free from the classloader that loaded > > jython.jar (but only when jython.jar is loaded from > > <context>/WEB-INF/lib). What we need to add is only the inspection of > > classes and jars found on ../lib and ../classes. The actual classloading > > magic is already handled by tomcat's servlet context logic. > > > > > I didn't test JSP, but I believe it should be the same as servlet. > > > > I have added this to PyServlet: > > > > props.setProperty("python.packages.directories", > > "java.ext.dirs,pyservlet.lib"); > > props.setProperty("pyservlet.lib", > > rootPath + File.separator + > > "WEB-INF" + File.separator + > > "lib"); > > > > props.setProperty("python.packages.paths", > > "java.class.path,sun.boot.class.path,"+ > > "pyservlet.classes"); > > props.setProperty("pyservlet.classes", > > rootPath + File.separator + > > "WEB-INF" + File.separator + > > "classes"); > > > > It seems to work, but I think we should have some kind of direct API for > > this purpose. > > > > PySystemState.add_classpath(String directoryPath) > > PySystemState.add_extdirs(String directoryPath) > > > > Any thoughs, Samuele? > > > > regards, > > finn > > > > > > _______________________________________________ > > Jython-dev mailing list > > Jyt...@li... > > http://lists.sourceforge.net/lists/listinfo/jython-dev > > > > > _______________________________________________ > Jython-dev mailing list > Jyt...@li... > http://lists.sourceforge.net/lists/listinfo/jython-dev > |
From: <bc...@wo...> - 2001-02-14 09:17:07
|
[Brian Zhou] >I figured it out. To answer myself: > > Currently in PyServlet.java, setting "python.home" in WEB-INF/web.xml >and the following code are MUTUALLY EXCLUSIVE! >... >Will it do any harm if we set pyservlet properties independent of >"python.home"? No. It was just the result of thoughless cut&paste. Now fixed in the CVS. regards, finn |
From: Brian Z. <bri...@ya...> - 2001-02-13 01:17:16
|
If I write: def init(self): # do something pass I got: TypeError: init() too many arguments; expected 1 got 2 If I write: def init(self, config): javax.servlet.http.HttpServlet.init(self, config) # line 14, want to do super.init(config) here # do something pass I got: line 14, in init TypeError: init() not enough arguments; expected 2 got 1 Am I missing anything? /Brian |
From: Robert W. B. <rb...@di...> - 2001-02-13 02:49:49
|
Hi Brian, On Mon, 12 Feb 2001, Brian Zhou wrote: > If I write: > > def init(self): > # do something > pass > > I got: TypeError: init() too many arguments; expected 1 got 2 This is referring to the line "def init(self):". PyServlet uses 'servlet.init(getServletConfig());', so this is expected. Using the parameterless init() is impossible as is. > > If I write: > > def init(self, config): > javax.servlet.http.HttpServlet.init(self, config) # line 14, want > to do super.init(config) here > # do something > pass > > I got: > line 14, in init > TypeError: init() not enough arguments; expected 2 got 1 This error is referring to "javax.servlet.http.HttpServlet.init(self,config)" not the same "def init" line as above. I thought it might help to point out that the two errors are unrelated. If PyServlet invoked the parameterless init "servlet.init();" on the Jython class, then it seems likely that getInitParameters and getInitParameterNames still work, and the config would be available with getServletConfig. If that works, would people be ok with pre-2.1 servlet being out of luck? -Robert |
From: Brian Z. <bri...@ya...> - 2001-02-13 05:36:25
Attachments:
snoop.py
|
Hi Robert and list, ----- Original Message ----- From: "Robert W. Bill" <rb...@di...> To: "Brian Zhou" <bri...@ya...> Cc: <jyt...@li...> Sent: Monday, February 12, 2001 11:46 PM Subject: Re: [Jython-dev] jython servlet init() override problem > Hi Brian, > > On Mon, 12 Feb 2001, Brian Zhou wrote: > > If I write: > > > > def init(self): > > # do something > > pass > > > > I got: TypeError: init() too many arguments; expected 1 got 2 > > This is referring to the line "def init(self):". PyServlet uses > 'servlet.init(getServletConfig());', so this is expected. Using the > parameterless init() is impossible as is. > I see. Is that because HttpServlet.init(self) HttpServlet.init(self, config) are difficult to differentiate in jython? I guess it's difficult, but still possible to do. Unlike TestClass.m(self, aString) TestClass.m(self, aFloat) which is understandably impossible. > > > If I write: > > > > def init(self, config): > > javax.servlet.http.HttpServlet.init(self, config) # line 14, want > > to do super.init(config) here > > # do something > > pass > > > > I got: > > line 14, in init > > TypeError: init() not enough arguments; expected 2 got 1 > > This error is referring to > "javax.servlet.http.HttpServlet.init(self,config)" not the same > "def init" line as above. > > I thought it might help to point out that the two errors are > unrelated. > > If PyServlet invoked the parameterless init "servlet.init();" on the > Jython class, then it seems likely that getInitParameters and > getInitParameterNames still work, and the config would be available > with getServletConfig. If that works, would people be ok with pre-2.1 > servlet being out of luck? > > -Robert > Just tried using servlet.init() in PyServlet, it broke snoop.py and threw NullPointerException. I guess the parmeterless HttpServlet.init() is only meant to be overriden or be called by HttpServlet.init(config). The question now becomes: why "HttpServlet.init(self, config)" on line 14 got TypeError? Isn't this the way python code calls super.method()? Regards, /Brian |
From: <bc...@wo...> - 2001-02-13 09:58:40
|
[Brian Zhou] >If I write: > >def init(self): > # do something > pass > >I got: TypeError: init() too many arguments; expected 1 got 2 > >If I write: > >def init(self, config): > javax.servlet.http.HttpServlet.init(self, config) # line 14, want >to do super.init(config) here > # do something > pass > >I got: >line 14, in init >TypeError: init() not enough arguments; expected 2 got 1 > >Am I missing anything? In java, it is two different methods, but in jython the two methods is merged into one. In a normal run this method will be called twice, first with a config param and second (from within GenericServlet.init) without any parameters. So the jython method must be prepared to handle both situations: def init(self, config=None): if config: HttpServlet.init(self, config) pass Obviously jython is completely defeating the purpose of the parameter-less init() method: """ void init() A convenience method which can be overridden so that there's no need to call super.init(config). """ In jython, we are forced to deal with both init() methods *and* we have to call super.init() as well. regards, finn |
From: Brian Z. <bri...@ya...> - 2001-02-14 04:38:18
|
Thanks Finn, the init(self, config=None) works pretty well. /Brian ----- Original Message ----- From: "Finn Bock" <bc...@wo...> To: <jyt...@li...> Sent: Tuesday, February 13, 2001 1:57 AM Subject: Re: [Jython-dev] jython servlet init() override problem > So the jython method must be prepared to handle both situations: > > def init(self, config=None): > if config: > HttpServlet.init(self, config) > pass > > Obviously jython is completely defeating the purpose of the > parameter-less init() method: > > """ > void init() > A convenience method which can be overridden so that there's no need > to call super.init(config). > """ > > In jython, we are forced to deal with both init() methods *and* we have > to call super.init() as well. > > regards, > finn > > _______________________________________________ > Jython-dev mailing list > Jyt...@li... > http://lists.sourceforge.net/lists/listinfo/jython-dev > |