Skip to content

bpo-40624: Add support for the XPath != operator in xml.etree #22147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 9, 2020

Conversation

ammaraskar
Copy link
Member

@ammaraskar ammaraskar commented Sep 8, 2020

Fairly simple implementation that uses the existing code for = and negates the condition if it's a !=.

https://bugs.python.org/issue40624

Copy link
Contributor

@scoder scoder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a pretty good start. Some comments below.

Comment on lines 264 to 266
matches = elem.get(key) == value
if negate_predicate:
matches = not matches
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look correct. If the element does not have such an attribute, .get() will return None, which probably does not compare equal to value. Negating that gives True. However, @attr != "value" should only match if there is an attribute attr that compares unequal.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aah good catch, thank you! Fixed.

negate_predicate = '!' in signature
def select(context, result):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use separate functions for performance reasons. They are deeply nested and a single instruction less can make a visible difference on large enough data. This implementation probably adds 4 byte code instructions to the inner loop.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha, added a separate function for the negated conditions.

Copy link
Contributor

@scoder scoder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs changes.

@bedevere-bot
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@ammaraskar
Copy link
Member Author

I have made the requested changes; please review again

Also added documentation that I realized I missed.

@bedevere-bot
Copy link

Thanks for making the requested changes!

@scoder: please review the changes made to this pull request.

@bedevere-bot bedevere-bot requested a review from scoder September 8, 2020 17:05
@ammaraskar
Copy link
Member Author

Test failure looks unrelated.

Copy link
Contributor

@scoder scoder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good now. A few more suggestions.

@scoder scoder merged commit 97e8b1e into python:master Nov 9, 2020
@scoder
Copy link
Contributor

scoder commented Nov 9, 2020

Thanks!

adorilson pushed a commit to adorilson/cpython that referenced this pull request Mar 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants