[IronPython] Accessing scope variables

Dino Viehland dinov at microsoft.com
Thu Aug 6 21:10:45 CEST 2009


The only way I can think of doing this today would be to use our Parser
And PythonWalker classes to parse the func def and then walk it and look
for NameExpressions.

You'd also need to look for GlobalStatement's, AssignmentStatement,
and AugmentedAssignmentStatement to know if it was a global or not.


OTOH if we fixed this bug (which you can vote on):
http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=15398

you could walk the function objects, get their func_code, and then
look at co_names:

x = 42
def f():
    print x
    y = 42

c = f.func_code
c.co_varnames
('y',)
c.co_names
('x',)

Which looks like it has the global names which would be resolved against
your scope.  I've slowly been fixing more and more of function code but
haven't gotten to this one yet :(


> -----Original Message-----
> From: users-bounces at lists.ironpython.com [mailto:users-
> bounces at lists.ironpython.com] On Behalf Of Sree Pillai
> Sent: Thursday, August 06, 2009 11:57 AM
> To: users at lists.ironpython.com
> Subject: [IronPython] Accessing scope variables
>
> Hello,
>
> We are hosting IronPython in our Windows forms-based application to
> allow our customers to write scripts. We allow customers to use our own
> variables within the script.
>
> public class Variable
> {
>      string Name;
>      int Value;
>      ..
> }
>
> ..
> Variable v1 = new Variable("Length", 10);  // Create a variable with
> Name=Length and Value=10
> vars.Add(v1);
> Variable v2 = new Variable("Width", 10);
> vars.Add(v1);
> vars.Add(v2);
>
>
> We set custom variables in scriptScope like this.
>
> ..
>             foreach (IVariable aVariable in vars)
>             {
>                 scriptScope.SetVariable(aVariable.Name, aVariable);
>             }
> ..
>
> With this, user can write a function like this
>
> def CalcArea() :
>     return Length * Width
>
>
> In our application, we are dealing with hundreds of variables and
> functions. As the user deletes or modifies a Variable (here Length and
> Width), they would like to know where the variables are used. In this
> example, the variable Length and Width are used in CalcArea function. I
> can't find a way to identify the list of custom variables used within a
> function using the API.
>
> Any suggestions? Thanks a lot.
>
> Sree
> _______________________________________________
> 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