PyChecker does STATIC analysis?

B

beliavsky

If I run PyChecker on the following program, stored in xtry.py,

m = 10000000
k = 0
for i in xrange(m):
k = k + i
print k
x = range(3)
print x[3]

the output is

49999995000000

Warnings...

xtry:1: NOT PROCESSED UNABLE TO IMPORT
Processing xtry...
Caught exception importing module xtry:
File "H:\Energy\rao\python\Lib\site-packages\pychecker\checker.py",
line 530, in setupMainCode()
module = imp.load_module(self.moduleName, file, filename, smt)
File "xtry.py", line 7
print x[3]
IndexError: list index out of range


I am surprised that PyChecker actually needs to execute the code block

for i in xrange(m):
k = k + i
print k

before finding the out-of-bounds error. I have seen PyChecker
described as a "static analysis" tool, and it does find some problems
that such a tool ought to. But it seems that for the simple program I
have shown, it basically runs the program, and I could just run the
Python program to find the same errors. An aspect of Python
programming that sometimes frustrates me is that a program will run,
perhaps for a few minutes, before stopping because of a misspelled
variable or an out-of-bounds variable. It seems that currently,
PyChecker will not solve this problem. I wish there were a tool that
did (and would be willing to pay for it).

The Lahey/Fujitsu Fortran 95 compiler is able to catch the
out-of-bounds error at COMPILE time for an analogous Fortran program.
 
D

Diez B. Roggisch

before finding the out-of-bounds error. I have seen PyChecker
described as a "static analysis" tool, and it does find some problems
that such a tool ought to. But it seems that for the simple program I
have shown, it basically runs the program, and I could just run the
Python program to find the same errors. An aspect of Python
programming that sometimes frustrates me is that a program will run,
perhaps for a few minutes, before stopping because of a misspelled
variable or an out-of-bounds variable. It seems that currently,
PyChecker will not solve this problem. I wish there were a tool that
did (and would be willing to pay for it).

The Lahey/Fujitsu Fortran 95 compiler is able to catch the
out-of-bounds error at COMPILE time for an analogous Fortran program.

while there might be not-so-obvious cases that can be found using static
analysis, the halting problem also applies here: There is no such thing as
a automated analysis that finds out if your prgram is going to die or not.

So automatically running a program as part of the analysis is as valid as
any other technique - as long as there are no permantent sideeffects (you
don't want your partitioning code accidentially ruin your hd while
developing...)

Given the fact that nobody prevents you from doing this:
xrange = lambda x: [1,2,3,4,5]
x = range(3)
print x[3]
4

you can fool a static analysis, I wont blame Pychecker too much.
 
Y

Yermat

If I run PyChecker on the following program, stored in xtry.py,

m = 10000000
k = 0
for i in xrange(m):
k = k + i
print k
x = range(3)
print x[3]


the output is

49999995000000

Warnings...

xtry:1: NOT PROCESSED UNABLE TO IMPORT
Processing xtry...
Caught exception importing module xtry:
File "H:\Energy\rao\python\Lib\site-packages\pychecker\checker.py",
line 530, in setupMainCode()
module = imp.load_module(self.moduleName, file, filename, smt)
File "xtry.py", line 7
print x[3]
IndexError: list index out of range


I am surprised that PyChecker actually needs to execute the code block

for i in xrange(m):
k = k + i
print k

before finding the out-of-bounds error. I have seen PyChecker
described as a "static analysis" tool, and it does find some problems
that such a tool ought to. But it seems that for the simple program I
have shown, it basically runs the program, and I could just run the
Python program to find the same errors. An aspect of Python
programming that sometimes frustrates me is that a program will run,
perhaps for a few minutes, before stopping because of a misspelled
variable or an out-of-bounds variable. It seems that currently,
PyChecker will not solve this problem. I wish there were a tool that
did (and would be willing to pay for it).

The Lahey/Fujitsu Fortran 95 compiler is able to catch the
out-of-bounds error at COMPILE time for an analogous Fortran program.

You're right but now try on :

def main():
m = 10000000
k = 0
for i in xrange(m):
k = k + i
print k
x = range(3)
print x[3]

if __name__=='__main__':
main()

And it should work !
PyChecker import your file and does not just do a source checking...
 
S

Skip Montanaro

beliavsky> If I run PyChecker on the following program, stored in xtry.py,

beliavsky> m = 10000000
beliavsky> k = 0
beliavsky> for i in xrange(m):
beliavsky> k = k + i
beliavsky> print k
beliavsky> x = range(3)
beliavsky> print x[3]

beliavsky> the output is

beliavsky> 49999995000000

beliavsky> Warnings...

beliavsky> xtry:1: NOT PROCESSED UNABLE TO IMPORT
...

beliavsky> I am surprised that PyChecker actually needs to execute the
beliavsky> code block

beliavsky> for i in xrange(m):
beliavsky> k = k + i
beliavsky> print k

beliavsky> before finding the out-of-bounds error.

Try modifying your code to this:

if __name__ == "__main__":
m = 10000000
k = 0
for i in xrange(m):
k = k + i
print k
x = range(3)
print x[3]

PyChecker simply imports your module. Importing a module causes the code at
the top level to be executed.

beliavsky> The Lahey/Fujitsu Fortran 95 compiler is able to catch the
beliavsky> out-of-bounds error at COMPILE time for an analogous Fortran
beliavsky> program.

Python is just slightly more dynamic than Fortran, and I'm sure the
Lahey/Fujitsu compiler has had a few more man-years of development put into
it than PyChecker. As an example of the former point, note that PyChecker
can't assume that the output of range(3) is a list of 3 elements. Another
module may have modified builtins before the code was executed.

Skip
 

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

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top