controlling method execution

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mic

    controlling method execution

    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


  • anton muhin

    #2
    Re: controlling method execution

    mic wrote:[color=blue]
    > 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
    >
    >[/color]
    Here comes my humble attempt:

    import inspect
    import new

    def before():
    print "before"

    class WrapperMeta(typ e):
    def __new__(cls, name, bases, attrs):
    wrapped = {}
    for n, v in attrs.iteritems ():
    if inspect.isfunct ion(v):
    class Controled(objec t):
    def __get__(self, obj, cls):
    before()
    return new.instancemet hod(v, obj, cls)

    wrapped[n] = Controled()
    attrs.update(wr apped)
    return super(WrapperMe ta, cls).__new__(cl s, name, bases, attrs)

    class Controled(objec t):
    __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.

    Comment

    • anton muhin

      #3
      Re: controlling method execution

      anton muhin wrote:
      [color=blue]
      > mic wrote:
      >[color=green]
      >> 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
      >>
      >>[/color]
      > Here comes my humble attempt:
      >
      > import inspect
      > import new
      >
      > def before():
      > print "before"
      >
      > class WrapperMeta(typ e):
      > def __new__(cls, name, bases, attrs):
      > wrapped = {}
      > for n, v in attrs.iteritems ():
      > if inspect.isfunct ion(v):
      > class Controled(objec t):
      > def __get__(self, obj, cls):
      > before()
      > return new.instancemet hod(v, obj, cls)
      >
      > wrapped[n] = Controled()
      > attrs.update(wr apped)
      > return super(WrapperMe ta, cls).__new__(cl s, name, bases, attrs)
      >
      > class Controled(objec t):
      > __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.
      >[/color]

      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(typ e):
      def __new__(cls, name, bases, attrs):
      wrapped = {}
      for n, v in attrs.iteritems ():
      if inspect.isfunct ion(v):
      def wrapper(self, *args, **kwargs):
      before()
      new.instancemet hod(v, self, cls)(*args, **kwargs)
      after()

      wrapped[n] = wrapper
      attrs.update(wr apped)
      return super(WrapperMe ta, cls).__new__(cl s, name, bases, attrs)

      class Controled(objec t):
      __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.

      Comment

      Working...