Skip to content

Commit

Permalink
Remove unsued and old code (#49)
Browse files Browse the repository at this point in the history
Co-authored-by: mschwager <[email protected]>
  • Loading branch information
clavedeluna and mschwager authored Sep 13, 2022
1 parent 3cd4c00 commit 35b08ab
Show file tree
Hide file tree
Showing 18 changed files with 31 additions and 171 deletions.
11 changes: 0 additions & 11 deletions dlint/linters/bad_exec_use.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/usr/bin/env python

from .helpers import bad_builtin_use
from . import base


class BadExecUseLinter(bad_builtin_use.BadBuiltinUseLinter):
Expand All @@ -14,16 +13,6 @@ class BadExecUseLinter(bad_builtin_use.BadBuiltinUseLinter):
_code = 'DUO105'
_error_tmpl = 'DUO105 use of "exec" is insecure'

# Python 2
def visit_Exec(self, node):
self.results.append(
base.Flake8Result(
lineno=node.lineno,
col_offset=node.col_offset,
message=self._error_tmpl
)
)

@property
def illegal_builtin(self):
return 'exec'
23 changes: 2 additions & 21 deletions dlint/linters/bad_input_use.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
#!/usr/bin/env python

import ast
import sys

from . import base


class BadInputUseLinter(base.BaseLinter):
"""This linter looks for use of the Python "input" function. In Python 2
this function is tantamount to eval(raw_input()), and thus should not be
used. In Python 3 raw_input() functionality has been moved to input().
"""This linter looks for use of the Python "input" function.
In Python 3 raw_input() functionality has been moved to input().
"""
off_by_default = False

Expand All @@ -21,21 +17,6 @@ def __init__(self, *args, **kwargs):

super(BadInputUseLinter, self).__init__(*args, **kwargs)

def visit_Call(self, node):
is_python_2 = sys.version_info < (3, 0)

if (is_python_2
and self.unsafe_input_import
and isinstance(node.func, ast.Name)
and node.func.id == 'input'):
self.results.append(
base.Flake8Result(
lineno=node.lineno,
col_offset=node.col_offset,
message=self._error_tmpl
)
)

def visit_ImportFrom(self, node):
# Using input from six.moves is valid, so if input is imported
# in a safe way, allow input to be used for the rest of the file
Expand Down
3 changes: 1 addition & 2 deletions dlint/linters/helpers/bad_builtin_use.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
import abc

from .. import base
from ... import util


class BadBuiltinUseLinter(base.BaseLinter, util.ABC):
class BadBuiltinUseLinter(base.BaseLinter, abc.ABC):
"""This abstract base class provides a simple interface for creating new
lint rules that block builtin functions.
"""
Expand Down
3 changes: 1 addition & 2 deletions dlint/linters/helpers/bad_kwarg_use.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@

from .. import base
from ... import tree
from ... import util


class BadKwargUseLinter(base.BaseLinter, util.ABC):
class BadKwargUseLinter(base.BaseLinter, abc.ABC):
"""This abstract base class provides a simple interface for creating new
lint rules that block bad kwarg use.
"""
Expand Down
3 changes: 1 addition & 2 deletions dlint/linters/helpers/bad_module_attribute_use.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

from .. import base
from ... import tree
from ... import util


class BadModuleAttributeUseLinter(base.BaseLinter, util.ABC):
class BadModuleAttributeUseLinter(base.BaseLinter, abc.ABC):
"""This abstract base class provides a simple interface for creating new
lint rules that block bad attributes within a module.
"""
Expand Down
3 changes: 1 addition & 2 deletions dlint/linters/helpers/bad_module_use.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

from .. import base
from ... import tree
from ... import util


class BadModuleUseLinter(base.BaseLinter, util.ABC):
class BadModuleUseLinter(base.BaseLinter, abc.ABC):
"""This abstract base class provides a simple interface for creating new
lint rules that block bad modules.
"""
Expand Down
3 changes: 1 addition & 2 deletions dlint/linters/helpers/bad_name_attribute_use.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@

from .. import base
from ... import tree
from ... import util

Assignment = collections.namedtuple(
'Assignment',
['variable', 'module_path', 'lineno', 'col_offset']
)


class BadNameAttributeUseLinter(base.BaseLinter, util.ABC):
class BadNameAttributeUseLinter(base.BaseLinter, abc.ABC):
"""This abstract base class provides a simple interface for creating new
lint rules that block bad attributes on a variable object.
"""
Expand Down
28 changes: 0 additions & 28 deletions dlint/linters/twisted/yield_return_statement.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
#!/usr/bin/env python

import ast
import sys

from .. import base
from ... import tree


class YieldReturnStatementLinter(base.BaseLinter):
Expand All @@ -19,27 +15,3 @@ class YieldReturnStatementLinter(base.BaseLinter):

def visit_FunctionDef(self, node):
self.generic_visit(node)

# https://twistedmatrix.com/documents/17.1.0/api/twisted.internet.defer.inlineCallbacks.html
is_python_3_3 = sys.version_info >= (3, 3)

if (is_python_3_3
or not tree.function_has_inlinecallbacks_decorator(node)):
return

results = []

def return_statement_callback(inner_node):
if isinstance(inner_node, ast.Return) and tree.non_empty_return(inner_node):
results.append(inner_node)

tree.walk_callback_same_scope(node, return_statement_callback)

self.results.extend(
base.Flake8Result(
lineno=result.lineno,
col_offset=result.col_offset,
message=self._error_tmpl
)
for result in results
)
12 changes: 1 addition & 11 deletions dlint/namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,7 @@
import ast
import copy

try:
from functools import lru_cache
except ImportError:
# Sorry Python 2 users, it's time to upgrade
def lru_cache(*args, **kwargs):
def decorator(function):
def noop(*inner_args, **inner_kwargs):
return function(*inner_args, **inner_kwargs)
return noop
return decorator

from functools import lru_cache
from . import util


Expand Down
7 changes: 0 additions & 7 deletions dlint/redos/detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@
import itertools
import sys

try:
# Python 2
from future_builtins import filter # Same as itertools.ifilter
except ImportError:
# Python 3
pass

CR = collections.namedtuple('CR', ['cr_min', 'cr_max'])

CATEGORY_TO_RANGE = {
Expand Down
28 changes: 7 additions & 21 deletions dlint/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ def non_empty_return(_return):


def walk_callback_same_scope(node, callback):
is_python_3_5 = sys.version_info >= (3, 5)
is_python_3_6 = sys.version_info >= (3, 6)

# If we change scope, e.g. enter into a new
# class or function definition, then halt iteration
scopes = (ast.ClassDef, ast.FunctionDef)
if is_python_3_5:
if is_python_3_6:
scopes += (ast.AsyncFunctionDef,)

def scope_predicate(inner_node):
Expand All @@ -99,25 +99,11 @@ def kwarg_not_present(call, kwarg_name):


def kwarg_primitive(call, kwarg_name, primitive):
try:
# Python 3
primitive_type = ast.NameConstant

def comparator(keyword, inner_primitive):
return (
isinstance(keyword.value, primitive_type)
and keyword.value.value == inner_primitive
)
except AttributeError:
# Python 2, AttributeError on ast.NameConstant
primitive_type = ast.Name

def comparator(keyword, inner_primitive):
return (
isinstance(keyword.value, primitive_type)
and keyword.value.id == str(inner_primitive)
)

def comparator(keyword, inner_primitive):
return (
isinstance(keyword.value, ast.NameConstant)
and keyword.value.value == inner_primitive
)
return any(
keyword.arg == kwarg_name
and comparator(keyword, primitive)
Expand Down
9 changes: 0 additions & 9 deletions dlint/util.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
#!/usr/bin/env python

import abc
import sys

if sys.version_info >= (3, 4):
ABC = abc.ABC
else:
ABC = abc.ABCMeta(str('ABC'), (), {})


def lstartswith(l1, l2):
if len(l2) > len(l1):
return False
Expand Down
4 changes: 0 additions & 4 deletions docs/linters/DUO105.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ input.
```python
malicious_user_input = 'open("/etc/passwd", "w").write("bad data")'

# Python 3
exec(malicious_user_input)

# Python 2
exec malicious_user_input
```

## Correct code
Expand Down
27 changes: 0 additions & 27 deletions tests/test_bad_exec_use.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
#!/usr/bin/env python

import sys
import unittest

import dlint

IS_PYTHON_3 = sys.version_info >= (3, 0)


class TestBadExecUse(dlint.test.base.BaseTest):

Expand All @@ -33,30 +30,6 @@ def test_bad_exec_usage(self):

assert result == expected

@unittest.skipIf(IS_PYTHON_3, 'exec statements are a SyntaxError in Python 3')
def test_bad_exec_statement_usage(self):
python_node = self.get_ast_node(
"""
var = 1
exec 'print var + 1'
"""
)

linter = dlint.linters.BadExecUseLinter()
linter.visit(python_node)

result = linter.get_results()
expected = [
dlint.linters.base.Flake8Result(
lineno=4,
col_offset=0,
message=dlint.linters.BadExecUseLinter._error_tmpl
)
]

assert result == expected


if __name__ == "__main__":
unittest.main()
11 changes: 1 addition & 10 deletions tests/test_bad_input_use.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
#!/usr/bin/env python

import sys
import unittest

import dlint

IS_PYTHON_2 = sys.version_info < (3, 0)


class TestBadInputUse(dlint.test.base.BaseTest):

Expand Down Expand Up @@ -37,13 +34,7 @@ def test_bad_input_usage(self):
linter.visit(python_node)

result = linter.get_results()
expected = [] if not IS_PYTHON_2 else [
dlint.linters.base.Flake8Result(
lineno=4,
col_offset=9,
message=dlint.linters.BadInputUseLinter._error_tmpl
)
]
expected = []

assert result == expected

Expand Down
6 changes: 3 additions & 3 deletions tests/test_helpers/test_bad_name_attribute_use.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import dlint

IS_PYTHON_3_5 = sys.version_info >= (3, 5)
IS_PYTHON_3_6 = sys.version_info >= (3, 6)


def get_bad_name_attribute_use_implementation(illegal_name_attributes):
Expand Down Expand Up @@ -125,7 +125,7 @@ def inner_func():

assert result == expected

@unittest.skipUnless(IS_PYTHON_3_5, 'async statements introduced in Python 3.5')
@unittest.skipUnless(IS_PYTHON_3_6, 'async statements introduced in Python 3.6')
def test_bad_name_attributes_async_nested(self):
python_node = self.get_ast_node(
"""
Expand Down Expand Up @@ -188,7 +188,7 @@ def inner_func():

assert result == expected

@unittest.skipUnless(IS_PYTHON_3_5, 'async statements introduced in Python 3.5')
@unittest.skipUnless(IS_PYTHON_3_6, 'async statements introduced in Python 3.6')
def test_bad_name_attributes_async_nested_overwrite(self):
python_node = self.get_ast_node(
"""
Expand Down
Loading

0 comments on commit 35b08ab

Please sign in to comment.