Visitar URL original
[3.10] bpo-44891: Tests `id` preserving on `* 1` for `str` and `bytes` (GH-27745) by miss-islington · Pull Request #27756 · python/cpython · GitHub
The Wayback Machine - https://web.archive.org/web/20221223133819/https://github.com/python/cpython/pull/27756/files
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.10] bpo-44891: Tests id preserving on * 1 for str and bytes (GH-27745) #27756

Merged
merged 1 commit into from Aug 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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
@@ -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)
@@ -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.