Hiding modules

Josiah Carlson jcarlson at nospam.uci.edu
Tue Feb 17 23:56:31 EST 2004


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)



More information about the Python-list mailing list