Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
looyolo
scrapy
提交
72e8cea8
S
scrapy
项目概览
looyolo
/
scrapy
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
scrapy
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
未验证
提交
72e8cea8
编写于
3月 22, 2021
作者:
E
Eugenio Lacuesta
提交者:
GitHub
3月 22, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Avoid exceptions in is_generator_with_return_value (#4935)
上级
ec5a7918
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
162 addition
and
34 deletion
+162
-34
scrapy/utils/misc.py
scrapy/utils/misc.py
+19
-8
tests/test_utils_misc/test_return_with_argument_inside_generator.py
..._utils_misc/test_return_with_argument_inside_generator.py
+143
-26
未找到文件。
scrapy/utils/misc.py
浏览文件 @
72e8cea8
...
...
@@ -9,7 +9,6 @@ from collections import deque
from
contextlib
import
contextmanager
from
importlib
import
import_module
from
pkgutil
import
iter_modules
from
textwrap
import
dedent
from
w3lib.html
import
replace_entities
...
...
@@ -227,7 +226,8 @@ def is_generator_with_return_value(callable):
return
value
is
None
or
isinstance
(
value
,
ast
.
NameConstant
)
and
value
.
value
is
None
if
inspect
.
isgeneratorfunction
(
callable
):
tree
=
ast
.
parse
(
dedent
(
inspect
.
getsource
(
callable
)))
code
=
re
.
sub
(
r
"^[\t ]+"
,
""
,
inspect
.
getsource
(
callable
))
tree
=
ast
.
parse
(
code
)
for
node
in
walk_callable
(
tree
):
if
isinstance
(
node
,
ast
.
Return
)
and
not
returns_none
(
node
):
_generator_callbacks_cache
[
callable
]
=
True
...
...
@@ -242,12 +242,23 @@ def warn_on_generator_with_return_value(spider, callable):
Logs a warning if a callable is a generator function and includes
a 'return' statement with a value different than None
"""
if
is_generator_with_return_value
(
callable
):
try
:
if
is_generator_with_return_value
(
callable
):
warnings
.
warn
(
f
'The "
{
spider
.
__class__
.
__name__
}
.
{
callable
.
__name__
}
" method is '
'a generator and includes a "return" statement with a value '
'different than None. This could lead to unexpected behaviour. Please see '
'https://docs.python.org/3/reference/simple_stmts.html#the-return-statement '
'for details about the semantics of the "return" statement within generators'
,
stacklevel
=
2
,
)
except
IndentationError
:
callable_name
=
spider
.
__class__
.
__name__
+
"."
+
callable
.
__name__
warnings
.
warn
(
f
'
The "
{
spider
.
__class__
.
__name__
}
.
{
callable
.
__name__
}
" method is
'
'
a generator and includes a "return" statement with a value
'
'different than None. This could lead to unexpected behaviour. Please se
e '
'
https://docs.python.org/3/reference/simple_stmts.html#the-return-statement
'
'for details about the semantics of the "return" statement within generators
'
,
f
'
Unable to determine whether or not "
{
callable_name
}
" is a generator with a return value.
'
'
This will not prevent your code from working, but it prevents Scrapy from detecting
'
f
'potential issues in your implementation of "
{
callable_name
}
". Please, report this in th
e '
'
Scrapy issue tracker (https://github.com/scrapy/scrapy/issues),
'
f
'including the code of "
{
callable_name
}
"
'
,
stacklevel
=
2
,
)
tests/test_utils_misc/test_return_with_argument_inside_generator.py
浏览文件 @
72e8cea8
import
unittest
import
warnings
from
unittest
import
mock
from
scrapy.utils.misc
import
is_generator_with_return_value
from
scrapy.utils.misc
import
is_generator_with_return_value
,
warn_on_generator_with_return_value
def
_indentation_error
(
*
args
,
**
kwargs
):
raise
IndentationError
()
def
top_level_return_something
():
"""
docstring
"""
url
=
"""
https://example.org
"""
yield
url
return
1
def
top_level_return_none
():
"""
docstring
"""
url
=
"""
https://example.org
"""
yield
url
return
def
generator_that_returns_stuff
():
yield
1
yield
2
return
3
class
UtilsMiscPy3TestCase
(
unittest
.
TestCase
):
def
test_generators_
with_return_statements
(
self
):
def
f
():
def
test_generators_
return_something
(
self
):
def
f
1
():
yield
1
return
2
def
g
():
def
g1
():
yield
1
return
"asdf"
def
h1
():
yield
1
return
'asdf'
def
h
():
def
helper
():
return
0
yield
helper
()
return
2
def
i1
():
"""
docstring
"""
url
=
"""
https://example.org
"""
yield
url
return
1
assert
is_generator_with_return_value
(
top_level_return_something
)
assert
is_generator_with_return_value
(
f1
)
assert
is_generator_with_return_value
(
g1
)
assert
is_generator_with_return_value
(
h1
)
assert
is_generator_with_return_value
(
i1
)
with
warnings
.
catch_warnings
(
record
=
True
)
as
w
:
warn_on_generator_with_return_value
(
None
,
top_level_return_something
)
self
.
assertEqual
(
len
(
w
),
1
)
self
.
assertIn
(
'The "NoneType.top_level_return_something" method is a generator'
,
str
(
w
[
0
].
message
))
with
warnings
.
catch_warnings
(
record
=
True
)
as
w
:
warn_on_generator_with_return_value
(
None
,
f1
)
self
.
assertEqual
(
len
(
w
),
1
)
self
.
assertIn
(
'The "NoneType.f1" method is a generator'
,
str
(
w
[
0
].
message
))
with
warnings
.
catch_warnings
(
record
=
True
)
as
w
:
warn_on_generator_with_return_value
(
None
,
g1
)
self
.
assertEqual
(
len
(
w
),
1
)
self
.
assertIn
(
'The "NoneType.g1" method is a generator'
,
str
(
w
[
0
].
message
))
with
warnings
.
catch_warnings
(
record
=
True
)
as
w
:
warn_on_generator_with_return_value
(
None
,
h1
)
self
.
assertEqual
(
len
(
w
),
1
)
self
.
assertIn
(
'The "NoneType.h1" method is a generator'
,
str
(
w
[
0
].
message
))
with
warnings
.
catch_warnings
(
record
=
True
)
as
w
:
warn_on_generator_with_return_value
(
None
,
i1
)
self
.
assertEqual
(
len
(
w
),
1
)
self
.
assertIn
(
'The "NoneType.i1" method is a generator'
,
str
(
w
[
0
].
message
))
def
test_generators_return_none
(
self
):
def
f2
():
yield
1
return
None
def
i
():
def
g2
():
yield
1
return
def
j
():
def
h2
():
yield
1
def
k
():
def
i2
():
yield
1
yield
from
g
()
yield
from
g
enerator_that_returns_stuff
()
def
m
():
def
j2
():
yield
1
def
helper
():
...
...
@@ -37,20 +118,56 @@ class UtilsMiscPy3TestCase(unittest.TestCase):
yield
helper
()
def
n
():
yield
1
def
helper
():
return
0
def
k2
():
"""
docstring
"""
url
=
"""
https://example.org
"""
yield
url
return
yield
helper
()
return
2
def
l2
():
return
assert
is_generator_with_return_value
(
f
)
assert
is_generator_with_return_value
(
g
)
assert
not
is_generator_with_return_value
(
h
)
assert
not
is_generator_with_return_value
(
i
)
assert
not
is_generator_with_return_value
(
j
)
assert
not
is_generator_with_return_value
(
k
)
# not recursive
assert
not
is_generator_with_return_value
(
m
)
assert
is_generator_with_return_value
(
n
)
assert
not
is_generator_with_return_value
(
top_level_return_none
)
assert
not
is_generator_with_return_value
(
f2
)
assert
not
is_generator_with_return_value
(
g2
)
assert
not
is_generator_with_return_value
(
h2
)
assert
not
is_generator_with_return_value
(
i2
)
assert
not
is_generator_with_return_value
(
j2
)
# not recursive
assert
not
is_generator_with_return_value
(
k2
)
# not recursive
assert
not
is_generator_with_return_value
(
l2
)
with
warnings
.
catch_warnings
(
record
=
True
)
as
w
:
warn_on_generator_with_return_value
(
None
,
top_level_return_none
)
self
.
assertEqual
(
len
(
w
),
0
)
with
warnings
.
catch_warnings
(
record
=
True
)
as
w
:
warn_on_generator_with_return_value
(
None
,
f2
)
self
.
assertEqual
(
len
(
w
),
0
)
with
warnings
.
catch_warnings
(
record
=
True
)
as
w
:
warn_on_generator_with_return_value
(
None
,
g2
)
self
.
assertEqual
(
len
(
w
),
0
)
with
warnings
.
catch_warnings
(
record
=
True
)
as
w
:
warn_on_generator_with_return_value
(
None
,
h2
)
self
.
assertEqual
(
len
(
w
),
0
)
with
warnings
.
catch_warnings
(
record
=
True
)
as
w
:
warn_on_generator_with_return_value
(
None
,
i2
)
self
.
assertEqual
(
len
(
w
),
0
)
with
warnings
.
catch_warnings
(
record
=
True
)
as
w
:
warn_on_generator_with_return_value
(
None
,
j2
)
self
.
assertEqual
(
len
(
w
),
0
)
with
warnings
.
catch_warnings
(
record
=
True
)
as
w
:
warn_on_generator_with_return_value
(
None
,
k2
)
self
.
assertEqual
(
len
(
w
),
0
)
with
warnings
.
catch_warnings
(
record
=
True
)
as
w
:
warn_on_generator_with_return_value
(
None
,
l2
)
self
.
assertEqual
(
len
(
w
),
0
)
@
mock
.
patch
(
"scrapy.utils.misc.is_generator_with_return_value"
,
new
=
_indentation_error
)
def
test_indentation_error
(
self
):
with
warnings
.
catch_warnings
(
record
=
True
)
as
w
:
warn_on_generator_with_return_value
(
None
,
top_level_return_none
)
self
.
assertEqual
(
len
(
w
),
1
)
self
.
assertIn
(
'Unable to determine'
,
str
(
w
[
0
].
message
))
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录