ReachableChecks.py :  » Development » PyChecker » pychecker-0.8.18 » pychecker2 » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Development » PyChecker 
PyChecker » pychecker 0.8.18 » pychecker2 » ReachableChecks.py
from pychecker2.Check import Check
from pychecker2.util import BaseVisitor
from pychecker2.Warning import Warning

from compiler import ast,walk

class ReachableCheck(Check):

    unreachable = Warning('Report unreachable code', 'Line is unreachable')

    implicitReturn = \
       Warning('Report implicit return in a function with explicit returns',
               'Function %s uses both implicit and explicit returns')

    def check(self, file, unused_checker):
        class ReturnsVisitor(BaseVisitor):
            def __init__(s):
                s.always_returns = 0    #  icky: result value by side-effect
                s.has_returns = 0

            def check_returns(s, node):
                s.always_returns = 0                
                s.visit(node)
                return s.always_returns
                
            def alternatives_with_else(s, nodes, else_):
                for n in nodes:
                    if not s.check_returns(n):
                        return
                s.always_returns = 0
                if else_:
                    s.visit(else_)

            def visitAssert(s, node):
                if isinstance(node.test, ast.Const):
                    s.always_returns = not node.test.value
                if isinstance(node.test, ast.Name):
                    if node.test.name == 'None':
                        s.always_returns = 1

            def visitReturn(s, node):
                s.always_returns = 1
                if not isinstance(node.value, ast.Const) or \
                   node.value.value is not None:
                    s.has_returns = 1
                
            def visitRaise(s, unused_node):
                s.always_returns = 1

            def visitTryExcept(s, node):
                # if body always returns, else code is unreachable
                if s.check_returns(node.body) and node.else_:
                    file.warning(node.else_.nodes[0], self.unreachable)
                s.always_returns = 0
                # no matter what happens in the try clause, it might
                # cause an exception, so check the handlers and else
                # conditions all return
                handlers = [code for exc, detail, code in node.handlers]
                s.alternatives_with_else(handlers, node.else_ or node.body)

            def visitIf(s, node):
                code = [code for cond, code in node.tests]
                s.alternatives_with_else(code, node.else_)
                    
            def visitStmt(s, node):
                for n in range(len(node.nodes) - 1):
                    if s.check_returns(node.nodes[n]):
                        file.warning(node.nodes[n + 1], self.unreachable)
                        break
                else:
                    if node.nodes and \
                       not s.check_returns(node.nodes[-1]) and \
                       isinstance(node.parent, ast.Function) and \
                       s.has_returns:
                        file.warning(node.nodes[-1], self.implicitReturn,
                                     node.parent.name)
                
            def visitFunction(s, node):
                pass
            visitClass = visitFunction

            def visitWhile(s, unused):
                # FIXME: while's may never execute and not return
                s.always_returns = 0

            visitFor = visitWhile

        for scope in file.scopes.values():
            if isinstance(scope.node, ast.Function):
                walk(scope.node.code, ReturnsVisitor())

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.