Finding the variables (read or write)

S

servekarimi

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
 
C

Chris Angelico

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
 
C

Chris Kaynor

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.
 
T

Terry Reedy

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 ;-).
 
C

Chris Angelico

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.
('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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top