[IronPython] How do I run python functions and make the python code remember variables between calls from C#?

BlackMan890 blackman at simnet.is
Wed Mar 18 00:47:30 PDT 2009


Hi there.

When I run the following .py from C#:

--- test.py ----------------

global crap
crap = None

def set_crap():
	crap = "yay"

def return_crap():
	return crap
---------------------------

And run the functions set_crap and return_crap from C# using the following
code:

--- program.cs ----------------
--- Code is not being displayed right here, view the uploaded file I sent
---
-------------------------------

static void Main(string[] args)
{
	ScriptEngine engine = Python.CreateEngine();
	ScriptSource source = engine.CreateScriptSourceFromFile("test.py");
	ScriptScope scope = engine.CreateScope();

	try
	{

		source.Execute(scope);

	}
	catch (Exception ex)
	{
		ExceptionOperations eo = engine.GetService<ExceptionOperations>(); 
		Console.WriteLine(eo.FormatException(ex));
	}

	SetData = scope.GetVariable<Func>("set_crap");
	GetData = scope.GetVariable<Func>("return_crap");

	SetData();
	object temp = GetData();

	Console.ReadLine();
}

static Func SetData;
static Func GetData;
-------------------------------

The code compiles and runs but "temp" is always null.
How would one go of running IronPython while maintaining all variables
inside python?
Cause the way I see it, whenever I run a function, all variables are
resettet.


Sincerely:
Jonatan Nilsson
Iceland


P.S.
I am using IronPython 2.0.1

P.P.S.
Your documentation for C# using IronPython 2.0.1 are nonexistant :S Where
are the documentation on using IronPython 2.0.1 in C#?
http://www.nabble.com/file/p22570584/program.cs program.cs 
-- 
View this message in context: http://www.nabble.com/How-do-I-run-python-functions-and-make-the-python-code-remember-variables-between-calls-from-C---tp22570584p22570584.html
Sent from the IronPython mailing list archive at Nabble.com.



More information about the Users mailing list