summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_bytes.py
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2018-10-21 12:56:10 (GMT)
committerGitHub <noreply@github.com>2018-10-21 12:56:10 (GMT)
commit8bb037167cf3204a7d620ba11fbf43d2a9ec36e6 (patch)
tree37acfe5845860d39aada14c730f030fcfe701abb /Lib/test/test_bytes.py
parentbeb83d013e0295c987087febf2be78b1da389b15 (diff)
downloadcpython-8bb037167cf3204a7d620ba11fbf43d2a9ec36e6.zip
cpython-8bb037167cf3204a7d620ba11fbf43d2a9ec36e6.tar.gz
cpython-8bb037167cf3204a7d620ba11fbf43d2a9ec36e6.tar.bz2
bpo-34973: Fix crash in bytes constructor. (GH-9841)
Constructing bytes from mutating list could cause a crash. (cherry picked from commit 914f9a078f997e58cfcfabcbb30fafdd1f277bef) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Diffstat (limited to 'Lib/test/test_bytes.py')
-rw-r--r--Lib/test/test_bytes.py17
1 files changed, 17 insertions, 0 deletions
diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py
index 8b19732..7fa2a38 100644
--- a/Lib/test/test_bytes.py
+++ b/Lib/test/test_bytes.py
@@ -113,6 +113,23 @@ class BaseBytesTest:
b = self.type2test([1, 2, 3])
self.assertEqual(b, b"\x01\x02\x03")
+ def test_from_mutating_list(self):
+ # Issue #34973: Crash in bytes constructor with mutating list.
+ class X:
+ def __index__(self):
+ a.clear()
+ return 42
+ a = [X(), X()]
+ self.assertEqual(bytes(a), b'*')
+
+ class Y:
+ def __index__(self):
+ if len(a) < 1000:
+ a.append(self)
+ return 42
+ a = [Y()]
+ self.assertEqual(bytes(a), b'*' * 1000) # should not crash
+
def test_from_index(self):
b = self.type2test([Indexable(), Indexable(1), Indexable(254),
Indexable(255)])