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

ut_timeout_modifed
liym27 5 years ago committed by GitHub
parent 3b8f5200a5
commit a1b640bc66
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -124,8 +124,13 @@ class OriginInfoAttacher(gast.NodeTransformer):
def _abs_lineno(self, node): def _abs_lineno(self, node):
# NOTE(liym27): # NOTE(liym27):
# If the first gast.FunctionDef has decorator, its lineno is 1, which # There are differences in ast_node.lineno between PY3.8+ and PY3.8-.
# equals to the lineno of the first decorator node. # 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 return self.lineno_offset + node.lineno
def _abs_col_offset(self, node): def _abs_col_offset(self, node):

@ -14,6 +14,7 @@
from __future__ import print_function from __future__ import print_function
import sys
import unittest import unittest
from paddle.fluid.dygraph.dygraph_to_static.ast_transformer import DygraphToStaticAst from paddle.fluid.dygraph.dygraph_to_static.ast_transformer import DygraphToStaticAst
@ -177,6 +178,18 @@ class TestOriginInfoWithDecoratedFunc(TestOriginInfo):
def set_dygraph_info(self): def set_dygraph_info(self):
self.line_num = 2 self.line_num = 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.line_index_list = [0, 2]
self.dy_rel_lineno_list = [0, 2] self.dy_rel_lineno_list = [0, 2]
self.dy_abs_col_offset = [0, 4] self.dy_abs_col_offset = [0, 4]
@ -199,6 +212,11 @@ class TestOriginInfoWithDecoratedFunc2(TestOriginInfo):
def set_dygraph_info(self): def set_dygraph_info(self):
self.line_num = 2 self.line_num = 2
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.line_index_list = [0, 3]
self.dy_rel_lineno_list = [0, 3] self.dy_rel_lineno_list = [0, 3]
self.dy_abs_col_offset = [0, 4] self.dy_abs_col_offset = [0, 4]

Loading…
Cancel
Save