Created
May 16, 2010 13:08
-
-
Save snim2/402869 to your computer and use it in GitHub Desktop.
Find exception names in a Python module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import compiler.ast | |
import compiler.visitor as visitor | |
__author__ = 'Sarah Mount <s.mount@wlv.ac.uk>' | |
__date__ = 'May 2010' | |
class ExceptionFinder(visitor.ASTVisitor): | |
"""List all exceptions raised by a module. | |
""" | |
def __init__(self, filename): | |
visitor.ASTVisitor.__init__(self) | |
self.filename = filename | |
self.exns = set() | |
return | |
def __visitName(self, node): | |
"""Should not be called by generic visit, otherwise every name | |
will be reported as an exception type. | |
""" | |
self.exns.add(node.name) | |
return | |
def __visitCallFunc(self, node): | |
"""Should not be called by generic visit, otherwise every name | |
will be reported as an exception type. | |
""" | |
self.__visitName(node.node) | |
return | |
def visitRaise(self, node): | |
"""Visit a raise statement. | |
Cheat the default dispatcher. | |
""" | |
if isinstance(node.expr1, compiler.ast.Name): | |
self.__visitName(node.expr1) | |
elif isinstance(node.expr1, compiler.ast.CallFunc): | |
self.__visitCallFunc(node.expr1) | |
return | |
if __name__ == '__main__': | |
import sys | |
if len(sys.argv) <= 1: | |
print 'Give me a file name!' | |
print 'Looking for exception types in: %s' % sys.argv[1] | |
lint = ExceptionFinder(sys.argv[1]) | |
compiler.walk(compiler.parseFile(sys.argv[1]), | |
lint, | |
walker=lint, | |
verbose=5) | |
for exn in lint.exns: | |
print '%s:%s is an exception type' % (sys.argv[1], exn) |
It's been a very long time since I looked at this code, so I'm not sure
that I can be of much help. However, I found this blog post:
https://deepsource.io/blog/python-asts-by-building-your-own-linter/
If you haven't seen it before, I think it gives sure a useful introduction
to the `ast` module.
…On Sat, 30 Jul 2022, 19:52 Levi, ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
@snim2 <https://github.com/snim2> Thanks for the reply. This seems so
much harder then I expected. It seems by default the AST only parses what
it sees. It doesn't drill down into other function calls.
I'm trying to walk all code that might be called. Like both sides of an if
statement. To record what exceptions might be raised from a given starting
point.
Take this example
def raise_a():
raise Exception('testing')
def raise_b():
raise TypeError()
def test_raise():
raise_a()
raise_b()
raise ValueError()
If I walk test_raise then I see the calls to raise_a() and raise_b() but
ast.walk does not walk the AST for the raise_a and raise_b.
I have actually found a hacky workaround that does work. But I'm having
trouble with my call to HTTPConnection.getresponse.
I cant start a new gist if you are interested in what I am up to.
—
Reply to this email directly, view it on GitHub
<https://gist.github.com/402869#gistcomment-4250515>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAAX3CWGE2VJBFXS5DZ2IZDVWV2XLANCNFSM55DNEFWQ>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@snim2 Thanks for the reply. This seems so much harder then I expected. It seems by default the AST only parses what it sees. It doesn't drill down into other function calls.
I'm trying to walk all code that might be called. Like both sides of an
if
statement. To record what exceptions might be raised from a given starting point.Take this example
If I walk
test_raise
then I see the calls toraise_a()
andraise_b()
butast.walk
does not walk the AST for theraise_a
andraise_b
.I have actually found a hacky workaround that does work. But I'm having trouble with my call to
HTTPConnection.getresponse
.I cant start a new gist if you are interested in what I am up to.