diff --git a/src/packagedcode/build.py b/src/packagedcode/build.py index 2f2d964758..8044f8c245 100644 --- a/src/packagedcode/build.py +++ b/src/packagedcode/build.py @@ -163,14 +163,14 @@ def parse(cls, location, package_only=False): args = {} for kw in statement.value.keywords: arg_name = kw.arg - if isinstance(kw.value, ast.Str): - args[arg_name] = kw.value.s + if isinstance(kw.value, ast.Constant) and isinstance(kw.value.value, str): + args[arg_name] = kw.value.value if isinstance(kw.value, ast.List): # We collect the elements of a list if the element is # not a function call args[arg_name] = [ - elt.s for elt in kw.value.elts + elt.value for elt in kw.value.elts if not isinstance(elt, ast.Call) ] if args: @@ -351,17 +351,17 @@ def parse(cls, location, package_only=True): statement_keys = statement.value.keys statement_values = statement.value.values for statement_k, statement_v in zip(statement_keys, statement_values): - if isinstance(statement_k, ast.Str): - key_name = statement_k.s + if isinstance(statement_k, ast.Constant) and isinstance(statement_k.value, str): + key_name = statement_k.value # The list values in a `METADATA.bzl` file seem to only contain strings if isinstance(statement_v, ast.List): value = [] for e in statement_v.elts: - if not isinstance(e, ast.Str): + if not (isinstance(e, ast.Constant) and isinstance(e.value, str)): continue - value.append(e.s) - if isinstance(statement_v, (ast.Str, ast.Constant)): - value = statement_v.s + value.append(e.value) + if isinstance(statement_v, ast.Constant): + value = statement_v.value metadata_fields[key_name] = value parties = [] diff --git a/src/packagedcode/pypi.py b/src/packagedcode/pypi.py index 2610f43a01..761c149a40 100644 --- a/src/packagedcode/pypi.py +++ b/src/packagedcode/pypi.py @@ -2157,8 +2157,8 @@ def get_setup_py_args_legacy(location, include_not_parsable=False): ] values.append(val) - elif isinstance(val, (ast.Str, ast.Constant,)): - values.append(val.s) + elif isinstance(val, ast.Constant): + values.append(val.value) else: if include_not_parsable: @@ -2183,8 +2183,8 @@ def get_setup_py_args_legacy(location, include_not_parsable=False): mapping = dict(zip(keys, values)) setup_args[arg_name] = mapping - elif isinstance(arg_value, (ast.Str, ast.Constant,)): - setup_args[arg_name] = arg_value.s + elif isinstance(arg_value, ast.Constant): + setup_args[arg_name] = arg_value.value else: if include_not_parsable: if isinstance(arg_value, ast.Attribute): diff --git a/src/packagedcode/pypi_setup_py.py b/src/packagedcode/pypi_setup_py.py index 5e2559af12..51a94313d2 100644 --- a/src/packagedcode/pypi_setup_py.py +++ b/src/packagedcode/pypi_setup_py.py @@ -119,15 +119,9 @@ def node_to_value(node, body): """ if node is None: return - if hasattr(ast, 'Constant'): - if isinstance(node, ast.Constant): - return node.value - if isinstance(node, ast.Str): - return node.s - - if isinstance(node, ast.Num): - return node.n + if isinstance(node, ast.Constant): + return node.value if isinstance(node, (ast.List, ast.Tuple, ast.Set,)): return [node_to_value(subnode, body) for subnode in node.elts]