未验证 提交 4615af2c 编写于 作者: W WangZhen 提交者: GitHub

[Dy2St]Fix abnormal format of message when raise KeyError in Dy2St (#44996)

* Fix abnormal format of message when raise KeyError in Dy2St

* Format code

* Format code

* Add UT

* Rename method
上级 ab37c8e9
......@@ -143,6 +143,10 @@ class SuggestionDict(object):
return self.suggestion_dict[key]
class Dy2StKeyError(Exception):
pass
class ErrorData(object):
"""
Error data attached to an exception which is raised in un-transformed code.
......@@ -159,6 +163,9 @@ class ErrorData(object):
def create_exception(self):
message = self.create_message()
if self.error_type is KeyError:
new_exception = Dy2StKeyError(message)
else:
new_exception = self.error_type(message)
setattr(new_exception, ERROR_DATA, self)
return new_exception
......
......@@ -443,6 +443,20 @@ class TestSuggestionErrorInRuntime(TestErrorBase):
for disable_new_error in [0, 1]:
self._test_raise_new_exception(disable_new_error)
@paddle.jit.to_static
def func_ker_error(x):
d = {
'x': x
}
y = d['y'] + x
return y
class TestKeyError(unittest.TestCase):
def test_key_error(self):
paddle.disable_static()
with self.assertRaises(error.Dy2StKeyError):
x = paddle.to_tensor([1])
func_ker_error(x)
if __name__ == '__main__':
unittest.main()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册