Finding the variables (read or write)
I'd like to develop a small debugging tool for python programs.In Dynamic Slicing How can I find the variables that are accessed in a statement? And find the type of access (read or write) for those variables (in Python). ### Write: A statement can change the program state. ### Read : A statement can read the program state . **For example in these 4 lines we have: (1) x = a+b=> write{x} & read{a,b} (2) y=6=> write{y} & read{} (3) while(n>1) => write{} & read{n} (4) n=n-1 => write{n} & read{n} @@If I use dis(disassembler) How can I get output of dis in python as dictionary? Thanks -- http://mail.python.org/mailman/listinfo/python-list
Re: Finding the variables (read or write)
On Tue, Jan 15, 2013 at 6:48 AM, wrote: > I'd like to develop a small debugging tool for python programs.In Dynamic > Slicing How can I find the variables that are accessed in a statement? And > find the type of access (read or write) for those variables (in Python). > ### Write: A statement can change the program state. > ### Read : A statement can read the program state . > **For example in these 4 lines we have: > (1) x = a+b=> write{x} & read{a,b} > (2) y=6=> write{y} & read{} > (3) while(n>1) => write{} & read{n} > (4) n=n-1 => write{n} & read{n} An interesting question. What's your definition of "variable"? For instance, what is written and what is read by this statement: self.lst[2] += 4 Is "self.lst" considered a variable? (In C++ etc, this would be a member function manipulating an instance variable.) Or is "self" the variable? And in either case, was it written to? What about: self.lst.append(self.lst[-1]+self.lst[-2]) (which might collect Fibonacci numbers)? ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: Finding the variables (read or write)
On Mon, Jan 14, 2013 at 1:28 PM, Chris Angelico wrote: > On Tue, Jan 15, 2013 at 6:48 AM, wrote: > > I'd like to develop a small debugging tool for python programs.In > Dynamic Slicing How can I find the variables that are accessed in a > statement? And find the type of access (read or write) for those variables > (in Python). > > ### Write: A statement can change the program state. > > ### Read : A statement can read the program state . > > **For example in these 4 lines we have: > > (1) x = a+b=> write{x} & read{a,b} > > (2) y=6=> write{y} & read{} > > (3) while(n>1) => write{} & read{n} > > (4) n=n-1 => write{n} & read{n} > > An interesting question. What's your definition of "variable"? For > instance, what is written and what is read by this statement: > > self.lst[2] += 4 > > Is "self.lst" considered a variable? (In C++ etc, this would be a > member function manipulating an instance variable.) Or is "self" the > variable? And in either case, was it written to? What about: > > self.lst.append(self.lst[-1]+self.lst[-2]) > > (which might collect Fibonacci numbers)? > And those aren't even covering the case that a, normally non-mutating, method actually mutates. Consider the following class (untested): class Test(object): def __init__(self, value): self.value = value self.adds = 0 def __add__(self, other): self.adds += 1 other.adds += 1 return Test(self.value + other.value) With that class, x = a + b would mutate x, a, and b, presuming a and b are instances of Test. > > ChrisA > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: Finding the variables (read or write)
On 1/14/2013 4:28 PM, Chris Angelico wrote: On Tue, Jan 15, 2013 at 6:48 AM, wrote: I'd like to develop a small debugging tool for python programs.In Dynamic Slicing How can I find the variables that are accessed in a statement? And find the type of access (read or write) for those variables (in Python). ### Write: A statement can change the program state. ### Read : A statement can read the program state . **For example in these 4 lines we have: (1) x = a+b=> write{x} & read{a,b} (2) y=6=> write{y} & read{} (3) while(n>1) => write{} & read{n} (4) n=n-1 => write{n} & read{n} I would try compiling the source code to an ast (abstract syntax tree). See the ast module for how to do that and how to 'read' the resulting tree. An interesting question. What's your definition of "variable"? For instance, what is written and what is read by this statement: self.lst[2] += 4 Is "self.lst" considered a variable? (In C++ etc, this would be a member function manipulating an instance variable.) Or is "self" the variable? And in either case, was it written to? 'self' is read, 'lst' is written to. What about: self.lst.append(self.lst[-1]+self.lst[-2]) (which might collect Fibonacci numbers)? 'self' read, 'lst' read and written. Knowing that for non-builtins is another matter ;-). -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: Finding the variables (read or write)
On Tue, Jan 15, 2013 at 8:37 AM, Chris Kaynor wrote: > And those aren't even covering the case that a, normally non-mutating, > method actually mutates. If it's static analysis, I'd quietly ignore those sorts of cases. Anything can be changed any time, including stuff that's completely unrelated to what you're working on. Now, if the OP just wants to know what names get referenced (without distinguishing reads from writes), that's quite possible, and in fact easy - if you're willing to analyze a whole function instead of a single statement. >>> def foo(): x=y+1 >>> foo.__code__.co_varnames ('x',) >>> foo.__code__.co_names ('y',) The first one is the ones that get assigned to (not quite the same as "written to"), the second is ones that don't. Well, more or less. In simple cases. ChrisA -- http://mail.python.org/mailman/listinfo/python-list