From: dman <ds...@ri...> - 2001-11-26 03:16:01
|
On Sun, Nov 25, 2001 at 06:21:24PM -0800, Edward Povazan wrote: | Hi, | | I had been writing a simple script language, and in looking for a run time | environment (as opposed to writing my own interpreter) I discovered my | expressions were exactly the same as Python expressions (with a little | overloading). | So I decided to use Jython instead ... why reinvent the wheel? | I will still parse the a python subset to make sure the user (typically a | non programmer) doesn't do something silly. | But I want a version for the more adventurous users. | | How do I restrict Jython use of 'import xxx' and 'from xxx import yyy' This one is easy : simply hook in your own function as __import__. The 'import' statement is simply syntactic sugar : import foo foo = __import__( "foo" ) from foo import bar bar = __import__( "foo" ).bar import foo as bar bar = __import__( "foo" ) You can (in pure python code) do something like the following : import __builtin__ # this is the namespace where built-in stuff is real_import = __builtin__.__import__ # save the real function for later def my_import( name , globals , locals , fromlist ) : print "Yo, the user wants to import '%s'" % name return real_import( name , globals , locals , fromlist ) __builtin__.__import__ = my_import # hook in my function instead In your function you can determine whether or not to allow the import or raise ImportError. HTH, -D -- Religion that God our Father accepts as pure and faultless is this: to look after orphans and widows in their distress and to keep oneself from being polluted by the world. |