Determine attributes of calling method

Discussion in 'Python' started by Joe, Jun 3, 2011.

  1. Joe

    Joe Guest

    Hello,

    I'm trying to implement a way to restrict method usage based on the
    caller's attributes. In the following example I'd like to execute the
    server method "bar" only if the caller's method has a "blue" value for
    it's color attribute.

    The current output is:

    blue
    red
    bar
    bar

    I'd like it to be:

    blue
    red
    bar

    I've worked my way through inspect but that doesn't seem to be the
    right approach.

    # Example
    class Client:

    def __init__(self, server):
    self.server=server

    def foo(self):
    self.server.bar()

    def fu(self):
    self.server.bar()

    foo.__dict__['color']='blue'
    fu.__dict__['color']='red'

    class BlueServer:

    def bar(self):
    """
    Goal is to only accept calls from "blue" client methods.
    Don't know how to do it :(
    """
    print "bar"

    s=BlueServer()
    c=Client(s)
    print c.foo.color
    print c.fu.color
    c.foo()
    c.fu()

    Thanks for your help!

    Joe
     
    Joe, Jun 3, 2011
    #1
    1. Advertising

  2. On Jun 3, 9:35 pm, Joe <> wrote:
    > Hello,
    >
    > I'm trying to implement a way to restrict method usage based on the
    > caller's attributes.  In the following example I'd like to execute the
    > server method "bar" only if the caller's method has a "blue" value for
    > it's color attribute.
    >
    > The current output is:
    >
    > blue
    > red
    > bar
    > bar
    >
    > I'd like it to be:
    >
    > blue
    > red
    > bar
    >
    > I've worked my way through inspect but that doesn't seem to be the
    > right approach.
    >
    > # Example
    > class Client:
    >
    >     def __init__(self, server):
    >         self.server=server
    >
    >     def foo(self):
    >         self.server.bar()
    >
    >     def fu(self):
    >         self.server.bar()
    >
    >     foo.__dict__['color']='blue'
    >     fu.__dict__['color']='red'
    >
    > class BlueServer:
    >
    >     def bar(self):
    >         """
    >         Goal is to only accept calls from "blue" client methods.
    >         Don't know how to do it :(
    >         """
    >         print "bar"
    >
    > s=BlueServer()
    > c=Client(s)
    > print c.foo.color
    > print c.fu.color
    > c.foo()
    > c.fu()
    >
    > Thanks for your help!
    >
    > Joe


    It is possible to get access to the code object of the parent
    execution frame. Function objects are just wrappers for code objects
    and are not part of the frame. You could do something with that
    though...

    _colors = {}
    def color(c):
    def decorator(f):
    code = f.func_code
    key = code.co_filename, code.co_firstlineno
    _colors.setdefault(key, set()).add(c)
    return f
    return decorator

    def require(c):
    import sys
    code = sys._getframe().f_back.f_back.f_code
    key = code.co_filename, code.co_firstlineno
    assert c in _colors.get(key, ())

    def tryit():
    require("blue")

    @color("blue")
    def bluefunc():
    tryit()

    @color("red")
    def redfunc():
    tryit()

    Executing untrusted Python code is nigh impossible to make secure of
    course. I'd suggest examining whether this is necessary. :)
     
    Richard Thomas, Jun 4, 2011
    #2
    1. Advertising

  3. Joe

    Ian Kelly Guest

    On Fri, Jun 3, 2011 at 2:35 PM, Joe <> wrote:
    >    foo.__dict__['color']='blue'
    >    fu.__dict__['color']='red'


    You don't need to use __dict__ to set function attributes. Just do:

    foo.color = 'blue'
     
    Ian Kelly, Jun 4, 2011
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mrdylan
    Replies:
    3
    Views:
    309
    Steven D'Aprano
    Apr 12, 2006
  2. Max
    Replies:
    1
    Views:
    498
    Joe Kesselman
    Sep 22, 2006
  3. Peña, Botp
    Replies:
    1
    Views:
    246
    Robert Klemme
    Jan 24, 2004
  4. why-em-jay

    Calling a method of the calling object ...

    why-em-jay, Sep 15, 2005, in forum: Perl Misc
    Replies:
    3
    Views:
    183
    Anno Siegel
    Sep 15, 2005
  5. Jayden
    Replies:
    16
    Views:
    540
    Steven D'Aprano
    Sep 29, 2012
Loading...

Share This Page