emory-libraries / eulxml

Utilities for using XPath to map XML data to Python objects and Django forms
http://eulxml.readthedocs.org
38 stars 12 forks source link

XPath AST serialization does not account for operator precedence #44

Open altin opened 3 years ago

altin commented 3 years ago

Serializing an XPath AST does not wrap expressions with higher precedence in parentheses, changing the meaning of the resultant XPath.

Example:

from eulxml import xpath
ast = xpath.parse('cond1 and (cond2 or cond3 or cond4)')
# >>> <BinaryExpression cond1 and cond2 or cond3 or cond4>
print(xpath.ast.serialize(ast))
# >>> cond1 and cond2 or cond3 or cond4 
# CORRECT >>> cond1 and (cond2 or cond3 or cond4)

cond1 and cond2 or cond3 or cond4 and cond1 and (cond2 or cond3 or cond4) are not equivalent.

It looks like this bug is in the serialization logic (ast.py) and not in the parsing logic, as the AST appears to have the correct hierarchy when indicating precedence using parentheses.