Mailing List Archive

running application config files written in Python
Say I have an application whose configuration language I want to be
Python. I have all my extra types implemented in C. Now what I'm
wondering is what's the best way to run that file so that the
functions, variables, etc. get imported into the Python interpreter
embedded in my program?

Along the same lines, once I have run the file, what's the easiest way
to find out if a particular function/variable has been defined? For
example, if I always wanted to run the user-defined function
`startup_func', how would I go about doing that?
--
Nathan | nathan.froyd@rose-hulman.edu | http://www.rose-hulman.edu/~froydnj/
God went through hell so we don't have to. ICQ:18861764 | AOL:myrlyn007
Avoid the gates of hell. Use Linux. Python:"x='x=%s;x%%`x`';x%`x`"
Evolution is a million line computer program falling into place by accident.
running application config files written in Python [ In reply to ]
Nathan Froyd <nathan.froyd@rose-hulman.edu> writes:

-> Say I have an application whose configuration language I want to be
-> Python.

I would like to do the same thing. However, my application will be
written in Python and translated to C or C++ as necessary. I just
wanted to point out that you will be executing actual code not unlike
the VBA macros in Microsoft Word with this technique. The Melissa
virus was written in VBA in a Word document. If there is a way to
sandbox such code, I would love to hear how because it is very useful
to be able to just recycle the Python interpreter for reading
configuration information.

--
David Steuber
http://www.david-steuber.com

If you wish to reply by mail, _please_ replace 'trashcan' with 'david'
in the e-mail address. The trashcan account really is a trashcan.

"Gee, Mudhead, everyone at More Science High has an
extracurricular activity except you."
"Well, gee, doesn't Louise count?"
"Only to ten, Mudhead."

-- Firesign Theater
running application config files written in Python [ In reply to ]
Hi,

According to David Steuber:
> wanted to point out that you will be executing actual code not unlike
> the VBA macros in Microsoft Word with this technique. The Melissa
> virus was written in VBA in a Word document. If there is a way to
> sandbox such code, I would love to hear how

Look at the documentation of the "rexec" module in the Library Reference;
should do exactly what you want.

Regards,
--Bjoern
PS David: a) Faking headers is Not A Good Thing. b) 12 lines of signature
(even more in your previous mails) are Not A Good Thing Either.
--
--------------------------------/\--One OS to rule them all---Windows NT-----
Bjoern Giesler / \ One OS to find them
<un4e@rz.uni-karlsruhe.de> / <> \ One OS to bring them all
-----------------------------/______\--And in the Darkness bind them---------
running application config files written in Python [ In reply to ]
Nathan Froyd <nathan.froyd@rose-hulman.edu> wrote:
: Say I have an application whose configuration language I want to be
: Python. I have all my extra types implemented in C. Now what I'm
: wondering is what's the best way to run that file so that the
: functions, variables, etc. get imported into the Python interpreter
: embedded in my program?

: Along the same lines, once I have run the file, what's the easiest way
: to find out if a particular function/variable has been defined? For
: example, if I always wanted to run the user-defined function
: `startup_func', how would I go about doing that?

You can do this in two ways easily (and other ways less easily).

1) Store the configuration files as modules, then import them.
If you are worried about security, you can import them using rexec.

char modulename[] = "app_config";
PyObject *config_module, *config_dict;
PyObject *startup_func, *result;
Py_Initialize();

if ((config_module = PyImport_ImportModule(modulename)) != NULL &&
(config_dict = PyModule_GetDict(config_module)) != NULL) {
startup_func = PyDict_GetItemString(config_dict, "startup_func");
/* look for startup function, but ignore if not present */
result = NULL;
if (startup_func != NULL && PyCallable_Check(startup_func))
result = PyObject_CallFunction(startup_func, "");
if ((startup_func == NULL || result == NULL) &&
PyErr_Occurred() &&
!PyErr_ExceptionMatches(PyExc_AttributeError))
/* ignore AttributeError exceptions */
PyErr_Print();
PyErr_Clear();
Py_XDECREF(startup_func);
Py_XDECREF(result);
} else {
/* the module could not be loaded */
PyErr_Print();
Py_Exit(1);
}

All values are stored in the module (accessed thru config_dict). The
exception handling is more verbose then the next method.

2) Run the equivalent of execfile().

char filename[] = "app_config.py";
FILE *app_config;

Py_Initialize();

app_config = fopen(filename, "r");
if (PyRun_Simplefile(app_config, filename) == -1) {
fclose(app_config);
/* the traceback has already been written to stderr */
Py_Exit(1);
}
fclose(app_config);
PyRun_SimpleString("try: startup_func()\n\
except NameError: pass\n");

All values are stored in the current namespace (the module __main__,
and accessed thru the undocumented function PyEval_GetGlobals() or thru
other PyRun_* calls). If there are exceptions raised, PyRun_* calls
handle the output; the return value is either -1 for failure, 0 for
success.

I would suggest using method (1). Good luck.
-Arcege
running application config files written in Python [ In reply to ]
Bjoern Giesler <un4e@rz.uni-karlsruhe.de> writes:

-> PS David: a) Faking headers is Not A Good Thing. b) 12 lines of signature
-> (even more in your previous mails) are Not A Good Thing Either.

a) Which headers are you referring too? The return address I am
using is valid. However, I get so much spam that I rarely check it
any more. I basicly expect replys to go the the news groups so that
everyone may benifit if by some miracle I ask a good question.

b) I still have some wrinkles to work out with my signature. I have
a fixed portion of text followed by the output of the fortune
program. I can shorten the fixed portion easily enough. However, I
don't know how to make fortune grab only short quotes. It is pot luck
whether it is one line or ten.

In the interim, I'll knock out four lines.

--
David Steuber | s/trashcan/david/ if you wish to reply by mail

Overheard in a bar:
Man: "Hey, Baby, I'd sure like to get in your pants!"
Woman: "No, thanks, I've already got one ass-hole in there now."
running application config files written in Python [ In reply to ]
David Steuber <trashcan@david-steuber.com> writes:

> Bjoern Giesler <un4e@rz.uni-karlsruhe.de> writes:
>
> b) I still have some wrinkles to work out with my signature. I have
> a fixed portion of text followed by the output of the fortune
> program. I can shorten the fixed portion easily enough. However, I
> don't know how to make fortune grab only short quotes. It is pot luck
> whether it is one line or ten.

Just a helpful suggestion...

--- begin shortfortune.py ---
#!/usr/bin/env python

import os,string

quote = os.popen("fortune").readlines()
while len(quote) > 4:
quote = os.popen("fortune").readlines()

for line in quote:
print string.rstrip(line)

---- end shortfortune.py ----

--

Magnus
Lie
Hetland http://arcadia.laiv.org <arcadia@laiv.org>
running application config files written in Python [ In reply to ]
On 30 Apr 1999 18:27:48 -0400, David Steuber wrote:

>b) I still have some wrinkles to work out with my signature. I have
>a fixed portion of text followed by the output of the fortune
>program. I can shorten the fixed portion easily enough. However, I
>don't know how to make fortune grab only short quotes. It is pot luck
>whether it is one line or ten.

"fortune -s"

Will give only short fortunes. Use the "-n X" option, where X is the
number of characters in a "short" fortune. (That's not the number of
lines, but oh well.)

"fortune -s -n 200"

Personally, I prefer to use fortune to find cool quotes, and then place
them in my .sig by hand.

>David Steuber | s/trashcan/david/ if you wish to reply by mail

--
-William "Billy" Tanksley
"But you shall not escape my iambics."
-- Gaius Valerius Catullus