473,378 Members | 1,413 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,378 software developers and data experts.

exec a string in an embedded environment

Hi all pythonistas!

I have a problem with my embedding of python inside a c app.

very short background
I work on a safety critical embedded application that runs on VxWorks.
I have successfully ported the interpreter to VW. In my solution I have
restricted other "classes" to use python through my simplified api. Its
a really complex architecture that force me to work this way, I have
encapsulated the entire interpreter with its own memspace and so on.
Redirected all IO signals and such.

To the problem
I need some way to execute a string and pass arguments to the functions
inside the string. We have discussed a solution where we first load the
string (containing some funcs) and then run something similar to
Py_RunString("foo(1.0, 'str')"); We need to do this in a generic way
so we can send in arbitrary arguments.

Can I use Py_CompileString to get a PyObject to that 'module' and then
in some magic way call a function within that module?

I am totaly out of ideas so please help me anyone.
//Tommy

Jan 11 '06 #1
7 2125
Tommy R wrote:
I need some way to execute a string and pass arguments to the functions
inside the string. We have discussed a solution where we first load the
string (containing some funcs) and then run something similar to
Py_RunString("foo(1.0, 'str')"); We need to do this in a generic way
so we can send in arbitrary arguments.


You could use

foo_ptr = Py_RunString("foo", Py_eval_input, globals_ptr,
locals_ptr)

to get a reference to the foo function, and then use

result_ptr = PyObject_Call(foo_ptr, args_ptr, kwargs_ptr)

to call it with arbitrary arguments and keyword arguments. Of course,
you're responsible for setting up the globals and locals, as well as
the args list and kwargs dictionary objects.

The PyObject_Call signature, from <abstract.h>, is:

PyAPI_FUNC(PyObject *) PyObject_Call(PyObject *callable_object,
PyObject *args, PyObject *kw);

Does this help?

Graham

Jan 11 '06 #2
On Wed, 11 Jan 2006 04:29:32 -0800, Tommy R wrote:
I work on a safety critical embedded application that runs on VxWorks.
I have successfully ported the interpreter to VW. In my solution I have


Sure wish you would of asked...I ported Python to VxWorks
some time back. I've been using it for some time now.

Greg

Jan 11 '06 #3
jon
Greg Copeland wrote:
On Wed, 11 Jan 2006 04:29:32 -0800, Tommy R wrote:
I work on a safety critical embedded application that runs on VxWorks.
I have successfully ported the interpreter to VW. In my solution I have


Sure wish you would of asked...I ported Python to VxWorks
some time back. I've been using it for some time now.


Greg,

I'm very interested to hear more about your experience in porting
Python to VxWorks. Prior to seeing your VxWorks-related postings as
well as Tommy's, the last VxWorks port related information I found was
back in '99-'00 timeframe.

So, do you have any recipes that you can post somewhere? Any help
pointers would be greatly appreciated.

Thanks,
Jon Wahlmann

Jan 12 '06 #4
Jon
Tommy, same question to you... :-)

-Jon

Jan 12 '06 #5
I would be happy to share my point with you. In fact, I'm fixing a
minor memory leak (socket module; vxWorks specific) in Python 2.3.4
(ported version) today. My port is actually on BE XScale.

Email me at g t copeland2002@@ya hoo...com and I'll be happy to talk
more with you.

Jan 12 '06 #6
No it didn't help me :(

the foo_ptr that is returned from PyRun_String is not a callable
object. Therefor I can't call it from PyObject_Call.
I would like to do the exact same procedure as I do when the code is
located in a module (file).

pModule = PyImport_Import(pName);
Py_DECREF(pName);

if (pModule != NULL) {
pFunc = PyObject_GetAttrString(pModule, inArg->funcName);
/* pFunc is a new reference */

if (pFunc && PyCallable_Check(pFunc)) {
It must be possible to import a module written inside a string with a
similar approach. The ultimate solution would be to change the
PyImport_Import() call to something that creates an object from a
string

Now some of you think why not have the code inside a file. We need to
create encrypted script files, thats why.

I hope someone can help me //Tommy

Jan 17 '06 #7
I can't post that much of what I have done but some questions might
answer if you e-mail me the question to my gmail.com address.
tommy.ryding@...

//Tommy

Jan 17 '06 #8

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

Similar topics

6
by: Hal Vaughan | last post by:
I have a script used to find and run Java on a Windows system.  It worked fine on original tests (on a Windows XP system).  It's now running on a Windows 2000 (sp3) system, and it won't work -- the...
0
by: Jan | last post by:
I store sql-commands in a database table. In the first step I get the sql command out of the database table with embedded sql. In the second step I try to execute the command, which i got from the...
16
by: Charles Law | last post by:
I have a string similar to the following: " MyString 40 "Hello world" all " It contains white space that may be spaces or tabs, or a combination, and I want to produce an array...
8
by: R. Bernstein | last post by:
In doing the extension to the python debugger which I have here: http://sourceforge.net/project/showfiles.php?group_id=61395&package_id=175827 I came across one little thing that it would be nice...
5
by: snicks | last post by:
I'm trying to exec a program external to my ASP.NET app using the following code. The external app is a VB.NET application. Dim sPPTOut As String sPPTOut = MDEPDirStr + sID + ".ppt" Dim p As...
4
by: Michael | last post by:
Hi, I'm having difficulty finding any previous discussion on this -- I keep finding people either having problems calling os.exec(lepev), or with using python's exec statement. Neither of...
21
by: comp.lang.tcl | last post by:
set php {<? print_r("Hello World"); ?>} puts $php; # PRINTS OUT <? print_r("Hello World"); ?> puts When I try this within TCL I get the following error:
5
by: Edward K Ream | last post by:
It looks like both exec and execfile are converting "\n" to an actual newline in docstrings! Start idle: Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) on win32
31
by: eliben | last post by:
Hello, In a Python program I'm writing I need to dynamically generate functions and store them in a dict. eval() can't work for me because a function definition is a statement and not an...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.