Skip to content
Open
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
4 changes: 2 additions & 2 deletions Lib/operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def xor(a, b):

def concat(a, b):
"Same as a + b, for a and b sequences."
if not hasattr(a, '__getitem__'):
if not hasattr(type(a), '__getitem__'):
msg = "'%s' object can't be concatenated" % type(a).__name__
raise TypeError(msg)
return a + b
Expand Down Expand Up @@ -356,7 +356,7 @@ def iand(a, b):

def iconcat(a, b):
"Same as a += b, for a and b sequences."
if not hasattr(a, '__getitem__'):
if not hasattr(type(a), '__getitem__'):
msg = "'%s' object can't be concatenated" % type(a).__name__
raise TypeError(msg)
a += b
Expand Down
13 changes: 13 additions & 0 deletions Lib/test/test_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ def test_concat(self):
self.assertEqual(operator.concat(Seq1([5, 6]), Seq1([7])), [5, 6, 7])
self.assertEqual(operator.concat(Seq2([5, 6]), Seq2([7])), [5, 6, 7])
self.assertRaises(TypeError, operator.concat, 13, 29)
class A:
pass
a = A()
a.__getitem__ = lambda: "spam"
self.assertRaisesRegex(TypeError, "object can't be concatenated",
operator.concat, a, a)

def test_countOf(self):
operator = self.module
Expand Down Expand Up @@ -551,6 +557,13 @@ def test_iconcat_without_getitem(self):
with self.assertRaisesRegex(TypeError, msg):
operator.iconcat(1, 0.5)

class A:
pass
a = A()
a.__getitem__ = lambda: "spam"
self.assertRaisesRegex(TypeError, "object can't be concatenated",
operator.iconcat, a, a)

def test_index(self):
operator = self.module
class X:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix :func:`operator.concat` and :func:`operator.iconcat` to return a correct
exception message, if the object class lacks the :meth:`~object.__getitem__`
dunder. Patch by Sergey B Kirpichev.
Loading