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

[Dy2Stat] Fix error message when the message has more than one lines. (#30714)

上级 5ace20fc
...@@ -143,9 +143,12 @@ class ErrorData(object): ...@@ -143,9 +143,12 @@ class ErrorData(object):
message_lines.append(traceback_frame.formated_message()) message_lines.append(traceback_frame.formated_message())
# Step3: Adds error message like "TypeError: dtype must be int32, but received float32". # Step3: Adds error message like "TypeError: dtype must be int32, but received float32".
error_message = " " * 4 + traceback.format_exception_only( # NOTE: `format_exception` is a list, its length is 1 in most cases, but sometimes its length
self.error_type, self.error_value)[0].strip("\n") # is gather than 1, for example, the error_type is IndentationError.
message_lines.append(error_message) format_exception = traceback.format_exception_only(self.error_type,
self.error_value)
error_message = [" " * 4 + line for line in format_exception]
message_lines.extend(error_message)
return '\n'.join(message_lines) return '\n'.join(message_lines)
......
...@@ -82,6 +82,22 @@ class LayerErrorInCompiletime(fluid.dygraph.Layer): ...@@ -82,6 +82,22 @@ class LayerErrorInCompiletime(fluid.dygraph.Layer):
return out return out
class LayerErrorInCompiletime2(fluid.dygraph.Layer):
def __init__(self):
super(LayerErrorInCompiletime2, self).__init__()
@paddle.jit.to_static
def forward(self):
self.test_func()
def test_func(self):
"""
NOTE: The next line has a tab. And this test to check the IndentationError when spaces and tabs are mixed.
A tab here.
"""
return
class TestFlags(unittest.TestCase): class TestFlags(unittest.TestCase):
def setUp(self): def setUp(self):
self.reset_flags_to_default() self.reset_flags_to_default()
...@@ -230,6 +246,32 @@ class TestErrorStaticLayerCallInCompiletime_2( ...@@ -230,6 +246,32 @@ class TestErrorStaticLayerCallInCompiletime_2(
] ]
class TestErrorStaticLayerCallInCompiletime_3(
TestErrorStaticLayerCallInCompiletime):
def setUp(self):
self.reset_flags_to_default()
self.set_func_call()
self.filepath = inspect.getfile(unwrap(self.func_call))
self.set_exception_type()
self.set_message()
def set_exception_type(self):
self.exception_type = IndentationError
def set_message(self):
self.expected_message = \
['File "{}", line 91, in forward'.format(self.filepath),
'self.test_func()',
]
def set_func_call(self):
layer = LayerErrorInCompiletime2()
self.func_call = lambda: layer()
def test_error(self):
self._test_raise_new_exception()
class TestErrorStaticLayerCallInRuntime(TestErrorStaticLayerCallInCompiletime): class TestErrorStaticLayerCallInRuntime(TestErrorStaticLayerCallInCompiletime):
def set_func(self): def set_func(self):
self.func = func_error_in_runtime self.func = func_error_in_runtime
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册