Skip to content

Commit 4c60a9a

Browse files
test: add regression test
1 parent 118f534 commit 4c60a9a

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

Lib/test/test_io/test_memoryio.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,25 @@ def test_setstate(self):
967967
memio.close()
968968
self.assertRaises(ValueError, memio.__setstate__, ("closed", "", 0, None))
969969

970+
def test_write_str_subclass(self):
971+
# Writing a str subclass should use the subclass's unicode data
972+
# directly, not call __str__() on it (which may return a different
973+
# value). gh-XXXXX
974+
class MyStr(str):
975+
def __str__(self):
976+
return "WRONG"
977+
978+
s = MyStr("correct")
979+
memio = self.ioclass()
980+
memio.write(s)
981+
self.assertEqual(memio.getvalue(), "correct")
982+
983+
# Also test the fast path where pos == string_size (STATE_ACCUMULATING)
984+
memio2 = self.ioclass()
985+
memio2.write(MyStr("hello "))
986+
memio2.write(MyStr("world"))
987+
self.assertEqual(memio2.getvalue(), "hello world")
988+
970989

971990
class CStringIOPickleTest(PyStringIOPickleTest):
972991
UnsupportedOperation = io.UnsupportedOperation

0 commit comments

Comments
 (0)