Embedding - Local dictionaries for local scripts

Rolf Kalbermatter rolf.kalbermatter at citeng.com
Mon Sep 9 04:32:43 EDT 2002


Maybe your problem is more basic about the actual context to create, to use
such Python functions just as I had in the beginning. Then something like
this
might work for you. I have split the actual compile and execute step because
my original project has some more possibilities than this. But that should
make no difference. Also you should think about proper PyThreadState
implementation and protection if your host application is multithreading and
could possibly call the Python core from different threads.  This has been
left out here for simplicity.

typedef struct tagSESS {
	int32	magic;
	PySession next;
	char *text;
	PyObject *co;
	PyObject *dl;
	PyThreadState *tstate;
} PySessionRec;

int PySetScript(PySession session, char *lpScriptText) {
	if (session) {
		int32 len = strlen(lpScriptText);

		if (session->text)
			free(session->text);
		session->text = (char*)malloc(len + 1);
		MoveBlock(lpScriptText, session->text, len);
		if (session->co) {
			Py_DECREF(session->co);
			session->co = NULL;
		}
		return TRUE;
	}
	return FALSE;
}
int PySetData(PySession session, char *name, void* data, int16*
typedescriptor) {
	if (session) {
		if (!session->dl) {
			/* If no local variable dictionary create new one */
			session->dl = PyDict_New();
		}
		if (session->dl) {
			/* Convert our data into a Python object */
			PyObject *value = PyCreateData(data, typedescriptor);
			if (value) {
				/* Add the input variable to the local variable dictionary */
				PyDict_SetItemString(session->dl, name, value);
				Py_DECREF(value);
				/* Return success/failure */
				success = PyErr_Occurred() ? FALSE : TRUE;
			}
		}
	}
	return FALSE;
}

int PyExecuteScript(PySession session, int *eStart, int *eEnd, char *eText)
{
	if (session) {
		if (!session->co) {
			/* Compile the script */
			session->co = Py_CompileStringFlags(session->text, "<host>",
Py_file_input, 0);
		}
		if (session->co) {
			PyObject *v, *m, *dg;

			/* Extract the global dictionary object */
			if (!(m = PyImport_AddModule("__main__")))
				goto errOut;
			if (!(dg = PyModule_GetDict(m))) {
				goto errOut;
			}
			Py_INCREF(dg);
			if (PyDict_GetItemString(dg, "__builtins__") == NULL) {
				if (PyDict_SetItemString(dg, "__builtins__", PyEval_GetBuiltins()) != 0)
{
					Py_DECREF(dg);
					goto errOut;
				}
			}
			/* Execute the script, passing in our arguments as local variables */
			if (v = PyEval_EvalCode((PyCodeObject *)session->co, dg, session->dl)) {
				Py_DECREF(v);
			}
			Py_DECREF(dg);
		}
		/* Extract eventual error information if any */
		return PyErrorInfo(eStart, eEnd, eText);
	}
errOut:
	return FALSE;
}

int PyGetData(PySession session, char *name, void* data, int16*
typedescriptor) {
	if (session) {
		if (session->dl) {
			PyObject *value;

			/* Retrieve variable value */
			if (value = PyDict_GetItemString(session->dl, name)) {
				/* Value is borrowed reference from dict */
				return PyConvertData(value, data, typedescriptor);
			}
		}
	}
	return FALSE;
}

> -----Original Message-----
> From: python-list-admin at python.org
> [mailto:python-list-admin at python.org]On Behalf Of Martin v. Lowis
> Sent: Monday, September 09, 2002 9:51 AM
> To: python-list at python.org
> Subject: Re: Embedding - Local dictionaries for local scripts
>
>
> chris.walsh at real-sense.com (Chris Walsh) writes:
>
> >     PyObject* PyRun_String(
> >         char *str,
> >         int start,
> >         PyObject *globals,
> >         PyObject *locals);
> >
> > I have seen some stuff in comp.lang.python about creating a dict and
> > adding the __builtin__ module but I have been searching now for an
> > hour and cannot find it.
> >
> > Can anyone point me to some code which creates this dictionary?
>
> The globals/locals dictionary is not really different from any other
> dictionary; you create them with PyDict_New. Python should
> transparently add an __builtins__ into the globals dictionary if there
> is none, but you can copy the __builtins__ entry just as well yourself
> (via PyDict_SetItemString).
>
> HTH,
> Martin
>
> --
> http://mail.python.org/mailman/listinfo/python-list





More information about the Python-list mailing list