473,387 Members | 1,573 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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.
Jun 27 '08 #1
5 1072
On Apr 12, 10:16 pm, John Antypas <respon...@antypas.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

http://pydoc.org/2.5.1/__builtin__.html#-compile
Jun 27 '08 #2
On Apr 13, 4:16 am, John Antypas <respon...@antypas.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.

Jun 27 '08 #3
On Apr 13, 7:16 am, John Antypas <respon...@antypas.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
Jun 27 '08 #4
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?
Jun 27 '08 #5
On Apr 13, 8:20 pm, Bryan Oakley <oak...@bardo.clearlight.comwrote:
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_SimpleString from ctypes or call PyRun_SimpleString
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
Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
by: Quest Master | last post by:
I am interested in developing an application where the user has an ample amount of power to customize the application to their needs, and I feel this would best be accomplished if a scripting...
7
by: Matt Jensen | last post by:
Howdy I want to simulate with .Net what I used to do with classic ASP where you would have a series of include files with utility functions that you would include when you needed them. I read...
2
by: Kenneth McDonald | last post by:
I'm not trying to persuade my company to offer Python as a scripting language for their product, but I am trying to give them examples of things that Python can do easily that cannot be done easily...
4
by: 4zumanga | last post by:
I have a bunch of really horrible hacked-up bash scripts which I would really like to convert to python, so I can extend and neaten them. However, I'm having some trouble mapping some constructs...
4
by: Mohsen | last post by:
Hello everybody, I am writing a C++ code to do some Quantitative Genetics calculations and I have to use another program (which is also a C++ code) inside my program. I have heard that it is...
3
by: heavydada | last post by:
I'm writing a small game in python and I need to be able to run some scripts inside the game. In the game I have these creatures each with some attributes like name and weight and an action. Right...
4
by: Julian | last post by:
Hi, first of all, I have to say I am new to Python. I have been working with a finite element analysis program written in c++. now, I am trying to 'rebuild' this code (possibly a full re-write)...
12
by: Tony Belding | last post by:
I'm interested in using an off-the-shelf interpreted language as a user-accessible scripting language for a MUCK. I'm just not sure if I can find one that does everything I need. The MUCK must be...
12
by: amogan | last post by:
**If interested & qualified, please reply with your resume directly to amogan@google.com** Referrals are always welcome!! Network System Test Engineer - Mountain View This position is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.