-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
gh-83122: Deprecate testing element truth values in ElementTree
#31149
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
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
acd875f
bpo-38941: Warn when testing element truth values in C version of Ele…
jacobtylerwalls 7e0be26
Fix bug in implementation
jacobtylerwalls f4738d2
Merge branch 'main' into c-etree-bool-warning
jacobtylerwalls 331a577
Move test to Python suite
jacobtylerwalls 87e8ad7
Concision
jacobtylerwalls b114195
Merge branch 'main' into c-etree-bool-warning
jacobtylerwalls 4e0e117
FutureWarning -> DeprecationWarning
jacobtylerwalls 6825e38
add missing `:exc:`
jacobtylerwalls 67b9320
Return int
jacobtylerwalls eee712a
Add note to deprecated in 3.12
jacobtylerwalls f91bfd2
Return -1
jacobtylerwalls 37a9446
Adjust discussion of this usage in etree docs
jacobtylerwalls 424f0bb
Add Pending Removal notice
jacobtylerwalls 9b75171
Merge branch 'main' into c-etree-bool-warning
jacobtylerwalls e8bf37d
Merge branch 'main' into c-etree-bool-warning
jacobtylerwalls 3223126
Avoid mentioning messy history
jacobtylerwalls 22a1618
Adjust warning message and stacklevel
jacobtylerwalls 447d738
Merge branch 'c-etree-bool-warning' of https://github.com/jacobtylerw…
jacobtylerwalls File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
Misc/NEWS.d/next/Library/2022-02-05-12-01-58.bpo-38941.8IhvyG.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
The :mod:`xml.etree.ElementTree` module now emits :exc:`DeprecationWarning` | ||
when testing the truth value of an :class:`xml.etree.ElementTree.Element`. | ||
Before, the Python implementation emitted :exc:`FutureWarning`, and the C | ||
implementation emitted nothing. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1449,6 +1449,23 @@ element_getitem(PyObject* self_, Py_ssize_t index) | |
return Py_NewRef(self->extra->children[index]); | ||
} | ||
|
||
static int | ||
element_bool(PyObject* self_) | ||
{ | ||
ElementObject* self = (ElementObject*) self_; | ||
if (PyErr_WarnEx(PyExc_DeprecationWarning, | ||
"Testing an element's truth value will raise an exception " | ||
"in future versions. Use specific 'len(elem)' or " | ||
"'elem is not None' test instead.", | ||
1) < 0) { | ||
return -1; | ||
}; | ||
if (self->extra ? self->extra->length : 0) { | ||
return 1; | ||
} | ||
return 0; | ||
Comment on lines
+1463
to
+1466
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It could be simply return self->extra && self->extra->length; |
||
} | ||
|
||
/*[clinic input] | ||
_elementtree.Element.insert | ||
|
||
|
@@ -4156,6 +4173,7 @@ static PyType_Slot element_slots[] = { | |
{Py_sq_length, element_length}, | ||
{Py_sq_item, element_getitem}, | ||
{Py_sq_ass_item, element_setitem}, | ||
{Py_nb_bool, element_bool}, | ||
{Py_mp_length, element_length}, | ||
{Py_mp_subscript, element_subscr}, | ||
{Py_mp_ass_subscript, element_ass_subscr}, | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.