8000 bpo-30061: Check if PyObject_Size()/PySequence_Size()/PyMapping_Size() by serhiy-storchaka · Pull Request #1096 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-30061: Check if PyObject_Size()/PySequence_Size()/PyMapping_Size() #1096

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
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add a Misc/NEWS entry and optimize set checks.
  • Loading branch information
serhiy-storchaka committed Apr 19, 2017
commit 25cc5b5cc529d48abf8682186d4ffe39d82c1213
5 changes: 5 additions & 0 deletions Misc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,11 @@ Extension Modules
Library
-------

- bpo-30061: Fixed crashes in IOBase methods __next__() and readlines() when
readline() or __next__() respectively return non-sizeable object.
Fixed possible other errors caused by not checking results of PyObject_Size(),
PySequence_Size(), or PyMapping_Size().

- bpo-10076: Compiled regular expression and match objects in the re module
now support copy.copy() and copy.deepcopy() (they are considered atomic).

Expand Down
10 changes: 7 additions & 3 deletions Objects/setobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1553,14 +1553,18 @@ set_difference(PySetObject *so, PyObject *other)
return set_copy(so);
}

if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
if (PyAnySet_Check(other) {
other_size = PySet_GET_SIZE(other);
}
else if (PyDict_CheckExact(other)) {
other_size = PyDict_GET_SIZE(other);
}
else {
return set_copy_and_difference(so, other);
}

/* If len(so) much more than len(other), it's more efficient to simply copy
* so and then iterate other looking for common elements. */
other_size = PyDict_CheckExact(other) ? PyDict_GET_SIZE(other)
: PySet_GET_SIZE(other);
if ((PySet_GET_SIZE(so) >> 2) > other_size) {
return set_copy_and_difference(so, other);
}
Expand Down
0