[Python.NET] Problem passing parameters to a python function from C#
Tiago Matias
tiago.matias at gmail.com
Wed May 10 22:54:55 CEST 2006
Hi all!
I'm trying to invoke a python function from a .NET application. The C# code
is this:
string templateFile = "Simple.tpl";
PythonEngine.Initialize();
PyObject module = PythonEngine.ImportModule("Engine");
PyObject method = module.GetAttr("FillTemplate");
PyObject result = method.Invoke(PyObject.FromManagedObject(templateFile));
Console.WriteLine(result.ToString());
and the python module is simply:
from Cheetah.Template import Template
def FillTemplate(templateName):
template = Template(file=templateName)
return template
Now, the problem is the parameter. If I hardcode "Simple.tpl" in the
constructor everything works as expected. Also, if I change the python code
to "return templateName" I get "Simple.tpl" back in the C# code.
But, if I run the code above a PythonException is thrown....I've also tried
to place a try...except block around the code but it didn't caught any
exception... I suspect that I must be calling the PyObject.Invoke() function
with the wrong arguments...
Essentially the purpose of this is to generate C# code with the help of
cheetah templates. The ideia is to construct an object containing the
metadata and then pass it to cheetah to generate code.
But I can't seem to be able to pass a simple string!
Any help would be grealty appreciated! Btw, bear in mind that this is my
first attempt with python... so the question maybe stupid ;)
Thanks in advance!
Tiago
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/pythondotnet/attachments/20060510/24d833b2/attachment.htm
More information about the PythonDotNet
mailing list