Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MindSpore
mindinsight
提交
ba1bcc95
M
mindinsight
项目概览
MindSpore
/
mindinsight
通知
7
Star
3
Fork
2
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
mindinsight
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
ba1bcc95
编写于
6月 20, 2020
作者:
M
mindspore-ci-bot
提交者:
Gitee
6月 20, 2020
浏览文件
操作
浏览文件
下载
差异文件
!314 Fix CI issue that code is redundant.
Merge pull request !314 from ggpolar/br_wzk_dev
上级
176a2462
01e96c9e
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
39 addition
and
33 deletion
+39
-33
mindinsight/mindconverter/ast_edits.py
mindinsight/mindconverter/ast_edits.py
+1
-0
mindinsight/mindconverter/code_analysis.py
mindinsight/mindconverter/code_analysis.py
+38
-33
未找到文件。
mindinsight/mindconverter/ast_edits.py
浏览文件 @
ba1bcc95
...
...
@@ -715,3 +715,4 @@ class AstEditVisitor(ast.NodeVisitor):
def
visit_Attribute
(
self
,
node
):
"""Callback function when visit AST tree"""
self
.
_check_isinstance_parameter
(
node
)
self
.
generic_visit
(
node
)
mindinsight/mindconverter/code_analysis.py
浏览文件 @
ba1bcc95
...
...
@@ -188,20 +188,26 @@ class CodeAnalyzer(ast.NodeVisitor):
@
staticmethod
def
_analyze_import_references
(
root_scope
):
"""Find out all references from the import statements."""
external_name_ref
=
{}
"""
Find out all references from the import statements.
Case1: (from)import alias, node_ref.name_ref.id is node_ref.name_ref.definition.asname.
Case2: import without alias, node_ref.name_ref.definition.asname is None.
e.g., import a.b.c, the reference definition id maybe is a, a.b or a.b.c.
The reference id a.b.c is really wanted.
"""
external_name_ref
=
dict
()
all_node_references
=
[]
for
node_references
in
root_scope
.
external_references
.
values
():
for
node_ref
in
node_references
:
if
node_ref
.
name_ref
:
# case1: (from)import alias, node_ref.name_ref.id is node_ref.name_ref.definition.asname.
# case2: import without alias, node_ref.name_ref.definition.asname is None.
# e.g., import a.b.c, the reference definition id maybe is a, a.b or a.b.c.
# The reference id a.b.c is really wanted.
if
node_ref
.
name_ref
.
id
in
[
node_ref
.
name_ref
.
definition
.
asname
,
node_ref
.
name_ref
.
definition
.
name
]:
external_name_ref
[
node_ref
.
name_ref
.
id
]
=
node_ref
else
:
pass
all_node_references
.
extend
(
node_references
)
for
node_ref
in
all_node_references
:
name_ref
=
node_ref
.
name_ref
if
not
name_ref
:
continue
definition
=
name_ref
.
definition
if
node_ref
.
name_ref
.
id
in
[
definition
.
asname
,
definition
.
name
]:
external_name_ref
[
name_ref
.
id
]
=
node_ref
return
external_name_ref
...
...
@@ -230,7 +236,7 @@ class CodeAnalyzer(ast.NodeVisitor):
full_name
=
self
.
_get_full_name
(
node
)
if
not
full_name
:
return
None
whole_name
=
full_name
# node is in stack top pos
if
node
is
self
.
_stack
[
-
1
]:
parent_index
=
-
1
...
...
@@ -238,8 +244,6 @@ class CodeAnalyzer(ast.NodeVisitor):
parent_index
-=
1
whole_name
=
self
.
_get_full_name
(
self
.
_stack
[
parent_index
])
else
:
whole_name
=
full_name
return
whole_name
def
_is_ref_convertible_imports
(
self
,
node
):
...
...
@@ -260,26 +264,24 @@ class CodeAnalyzer(ast.NodeVisitor):
return
check_result
@
staticmethod
def
_get_external_node
(
external_references
):
def
_get_external_node
(
external_references
,
only_convertible
=
False
):
"""Get all external reference nodes."""
external_nodes
=
{}
for
ref_name
,
ref_info
in
external_references
.
items
():
external_nodes
.
update
({
ref_info
[
'external_ref_info'
].
node
:
ref_name
})
is_add
=
False
if
only_convertible
:
if
ref_info
[
'external_ref_info'
].
name
in
APIAnalysisSpec
.
get_convertible_external_names
():
is_add
=
True
else
:
is_add
=
True
if
is_add
:
external_nodes
.
update
({
ref_info
[
'external_ref_info'
].
node
:
ref_name
})
return
external_nodes
@
staticmethod
def
_get_convertible_external_node
(
external_name_ref
):
"""Get all convertible external reference nodes."""
convertible_external_nodes
=
{}
for
ref_name
,
ref_info
in
external_name_ref
.
items
():
if
ref_info
[
'external_ref_info'
].
name
in
APIAnalysisSpec
.
get_convertible_external_names
():
convertible_external_nodes
.
update
({
ref_info
[
'external_ref_info'
].
node
:
ref_name
})
return
convertible_external_nodes
def
_update_external_ref_parent
(
self
,
node
):
"""Set external reference parent node info."""
external_nodes
=
self
.
_get_external_node
(
self
.
_external_references
)
convertible_external_nodes
=
self
.
_get_
convertible_external_node
(
self
.
_external_references
)
external_nodes
=
self
.
_get_external_node
(
self
.
_external_references
,
only_convertible
=
False
)
convertible_external_nodes
=
self
.
_get_
external_node
(
self
.
_external_references
,
only_convertible
=
True
)
for
name_node
in
node
.
names
:
if
name_node
in
convertible_external_nodes
.
keys
():
if
len
(
node
.
names
)
>
1
:
...
...
@@ -326,15 +328,18 @@ class CodeAnalyzer(ast.NodeVisitor):
self
.
generic_visit
(
node
)
def
visit_Impor
t
(
self
,
node
):
"""
Callback function when visit AST tree
"""
def
_update_external_when_visi
t
(
self
,
node
):
"""
Update external reference when visiting import and import from statements.
"""
self
.
_update_external_ref_parent
(
node
)
self
.
generic_visit
(
node
)
def
visit_Import
(
self
,
node
):
"""Callback function when visit AST tree"""
self
.
_update_external_when_visit
(
node
)
def
visit_ImportFrom
(
self
,
node
):
"""Callback function when visit AST tree"""
self
.
_update_external_ref_parent
(
node
)
self
.
generic_visit
(
node
)
self
.
_update_external_when_visit
(
node
)
def
visit_Call
(
self
,
node
):
"""Callback function when visit AST tree"""
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录