473,394 Members | 1,770 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Python Code Auditing Tool

Does anybody know of a tool that can tell me all possible exceptions that
might occur in each line of code? What I'm hoping to find is something
like the following:

given all necessary python source and a given line ( my.py:40 ) it would
generate a list of possible exception classes sorted by function
(preferably in a tree).

Example:
------------------

my.py:40 | parsestring(genstring())

Possible Exceptions:

-def parsestring():
InvalidCharacterException
EmptyStringException
-class string, def split():
(All Exceptions that might occur directly in string.split() that are
not caught by parsestring())
(All functions called by string.split() and their exceptions and sub-
functions)
-def genstring():
SomeException
...

--------------------

This would be extremely useful for deciding how to write try: except
blocks and in figuring out what all possible errors that might occur would
be.

-Robey Holderith

Jul 18 '05 #1
9 1394
Robey Holderith <ro***@flaminglunchbox.net> writes:
Does anybody know of a tool that can tell me all possible exceptions that
might occur in each line of code? What I'm hoping to find is something
like the following:


That is impossible. The parameter to the raise statement is a class
object, which can be anything. I.e. you could say:

class ex1: pass
class ex2: pass

if something(): my_ex = ex1
else: my_ex = ex2

raise my_ex # static tool can't know what exception gets raised here.
Jul 18 '05 #2
On Tue, 01 Feb 2005 21:52:28 -0800, Paul Rubin wrote:
Robey Holderith <ro***@flaminglunchbox.net> writes:
Does anybody know of a tool that can tell me all possible exceptions that
might occur in each line of code? What I'm hoping to find is something
like the following:


That is impossible. The parameter to the raise statement is a class
object, which can be anything. I.e. you could say:

class ex1: pass
class ex2: pass

if something(): my_ex = ex1
else: my_ex = ex2

raise my_ex # static tool can't know what exception gets raised here.


I suppose that I am willing to lessen my expectations from _all_ to most.
;-) Regarding your example I could also do:

if something():
def nothing(): return 0
else:
def nothing(): return 1

But this doesn't stop IDEs from attempting to do auto-completion. I'm not
trying to find hidden exceptions... just trying to easily get an idea of
what could go wrong on each line of code.

-Robey Holderith

Jul 18 '05 #3
> I suppose that I am willing to lessen my expectations from _all_ to most.
;-) Regarding your example I could also do:

if something():
def nothing(): return 0
else:
def nothing(): return 1

But this doesn't stop IDEs from attempting to do auto-completion. I'm not
trying to find hidden exceptions... just trying to easily get an idea of
what could go wrong on each line of code.


There is AFAIK only one language that this can de accomplished - java, and
that's because of these checked exceptions of theirs. But checked
exceptions are considered harmful:

http://www.gcek.net/ref/books/sw/ooad/tip/#_Toc41169682

I totally agree with that - in java, I tend to throw SystemExceptions to rid
myself of endless try/catch clauses that obscure the real problem.

So - there is no way of knowing this. The only thing I can think of is to
keep some docs around that specify what exceptions to be expected, and that
tool of yours could try and see if it can identify a function/method by
name and notify you of the possible exceptions thrown. Might actually work
out quite well for the standardlib, if one does the work for annotating all
functions/methods properly.

--
Regards,

Diez B. Roggisch
Jul 18 '05 #4
Diez B. Roggisch wrote:
I suppose that I am willing to lessen my expectations from _all_ to most.
;-) Regarding your example I could also do:

<

<snip>
There is AFAIK only one language that this can de accomplished - java, and
that's because of these checked exceptions of theirs. But checked
exceptions are considered harmful:

http://www.gcek.net/ref/books/sw/ooad/tip/#_Toc41169682

I totally agree with that - in java, I tend to throw SystemExceptions to rid
myself of endless try/catch clauses that obscure the real problem.

<snip>
Hello,

I'm afraid that the only reliable way to gather what exceptions are
raised is to read docs and/or come up with test cases. This has been a
bugbear of mine in Python as it's not common to find a nice :Exceptions:
IOError <desc>, IllegalArgumentError <desc> type of description in the docs.

However if you want an incomplete test, you could parse the code and
check for raises and retrieve the class name of the exception - however
this would be patchy at best. Therefore it would sort of negate the
point of doing the analysis in the first place.

Even in Java you cannot find every exception that will be
thrown, only 'checked' exceptions but this is a percentage of all the
exceptions (BTW why do you throw SystemException - it's a CORBA
exception! OK, it's a runtime exception but why not just simply extend
RuntimeException?). Also, if someone ever puts - catch (Exception e){}
in their code they deserve to be kneecapped, IMHO the fault is with
sloppy coding not with the supplied tools.

Unfortunately its docs and testing again, that's why we get paid (if
you're doing a job) or not paid (if you're doing it for fun!). Although
one language which comes closer is Eiffel which has require and ensure
clauses on every method (following Meyer's Programming by contract
philosophy).

Cheers,

Neil

--

Neil Benn
Senior Automation Engineer
Cenix BioScience
BioInnovations Zentrum
Tatzberg 46
D-01307
Dresden
Germany

Tel : +49 (0)351 4173 154
e-mail : be**@cenix-bioscience.com
Cenix Website : http://www.cenix-bioscience.com

Jul 18 '05 #5
Hi,
I'm afraid that the only reliable way to gather what exceptions are
raised is to read docs and/or come up with test cases. This has been a
bugbear of mine in Python as it's not common to find a nice :Exceptions:
IOError <desc>, IllegalArgumentError <desc> type of description in the
docs.

However if you want an incomplete test, you could parse the code and
check for raises and retrieve the class name of the exception - however
this would be patchy at best. Therefore it would sort of negate the
point of doing the analysis in the first place.
I don't want that - the OP wants. I agree with you.
Even in Java you cannot find every exception that will be
thrown, only 'checked' exceptions but this is a percentage of all the
exceptions (BTW why do you throw SystemException - it's a CORBA
exception! OK, it's a runtime exception but why not just simply extend
RuntimeException?). Also, if someone ever puts - catch (Exception e){}
in their code they deserve to be kneecapped, IMHO the fault is with
sloppy coding not with the supplied tools.
Most probably I throw RuntimeException - that was out of my head, I luckily
I haven't been coding java too much lately :)
Unfortunately its docs and testing again, that's why we get paid (if
you're doing a job) or not paid (if you're doing it for fun!). Although
one language which comes closer is Eiffel which has require and ensure
clauses on every method (following Meyer's Programming by contract
philosophy).


Full ack again.

--
Regards,

Diez B. Roggisch
Jul 18 '05 #6
Does anybody know of a tool that can tell me all possible exceptions
that might occur in each line of code? What I'm hoping to find is
something like the following:

Paul> That is impossible. The parameter to the raise statement is a
Paul> class object, which can be anything.

Sure, but in all but the rarest of cases the first arg to raise is a
specific exception, probably one of the standard exceptions. In the Python
code in the distribution (ignoring the test directory where all sorts of
mischief is done to stress things), here are the most common words following
"raise" where "raise" is the first word on the line:

% find . -name '*.py' \ | egrep -v '\./test' \
| xargs egrep '^ *raise ' \
| awk '{print $3}' \
| sed -e 's/[(,].*//' \
| sort \
| uniq -c \
| sort -rn \
| head -15

246 ValueError
227 aetools.Error
216 Error
124 TypeError
101 error
75 RuntimeError
53 IOError
36 NotImplementedError
36 ImportError
36 EOFError
31 SyntaxError
23 KeyError
23 AttributeError
22 DistutilsPlatformError
21 UnicodeError

Without checking, my guess is that #5 ("error") is one of a handful of
exception classes defined at module scope (ftplib, anydbm, sre_constants,
poplib, among others all define such an exception class), and not a variable
that accepts multiple values as in your example.

In short, while not perfect, simply grepping for '^ *(class|def|raise) ' and
printing the first and second words of each output line would probably give
you a pretty good idea of what gets raised where.

Skip
Jul 18 '05 #7
In article <ma***************************************@python. org>,
System Administrator <sk**@pobox.com> wrote:
>> Does anybody know of a tool that can tell me all possible exceptions
>> that might occur in each line of code? What I'm hoping to find is
>> something like the following:


Paul> That is impossible. The parameter to the raise statement is a
Paul> class object, which can be anything.

Sure, but in all but the rarest of cases the first arg to raise is a
specific exception, probably one of the standard exceptions. In the Python
code in the distribution (ignoring the test directory where all sorts of
mischief is done to stress things), here are the most common words following
"raise" where "raise" is the first word on the line:

% find . -name '*.py' \
> | egrep -v '\./test' \
> | xargs egrep '^ *raise ' \
> | awk '{print $3}' \
> | sed -e 's/[(,].*//' \
> | sort \
> | uniq -c \
> | sort -rn \
> | head -15

246 ValueError
227 aetools.Error
216 Error
124 TypeError
101 error
75 RuntimeError
53 IOError
36 NotImplementedError
36 ImportError
36 EOFError
31 SyntaxError
23 KeyError
23 AttributeError
22 DistutilsPlatformError
21 UnicodeError


It's kind of interesting (scarry?) that in roughly 20% of the cases
nothing more specific than Error is raised.
Jul 18 '05 #8
Roy Smith wrote:
Skip Montanaro wrote:
246 ValueError
227 aetools.Error
216 Error
124 TypeError
101 error
75 RuntimeError
53 IOError
36 NotImplementedError
36 ImportError
36 EOFError
31 SyntaxError
23 KeyError
23 AttributeError
22 DistutilsPlatformError
21 UnicodeError


It's kind of interesting (scarry?) that in roughly 20% of the cases
nothing more specific than Error is raised.


(In case someone reading doesn't know) there isn't actually
an "Error" in the standard set of exceptions. It seems
likely that pretty much all of those uses are actually
module-specific Errors, and as such they are probably much
more specific than the unadorned name might imply.

Also, when one is trying to pick an appropriate exception
to raise, it is often the case that none of the standard
exceptions seems appropriate. In those cases (although I
personally would prefer a different name than "Error")
there's often no good alternative to making your own
unique module-specific Exception subclass.

-Peter
Jul 18 '05 #9
246 ValueError
227 aetools.Error
216 Error
124 TypeError
101 error
75 RuntimeError
53 IOError
36 NotImplementedError
36 ImportError
36 EOFError
31 SyntaxError
23 KeyError
23 AttributeError
22 DistutilsPlatformError
21 UnicodeError


Roy> It's kind of interesting (scarry?) that in roughly 20% of the cases
Roy> nothing more specific than Error is raised.

Not really. You might have code in a module named mod like this:

class Error(Exception): pass

then later:

def foo(...):
... blah blah blah ...
if condition:
raise Error, "hey dummy!"

The caller might look like:

import mod

...

try:
mod.foo()
except mod.Error, msg:
print msg

That said, the tendency for many newer modules seems to be to discriminate
exceptions based on type (look at urllib2 for example) while older modules
tended to have just a single exception they raised (look at ftplib). I
suspect that has something to do with whether the module was originally
written before or after Python introduced class exceptions.

Skip
Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Joakim Persson | last post by:
Hello all. I am involved in a project where we have a desire to improve our software testing tools, and I'm in charge of looking for solutions regarding the logging of our software (originating...
0
by: RdR | last post by:
Is it true that DB2 will have an auditing tool to be used for auditing requirements such as Sarbanes-Oxley, etc? Something called Websphere Compliance Auditing? Rumours have it that IBM has a new...
35
by: John Coleman | last post by:
Greetings, I have a rough classification of languages into 2 classes: Zen languages and tool languages. A tool language is a language that is, well, a *tool* for programming a computer. C is the...
122
by: Edward Diener No Spam | last post by:
The definition of a component model I use below is a class which allows properties, methods, and events in a structured way which can be recognized, usually through some form of introspection...
6
by: Rico | last post by:
Hello, I'm creating an audit table and associated triggers to be able to capture any updates and deletes from various tables in the database. I know how to capture the records that have been...
41
by: Carl J. Van Arsdall | last post by:
Hey everyone, I have a question about python threads. Before anyone goes further, this is not a debate about threads vs. processes, just a question. With that, are python threads reliable? Or...
8
by: Ronald S. Cook | last post by:
On a new project, my management thought it would be a good idea to utilize a 3rd party tool to generate the database, and middle-tier classes. We chose DeKlarit and I went to work. While...
0
by: corey | last post by:
Secure Bytes audit and vulnerability assessment software Secure Auditor named “Versatile tool” and earn “Five Star Ratings” in SC Magazine Group Test Secure Bytes is really pleased to share this...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.