473,382 Members | 1,357 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Hiding modules

Hi,

I have a custom application in which I have a Python interpreter embedded.
Everything works really well and Python is taking over bigger and bigger
chunks of the hosting application (it's spreading like a cancer :-). My
users have direct access to the interpreter through a console window. So
they can run pretty much any code they like. Now I have some modules that I
use in my code and that I don't want my users to access. Is there anything
that can be done to import modules conditionally?
I guess I could just check for import statements or write an __import__
hook, however, I want my code still to be able to access the modules in
question.
Any ideas?

Thanks,
T
Jul 18 '05 #1
5 1305
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
Jul 18 '05 #2
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" <mi*********@zoran.com> wrote in message
news:4f**************************@posting.google.c om... 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

Jul 18 '05 #3

import sys
import traceback
class MyDict(UserDict):
def __getitem__(self, key):
try:
raise ""
except:
a = traceback.extract_stack()
#do all your call checking on a
#similar checks for __setitem__

sys.modules = MyDict(sys.modules)

Do as many checks on the caller that tries to access a module via
sys.modules as you want.

You could also replace new.module with similar attribute get and set
permission checking, though my testing suggests that this may not be
enough (perhaps import keeps a reference to the original version of
new.module, or something).

- Josiah
Jul 18 '05 #4
Josiah,

thanks, I will check out your solution. It looks similar to an import hook
to me. There is still a problem that the user could just reset sys.modules
to his own dict.

Thanks,
Thomas
"Josiah Carlson" <jc******@nospam.uci.edu> wrote in message
news:c0**********@news.service.uci.edu...

import sys
import traceback
class MyDict(UserDict):
def __getitem__(self, key):
try:
raise ""
except:
a = traceback.extract_stack()
#do all your call checking on a
#similar checks for __setitem__

sys.modules = MyDict(sys.modules)

Do as many checks on the caller that tries to access a module via
sys.modules as you want.

You could also replace new.module with similar attribute get and set
permission checking, though my testing suggests that this may not be
enough (perhaps import keeps a reference to the original version of
new.module, or something).

- Josiah

Jul 18 '05 #5
Tommy,

Heh, derive all of your classes from a single base class. Call this
base class "Restrictor" or something similarly funny. Give it the
equivalent __getattr__ and __setattr__ controls as the MyDict example's
__getitem__ and __setitem__.

Now all attribute accesses (including function calls) need to be
validated. It would slow things down considerably
(traceback.extract_stack() is slow), but you could restrict as much as
you want.

- Josiah
thanks, I will check out your solution. It looks similar to an import hook
to me. There is still a problem that the user could just reset sys.modules
to his own dict.
import sys
import traceback
class MyDict(UserDict):
def __getitem__(self, key):
try:
raise ""
except:
a = traceback.extract_stack()
#do all your call checking on a
#similar checks for __setitem__

sys.modules = MyDict(sys.modules)

Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Lucas Raab | last post by:
I'm looking to play a joke on a friend and I'm wondering if there's a way to not show or hide the DOS console. My friend doesn't have python so I have to compile it to an EXE. TIA
18
by: Anchorman | last post by:
We have a customer that wants us to put our ASP application on their server, so they can have direct access to the database. However, we don't want to leave our code wide open for them to copy...
12
by: Alex Hunsley | last post by:
There's no really specific questions in this post, but I'm looking for people's thought on the issues within... The two main versions I've encountered for data pseudo-hiding (encapsulation) in...
17
by: Bob Weiner | last post by:
What is the purpose of hiding intead of overriding a method? I have googled the question but haven't found anything that makes any sense of it. In the code below, the only difference is that...
9
by: bob | last post by:
Hi, I know there exists a good reason why the designers of c++ decided that function hiding should exist. But I don't know why. Can anybody provide a good reason/example of a case where function...
3
by: Nicolas Castagne | last post by:
Hi all, I have been wondering for a while why function hiding (in a derived class) exists in C++, e.g. why when writing class Base { void foo( int ) {} }; class Derived: public Base { void...
17
by: rohitchawla | last post by:
i am trying to show and hide a div when onmouseover and onmouseover another div element. i am setting a setTimeout duration on onmouseout to delay the hiding of div for around two second The...
162
by: Sh4wn | last post by:
Hi, first, python is one of my fav languages, and i'll definitely keep developing with it. But, there's 1 one thing what I -really- miss: data hiding. I know member vars are private when you...
2
by: Brendan Miller | last post by:
How would I implement something equivalent to java's package private in python? Say if I have package/__init__.py package/utility_module.py and utility_module.py is an implementation...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.