Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #6969 > unrolled thread

Determine attributes of calling method

Started byJoe <joe.cwikla@gmail.com>
First post2011-06-03 13:35 -0700
Last post2011-06-04 14:02 -0600
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Determine attributes of calling method Joe <joe.cwikla@gmail.com> - 2011-06-03 13:35 -0700
    Re: Determine attributes of calling method Richard Thomas <chardster@gmail.com> - 2011-06-04 04:54 -0700
    Re: Determine attributes of calling method Ian Kelly <ian.g.kelly@gmail.com> - 2011-06-04 14:02 -0600

#6969 — Determine attributes of calling method

FromJoe <joe.cwikla@gmail.com>
Date2011-06-03 13:35 -0700
SubjectDetermine attributes of calling method
Message-ID<6708872e-bf6e-4f7c-b9ec-d00a71cddc4f@j23g2000yqc.googlegroups.com>
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

[toc] | [next] | [standalone]


#7005

FromRichard Thomas <chardster@gmail.com>
Date2011-06-04 04:54 -0700
Message-ID<8880269a-2a6c-46a7-8cb5-cdd8b9eb2e59@g24g2000vbz.googlegroups.com>
In reply to#6969
On Jun 3, 9:35 pm, Joe <joe.cwi...@gmail.com> 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. :-)

[toc] | [prev] | [next] | [standalone]


#7021

FromIan Kelly <ian.g.kelly@gmail.com>
Date2011-06-04 14:02 -0600
Message-ID<mailman.2458.1307217779.9059.python-list@python.org>
In reply to#6969
On Fri, Jun 3, 2011 at 2:35 PM, Joe <joe.cwikla@gmail.com> 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'

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web