Skip to content

Commit

Permalink
Backward compatibility wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
Hook25 committed Nov 12, 2024
1 parent 63cc852 commit 3c3f501
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions checkbox-ng/plainbox/impl/new_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,13 @@ def __call__(self, *args):
def from_unary_op(cls, parsed_ast):
if not isinstance(parsed_ast.op, ast.USub):
raise ValueError("Unsupported operator {}".format(parsed_ast))

Check warning on line 173 in checkbox-ng/plainbox/impl/new_resource.py

View check run for this annotation

Codecov / codecov/patch

checkbox-ng/plainbox/impl/new_resource.py#L173

Added line #L173 was not covered by tests
parsed_ast.operand.value *= -1
return cls(parsed_ast.operand)
operand = getter_from_ast(parsed_ast.operand)
if not isinstance(operand, ConstantGetter):
raise ValueError(

Check warning on line 176 in checkbox-ng/plainbox/impl/new_resource.py

View check run for this annotation

Codecov / codecov/patch

checkbox-ng/plainbox/impl/new_resource.py#L176

Added line #L176 was not covered by tests
"`-` operator can't be applied to non-constant operands"
)
operand.value *= -1
return cls(operand)

def __str__(self):
return str(self.value)
Expand Down Expand Up @@ -219,11 +224,25 @@ def __str__(self):

legacy_getters = {}
if sys.version_info[0] == 3 and sys.version_info[1] < 8:
# older version of python have
from collections import namedtuple

# older version of python have slightly different nodes to parse
# constants. Here we wrap them for forward compatibility putting the old
# attribute where the ConstantGetter expects to find it
Wrapper = namedtuple("Wrapper", ["value"])

def wrapping(attr):
def _f(parsed_ast):
wrapped_parsed_ast = Wrapper(getattr(parsed_ast, attr))
return ConstantGetter(wrapped_parsed_ast)

return _f

legacy_getters = {
ast.Str: ConstantGetter,
ast.Num: ConstantGetter,
ast.Bytes: ConstantGetter,
ast.Str: wrapping("s"),
ast.Num: wrapping("n"),
ast.Bytes: wrapping("s"),
# this actually uses .value
ast.NameConstant: ConstantGetter,
}

Expand Down

0 comments on commit 3c3f501

Please sign in to comment.