[Python-checkins] bpo-44891: Tests `id` preserving on `* 1` for `str` and `bytes` (GH-27745)

ambv webhook-mailer at python.org
Fri Aug 13 06:36:36 EDT 2021


https://github.com/python/cpython/commit/a2ce538e16d5e3a6168704366bdd7a8c5af29881
commit: a2ce538e16d5e3a6168704366bdd7a8c5af29881
branch: main
author: Nikita Sobolev <mail at sobolevn.me>
committer: ambv <lukasz at langa.pl>
date: 2021-08-13T12:36:22+02:00
summary:

bpo-44891: Tests `id` preserving on `* 1` for `str` and `bytes` (GH-27745)

Co-authored-by: Łukasz Langa <lukasz at langa.pl>

files:
A Misc/NEWS.d/next/Tests/2021-08-13-12-11-06.bpo-44891.T9_mBT.rst
M Lib/test/test_bytes.py
M Lib/test/test_unicode.py

diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py
index 381030fe0e8383..13ad238242d0c5 100644
--- a/Lib/test/test_bytes.py
+++ b/Lib/test/test_bytes.py
@@ -1169,6 +1169,28 @@ class BufferBlocked(bytearray):
         self.assertEqual(bytes(ba), b'ab')
         self.assertRaises(TypeError, bytes, bb)
 
+    def test_repeat_id_preserving(self):
+        a = b'123abc1@'
+        b = b'456zyx-+'
+        self.assertEqual(id(a), id(a))
+        self.assertNotEqual(id(a), id(b))
+        self.assertNotEqual(id(a), id(a * -4))
+        self.assertNotEqual(id(a), id(a * 0))
+        self.assertEqual(id(a), id(a * 1))
+        self.assertEqual(id(a), id(1 * a))
+        self.assertNotEqual(id(a), id(a * 2))
+
+        class SubBytes(bytes):
+            pass
+
+        s = SubBytes(b'qwerty()')
+        self.assertEqual(id(s), id(s))
+        self.assertNotEqual(id(s), id(s * -4))
+        self.assertNotEqual(id(s), id(s * 0))
+        self.assertNotEqual(id(s), id(s * 1))
+        self.assertNotEqual(id(s), id(1 * s))
+        self.assertNotEqual(id(s), id(s * 2))
+
 
 class ByteArrayTest(BaseBytesTest, unittest.TestCase):
     type2test = bytearray
diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py
index ffe3e82804ea9c..a14eead3411f30 100644
--- a/Lib/test/test_unicode.py
+++ b/Lib/test/test_unicode.py
@@ -508,6 +508,28 @@ def test_replace_id(self):
         text = 'abc def'
         self.assertIs(text.replace(pattern, pattern), text)
 
+    def test_repeat_id_preserving(self):
+        a = '123abc1@'
+        b = '456zyx-+'
+        self.assertEqual(id(a), id(a))
+        self.assertNotEqual(id(a), id(b))
+        self.assertNotEqual(id(a), id(a * -4))
+        self.assertNotEqual(id(a), id(a * 0))
+        self.assertEqual(id(a), id(a * 1))
+        self.assertEqual(id(a), id(1 * a))
+        self.assertNotEqual(id(a), id(a * 2))
+
+        class SubStr(str):
+            pass
+
+        s = SubStr('qwerty()')
+        self.assertEqual(id(s), id(s))
+        self.assertNotEqual(id(s), id(s * -4))
+        self.assertNotEqual(id(s), id(s * 0))
+        self.assertNotEqual(id(s), id(s * 1))
+        self.assertNotEqual(id(s), id(1 * s))
+        self.assertNotEqual(id(s), id(s * 2))
+
     def test_bytes_comparison(self):
         with warnings_helper.check_warnings():
             warnings.simplefilter('ignore', BytesWarning)
diff --git a/Misc/NEWS.d/next/Tests/2021-08-13-12-11-06.bpo-44891.T9_mBT.rst b/Misc/NEWS.d/next/Tests/2021-08-13-12-11-06.bpo-44891.T9_mBT.rst
new file mode 100644
index 00000000000000..2f83389ec15855
--- /dev/null
+++ b/Misc/NEWS.d/next/Tests/2021-08-13-12-11-06.bpo-44891.T9_mBT.rst
@@ -0,0 +1,2 @@
+Tests were added to clarify :func:`id` is preserved when ``obj * 1`` is used
+on :class:`str` and :class:`bytes` objects. Patch by Nikita Sobolev.



More information about the Python-checkins mailing list