From: Mark P. <li...@ma...> - 2004-10-21 22:38:47
|
Yes if I could pass the imports via the dict that would be great then I can continue to use Py.runCode - which I assume is cheaper than having lots of PythonInterpretors - anyone care to explain the inner workings here? I know how to bind a single class type via the dict but not how to do it for statements like "from module import *" Mark Samuele Pedroni wrote: > Mark Proctor wrote: > >> Just found out why I can't use PythonInterp shared amongst RuleSet. >> While the imports are shared and some of the functions each >> condition/consequence has its own variables passed as locals and no >> mangling or sharing should occur; hence why we were using >> Py.runCode(PyCode code, PyObject locals, PyObject globals) - All >> variables are passed via a dict andthe locals parameter. > > > the most natural way is likely to pass the dict with shared imports > and functions as globals. > > > > >> Mark >> Mark Proctor wrote: >> >>> Just tried that, needed to be wrapped in a function otherwise it >>> errors. Gonna get the shared interp thing working. >>> >>> Mark >>> Mark Proctor wrote: >>> >>>> just chatting to one of the other developers, think we might be >>>> able to have a PythonInterpretor for each RuleSet, which should >>>> solve this issue. Otherwise whats potentially wrong with doing an >>>> exec with the following to emulate an expression: >>>> String tempText = stripOuterIndention( text ); >>>> if (type.equals("eval")) >>>> { >>>> tempText = "return (" + tempText + ")"; >>>> } >>>> this.text = newText.append(tempText).toString(); >>>> >>>> >>>> Mark >>>> Oti wrote: >>>> >>>>> IMHO you cannot eval an import statement: >>>>> Jython 2.1 on java1.4.2_05 (JIT: null) >>>>> Type "copyright", "credits" or "license" for more information. >>>>> >>> exec("from java.lang import String") >>>>> >>> eval("from java.lang import String") >>>>> Traceback (innermost last): >>>>> File "<console>", line 1, in ? >>>>> File "<string>", line 1 from java.lang import String >>>>> ^ >>>>> SyntaxError: invalid syntax >>>>> >>> >>>>> so you have to exec the imports first, e.g. like Jim suggested. >>>>> >>>>> Best wishes, >>>>> Oti. >>>>> >>>>> --- Jim Adrig <ji...@tr...> wrote: >>>>> >>>>> >>>>> >>>>>> If you are running these from Java, maybe something like what we >>>>>> do will work? >>>>>> >>>>>> First we 'add_package' since we have the cachedir stuff turned off: >>>>>> >>>>>> // for Swing GUI building: >>>>>> PySystemState.add_package("javax"); >>>>>> PySystemState.add_package("javax.swing"); >>>>>> PySystemState.add_package("javax.swing.text"); >>>>>> PySystemState.add_package("java.awt"); // Color, etc. >>>>>> PySystemState.add_package("java.awt.event"); >>>>>> >>>>>> Then create an interpreter and 'execfile' all our standard >>>>>> imports/functions >>>>>> >>>>>> python.execfile( functionFileStream, functionFileName ); // >>>>>> pass >>>>>> the name for parsing errors >>>>>> >>>>>> The 'functionFileStream' contains lots of things like: >>>>>> >>>>>> from javax.swing import JCheckBox, JComboBox, JFrame, JMenuBar, >>>>>> JMenu, JMenuItem, JTable, JButton, JOptionPane, JRadioButton, >>>>>> JTextField >>>>>> from java.awt import Color, GridLayout, GridBagLayout >>>>>> >>>>>> >>>>>> On Oct 21, 2004, at 12:45 PM, Mark Proctor wrote: >>>>>> >>>>>> >>>>>> >>>>>>> I'm compiling expressions and blocks for use in our application, to >>>>>>> >>>>>>> this we pre-append import statements declared elsewhere. It works >>>>>>> >>>>>> >>>>>> fine >>>>>> >>>>>>> when type is "exec" for blocks but fails when its "eval" for >>>>>>> expressions. Any ideas of how to get this to work? >>>>>>> >>>>>>> Mark >>>>>>> >>>>>>> protected Interp(String text, Imports imports, String type) >>>>>>> { >>>>>>> this.origininalText = text; >>>>>>> StringBuffer newText = new StringBuffer(); >>>>>>> >>>>>>> if ((imports != null)&&(imports.getImportEntries() != null)) >>>>>>> { >>>>>>> Iterator it =imports.getImportEntries().iterator(); >>>>>>> while (it.hasNext()) >>>>>>> { ImportEntry importEntry = >>>>>>> >>>>>>> (ImportEntry) it.next(); >>>>>>> if (importEntry instanceof PythonImportEntry) >>>>>>> { >>>>>>> newText.append(importEntry.getImportEntry()); >>>>>>> newText.append(";"); >>>>>>> newText.append(newline); >>>>>>> } >>>>>>> } } >>>>>>> this.text = >>>>>>> newText.append(stripOuterIndention( text )).toString(); >>>>>>> >>>>>>> this.node = ( modType ) parser.parse( this.text, type ); >>>>>>> this.code = Py.compile( this.node, "<jython>" ); >>>>>>> >>>>>> >>>>> >>>>> >>>>> >>>>> >>>> >>> >> > > |