ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> Jython-dev
Jython-dev
Re: [Jython-dev] imports with parser type eval
by Samuele Pedroni other posts by this author
Oct 21 2004 10:36PM messages near this date
Re: [Jython-dev] imports with parser type eval | Re: [Jython-dev] imports with parser type eval
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 <jim@[...].com> 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>" );
> >>>>>      
> >>>>>
> >>>
> >>>
> >>>  
> >>>
> >>
> >
>  



-------------------------------------------------------
This SF.net email is sponsored by: IT Product Guide on ITManagersJournal
Use IT products in your business? Tell us what you think of them. Give us
Your Opinions, Get Free ThinkGeek Gift Certificates! Click to find out more
http://productguide.itmanagersjournal.com/guidepromo.tmpl
_______________________________________________
Jython-dev mailing list
Jython-dev@[...].net
https://lists.sourceforge.net/lists/listinfo/jython-dev

Privacy Policy | Email Opt-out | Feedback | Syndication
© 2004 ActiveState, a division of Sophos All rights reserved