예제 #1
0
    def _onEnterNode(self, node):
        # This has many different things it deals with, so there need to be a
        # lot of branches and statements, pylint: disable=R0912,R0915

        # Also all self specific things have been done on the outside,
        # pylint: disable=R0201

        # Find nodes with only compile time constant children, these are
        # missing some obvious optimization potentially.
        if False:  # For searching only, pylint: disable=W0125
            if not node.isStatementReturn() and \
               not node.isExpressionYield() and \
               not node.isStatementRaiseException() and \
               not node.isExpressionCall() and \
               not node.isExpressionBuiltinIter1():

                children = node.getVisitableNodes()

                if children:
                    for child in children:
                        if child.isStatement() or child.isStatementsSequence():
                            break

                        if not child.isCompileTimeConstant():
                            break
                    else:
                        assert False, (node, node.parent, children)

        if node.isExpressionFunctionBody():
            if node.isUnoptimized():
                node.markAsLocalsDict()

        if node.needsLocalsDict():
            provider = node.getParentVariableProvider()

            if not provider.isCompiledPythonModule():
                provider.markAsLocalsDict()

        if node.isStatementReturn() or node.isStatementGeneratorReturn():
            search = node

            in_tried_block = False

            # Search up to the containing function, and check for a try/finally
            # containing the "return" statement.
            search = search.getParentReturnConsumer()

            if search.isExpressionGeneratorObjectBody() or \
               search.isExpressionCoroutineObjectBody():
                if in_tried_block:
                    search.markAsNeedsGeneratorReturnHandling(2)
                else:
                    search.markAsNeedsGeneratorReturnHandling(1)

        if node.isExpressionBuiltinImport() and \
           not Options.getShallFollowExtra() and \
           not Options.getShallFollowExtraFilePatterns() and \
           not Options.shallFollowNoImports() and \
           not isWhiteListedImport(node) and \
           not Plugins.suppressBuiltinImportWarning(node.getParentModule(), node.getSourceReference()):
            warning("""Unresolved '__import__' call at '%s' may require use \
of '--recurse-directory'.""" % (node.getSourceReference().getAsString()))

        if node.isExpressionFunctionCreation():
            if not node.getParent().isExpressionFunctionCall() or \
               node.getParent().getFunction() is not node:
                node.getFunctionRef().getFunctionBody().markAsNeedsCreation()

        if node.isExpressionFunctionCall():
            node.getFunction().getFunctionRef().getFunctionBody().\
              markAsDirectlyCalled()

        if node.isExpressionFunctionRef():
            function_body = node.getFunctionBody()
            parent_module = function_body.getParentModule()

            node_module = node.getParentModule()
            if node_module is not parent_module:
                function_body.markAsCrossModuleUsed()

                node_module.addCrossUsedFunction(function_body)

        if node.isStatementAssignmentVariable():
            target_var = node.getTargetVariableRef().getVariable()
            assign_source = node.getAssignSource()

            if assign_source.isExpressionOperationBinary():
                left_arg = assign_source.getLeft()

                if left_arg.isExpressionVariableRef():
                    if assign_source.getLeft().getVariable().isModuleVariable(
                    ):
                        assign_source.unmarkAsInplaceSuspect()
                    elif assign_source.getLeft().getVariable() is target_var:
                        if assign_source.isInplaceSuspect():
                            node.markAsInplaceSuspect()

        if node.isStatementPublishException():
            node.getParentStatementsFrame().markAsFrameExceptionPreserving()

        if python_version >= 300:
            if node.isExpressionYield() or node.isExpressionYieldFrom():
                search = node.getParent()

                while not search.isExpressionGeneratorObjectBody():
                    last_search = search
                    search = search.getParent()

                    if search.isStatementTry() and \
                       last_search == search.getBlockExceptHandler():
                        node.markAsExceptionPreserving()
                        break
예제 #2
0
    def _onEnterNode(self, node):
        # This has many different things it deals with, so there need to be a
        # lot of branches and statements, pylint: disable=too-many-branches,too-many-statements

        # Also all self specific things have been done on the outside,
        # pylint: disable=no-self-use
        if node.isStatementReturn() or node.isStatementGeneratorReturn():
            search = node

            in_tried_block = False

            # Search up to the containing function, and check for a try/finally
            # containing the "return" statement.
            search = search.getParentReturnConsumer()

            if (search.isExpressionGeneratorObjectBody()
                    or search.isExpressionCoroutineObjectBody()
                    or search.isExpressionAsyncgenObjectBody()):
                if in_tried_block:
                    search.markAsNeedsGeneratorReturnHandling(2)
                else:
                    search.markAsNeedsGeneratorReturnHandling(1)

        if (node.isExpressionBuiltinImport()
                and not Options.getShallFollowExtra()
                and not Options.getShallFollowExtraFilePatterns()
                and not Options.shallFollowNoImports()
                and not isWhiteListedImport(node)
                and not node.recurse_attempted
                and not Plugins.suppressBuiltinImportWarning(
                    node.getParentModule(), node.getSourceReference())):
            warning("""Unresolved '__import__' call at '%s' may require use \
of '--include-plugin-directory' or '--include-plugin-files'.""" %
                    (node.getSourceReference().getAsString()))

        if node.isExpressionBuiltinImport() and node.recurse_attempted:
            module_name = node.getImportName()

            if module_name.isCompileTimeConstant():
                imported_module_name = module_name.getCompileTimeConstant()

                if type(imported_module_name) in (str, unicode):
                    if imported_module_name:
                        imported_names.add(imported_module_name)

        if node.isExpressionFunctionCreation():
            if (not node.getParent().isExpressionFunctionCall()
                    or node.getParent().getFunction() is not node):
                node.getFunctionRef().getFunctionBody().markAsNeedsCreation()

        if node.isExpressionFunctionCall():
            node.getFunction().getFunctionRef().getFunctionBody(
            ).markAsDirectlyCalled()

        if node.isExpressionFunctionRef():
            function_body = node.getFunctionBody()
            parent_module = function_body.getParentModule()

            node_module = node.getParentModule()
            if node_module is not parent_module:
                function_body.markAsCrossModuleUsed()

                node_module.addCrossUsedFunction(function_body)

        if node.isStatementAssignmentVariable():
            target_var = node.getVariable()
            assign_source = node.getAssignSource()

            if assign_source.isExpressionOperationBinary():
                left_arg = assign_source.getLeft()

                if left_arg.isExpressionVariableRef():
                    if assign_source.getLeft().getVariable() is target_var:
                        if assign_source.isInplaceSuspect():
                            node.markAsInplaceSuspect()
                elif left_arg.isExpressionLocalsVariableRefORFallback():
                    assign_source.unmarkAsInplaceSuspect()

        if node.isStatementLocalsDictOperationSet():
            assign_source = node.getAssignSource()

            if assign_source.isExpressionOperationBinary():
                assign_source.unmarkAsInplaceSuspect()

        if python_version < 300 and node.isStatementPublishException():
            node.getParentStatementsFrame().markAsFrameExceptionPreserving()

        if python_version >= 300:
            if (node.isExpressionYield() or node.isExpressionYieldFrom()
                    or node.isExpressionYieldFromWaitable()):
                search = node.getParent()

                while (not search.isExpressionGeneratorObjectBody()
                       and not search.isExpressionCoroutineObjectBody()
                       and not search.isExpressionAsyncgenObjectBody()):

                    last_search = search
                    search = search.getParent()

                    if (search.isStatementTry()
                            and last_search == search.getBlockExceptHandler()):
                        node.markAsExceptionPreserving()
                        break
예제 #3
0
    def _onEnterNode(self, node):
        # This has many different things it deals with, so there need to be a
        # lot of branches and statements, pylint: disable=R0912,R0915

        # Also all self specific things have been done on the outside,
        # pylint: disable=R0201

        # Find nodes with only compile time constant children, these are
        # missing some obvious optimization potentially.
        if False: # For searching only, pylint: disable=W0125
            if not node.isStatementReturn() and \
               not node.isExpressionYield() and \
               not node.isStatementRaiseException() and \
               not node.isExpressionCall() and \
               not node.isExpressionBuiltinIter1():

                children = node.getVisitableNodes()

                if children:
                    for child in children:
                        if child.isStatement() or child.isStatementsSequence():
                            break

                        if not child.isCompileTimeConstant():
                            break
                    else:
                        assert False, (node, node.parent, children)

        if node.isExpressionFunctionBody():
            if node.isUnoptimized():
                node.markAsLocalsDict()

        if node.needsLocalsDict():
            provider = node.getParentVariableProvider()

            if not provider.isCompiledPythonModule():
                provider.markAsLocalsDict()

        if node.isStatementReturn() or node.isStatementGeneratorReturn():
            search = node

            in_tried_block = False

            # Search up to the containing function, and check for a try/finally
            # containing the "return" statement.
            search = search.getParentReturnConsumer()

            if search.isExpressionGeneratorObjectBody() or \
               search.isExpressionCoroutineObjectBody():
                if in_tried_block:
                    search.markAsNeedsGeneratorReturnHandling(2)
                else:
                    search.markAsNeedsGeneratorReturnHandling(1)

        if node.isExpressionBuiltinImport() and \
           not Options.getShallFollowExtra() and \
           not Options.getShallFollowExtraFilePatterns() and \
           not Options.shallFollowNoImports() and \
           not isWhiteListedImport(node) and \
           not Plugins.suppressBuiltinImportWarning(node.getParentModule(), node.getSourceReference()):
            warning("""Unresolved '__import__' call at '%s' may require use \
of '--recurse-directory'.""" % (
                    node.getSourceReference().getAsString()
                )
            )

        if node.isExpressionFunctionCreation():
            if not node.getParent().isExpressionFunctionCall() or \
               node.getParent().getFunction() is not node:
                node.getFunctionRef().getFunctionBody().markAsNeedsCreation()

        if node.isExpressionFunctionCall():
            node.getFunction().getFunctionRef().getFunctionBody().\
              markAsDirectlyCalled()

        if node.isExpressionFunctionRef():
            function_body = node.getFunctionBody()
            parent_module = function_body.getParentModule()

            node_module = node.getParentModule()
            if node_module is not parent_module:
                function_body.markAsCrossModuleUsed()

                node_module.addCrossUsedFunction(function_body)

        if node.isStatementAssignmentVariable():
            target_var = node.getTargetVariableRef().getVariable()
            assign_source = node.getAssignSource()

            if assign_source.isExpressionOperationBinary():
                left_arg = assign_source.getLeft()

                if left_arg.isExpressionVariableRef():
                    if assign_source.getLeft().getVariable().isModuleVariable():
                        assign_source.unmarkAsInplaceSuspect()
                    elif assign_source.getLeft().getVariable() is target_var:
                        if assign_source.isInplaceSuspect():
                            node.markAsInplaceSuspect()

        if node.isStatementPublishException():
            node.getParentStatementsFrame().markAsFrameExceptionPreserving()

        if python_version >= 300:
            if node.isExpressionYield() or node.isExpressionYieldFrom():
                search = node.getParent()

                while not search.isExpressionGeneratorObjectBody():
                    last_search = search
                    search = search.getParent()

                    if search.isStatementTry() and \
                       last_search == search.getBlockExceptHandler():
                        node.markAsExceptionPreserving()
                        break
예제 #4
0
    def _onEnterNode(self, node):
        # This has many different things it deals with, so there need to be a
        # lot of branches and statements, pylint: disable=too-many-branches,too-many-statements

        # Also all self specific things have been done on the outside,
        # pylint: disable=no-self-use
        if node.isStatementReturn() or node.isStatementGeneratorReturn():
            search = node

            in_tried_block = False

            # Search up to the containing function, and check for a try/finally
            # containing the "return" statement.
            search = search.getParentReturnConsumer()

            if (
                search.isExpressionGeneratorObjectBody()
                or search.isExpressionCoroutineObjectBody()
                or search.isExpressionAsyncgenObjectBody()
            ):
                if in_tried_block:
                    search.markAsNeedsGeneratorReturnHandling(2)
                else:
                    search.markAsNeedsGeneratorReturnHandling(1)

        if (
            node.isExpressionBuiltinImport()
            and not Options.getShallFollowExtra()
            and not Options.getShallFollowExtraFilePatterns()
            and not Options.shallFollowNoImports()
            and not isWhiteListedImport(node)
            and not node.recurse_attempted
            and not Plugins.suppressBuiltinImportWarning(
                node.getParentModule(), node.getSourceReference()
            )
        ):
            warning(
                """Unresolved '__import__' call at '%s' may require use \
of '--include-plugin-directory' or '--include-plugin-files'."""
                % (node.getSourceReference().getAsString())
            )

        if node.isExpressionBuiltinImport() and node.recurse_attempted:
            module_name = node.getImportName()

            if module_name.isCompileTimeConstant():
                imported_module_name = module_name.getCompileTimeConstant()

                if type(imported_module_name) in (str, unicode):
                    if imported_module_name:
                        imported_names.add(imported_module_name)

        if node.isExpressionFunctionCreation():
            if (
                not node.getParent().isExpressionFunctionCall()
                or node.getParent().getFunction() is not node
            ):
                node.getFunctionRef().getFunctionBody().markAsNeedsCreation()

        if node.isExpressionFunctionCall():
            node.getFunction().getFunctionRef().getFunctionBody().markAsDirectlyCalled()

        if node.isExpressionFunctionRef():
            function_body = node.getFunctionBody()
            parent_module = function_body.getParentModule()

            node_module = node.getParentModule()
            if node_module is not parent_module:
                function_body.markAsCrossModuleUsed()

                node_module.addCrossUsedFunction(function_body)

        if node.isStatementAssignmentVariable():
            target_var = node.getVariable()
            assign_source = node.getAssignSource()

            if assign_source.isExpressionOperationBinary():
                left_arg = assign_source.getLeft()

                if left_arg.isExpressionVariableRef():
                    if assign_source.getLeft().getVariable() is target_var:
                        if assign_source.isInplaceSuspect():
                            node.markAsInplaceSuspect()
                elif left_arg.isExpressionLocalsVariableRefORFallback():
                    assign_source.unmarkAsInplaceSuspect()

        if node.isStatementLocalsDictOperationSet():
            assign_source = node.getAssignSource()

            if assign_source.isExpressionOperationBinary():
                assign_source.unmarkAsInplaceSuspect()

        if python_version < 300 and node.isStatementPublishException():
            node.getParentStatementsFrame().markAsFrameExceptionPreserving()

        if python_version >= 300:
            if (
                node.isExpressionYield()
                or node.isExpressionYieldFrom()
                or node.isExpressionYieldFromWaitable()
            ):
                search = node.getParent()

                while (
                    not search.isExpressionGeneratorObjectBody()
                    and not search.isExpressionCoroutineObjectBody()
                    and not search.isExpressionAsyncgenObjectBody()
                ):

                    last_search = search
                    search = search.getParent()

                    if (
                        search.isStatementTry()
                        and last_search == search.getBlockExceptHandler()
                    ):
                        node.markAsExceptionPreserving()
                        break