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,179 changes: 1,097 additions & 1,082 deletions Include/internal/pycore_uop_ids.h

Large diffs are not rendered by default.

95 changes: 79 additions & 16 deletions Include/internal/pycore_uop_metadata.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Lib/encodings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from . import aliases

_cache = {}
_MAXCACHE = 500
_unknown = '--unknown--'
_import_tail = ['*']
_aliases = aliases.aliases
Expand Down Expand Up @@ -111,6 +112,8 @@ def search_function(encoding):

if mod is None:
# Cache misses
if len(_cache) >= _MAXCACHE:
_cache.clear()
_cache[encoding] = None
return None

Expand All @@ -132,6 +135,8 @@ def search_function(encoding):
entry = codecs.CodecInfo(*entry)

# Cache the codec registry entry
if len(_cache) >= _MAXCACHE:
_cache.clear()
_cache[encoding] = entry

# Register its aliases (without overwriting previously registered
Expand Down
16 changes: 16 additions & 0 deletions Lib/test/test_capi/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -3981,6 +3981,22 @@ def testfunc(n):
self.assertIn("_POP_TOP_NOP", uops)
self.assertLessEqual(count_ops(ex, "_POP_TOP"), 2)

def test_iter_check_list(self):
def testfunc(n):
x = 0
for _ in range(n):
l = [1]
for num in l: # unguarded
x += num
return x

res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
self.assertEqual(res, TIER2_THRESHOLD)
uops = get_opnames(ex)

self.assertIn("_BUILD_LIST", uops)
self.assertNotIn("_ITER_CHECK_LIST", uops)

def test_match_class(self):
def testfunc(n):
class A:
Expand Down
11 changes: 11 additions & 0 deletions Lib/test/test_codecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3908,5 +3908,16 @@ def test_encodings_normalize_encoding(self):
self.assertEqual(normalize('utf\xE9\u20AC\U0010ffff-8'), 'utf_8')


class CodecCacheTest(unittest.TestCase):
def test_cache_bounded(self):
for i in range(encodings._MAXCACHE + 1000):
try:
b'x'.decode(f'nonexist_{i}')
except LookupError:
pass

self.assertLessEqual(len(encodings._cache), encodings._MAXCACHE)


if __name__ == "__main__":
unittest.main()
13 changes: 13 additions & 0 deletions Lib/test/test_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -1280,6 +1280,19 @@ class mydialect(csv.Dialect):
self.assertRaises(ValueError, create_invalid, field_name, " ",
skipinitialspace=True)

def test_dialect_getattr_non_attribute_error_propagates(self):
# gh-145966: non-AttributeError exceptions raised by __getattr__
# during dialect attribute lookup must propagate, not be silenced.
class BadDialect:
def __getattr__(self, name):
raise RuntimeError("boom")

with self.assertRaises(RuntimeError):
csv.reader([], dialect=BadDialect())

with self.assertRaises(RuntimeError):
csv.writer(StringIO(), dialect=BadDialect())


class TestSniffer(unittest.TestCase):
sample1 = """\
Expand Down
37 changes: 37 additions & 0 deletions Lib/test/test_wave.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,43 @@ def test_open_in_write_raises(self):
support.gc_collect()
self.assertIsNone(cm.unraisable)

@support.subTests('arg', (
# rounds to 0, should raise:
0.5,
0.4,
# Negative values should still raise:
-1,
-0.5,
-0.4,
# 0 should raise:
0,
))
def test_setframerate_validates_rounded_values(self, arg):
"""Test that setframerate that round to 0 or negative are rejected"""
with wave.open(io.BytesIO(), 'wb') as f:
f.setnchannels(1)
f.setsampwidth(2)
with self.assertRaises(wave.Error):
f.setframerate(arg)
with self.assertRaises(wave.Error):
f.close()

@support.subTests(('arg', 'expected'), (
(1.4, 1),
(1.5, 2),
(1.6, 2),
(44100.4, 44100),
(44100.5, 44100),
(44100.6, 44101),
))
def test_setframerate_rounds(self, arg, expected):
"""Test that setframerate is rounded"""
with wave.open(io.BytesIO(), 'wb') as f:
f.setnchannels(1)
f.setsampwidth(2)
f.setframerate(arg)
self.assertEqual(f.getframerate(), expected)


class WaveOpen(unittest.TestCase):
def test_open_pathlike(self):
Expand Down
3 changes: 2 additions & 1 deletion Lib/wave.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,9 +493,10 @@ def getsampwidth(self):
def setframerate(self, framerate):
if self._datawritten:
raise Error('cannot change parameters after starting to write')
framerate = int(round(framerate))
if framerate <= 0:
raise Error('bad frame rate')
self._framerate = int(round(framerate))
self._framerate = framerate

def getframerate(self):
if not self._framerate:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Optimize ``_ITER_CHECK_RANGE`` and ``_ITER_CHECK_LIST`` in the JIT
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:meth:`wave.Wave_write.setframerate` now validates the frame rate after
rounding to an integer, preventing values like ``0.5`` from being accepted
and causing confusing errors later. Patch by Michiel Beijen.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Non-:exc:`AttributeError` exceptions raised during dialect attribute lookup
in :mod:`csv` are no longer silently suppressed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Limit the size of :func:`encodings.search_function` cache.
Found by OSS Fuzz in :oss-fuzz:`493449985`.
14 changes: 7 additions & 7 deletions Modules/_csv.c
Original file line number Diff line number Diff line change
Expand Up @@ -497,13 +497,13 @@ dialect_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
Py_XINCREF(skipinitialspace);
Py_XINCREF(strict);
if (dialect != NULL) {
#define DIALECT_GETATTR(v, n) \
do { \
if (v == NULL) { \
v = PyObject_GetAttrString(dialect, n); \
if (v == NULL) \
PyErr_Clear(); \
} \
#define DIALECT_GETATTR(v, n) \
do { \
if (v == NULL) { \
if (PyObject_GetOptionalAttrString(dialect, n, &v) < 0) { \
goto err; \
} \
} \
} while (0)
DIALECT_GETATTR(delimiter, "delimiter");
DIALECT_GETATTR(doublequote, "doublequote");
Expand Down
33 changes: 31 additions & 2 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -5755,10 +5755,39 @@ dummy_func(
Py_UNREACHABLE();
}

tier2 op(_GUARD_CODE_VERSION, (version/2 -- )) {
tier2 op(_GUARD_CODE_VERSION__PUSH_FRAME, (version/2 -- )) {
PyObject *code = PyStackRef_AsPyObjectBorrow(frame->f_executable);
assert(PyCode_Check(code));
EXIT_IF(((PyCodeObject *)code)->co_version != version);
if (((PyCodeObject *)code)->co_version != version) {
EXIT_IF(true);
}
}

tier2 op(_GUARD_CODE_VERSION_YIELD_VALUE, (version/2 -- )) {
PyObject *code = PyStackRef_AsPyObjectBorrow(frame->f_executable);
assert(PyCode_Check(code));
if (((PyCodeObject *)code)->co_version != version) {
frame->instr_ptr += 1 + INLINE_CACHE_ENTRIES_SEND;
EXIT_IF(true);
}
}

tier2 op(_GUARD_CODE_VERSION_RETURN_VALUE, (version/2 -- )) {
PyObject *code = PyStackRef_AsPyObjectBorrow(frame->f_executable);
assert(PyCode_Check(code));
if (((PyCodeObject *)code)->co_version != version) {
frame->instr_ptr += frame->return_offset;
EXIT_IF(true);
}
}

tier2 op(_GUARD_CODE_VERSION_RETURN_GENERATOR, (version/2 -- )) {
PyObject *code = PyStackRef_AsPyObjectBorrow(frame->f_executable);
assert(PyCode_Check(code));
if (((PyCodeObject *)code)->co_version != version) {
frame->instr_ptr += frame->return_offset;
EXIT_IF(true);
}
}

tier2 op(_GUARD_IP__PUSH_FRAME, (ip/4 --)) {
Expand Down
Loading
Loading