From 8813ef1d0a2ce349467b3495b525b5808abfaf5f Mon Sep 17 00:00:00 2001 From: Carmen Bianca Bakker Date: Wed, 22 May 2019 17:00:55 +0200 Subject: [PATCH 1/3] Licensing.parse: Raise ExpressionParseError instead of ParseError ExpressionParseError is a subclass of both ParseError and ExpressionError. This allows the user to do `except ParseError` as they had done before, and nothing will break. If the user does `except ExpressionError`, the ParseErrors will now also be caught. The advantage of doing this is that `except ExpressionError` now catches all exceptions coming out of this method. This is therefore a breaking change. Signed-off-by: Carmen Bianca Bakker --- src/license_expression/__init__.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/license_expression/__init__.py b/src/license_expression/__init__.py index c7f233a..72e6f55 100644 --- a/src/license_expression/__init__.py +++ b/src/license_expression/__init__.py @@ -103,6 +103,10 @@ class ExpressionError(Exception): pass +class ExpressionParseError(ParseError, ExpressionError): + pass + + # Used for tokenizing Keyword = namedtuple('Keyword', 'value type') Keyword.__len__ = lambda self: len(self.value) @@ -380,7 +384,7 @@ def parse(self, expression, validate=False, strict=False, simple=False, **kwargs """ Return a new license LicenseExpression object by parsing a license `expression` string. Check that the expression syntax is valid and raise - an Exception, an ExpressionError or a ParseError on errors. + an ExpressionError or an ExpressionParseError on errors. Return None for empty expressions. `expression` is either a string or a LicenseExpression object. If this is a LicenseExpression it is returned as-is. @@ -433,9 +437,11 @@ def parse(self, expression, validate=False, strict=False, simple=False, **kwargs # this will raise a ParseError on errors tokens = list(self.tokenize(expression, strict=strict, simple=simple)) expression = super(Licensing, self).parse(tokens) - except TypeError as e: - msg = 'Invalid expression syntax: ' + repr(e) - raise ExpressionError(msg) + except ParseError as e: + new_error = ExpressionParseError( + token_type=e.token_type, token_string=e.token_string, + position=e.position, error_code=e.error_code) + raise new_error from e if not isinstance(expression, LicenseExpression): raise ExpressionError('expression must be a LicenseExpression once parsed.') From 9a33a899af2affb049d320a95ddf631e61c94dce Mon Sep 17 00:00:00 2001 From: Carmen Bianca Bakker Date: Wed, 22 May 2019 17:08:03 +0200 Subject: [PATCH 2/3] Add self to AUTHORS.rst Signed-off-by: Carmen Bianca Bakker --- AUTHORS.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS.rst b/AUTHORS.rst index 4377268..997e20d 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -3,3 +3,4 @@ The following organizations or individuals have contributed to this code: - nexB Inc. @nexB - Philippe Ombredanne @pombredanne - Thomas Druez @tdruez +- Carmen Bianca Bakker @carmenbianca From 84a5621f554a2e1b641c4a7bf49cdf7ea2fe03df Mon Sep 17 00:00:00 2001 From: Carmen Bianca Bakker Date: Wed, 22 May 2019 19:11:31 +0200 Subject: [PATCH 3/3] Use Python 2 syntax for compatibility Signed-off-by: Carmen Bianca Bakker --- src/license_expression/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/license_expression/__init__.py b/src/license_expression/__init__.py index 72e6f55..f856e82 100644 --- a/src/license_expression/__init__.py +++ b/src/license_expression/__init__.py @@ -441,7 +441,7 @@ def parse(self, expression, validate=False, strict=False, simple=False, **kwargs new_error = ExpressionParseError( token_type=e.token_type, token_string=e.token_string, position=e.position, error_code=e.error_code) - raise new_error from e + raise new_error if not isinstance(expression, LicenseExpression): raise ExpressionError('expression must be a LicenseExpression once parsed.')