Looking for a way to include Pyhtho scripting INSIDE a python program

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

    Looking for a way to include Pyhtho scripting INSIDE a python program

    Hello all,

    I'm writing in tool in Python that manipulates various data objects read
    from various streams. I wanted to give the user a chance to do advanced
    work that could not easily be done from a GUI.

    At first, I tried putting in a lightweight scripting language, and then
    I thought, why not include Python in itself -- it is certainly powerful
    enough.

    I had assumed I'd present the user with a text window in which they
    could type arbitrary python code. I'd wrap that code around a function
    and pass that function a call of objects they could manipulate by
    calling the methods of that class.

    1. How can a python program invoke ANOTHER interpreter?
    2. How can I pass the class in as its argument and get the modified
    class back?

    I know I can do something very ugly -- call a C method that calls a new
    python interpreter but that seems VERY ugly.

    Help?

    Thanks.
  • Bryon

    #2
    Re: Looking for a way to include Pyhtho scripting INSIDE a pythonprogram

    On Apr 12, 10:16 pm, John Antypas <respon...@anty pas.netwrote:
    Hello all,
    >
    I'm writing in tool in Python that manipulates various data objects read
    from various streams. I wanted to give the user a chance to do advanced
    work that could not easily be done from a GUI.
    >
    At first, I tried putting in a lightweight scripting language, and then
    I thought, why not include Python in itself -- it is certainly powerful
    enough.
    >
    I had assumed I'd present the user with a text window in which they
    could type arbitrary python code. I'd wrap that code around a function
    and pass that function a call of objects they could manipulate by
    calling the methods of that class.
    >
    1. How can a python program invoke ANOTHER interpreter?
    2. How can I pass the class in as its argument and get the modified
    class back?
    >
    I know I can do something very ugly -- call a C method that calls a new
    python interpreter but that seems VERY ugly.
    >
    Help?
    >
    Thanks.
    I'm very new to python but I think this is what you're looking for


    Comment

    • Paddy

      #3
      Re: Looking for a way to include Pyhtho scripting INSIDE a pythonprogram

      On Apr 13, 4:16 am, John Antypas <respon...@anty pas.netwrote:
      Hello all,
      >
      I'm writing in tool in Python that manipulates various data objects read
      from various streams. I wanted to give the user a chance to do advanced
      work that could not easily be done from a GUI.
      >
      At first, I tried putting in a lightweight scripting language, and then
      I thought, why not include Python in itself -- it is certainly powerful
      enough.
      >
      I had assumed I'd present the user with a text window in which they
      could type arbitrary python code. I'd wrap that code around a function
      and pass that function a call of objects they could manipulate by
      calling the methods of that class.
      >
      1. How can a python program invoke ANOTHER interpreter?
      2. How can I pass the class in as its argument and get the modified
      class back?
      >
      I know I can do something very ugly -- call a C method that calls a new
      python interpreter but that seems VERY ugly.
      >
      Help?
      >
      Thanks.
      You might try ipython at http://ipython.scipy.org/moin/; or 'python -
      i'; or the exec and eval statements.

      There is also the compiler module: http://docs.python.org/lib/compiler.html


      - Paddy.

      Comment

      • Ivan Illarionov

        #4
        Re: Looking for a way to include Pyhtho scripting INSIDE a pythonprogram

        On Apr 13, 7:16 am, John Antypas <respon...@anty pas.netwrote:
        Hello all,
        >
        I'm writing in tool in Python that manipulates various data objects read
        from various streams. I wanted to give the user a chance to do advanced
        work that could not easily be done from a GUI.
        >
        At first, I tried putting in a lightweight scripting language, and then
        I thought, why not include Python in itself -- it is certainly powerful
        enough.
        >
        I had assumed I'd present the user with a text window in which they
        could type arbitrary python code. I'd wrap that code around a function
        and pass that function a call of objects they could manipulate by
        calling the methods of that class.
        >
        1. How can a python program invoke ANOTHER interpreter?
        2. How can I pass the class in as its argument and get the modified
        class back?
        >
        I know I can do something very ugly -- call a C method that calls a new
        python interpreter but that seems VERY ugly.
        >
        Help?
        >
        Thanks.
        You don't need to envoke another interpreter.
        Python can interpret arbitrary python code with exec statement.
        Wrap user's string inside function definition, and exec it.

        You might want to disable words like `import`, `exec` and `eval` in
        user's code because it's a big security risk.

        --
        Ivan Illarionov

        Comment

        • Bryan Oakley

          #5
          Re: Looking for a way to include Pyhtho scripting INSIDE a pythonprogram

          Ivan Illarionov wrote:
          You don't need to envoke another interpreter.
          Python can interpret arbitrary python code with exec statement.
          Wrap user's string inside function definition, and exec it.
          >
          You might want to disable words like `import`, `exec` and `eval` in
          user's code because it's a big security risk.
          The above statement is exactly why one would want to eval the code
          inside a separate interpreter. Not just for security, but to prevent
          user code from stomping all over the application code by creating or
          destroying global resources.

          Is it possible to create a nested interpreter like you can do in some
          other languages?

          Comment

          • Ivan Illarionov

            #6
            Re: Looking for a way to include Pyhtho scripting INSIDE a pythonprogram

            On Apr 13, 8:20 pm, Bryan Oakley <oak...@bardo.c learlight.comwr ote:
            Ivan Illarionov wrote:
            You don't need to envoke another interpreter.
            Python can interpret arbitrary python code with exec statement.
            Wrap user's string inside function definition, and exec it.
            >
            You might want to disable words like `import`, `exec` and `eval` in
            user's code because it's a big security risk.
            >
            The above statement is exactly why one would want to eval the code
            inside a separate interpreter. Not just for security, but to prevent
            user code from stomping all over the application code by creating or
            destroying global resources.
            >
            Is it possible to create a nested interpreter like you can do in some
            other languages?
            Yes. Call PyRun_SimpleStr ing from ctypes or call PyRun_SimpleStr ing
            from custom python extension. But it does nothing what exec can't do.



            We have:

            exec `something` in `where_we_exec`



            if `where_we_exec` is an empty dictionary the exec'd code has no
            access to app code or global resources.



            Even more, it's harder to control the nested interpreter than strings
            about to be exec'd. And you still have to worry about security. So,
            not only you gain nothing by this approach, you make your software
            more vulnerable. The code like `import os\n os.*killme*` or
            eval("__import_ _('os').*killme *") will be harder to disable.

            --
            Ivan

            Comment

            Working...