Is it possible to control method exectution using similiar mechanism as with
get/setatrribute special methods for attributes? I'd like to have every (or
some of ) method of class instance run common code defined outside the class
without making any explicit references to it.
To give an example, I'd like to prepare secure environment, in which users
could execute only methods that can be accessed by them. IMO the most
universal way to do it would be to run automatically authorisation routine
before every method execution.
Regards,
Michal 2 1344
mic wrote: Is it possible to control method exectution using similiar mechanism as with get/setatrribute special methods for attributes? I'd like to have every (or some of ) method of class instance run common code defined outside the class without making any explicit references to it. To give an example, I'd like to prepare secure environment, in which users could execute only methods that can be accessed by them. IMO the most universal way to do it would be to run automatically authorisation routine before every method execution.
Regards,
Michal
Here comes my humble attempt:
import inspect
import new
def before():
print "before"
class WrapperMeta(type):
def __new__(cls, name, bases, attrs):
wrapped = {}
for n, v in attrs.iteritems():
if inspect.isfunction(v):
class Controled(object):
def __get__(self, obj, cls):
before()
return new.instancemethod(v, obj, cls)
wrapped[n] = Controled()
attrs.update(wrapped)
return super(WrapperMeta, cls).__new__(cls, name, bases, attrs)
class Controled(object):
__metaclass__ = WrapperMeta
class Test(Controled):
def method(self, x, y):
print 'Test.method(%s, %s)' % (x, y)
test = Test()
test.method(10, 11)
I hope gurus will suggest better approaches. However, I'm afraid that
metaclass approach cannot guarantee enough security: one can simple get
rid of metaclass to bypass the mechanism.
regards,
anton.
anton muhin wrote: mic wrote:
Is it possible to control method exectution using similiar mechanism as with get/setatrribute special methods for attributes? I'd like to have every (or some of ) method of class instance run common code defined outside the class without making any explicit references to it. To give an example, I'd like to prepare secure environment, in which users could execute only methods that can be accessed by them. IMO the most universal way to do it would be to run automatically authorisation routine before every method execution.
Regards,
Michal
Here comes my humble attempt:
import inspect import new
def before(): print "before"
class WrapperMeta(type): def __new__(cls, name, bases, attrs): wrapped = {} for n, v in attrs.iteritems(): if inspect.isfunction(v): class Controled(object): def __get__(self, obj, cls): before() return new.instancemethod(v, obj, cls)
wrapped[n] = Controled() attrs.update(wrapped) return super(WrapperMeta, cls).__new__(cls, name, bases, attrs)
class Controled(object): __metaclass__ = WrapperMeta
class Test(Controled): def method(self, x, y): print 'Test.method(%s, %s)' % (x, y)
test = Test() test.method(10, 11)
I hope gurus will suggest better approaches. However, I'm afraid that metaclass approach cannot guarantee enough security: one can simple get rid of metaclass to bypass the mechanism.
regards, anton.
A bug: before gets called on simple Test.method. Better solution:
import inspect
import new
def before():
print 'before'
def after():
print 'after'
class WrapperMeta(type):
def __new__(cls, name, bases, attrs):
wrapped = {}
for n, v in attrs.iteritems():
if inspect.isfunction(v):
def wrapper(self, *args, **kwargs):
before()
new.instancemethod(v, self, cls)(*args, **kwargs)
after()
wrapped[n] = wrapper
attrs.update(wrapped)
return super(WrapperMeta, cls).__new__(cls, name, bases, attrs)
class Controled(object):
__metaclass__ = WrapperMeta
class Test(Controled):
def method(self, x, y):
print 'Test.method(%s, %s)' % (x, y)
test = Test()
test.method(10, 11)
test.method
sorry,
anton. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Gregory Lielens |
last post by:
Hello all,
I am not sure this is the correct list to ask as my problem is really
related to python low level implementation, but as I do not have found a
python.dev list, here it is :-)
We...
|
by: Rob Vermeulen |
last post by:
Hi folks,
Please forgive my ignorance but I need to know if it is possible to control
ActiveX components on my website from within my serverside C# code.
I'm quite an experienced s/w developer...
|
by: GaryDean |
last post by:
We have been noticing that questions on vs.2005/2.0 don't appear to get much
in answers so I'm reposting some questions posted by some of the programmers
here in our organization that never got...
|
by: Michael |
last post by:
Hi,
I'm having difficulty finding any previous discussion on this -- I
keep finding people either having problems calling os.exec(lepev), or
with using python's exec statement. Neither of...
|
by: Herb |
last post by:
I've found how to use javascript to embed a Windows Media Player in a web
page. How do I go about controlling the player in response to user input?
There should be calls to start, stop and also...
|
by: florian.loitsch |
last post by:
hi,
I'm having troubles understanding the meaning of the "valueOf" method,
as described in the ECMA-262 (3rd Edition) standard.
I quote (section 15.2.4.4, page 85):
===
"The _valueOf_ method...
|
by: Virat Sarswat |
last post by:
was trying to suspend and resume the inprocess function calls using .Net
threading but didn't get success. for making it more clear below is the code
i used
Thread ThreadSuspend = new...
|
by: Shanthini Ganesh |
last post by:
hi all. in my shopping cart project , i tested with two buttons, one for execution of client click and another for exection server method. these two buttons are as follows..
<input id="addbutton"...
|
by: Paul Schwann |
last post by:
Hi group,
I am relatively new to C# (although I have a lot of programming
excperience in other languages like Java and C). Currently I am
searching for a solution to this problem:
Suppose you...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |