[IronPython] How to get ToString() working, and questions about GetSysModule() and GetClrModule()

Dino Viehland dinov at microsoft.com
Mon Sep 21 13:08:44 PDT 2009


I'm actually surprised this works.  In theory calling GetClrModule should never effect any other code.  In practice there's currently a bug in that we mark a default scope as having done import clr.  But you're executing the code in a newly created scope which should have its own setting as to whether or not import clr has happened; in general each scope tracks this independently of all other scopes and there can be as many scopes as you'd like.

Basically GetClrModule() and GetSysModule() are supposed to just be a convenience functions so that you can get the module objects and invoke methods against it using ObjectOperations.  Actually marking a module as having done "import clr" is just setting a bit on whatever Scope object the code is executing against.  And in this case you haven't done anything to mark _scope as having done an import clr.

-----Original Message-----
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Thomas Gagne
Sent: Monday, September 21, 2009 12:15 PM
To: Discussion of IronPython
Subject: Re: [IronPython] How to get ToString() working, and questions about GetSysModule() and GetClrModule()

I /think/ it may be working.  At least my unit tests are working.  The 
code below runs the unit test, the unit test is at the bottom.

        public DynamicAgent(ScriptEngine aScriptEngine)
        {
            _engine = aScriptEngine;
            _engine.Runtime.GetClrModule();
            _scope = _engine.CreateScope();
           
        }

        public virtual object PrepareString(string anExpression)
        {
            return _engine.CreateScriptSourceFromString(anExpression, 
SourceCodeKind.Expression);
        }

        public virtual object Evaluate(ScriptSource source)
        {
            return source.Execute(_scope);
        }

        public virtual object EvaluateString(string anExpression)
        {
            var source = PrepareString(anExpression);
            return Evaluate(source);
        }
----

        [TestMethod]
        public void pythonToStringTest()
        {
            var agent = new PythonAgent();
            agent.SetVariable("aMoney", 1234.56M);
            var result = 
agent.EvaluateString("aMoney.ToString('#,##0.00')");
            Assert.AreEqual("1,234.56", result);
        }

I'm not convinced if it's working because I'm unsure what GetClrModule() 
precisely does or how many scopes there can be, or if there's really 
only one scope and everything is additive, or...

Dino Viehland wrote:
> Because this is on a per-module basis calling GetClrModule() won't affect the module you later want to do .ToString from.
>
> I think the easiest way to do this would be to do:
>
> var code = Engine.CreateScriptSourceFromString("import clr", SourceCodeKind.Statements).Compile();
>
> and whenever you want a module to use import clr do:
>
> code.Execute(_scope);
>
> If you open a bug on CodePlex though we could look at adding an extension method like "scope.EnableClr()" or something along those lines that makes this simpler.
>
>
>
> -----Original Message-----
> From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Thomas Gagne
> Sent: Monday, September 21, 2009 12:00 PM
> To: users at lists.ironpython.com
> Subject: [IronPython] How to get ToString() working, and questions about GetSysModule() and GetClrModule()
>
> We've hosted Python inside our C# application and all seems to work 
> well.  Recently, one of our programmers has asked to use ToString inside 
> a python expression.  I read online we need to import the "clr" to make 
> that happen.
>
> The code for kicking it off looks like:
>
> _engine = Python.CreateEngine();
> _scope = _engine.CreateScope();
>
> I'm curious, if I want the Clr already-imported to a program can ask for 
> a fixed-point's ToString("C"), am I supposed to be able to use the 
> GetClrModule()?
>
> Since both it and CreateScope() return scopes I thought I'd try using 
> the scope returned by GetClrModule() to evaluate an expression.  
> Suddenly, "True" no longer evaluated.  It had no binding.
>
> Signed confused.
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>   
_______________________________________________
Users mailing list
Users at lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com


More information about the Users mailing list