From 348e0d105e7b973aab6ce85acd900559af7a6f6d Mon Sep 17 00:00:00 2001 From: Chin Yeung Li Date: Fri, 13 Mar 2026 14:59:44 +0800 Subject: [PATCH 1/2] Fixed the crash error as token_string was not defined #125 Signed-off-by: Chin Yeung Li --- src/license_expression/__init__.py | 10 +++++++--- tests/test_license_expression.py | 9 +++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/license_expression/__init__.py b/src/license_expression/__init__.py index dc1ab31..84547b4 100644 --- a/src/license_expression/__init__.py +++ b/src/license_expression/__init__.py @@ -87,7 +87,9 @@ class ExpressionError(Exception): - pass + def __init__(self, message, token_string=None): + super().__init__(message) + self.token_string = token_string class ExpressionParseError(ParseError, ExpressionError): @@ -1210,7 +1212,8 @@ def __init__( raise ExpressionError( "Invalid license key: the valid characters are: letters and " "numbers, underscore, dot, colon or hyphen signs and " - f"spaces: {key!r}" + f"spaces: {key!r}", + token_string=f"{key!r}", ) # normalize spaces @@ -1219,7 +1222,8 @@ def __init__( if key.lower() in KEYWORDS_STRINGS: raise ExpressionError( 'Invalid license key: a key cannot be a reserved keyword: "or",' - f' "and" or "with": {key!r}' + f' "and" or "with": {key!r}', + token_string=f"{key!r}", ) self.key = key diff --git a/tests/test_license_expression.py b/tests/test_license_expression.py index 193fafd..82cebd5 100644 --- a/tests/test_license_expression.py +++ b/tests/test_license_expression.py @@ -2422,6 +2422,15 @@ def test_validation_invalid_license_key(self): assert result.errors == ["Unknown license key(s): cool-license"] assert result.invalid_symbols == ["cool-license"] + def test_validation_invalid_license_key_chara(self): + result = self.licensing.validate("cool,license") + assert result.original_expression == "cool,license" + assert not result.normalized_expression + assert result.errors == [ + "Invalid license key: the valid characters are: letters and numbers, underscore, dot, colon or hyphen signs and spaces: 'cool,license'" + ] + assert result.invalid_symbols == ["'cool,license'"] + def test_validate_exception(self): result = self.licensing.validate("GPL-2.0-or-later WITH WxWindows-exception-3.1") assert result.original_expression == "GPL-2.0-or-later WITH WxWindows-exception-3.1" From 867a5e6f000fa350111b3525d3c5af43cdc052ff Mon Sep 17 00:00:00 2001 From: Chin Yeung Li Date: Fri, 13 Mar 2026 17:54:03 +0800 Subject: [PATCH 2/2] Revert change that made for ExpressionError #125 - Update the try/except clause instead of modifying the ExpressionError class. Signed-off-by: Chin Yeung Li --- src/license_expression/__init__.py | 16 ++++++++-------- tests/test_license_expression.py | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/license_expression/__init__.py b/src/license_expression/__init__.py index 84547b4..21d44ef 100644 --- a/src/license_expression/__init__.py +++ b/src/license_expression/__init__.py @@ -87,9 +87,7 @@ class ExpressionError(Exception): - def __init__(self, message, token_string=None): - super().__init__(message) - self.token_string = token_string + pass class ExpressionParseError(ParseError, ExpressionError): @@ -787,10 +785,14 @@ def validate(self, expression, strict=True, **kwargs): # Check `expression` type and syntax try: parsed_expression = self.parse(expression, strict=strict) - except ExpressionError as e: + except ExpressionParseError as e: expression_info.errors.append(str(e)) expression_info.invalid_symbols.append(e.token_string) return expression_info + except ExpressionError as e: + expression_info.errors.append(str(e)) + expression_info.invalid_symbols.append(str(expression)) + return expression_info # Check `expression` keys (validate) try: @@ -1212,8 +1214,7 @@ def __init__( raise ExpressionError( "Invalid license key: the valid characters are: letters and " "numbers, underscore, dot, colon or hyphen signs and " - f"spaces: {key!r}", - token_string=f"{key!r}", + f"spaces: {key!r}" ) # normalize spaces @@ -1222,8 +1223,7 @@ def __init__( if key.lower() in KEYWORDS_STRINGS: raise ExpressionError( 'Invalid license key: a key cannot be a reserved keyword: "or",' - f' "and" or "with": {key!r}', - token_string=f"{key!r}", + f' "and" or "with": {key!r}' ) self.key = key diff --git a/tests/test_license_expression.py b/tests/test_license_expression.py index 82cebd5..a6a4306 100644 --- a/tests/test_license_expression.py +++ b/tests/test_license_expression.py @@ -2429,7 +2429,7 @@ def test_validation_invalid_license_key_chara(self): assert result.errors == [ "Invalid license key: the valid characters are: letters and numbers, underscore, dot, colon or hyphen signs and spaces: 'cool,license'" ] - assert result.invalid_symbols == ["'cool,license'"] + assert result.invalid_symbols == ["cool,license"] def test_validate_exception(self): result = self.licensing.validate("GPL-2.0-or-later WITH WxWindows-exception-3.1")