From: Ype K. <yk...@xs...> - 2001-09-21 18:46:48
|
John, >I'd like to allow users of my app to be able to enter Jython code to >control my app. But I only want to support a subset of the language. > >Ideally I'd like to look at the description of the Jython language in >the form of a hierarchy and pick and choose what to activate and what >to suppress. For example, I'd like support for assignment statements >but not the creation of classes. > >Does this sound like a reasonable goal? Is anything like this possible >w/o a lot of work? You can use execfile() or exec in you program, and perform some simple check on the actual program contents before allowing this. Eg. to define a class in python the word 'class' must be at the beginning of a line (after evt. whitespace). You can easily search for that using regular expressions, and give the user some error message when this happens. Same for 'import'. Beware of the standard functions: for example anything goes when you allow __import__() or exec or execfile() in user code. When the subset is easy enough you might define a simple parser for it and use it as a check before executing the code by the full interpreter. However, I don't like the idea of giving users anything less than the full python language. Most of them can decide how much of the language they would want to use. Using your own parser as a filter at least allows an 'upgrade' to the full language... Have fun, Ype |