Hiding modules

Tommy Trojan hauck at gseos.com
Mon Feb 16 07:35:14 EST 2004


Mike,

thanks for the tip. I thought along the same lines, but that doesn't get
around the problem that the user could just embed an import in a script, or
import a script that I provide and that internally uses the module that
should not be exposed, i.e.:

>>> import UserModule

in UserModule:

import RestrictedModule


This would get around the simple loop in checking the input arguments of the
interactive prompt. It needs to be a hook that is further down in the
system. The problem with an import hook is that it would need to distinguish
when it is okay to import the module (when I call it) and when it is not
okay (when my user calls it).

Thanks,
  Thomas


"Miki Tebeka" <miki.tebeka at zoran.com> wrote in message
news:4f0a9fdb.0402160359.33f90363 at posting.google.com...
> Hello Tommy,
>
>
> > Is there anything that can be done to import modules conditionally?
> > Any ideas?
> Writing a custom REPL (read-eval-print-loop) should be easy enough:
> import re
> split = re.compile("\s+").split
>
> forbidden = ("os", "sys")
>
> def repl():
>     while 1:
>         s = raw_input(">>> ").strip()
>         ok = 1
>         if s.startswith("import"):
>             for module in split(s.strip())[1:]:
>                 if module in forbidden:
>                     print "can't use %s module" % module
>                     ok = 0
>                     break
>         if ok:
>             exec(s)
>
> while 1:
>     repl()
>
> HTH.
> Miki





More information about the Python-list mailing list