未验证 提交 a1b640bc 编写于 作者: L liym27 提交者: GitHub

Fix test_origin_info to be compatible with PY3.8, because ast module is different in PY3.8 (#27201)

上级 3b8f5200
......@@ -124,8 +124,13 @@ class OriginInfoAttacher(gast.NodeTransformer):
def _abs_lineno(self, node):
# NOTE(liym27):
# If the first gast.FunctionDef has decorator, its lineno is 1, which
# equals to the lineno of the first decorator node.
# There are differences in ast_node.lineno between PY3.8+ and PY3.8-.
# If the first gast.FunctionDef has decorator, the lineno of gast.FunctionDef is differs.
# 1. < PY3.8
# its lineno equals to the lineno of the first decorator node, which is not right.
# 2. >= PY3.8
# its lineno is the actual lineno, which is right.
return self.lineno_offset + node.lineno
def _abs_col_offset(self, node):
......
......@@ -14,6 +14,7 @@
from __future__ import print_function
import sys
import unittest
from paddle.fluid.dygraph.dygraph_to_static.ast_transformer import DygraphToStaticAst
......@@ -177,8 +178,20 @@ class TestOriginInfoWithDecoratedFunc(TestOriginInfo):
def set_dygraph_info(self):
self.line_num = 2
self.line_index_list = [0, 2]
self.dy_rel_lineno_list = [0, 2]
# NOTE(liym27):
# There are differences in ast_node.lineno between PY3.8+ and PY3.8-.
# If the first gast.FunctionDef has decorator, the lineno of gast.FunctionDef is differs.
# 1. < PY3.8
# its lineno equals to the lineno of the first decorator node, which is not right.
# 2. >= PY3.8
# its lineno is the actual lineno, which is right.
if sys.version_info >= (3, 8):
self.line_index_list = [1, 2]
self.dy_rel_lineno_list = [1, 2]
else:
self.line_index_list = [0, 2]
self.dy_rel_lineno_list = [0, 2]
self.dy_abs_col_offset = [0, 4]
self.dy_func_name = [self.dygraph_func.__name__] * self.line_num
......@@ -199,8 +212,13 @@ class TestOriginInfoWithDecoratedFunc2(TestOriginInfo):
def set_dygraph_info(self):
self.line_num = 2
self.line_index_list = [0, 3]
self.dy_rel_lineno_list = [0, 3]
if sys.version_info >= (3, 8):
self.line_index_list = [2, 3]
self.dy_rel_lineno_list = [2, 3]
else:
self.line_index_list = [0, 3]
self.dy_rel_lineno_list = [0, 3]
self.dy_abs_col_offset = [0, 4]
self.dy_func_name = [self.dygraph_func.__name__] * self.line_num
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册