cpython: 3cbbb2a7c56d (original) (raw)

--- a/Lib/test/test_heapq.py +++ b/Lib/test/test_heapq.py @@ -1,16 +1,31 @@ """Unittests for heapq.""" +import sys import random -import unittest + from test import test_support -import sys +from unittest import TestCase, skipUnless + +py_heapq = test_support.import_fresh_module('heapq', blocked=['_heapq']) +c_heapq = test_support.import_fresh_module('heapq', fresh=['_heapq']) -# We do a bit of trickery here to be able to test both the C implementation -# and the Python implementation of the module. -import heapq as c_heapq -py_heapq = test_support.import_fresh_module('heapq', blocked=['_heapq']) +# _heapq.nlargest/nsmallest are saved in heapq._nlargest/_smallest when +# _heapq is imported, so check them there +func_names = ['heapify', 'heappop', 'heappush', 'heappushpop',

-class TestHeap(unittest.TestCase): +class TestModules(TestCase):

+

+ + +class TestHeap(TestCase): module = None def test_push_pop(self): @@ -175,16 +190,12 @@ class TestHeap(unittest.TestCase): self.assertEqual(self.module.nlargest(n, data, key=f), sorted(data, key=f, reverse=True)[:n]) + class TestHeapPython(TestHeap): module = py_heapq

- +@skipUnless(c_heapq, 'requires _heapq') class TestHeapC(TestHeap): module = c_heapq @@ -304,7 +315,7 @@ def L(seqn): 'Test multiple tiers of iterators' return chain(imap(lambda x:x, R(Ig(G(seqn))))) -class TestErrorHandling(unittest.TestCase): +class TestErrorHandling(TestCase): def test_non_sequence(self): for f in (self.module.heapify, self.module.heappop): @@ -349,9 +360,12 @@ class TestErrorHandling(unittest.TestCas self.assertRaises(TypeError, f, 2, N(s)) self.assertRaises(ZeroDivisionError, f, 2, E(s)) + class TestErrorHandling_Python(TestErrorHandling): module = py_heapq + +@skipUnless(c_heapq, 'requires _heapq') class TestErrorHandling_C(TestErrorHandling): module = c_heapq @@ -360,8 +374,8 @@ class TestErrorHandling_C(TestErrorHandl def test_main(verbose=None):

--- a/Misc/NEWS +++ b/Misc/NEWS @@ -421,6 +421,8 @@ IDLE Tests ----- +- Issue #11910: Fix test_heapq to skip the C tests when _heapq is missing. +