next up previous contents
Next: Python and the Up: New in Release Previous: Documentation Strings

Customizing Import and Built-Ins

In preparation for a ``restricted execution mode'' which will be usable to run code received from an untrusted source (such as a WWW server or client), the mechanism by which modules are imported has been redesigned. It is now possible to provide your own function __import__ which is called whenever an import statement is executed. There's a built-in function __import__ which provides the default implementation, but more interesting, the various steps it takes are available separately from the new built-in module imp. (See the section on imp in the Library Reference Manual for more information on this module -- it also contains a complete example of how to write your own __import__ function.)

When you do dir() in a fresh interactive interpreter you will see another ``secret'' object that's present in every module: __builtins__. This is either a dictionary or a module containing the set of built-in objects used by functions defined in current module. Although normally all modules are initialized with a reference to the same dictionary, it is now possible to use a different set of built-ins on a per-module basis. Together with the fact that the import statement uses the __import__ function it finds in the importing modules' dictionary of built-ins, this forms the basis for a future restricted execution mode.



guido@cwi.nl