Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion Doc/howto/functional.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1042,7 +1042,7 @@ first calculation. ::
>>> functools.reduce(operator.concat, [])
Traceback (most recent call last):
...
TypeError: reduce() of empty sequence with no initial value
TypeError: reduce() of empty iterable with no initial value
>>> functools.reduce(operator.mul, [1, 2, 3], 1)
6
>>> functools.reduce(operator.mul, [], 1)
Expand Down
6 changes: 0 additions & 6 deletions Lib/shutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -1307,12 +1307,6 @@ def unregister_unpack_format(name):
"""Removes the pack format from the registry."""
del _UNPACK_FORMATS[name]

def _ensure_directory(path):
"""Ensure that the parent directory of `path` exists"""
dirname = os.path.dirname(path)
if not os.path.isdir(dirname):
os.makedirs(dirname)

def _unpack_zipfile(filename, extract_dir):
"""Unpack zip `filename` to `extract_dir`
"""
Expand Down
15 changes: 15 additions & 0 deletions Lib/test/test_bz2.py
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,21 @@ def test_failure(self):
# Previously, a second call could crash due to internal inconsistency
self.assertRaises(Exception, bzd.decompress, self.BAD_DATA * 30)

def test_decompress_after_data_error(self):
data = bytes.fromhex(
"425a6839314159265359000000000000007fffff000000000000000000000000"
"00000000000000000000000000000000000000e0370000000000000000000000"
"000000000000000000000000000000000000000000000000000083f3"
)
bzd = BZ2Decompressor()
with self.assertRaisesRegex(OSError, "Invalid data stream"):
bzd.decompress(data)
# Previously, a second call could crash due to internal inconsistency
self.assertFalse(bzd.needs_input)
self.assertFalse(bzd.eof)
with self.assertRaisesRegex(ValueError, "previous error"):
bzd.decompress(b'\x00' * 18)

@support.refcount_test
def test_refleaks_in___init__(self):
gettotalrefcount = support.get_attribute(sys, 'gettotalrefcount')
Expand Down
2 changes: 2 additions & 0 deletions Lib/test/test_mmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,8 @@ def test_find_end(self):
self.assertEqual(m.find(b'one', 1, -1), 8)
self.assertEqual(m.find(b'one', 1, -2), -1)
self.assertEqual(m.find(bytearray(b'one')), 0)
self.assertEqual(m.find(b'', n + 1), -1)
self.assertEqual(m.rfind(b'', n + 1), -1)

for i in range(-n-1, n+1):
for j in range(-n-1, n+1):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Speed up frame local variable item collection by appending result pairs to the
output list without an extra reference-count round-trip (using the internal
reference-stealing list append helper). Patch by Omkar Kabde.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Speed up :func:`re.findall`, :func:`re.sub` and :func:`re.subn` by appending
result items to the output list without an extra reference-count round-trip
(using the internal reference-stealing list append helper).
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix :meth:`mmap.mmap.find` and :meth:`~mmap.mmap.rfind` to return ``-1``
when searching for an empty subsequence with a start position past the end
of the mapping.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix a possible stack buffer overflow in :mod:`bz2` when a
:class:`bz2.BZ2Decompressor` is reused after a decompression error.
The decompressor now becomes unusable after libbz2 reports an error.
18 changes: 15 additions & 3 deletions Modules/_bz2module.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ typedef struct {
typedef struct {
PyObject_HEAD
bz_stream bzs;
int bzerror;
char eof; /* Py_T_BOOL expects a char */
PyObject *unused_data;
char needs_input;
Expand Down Expand Up @@ -435,8 +436,11 @@ decompress_buf(BZ2Decompressor *d, Py_ssize_t max_length)

d->bzs_avail_in_real += bzs->avail_in;

if (catch_bz2_error(bzret))
if (catch_bz2_error(bzret)) {
d->bzerror = bzret;
FT_ATOMIC_STORE_CHAR_RELAXED(d->needs_input, 0);
goto error;
}
if (bzret == BZ_STREAM_END) {
FT_ATOMIC_STORE_CHAR_RELAXED(d->eof, 1);
break;
Expand Down Expand Up @@ -607,10 +611,17 @@ _bz2_BZ2Decompressor_decompress_impl(BZ2Decompressor *self, Py_buffer *data,
PyObject *result = NULL;

PyMutex_Lock(&self->mutex);
if (self->eof)
if (self->eof) {
PyErr_SetString(PyExc_EOFError, "End of stream already reached");
else
}
else if (self->bzerror) {
// Re-entering BZ2_bzDecompress() after an error can write out of bounds.
PyErr_SetString(PyExc_ValueError,
"Decompressor is unusable after a previous error");
}
else {
result = decompress(self, data->buf, data->len, max_length);
}
PyMutex_Unlock(&self->mutex);
return result;
}
Expand Down Expand Up @@ -638,6 +649,7 @@ _bz2_BZ2Decompressor_impl(PyTypeObject *type)
}

self->mutex = (PyMutex){0};
self->bzerror = 0;
self->needs_input = 1;
self->bzs_avail_in_real = 0;
self->input_buffer = NULL;
Expand Down
13 changes: 5 additions & 8 deletions Modules/_sre/sre.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ static const char copyright[] =
#include "pycore_critical_section.h" // Py_BEGIN_CRITICAL_SECTION
#include "pycore_dict.h" // _PyDict_Next()
#include "pycore_long.h" // _PyLong_GetZero()
#include "pycore_list.h" // _PyList_AppendTakeRef()
#include "pycore_moduleobject.h" // _PyModule_GetState()
#include "pycore_tuple.h" // _PyTuple_FromPairSteal
#include "pycore_unicodeobject.h" // _PyUnicode_Copy
Expand Down Expand Up @@ -986,8 +987,7 @@ _sre_SRE_Pattern_findall_impl(PatternObject *self, PyObject *string,
break;
}

status = PyList_Append(list, item);
Py_DECREF(item);
status = _PyList_AppendTakeRef((PyListObject *)list, item);
if (status < 0)
goto error;

Expand Down Expand Up @@ -1333,8 +1333,7 @@ pattern_subx(_sremodulestate* module_state,
string, i, b);
if (!item)
goto error;
status = PyList_Append(list, item);
Py_DECREF(item);
status = _PyList_AppendTakeRef((PyListObject *)list, item);
if (status < 0)
goto error;

Expand Down Expand Up @@ -1363,8 +1362,7 @@ pattern_subx(_sremodulestate* module_state,

/* add to list */
if (item != Py_None) {
status = PyList_Append(list, item);
Py_DECREF(item);
status = _PyList_AppendTakeRef((PyListObject *)list, item);
if (status < 0)
goto error;
}
Expand All @@ -1381,8 +1379,7 @@ pattern_subx(_sremodulestate* module_state,
string, i, state.endpos);
if (!item)
goto error;
status = PyList_Append(list, item);
Py_DECREF(item);
status = _PyList_AppendTakeRef((PyListObject *)list, item);
if (status < 0)
goto error;
}
Expand Down
2 changes: 0 additions & 2 deletions Modules/mmapmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -620,8 +620,6 @@ mmap_gfind_lock_held(mmap_object *self, Py_buffer *view, PyObject *start_obj,
start += self->size;
if (start < 0)
start = 0;
else if (start > self->size)
start = self->size;

if (end < 0)
end += self->size;
Expand Down
9 changes: 3 additions & 6 deletions Objects/frameobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "pycore_function.h" // _PyFunction_FromConstructor()
#include "pycore_genobject.h" // _PyGen_GetGeneratorFromFrame()
#include "pycore_interpframe.h" // _PyFrame_GetLocalsArray()
#include "pycore_list.h" // _PyList_AppendTakeRef()
#include "pycore_modsupport.h" // _PyArg_CheckPositional()
#include "pycore_object.h" // _PyObject_GC_UNTRACK()
#include "pycore_opcode_metadata.h" // _PyOpcode_Caches
Expand Down Expand Up @@ -636,9 +637,7 @@ framelocalsproxy_items(PyObject *self, PyObject *Py_UNUSED(ignored))
goto error;
}

int rc = PyList_Append(items, pair);
Py_DECREF(pair);
if (rc < 0) {
if (_PyList_AppendTakeRef((PyListObject *)items, pair) < 0) {
goto error;
}
}
Expand All @@ -655,9 +654,7 @@ framelocalsproxy_items(PyObject *self, PyObject *Py_UNUSED(ignored))
goto error;
}

int rc = PyList_Append(items, pair);
Py_DECREF(pair);
if (rc < 0) {
if (_PyList_AppendTakeRef((PyListObject *)items, pair) < 0) {
goto error;
}
}
Expand Down
Loading