From ee30d7c6bd37061da165c1117f4617c16caa0adc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Wirtel?= Date: Wed, 20 Feb 2019 12:07:13 +0100 Subject: [PATCH 1/7] bpo-36049: Add __repr__ to Queue, PriorityQueue and LifoQueue --- Lib/queue.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/Lib/queue.py b/Lib/queue.py index ef07957781a474..7a0ed55ebe370d 100644 --- a/Lib/queue.py +++ b/Lib/queue.py @@ -216,6 +216,24 @@ def _put(self, item): def _get(self): return self.queue.popleft() + def __repr__(self): + items = list(self.queue) + output = '' + if items: + len_items = len(items) + + if len_items > 1: + items = repr(items[0]), repr(items[-1]) + else: + items = repr(items[0]), + + if len_items > 2: + output = ''.join([items[0], '...', items[1]]) + else: + output = ','.join(items) + + return f'{self.__class__.__name__}({output})' + class PriorityQueue(Queue): '''Variant of Queue that retrieves open entries in priority order (lowest first). From 3654ff6f244cc85c004116f9c33f2eb34c017779 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Wirtel?= Date: Wed, 20 Feb 2019 12:08:35 +0100 Subject: [PATCH 2/7] Add the blurb entry --- .../next/Library/2019-02-20-12-08-28.bpo-36049.kLMbTL.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2019-02-20-12-08-28.bpo-36049.kLMbTL.rst diff --git a/Misc/NEWS.d/next/Library/2019-02-20-12-08-28.bpo-36049.kLMbTL.rst b/Misc/NEWS.d/next/Library/2019-02-20-12-08-28.bpo-36049.kLMbTL.rst new file mode 100644 index 00000000000000..b29c88a719c5cc --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-02-20-12-08-28.bpo-36049.kLMbTL.rst @@ -0,0 +1,2 @@ +Add __repr__ for Queue, PriorityQueue and LifoQueue. Contributed by Stéphane +Wirtel From a47635f029f1ffc32a928af4684fdf36c75369b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Wirtel?= Date: Wed, 20 Feb 2019 12:15:28 +0100 Subject: [PATCH 3/7] Add the tests --- Lib/test/test_queue.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Lib/test/test_queue.py b/Lib/test/test_queue.py index c8528f97741dcc..88c746bb08f719 100644 --- a/Lib/test/test_queue.py +++ b/Lib/test/test_queue.py @@ -239,6 +239,17 @@ def test_shrinking_queue(self): with self.assertRaises(queue.Full): q.put_nowait(4) + def test_repr(self): + q = self.type2test(3) + class_name = q.__class__.__name__ + self.assertEqual(f'{class_name}()', repr(q)) + q.put('a') + self.assertEqual(f"{class_name}('a')", repr(q)) + q.put('b') + self.assertEqual(f"{class_name}('a','b')", repr(q)) + q.put('c') + self.assertEqual(f"{class_name}('a'...'c')", repr(q)) + class QueueTest(BaseQueueTestMixin, unittest.TestCase): type2test = queue.Queue From 9e7da3b5e679ff285773bb23955959a6fe8f9bc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Wirtel?= Date: Wed, 20 Feb 2019 12:16:17 +0100 Subject: [PATCH 4/7] Fix whitespace --- Lib/queue.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/queue.py b/Lib/queue.py index 7a0ed55ebe370d..f5016ee41417e8 100644 --- a/Lib/queue.py +++ b/Lib/queue.py @@ -225,8 +225,8 @@ def __repr__(self): if len_items > 1: items = repr(items[0]), repr(items[-1]) else: - items = repr(items[0]), - + items = repr(items[0]) + if len_items > 2: output = ''.join([items[0], '...', items[1]]) else: From 70d4bd786722b5a5f3fbe34cf28f15d45330504a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Wirtel?= Date: Wed, 20 Feb 2019 12:37:34 +0100 Subject: [PATCH 5/7] Fix the output of queue.Queue.__repr__ --- Lib/queue.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Lib/queue.py b/Lib/queue.py index f5016ee41417e8..f63dc8f9065311 100644 --- a/Lib/queue.py +++ b/Lib/queue.py @@ -223,14 +223,14 @@ def __repr__(self): len_items = len(items) if len_items > 1: - items = repr(items[0]), repr(items[-1]) + items = items[0], items[-1] else: - items = repr(items[0]) + items = items[0] if len_items > 2: - output = ''.join([items[0], '...', items[1]]) + output = ''.join([repr(items[0]), '...', repr(items[1])]) else: - output = ','.join(items) + output = ','.join(map(repr, items)) return f'{self.__class__.__name__}({output})' From f86eab6ff1906c63c695363f4ce8a82c6b513bde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Wirtel?= Date: Wed, 20 Feb 2019 12:55:27 +0100 Subject: [PATCH 6/7] Fix the markup in blurb entry --- .../next/Library/2019-02-20-12-08-28.bpo-36049.kLMbTL.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2019-02-20-12-08-28.bpo-36049.kLMbTL.rst b/Misc/NEWS.d/next/Library/2019-02-20-12-08-28.bpo-36049.kLMbTL.rst index b29c88a719c5cc..05387f6b31eaf6 100644 --- a/Misc/NEWS.d/next/Library/2019-02-20-12-08-28.bpo-36049.kLMbTL.rst +++ b/Misc/NEWS.d/next/Library/2019-02-20-12-08-28.bpo-36049.kLMbTL.rst @@ -1,2 +1,2 @@ -Add __repr__ for Queue, PriorityQueue and LifoQueue. Contributed by Stéphane +Add ``_repr__`` for Queue, PriorityQueue and LifoQueue. Contributed by Stéphane Wirtel From f896e34de540277a901e89b12c5acf737814d22f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Wirtel?= Date: Wed, 20 Feb 2019 14:16:54 +0100 Subject: [PATCH 7/7] =?UTF-8?q?Fix=20queue.Queue.=5F=5Frepr=5F=5F()=20with?= =?UTF-8?q?=20the=20recommendations=20of=20R=C3=A9mi=20Lapeyre?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Lib/queue.py | 2 +- Lib/test/test_queue.py | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Lib/queue.py b/Lib/queue.py index f63dc8f9065311..05baeb2f1017ef 100644 --- a/Lib/queue.py +++ b/Lib/queue.py @@ -232,7 +232,7 @@ def __repr__(self): else: output = ','.join(map(repr, items)) - return f'{self.__class__.__name__}({output})' + return f'<{self.__class__.__name__}({output})>' class PriorityQueue(Queue): diff --git a/Lib/test/test_queue.py b/Lib/test/test_queue.py index 88c746bb08f719..a0c4e6e924538b 100644 --- a/Lib/test/test_queue.py +++ b/Lib/test/test_queue.py @@ -242,13 +242,13 @@ def test_shrinking_queue(self): def test_repr(self): q = self.type2test(3) class_name = q.__class__.__name__ - self.assertEqual(f'{class_name}()', repr(q)) + self.assertEqual(f'<{class_name}()>', repr(q)) q.put('a') - self.assertEqual(f"{class_name}('a')", repr(q)) + self.assertEqual(f"<{class_name}('a')>", repr(q)) q.put('b') - self.assertEqual(f"{class_name}('a','b')", repr(q)) + self.assertEqual(f"<{class_name}('a','b')>", repr(q)) q.put('c') - self.assertEqual(f"{class_name}('a'...'c')", repr(q)) + self.assertEqual(f"<{class_name}('a'...'c')>", repr(q)) class QueueTest(BaseQueueTestMixin, unittest.TestCase): type2test = queue.Queue