Skip to content

Commit

Permalink
[ADD] request-without-timeout: It could wait for a long time
Browse files Browse the repository at this point in the history
  • Loading branch information
moylop260 committed May 24, 2022
1 parent d260411 commit 11da8c4
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
39 changes: 38 additions & 1 deletion pylint_odoo/checkers/no_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@
'str-format-used',
settings.DESC_DFLT
),
'E%d06' % settings.BASE_NOMODULE_ID: (
'Use of `requests.%s` method without timeout. '
'It could wait for a long time',
'request-without-timeout',
settings.DESC_DFLT
),
'C%d01' % settings.BASE_NOMODULE_ID: (
'One of the following authors must be present in manifest: %s',
'manifest-required-author',
Expand Down Expand Up @@ -415,6 +421,14 @@ class NoModuleChecker(misc.PylintOdooChecker):
}),
)

def visit_module(self, node):
"""Initialize dictionary to store imported nodes names"""
self.imported_names = {}

def leave_module(self, node):
"""Clear variables"""
self.imported_names = {}

def open(self):
super(NoModuleChecker, self).open()
self.config.deprecated_field_parameters = \
Expand Down Expand Up @@ -589,11 +603,13 @@ def visit_print(self, node):
'translation-contains-variable',
'print-used', 'translation-positional-used',
'str-format-used', 'context-overridden',
'request-without-timeout',
)
def visit_call(self, node):
infer_node = utils.safe_infer(node.func)
if utils.is_builtin_object(infer_node) and infer_node.name == 'print':
self.add_message('print-used', node=node)
# TODO: Add self.linter.is_message_enabled() for each section
if ('fields' == self.get_func_lib(node.func) and
isinstance(node.parent, astroid.Assign) and
isinstance(node.parent.parent, astroid.ClassDef)):
Expand Down Expand Up @@ -750,6 +766,20 @@ def visit_call(self, node):
if self._check_sql_injection_risky(node):
self.add_message('sql-injection', node=node)

# requests calls
func_name = self.get_func_name(node.func)
lib_func = "%s.%s" % (self.get_func_lib(node.func), func_name)
if (self.imported_names.get(func_name) in ["requests.get", "requests.post"] or
lib_func in ["requests.get", "requests.post"]):
for argument in misc.join_node_args_kwargs(node):
if not isinstance(argument, astroid.Keyword):
continue
if argument.arg == 'timeout':
break
else:
self.add_message(
'request-without-timeout', node=node, args=(func_name,))

@utils.check_messages(
'license-allowed', 'manifest-author-string', 'manifest-deprecated-key',
'manifest-required-author', 'manifest-required-key',
Expand Down Expand Up @@ -911,12 +941,19 @@ def visit_functiondef(self, node):
node.name not in self.config.no_missing_return:
self.add_message('missing-return', node=node, args=(node.name))

@utils.check_messages('openerp-exception-warning')
@utils.check_messages('openerp-exception-warning', 'request-without-timeout')
def visit_importfrom(self, node):
if node.modname == 'openerp.exceptions':
for (import_name, import_as_name) in node.names:
if import_name == 'Warning' and import_as_name != 'UserError':
self.add_message('openerp-exception-warning', node=node)
# Fill dict "self.imported_names" to know the source library imported
if node.modname == 'requests':
self.imported_names.update({
alias or name: "requests.%s" % name
for name, alias in node.names
# "import requests" will be detected with "requests.get"
if not (name == 'requests' and not alias)})

@utils.check_messages('class-camelcase')
def visit_classdef(self, node):
Expand Down
1 change: 1 addition & 0 deletions pylint_odoo/test/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
'po-msgstr-variables': 6,
'print-used': 1,
'redundant-modulename-xml': 1,
'request-without-timeout': 6,
'rst-syntax-error': 2,
'sql-injection': 21,
'str-format-used': 3,
Expand Down
20 changes: 20 additions & 0 deletions pylint_odoo/test_repo/broken_module/models/broken_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import psycopg2
from psycopg2 import sql
from psycopg2.sql import SQL, Identifier
import requests
from requests import post, get
from requests import post as post_r, get as get_r

from openerp import fields, models, _
from openerp.exceptions import Warning as UserError
Expand Down Expand Up @@ -537,6 +540,23 @@ def func(self, a):
length = len(a)
return length

def requests_test(self):
# request without timeout
requests.post('http://localhost')
requests.get('http://localhost')
post('http://localhost')
get('http://localhost')
post_r('http://localhost')
get_r('http://localhost')

# Valid cases
requests.post('http://localhost', timeout=10)
requests.get('http://localhost', timeout=10)
post('http://localhost', timeout=10)
get('http://localhost', timeout=10)
post_r('http://localhost', timeout=10)
get_r('http://localhost', timeout=10)


class NoOdoo(object):
length = 0
Expand Down

0 comments on commit 11da8c4

Please sign in to comment.