A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://mail.python.org/pipermail/python-list/2005-September/303481.html below:

embedding python in C, working but with exception at the end

embedding python in C, working but with exception at the endbrobigi at yahoo.com brobigi at yahoo.com
Sat Sep 3 09:33:49 EDT 2005
well I manage to figure it out myself. I'm using Bloodshed Dev-cpp
Here's the code:


#include "python.h"
#include <stdio.h>

int main(int argc, char* argv[])
{
	double answer = 0;
	PyObject *modname, *mod, *mdict, *func, *stringarg, *args, *rslt;

	Py_Initialize();
    modname = PyString_FromString("Test");
    mod = PyImport_Import(modname);
	if (mod)
	{
		mdict = PyModule_GetDict(mod);
		func = PyDict_GetItemString(mdict, "doit"); /* borrowed reference */
		if (func)
		{
			if (PyCallable_Check(func))
			{
				stringarg = PyString_FromString("5");/*pay attention here*/
				args = PyTuple_New(1);
				PyTuple_SetItem(args, 0, stringarg);
				rslt = PyObject_CallObject(func, args);
				if (rslt)
				{
					answer = PyFloat_AsDouble(rslt);
					Py_XDECREF(rslt);
				}
              Py_XDECREF(stringarg);
              Py_XDECREF(args);
			}
		}

		Py_XDECREF(mod);
	}

	Py_XDECREF(modname);

	Py_Finalize();

	printf("%g",answer);

	return 0;
}

I need to add include and lib directories to the project in order to
everything works fine. Also Test.py  is copied in Dev-cpp source code's
folder.
Test file contains the following code

def doit(x1):
    try:
        x2 = eval(x1)
    except:
        print 'Error!'
        return 0

    else:
        return x2




However there is an error. Look at the line stringarg =
PyString_FromString("5"); If I put this:
stringarg = PyString_FromString("5+2"); or even this stringarg =
PyString_FromString("5.0");
Program crashes at line Py_Finalize(). Program tries to read some
memory location and suffer run time exception.
Only suggestion is to try to send it to Microsoft.
Why is this happening?
Everything (seems to) works fine I comment line Py_Finalize(), but I
know that this is not a real solution.

Does anybode have a clue what is happening?

Thanks


More information about the Python-list mailing list

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4