Skip to content

Commit 4f523dc

Browse files
committed
byte string
1 parent adcc3b3 commit 4f523dc

3 files changed

Lines changed: 37 additions & 32 deletions

File tree

Lib/test/test_capi/test_opt.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2293,12 +2293,14 @@ def testfunc(n):
22932293
_ = len("abc")
22942294
d = ''
22952295
_ = len(d)
2296+
_ = len(b"def")
2297+
_ = len(b"")
22962298

22972299
_, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
22982300
self.assertIsNotNone(ex)
22992301
uops = get_opnames(ex)
23002302
self.assertNotIn("_CALL_LEN", uops)
2301-
self.assertIn("_SHUFFLE_3_LOAD_CONST_INLINE_BORROW", uops)
2303+
self.assertEqual(count_ops(ex, "_SHUFFLE_3_LOAD_CONST_INLINE_BORROW"), 4)
23022304

23032305
def test_call_len_known_length_small_int(self):
23042306
# Make sure that len(t) is optimized for a tuple of length 5.

Python/optimizer_bytecodes.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,28 +1417,28 @@ dummy_func(void) {
14171417

14181418
op(_CALL_LEN, (callable, null, arg -- res, a, c)) {
14191419
res = sym_new_type(ctx, &PyLong_Type);
1420-
PyObject *temp = NULL;
1421-
14221420
Py_ssize_t length = sym_tuple_length(arg);
1423-
if (length >= 0) {
1424-
temp = PyLong_FromSsize_t(length);
1425-
if (temp == NULL) {
1426-
goto error;
1427-
}
1428-
}
1429-
else if (sym_is_const(ctx, arg)) {
1421+
1422+
// Not a tuple, check if it's a const string
1423+
if (length < 0 && sym_is_const(ctx, arg)) {
14301424
PyObject *const_val = sym_get_const(ctx, arg);
1431-
if (const_val != NULL && PyUnicode_CheckExact(const_val)) {
1432-
temp = PyLong_FromSsize_t(PyUnicode_GET_LENGTH(const_val));
1433-
if (temp == NULL) {
1434-
goto error;
1425+
if (const_val != NULL) {
1426+
if (PyUnicode_CheckExact(const_val)) {
1427+
length = PyUnicode_GET_LENGTH(const_val);
1428+
}
1429+
else if (PyBytes_CheckExact(const_val)) {
1430+
length = PyBytes_GET_SIZE(const_val);
14351431
}
14361432
}
14371433
}
1438-
if (temp != NULL) {
1434+
1435+
if (length >= 0) {
1436+
PyObject *temp = PyLong_FromSsize_t(length);
1437+
if (temp == NULL) {
1438+
goto error;
1439+
}
14391440
if (_Py_IsImmortal(temp)) {
1440-
ADD_OP(_SHUFFLE_3_LOAD_CONST_INLINE_BORROW,
1441-
0, (uintptr_t)temp);
1441+
ADD_OP(_SHUFFLE_3_LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)temp);
14421442
}
14431443
res = sym_new_const(ctx, temp);
14441444
Py_DECREF(temp);

Python/optimizer_cases.c.h

Lines changed: 18 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)