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

Dino Viehland dinov at microsoft.com
Wed Mar 18 16:50:00 CET 2009


The documentation for using the hosting APIs is available here: http://dlr.codeplex.com/Wiki/View.aspx?title=Docs%20and%20specs&referringTitle=Home as "dlr-hosting-spec.doc"


> -----Original Message-----
> From: users-bounces at lists.ironpython.com [mailto:users-
> bounces at lists.ironpython.com] On Behalf Of BlackMan890
> Sent: Wednesday, March 18, 2009 12:48 AM
> To: users at lists.ironpython.com
> Subject: [IronPython] How do I run python functions and make the python
> code remember variables between calls from C#?
> 
> 
> 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.
> 
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com



More information about the Ironpython-users mailing list