Dynamic/runtime code introspection/compilation

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

    Dynamic/runtime code introspection/compilation

    Maybe a stupid subject, but this is what I want to do :

    I got some python code stored in a string:

    somecode = """

    from somemodule import ISomeInterface

    class Foo(ISomeInterf ace):
    param1 = ...
    param2 = ....

    """

    and I want to compile that code so that I can use the Foo-class and
    check what class it extends, in this case ISomeInterface etc. I've
    tried eval, codeop etc. but it doesn't work. Something like this would
    be nice :

    from somemodule import ISomeInteface

    d = compile(sourcec ode)

    myfoo = d.Foo()

    print ISomeInterface in myfoo.__bases__

    Any hints?

  • Fredrik Lundh

    #2
    Re: Dynamic/runtime code introspection/compilation

    Thomas W wrote:
    from somemodule import ISomeInteface
    >
    d = compile(sourcec ode)
    >
    myfoo = d.Foo()
    >
    print ISomeInterface in myfoo.__bases__
    >
    Any hints?
    Python is a dynamic language, so compiling something won't tell you much
    about what the code actually does. the only reliable way to do that is
    to execute the code:

    code = compile(sourcec ode)
    namespace = {}
    exec code in namespace
    print issubclass(name space["Foo"])

    </F>

    Comment

    • Leo Kislov

      #3
      Re: Dynamic/runtime code introspection/compilation


      Thomas W wrote:
      Maybe a stupid subject, but this is what I want to do :
      >
      I got some python code stored in a string:
      >
      somecode = """
      >
      from somemodule import ISomeInterface
      >
      class Foo(ISomeInterf ace):
      param1 = ...
      param2 = ....
      >
      """
      >
      and I want to compile that code so that I can use the Foo-class and
      check what class it extends, in this case ISomeInterface etc. I've
      tried eval, codeop etc. but it doesn't work. Something like this would
      be nice :
      >
      from somemodule import ISomeInteface
      >
      d = compile(sourcec ode)
      >
      myfoo = d.Foo()
      >
      print ISomeInterface in myfoo.__bases__
      >
      Any hints?
      Here is hello world program for plugins:

      import sys

      somecode = """
      class Foo:
      param1 = "Hello, world!"
      """

      plugin = type(sys)('unkn own_plugin') # Create new empty module
      exec somecode in plugin.__dict__

      print plugin.Foo.para m1

      -- Leo

      Comment

      • Thomas W

        #4
        Re: Dynamic/runtime code introspection/compilation

        Great !!! That works like charm.

        Thanks alot.

        Thomas


        Leo Kislov wrote:
        Thomas W wrote:
        Maybe a stupid subject, but this is what I want to do :

        I got some python code stored in a string:

        somecode = """

        from somemodule import ISomeInterface

        class Foo(ISomeInterf ace):
        param1 = ...
        param2 = ....

        """

        and I want to compile that code so that I can use the Foo-class and
        check what class it extends, in this case ISomeInterface etc. I've
        tried eval, codeop etc. but it doesn't work. Something like this would
        be nice :

        from somemodule import ISomeInteface

        d = compile(sourcec ode)

        myfoo = d.Foo()

        print ISomeInterface in myfoo.__bases__

        Any hints?
        >
        Here is hello world program for plugins:
        >
        import sys
        >
        somecode = """
        class Foo:
        param1 = "Hello, world!"
        """
        >
        plugin = type(sys)('unkn own_plugin') # Create new empty module
        exec somecode in plugin.__dict__
        >
        print plugin.Foo.para m1
        >
        -- Leo

        Comment

        Working...