diff --git a/CHANGELOG.md b/CHANGELOG.md index 774c980..e0436eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # ChangeLog +# 2.0.15 +- Fixed `$Error.message` codegen to avoid accessing non-existent `Exception.message` in Python 3. + +# 2.0.14 +- Fixed inner module import path to use snake_case for exported dara filenames. + # 2.0.12 - Fixed the catch id error. @@ -35,4 +41,4 @@ - Fixed the Inner Import & Export # 2.0.0 -- Upgrade By Darbaonba V2 \ No newline at end of file +- Upgrade By Darbaonba V2 diff --git a/ChangeLog.md b/ChangeLog.md index c3ff935..e0436eb 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -1,5 +1,8 @@ # ChangeLog +# 2.0.15 +- Fixed `$Error.message` codegen to avoid accessing non-existent `Exception.message` in Python 3. + # 2.0.14 - Fixed inner module import path to use snake_case for exported dara filenames. @@ -38,4 +41,4 @@ - Fixed the Inner Import & Export # 2.0.0 -- Upgrade By Darbaonba V2 \ No newline at end of file +- Upgrade By Darbaonba V2 diff --git a/lib/generator.js b/lib/generator.js index 94879d0..5ffe914 100644 --- a/lib/generator.js +++ b/lib/generator.js @@ -1886,6 +1886,20 @@ setup( const suffix = env.left ? ']' : ')'; var current = ast.id.inferred; + // Python's base Exception has no `message` attribute (removed in Py3). + // Untyped catch is emitted as `except Exception`, so `$Error.message` + // must not become `error.message`. Prefer DaraException.message when + // present, otherwise fall back to str(error). + if (!env.left + && current + && current.type === 'model' + && current.name === '$Error' + && ast.propertyPath.length === 1 + && _name(ast.propertyPath[0]) === 'message') { + this.emit(`getattr(${expr}, 'message', str(${expr}))`); + return; + } + for (var i = 0; i < ast.propertyPath.length; i++) { var name = _name(ast.propertyPath[i]); if (current.type === 'model') { diff --git a/package.json b/package.json index ed97197..b6ccd74 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@darabonba/python-generator", - "version": "2.0.14", + "version": "2.0.15", "description": "The darabonba generator for Python", "main": "lib/generator.js", "directories": { diff --git a/tests/expected/try/tea_python_tests/__init__.py b/tests/expected/try/tea_python_tests/__init__.py new file mode 100644 index 0000000..5becc17 --- /dev/null +++ b/tests/expected/try/tea_python_tests/__init__.py @@ -0,0 +1 @@ +__version__ = "1.0.0" diff --git a/tests/expected/try/tea_python_tests/client.py b/tests/expected/try/tea_python_tests/client.py new file mode 100644 index 0000000..d0f0a0c --- /dev/null +++ b/tests/expected/try/tea_python_tests/client.py @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- +# This file is auto-generated, don't edit it. Thanks. +from __future__ import annotations + +from darabonba.exceptions import DaraException + +class Client: + + def __init__(self): + pass + + @staticmethod + def wrap_error() -> None: + try : + raise DaraException({ + 'message': 'boom' + }) + except Exception as error : + raise DaraException({ + 'message': getattr(error, 'message', str(error)) + }) + + + @staticmethod + def typed_error( + e: DaraException, + ) -> str: + return getattr(e, 'message', str(e)) diff --git a/tests/fixtures/try/Darafile b/tests/fixtures/try/Darafile new file mode 100644 index 0000000..ed384e2 --- /dev/null +++ b/tests/fixtures/try/Darafile @@ -0,0 +1,6 @@ +{ + "scope": "alibabacloud", + "name": "main", + "version": "0.0.1", + "main": "./main.dara" +} diff --git a/tests/fixtures/try/main.dara b/tests/fixtures/try/main.dara new file mode 100644 index 0000000..c14ab40 --- /dev/null +++ b/tests/fixtures/try/main.dara @@ -0,0 +1,15 @@ +static function wrapError(): void { + try { + throw { + message = "boom" + }; + } catch(error) { + throw { + message = error.message + }; + } +} + +static function typedError(e: $Error): string { + return e.message; +} diff --git a/tests/main.tests.js b/tests/main.tests.js index 32c20de..62a2aea 100644 --- a/tests/main.tests.js +++ b/tests/main.tests.js @@ -194,6 +194,16 @@ describe('Python Generator', function () { }); }); + it('try $Error.message should ok', function () { + check('try', 'tea_python_tests', + { + python: { + package: 'tea_python_tests', + clientName: 'client' + } + }); + }); + it('function should ok', function () { check('function', 'tea_python_tests', {