Skip to content

Commit b3cd50d

Browse files
authored
Merge pull request #509 from python/main
Sync Fork from Upstream Repo
2 parents f87f5e8 + 107a2c5 commit b3cd50d

7 files changed

Lines changed: 25 additions & 11 deletions

File tree

Doc/library/dis.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -936,7 +936,7 @@ All of the following opcodes use their arguments.
936936
.. versionadded:: 3.9
937937

938938

939-
.. opcode:: DICT_MERGE
939+
.. opcode:: DICT_MERGE (i)
940940

941941
Like :opcode:`DICT_UPDATE` but raises an exception for duplicate keys.
942942

Lib/test/string_tests.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,14 @@ class subtype(self.__class__.type2test):
8080
self.assertIsNot(obj, realresult)
8181

8282
# check that obj.method(*args) raises exc
83-
def checkraises(self, exc, obj, methodname, *args):
83+
def checkraises(self, exc, obj, methodname, *args, expected_msg=None):
8484
obj = self.fixtype(obj)
8585
args = self.fixtype(args)
8686
with self.assertRaises(exc) as cm:
8787
getattr(obj, methodname)(*args)
8888
self.assertNotEqual(str(cm.exception), '')
89+
if expected_msg is not None:
90+
self.assertEqual(str(cm.exception), expected_msg)
8991

9092
# call obj.method(*args) without any checks
9193
def checkcall(self, obj, methodname, *args):
@@ -1195,6 +1197,10 @@ def test_subscript(self):
11951197

11961198
self.checkraises(TypeError, 'abc', '__getitem__', 'def')
11971199

1200+
for idx_type in ('def', object()):
1201+
expected_msg = "string indices must be integers, not '{}'".format(type(idx_type).__name__)
1202+
self.checkraises(TypeError, 'abc', '__getitem__', idx_type, expected_msg=expected_msg)
1203+
11981204
def test_slice(self):
11991205
self.checkequal('abc', 'abc', '__getitem__', slice(0, 1000))
12001206
self.checkequal('abc', 'abc', '__getitem__', slice(0, 3))

Lib/test/test_userstring.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@ def checkequal(self, result, object, methodname, *args, **kwargs):
2727
realresult
2828
)
2929

30-
def checkraises(self, exc, obj, methodname, *args):
30+
def checkraises(self, exc, obj, methodname, *args, expected_msg=None):
3131
obj = self.fixtype(obj)
3232
# we don't fix the arguments, because UserString can't cope with it
3333
with self.assertRaises(exc) as cm:
3434
getattr(obj, methodname)(*args)
3535
self.assertNotEqual(str(cm.exception), '')
36+
if expected_msg is not None:
37+
self.assertEqual(str(cm.exception), expected_msg)
3638

3739
def checkcall(self, object, methodname, *args):
3840
object = self.fixtype(object)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve :func:`str.__getitem__` error message
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
Allow clearing the :mod:`sqlite3` authorizer callback by passing
2-
:const:``None`` to :meth:`~sqlite3.Connection.set_authorizer`. Patch by
2+
:const:`None` to :meth:`~sqlite3.Connection.set_authorizer`. Patch by
33
Erlend E. Aasland.

Objects/unicodeobject.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14279,7 +14279,8 @@ unicode_subscript(PyObject* self, PyObject* item)
1427914279
assert(_PyUnicode_CheckConsistency(result, 1));
1428014280
return result;
1428114281
} else {
14282-
PyErr_SetString(PyExc_TypeError, "string indices must be integers");
14282+
PyErr_Format(PyExc_TypeError, "string indices must be integers, not '%.200s'",
14283+
Py_TYPE(item)->tp_name);
1428314284
return NULL;
1428414285
}
1428514286
}

Parser/asdl_c.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,20 @@ def reflow_lines(s, depth):
7171
def reflow_c_string(s, depth):
7272
return '"%s"' % s.replace('\n', '\\n"\n%s"' % (' ' * depth * TABSIZE))
7373

74-
def is_simple(sum):
74+
def is_simple(sum_type):
7575
"""Return True if a sum is a simple.
7676
77-
A sum is simple if its types have no fields, e.g.
77+
A sum is simple if it's types have no fields and itself
78+
doesn't have any attributes. Instances of these types are
79+
cached at C level, and they act like singletons when propagating
80+
parser generated nodes into Python level, e.g.
7881
unaryop = Invert | Not | UAdd | USub
7982
"""
80-
for t in sum.types:
81-
if t.fields:
82-
return False
83-
return True
83+
84+
return not (
85+
sum_type.attributes or
86+
any(constructor.fields for constructor in sum_type.types)
87+
)
8488

8589
def asdl_of(name, obj):
8690
if isinstance(obj, asdl.Product) or isinstance(obj, asdl.Constructor):

0 commit comments

Comments
 (0)