Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #36816 > unrolled thread

Finding the variables (read or write)

Started byservekarimi@gmail.com
First post2013-01-14 11:48 -0800
Last post2013-01-15 08:46 +1100
Articles 5 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  Finding the variables (read or write) servekarimi@gmail.com - 2013-01-14 11:48 -0800
    Re: Finding the variables (read or write) Chris Angelico <rosuav@gmail.com> - 2013-01-15 08:28 +1100
    Re: Finding the variables (read or write) Chris Kaynor <ckaynor@zindagigames.com> - 2013-01-14 13:37 -0800
    Re: Finding the variables (read or write) Terry Reedy <tjreedy@udel.edu> - 2013-01-14 16:42 -0500
    Re: Finding the variables (read or write) Chris Angelico <rosuav@gmail.com> - 2013-01-15 08:46 +1100

#36816 — Finding the variables (read or write)

Fromservekarimi@gmail.com
Date2013-01-14 11:48 -0800
SubjectFinding the variables (read or write)
Message-ID<46c8a630-de27-41dc-8b8b-1951ba747447@googlegroups.com>
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

[toc] | [next] | [standalone]


#36821

FromChris Angelico <rosuav@gmail.com>
Date2013-01-15 08:28 +1100
Message-ID<mailman.520.1358198911.2939.python-list@python.org>
In reply to#36816
On Tue, Jan 15, 2013 at 6:48 AM,  <servekarimi@gmail.com> 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

[toc] | [prev] | [next] | [standalone]


#36822

FromChris Kaynor <ckaynor@zindagigames.com>
Date2013-01-14 13:37 -0800
Message-ID<mailman.521.1358199482.2939.python-list@python.org>
In reply to#36816

[Multipart message — attachments visible in raw view] — view raw

On Mon, Jan 14, 2013 at 1:28 PM, Chris Angelico <rosuav@gmail.com> wrote:

> On Tue, Jan 15, 2013 at 6:48 AM,  <servekarimi@gmail.com> 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
>

[toc] | [prev] | [next] | [standalone]


#36823

FromTerry Reedy <tjreedy@udel.edu>
Date2013-01-14 16:42 -0500
Message-ID<mailman.522.1358199785.2939.python-list@python.org>
In reply to#36816
On 1/14/2013 4:28 PM, Chris Angelico wrote:
> On Tue, Jan 15, 2013 at 6:48 AM,  <servekarimi@gmail.com> 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

[toc] | [prev] | [next] | [standalone]


#36824

FromChris Angelico <rosuav@gmail.com>
Date2013-01-15 08:46 +1100
Message-ID<mailman.523.1358200002.2939.python-list@python.org>
In reply to#36816
On Tue, Jan 15, 2013 at 8:37 AM, Chris Kaynor <ckaynor@zindagigames.com> 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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web