Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8269a44

Browse files
committed
#11910: Fix test_heapq to skip the C tests when _heapq is missing.
1 parent 199e085 commit 8269a44

2 files changed

Lines changed: 29 additions & 20 deletions

File tree

Lib/test/test_heapq.py

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,31 @@
11
"""Unittests for heapq."""
22

3+
import sys
34
import random
4-
import unittest
5+
56
from test import support
6-
import sys
7+
from unittest import TestCase, skipUnless
78

8-
# We do a bit of trickery here to be able to test both the C implementation
9-
# and the Python implementation of the module.
10-
import heapq as c_heapq
119
py_heapq = support.import_fresh_module('heapq', blocked=['_heapq'])
10+
c_heapq = support.import_fresh_module('heapq', fresh=['_heapq'])
11+
12+
# _heapq.nlargest/nsmallest are saved in heapq._nlargest/_smallest when
13+
# _heapq is imported, so check them there
14+
func_names = ['heapify', 'heappop', 'heappush', 'heappushpop',
15+
'heapreplace', '_nlargest', '_nsmallest']
16+
17+
class TestModules(TestCase):
18+
def test_py_functions(self):
19+
for fname in func_names:
20+
self.assertEqual(getattr(py_heapq, fname).__module__, 'heapq')
21+
22+
@skipUnless(c_heapq, 'requires _heapq')
23+
def test_c_functions(self):
24+
for fname in func_names:
25+
self.assertEqual(getattr(c_heapq, fname).__module__, '_heapq')
1226

13-
class TestHeap(unittest.TestCase):
27+
28+
class TestHeap(TestCase):
1429
module = None
1530

1631
def test_push_pop(self):
@@ -176,16 +191,12 @@ def test_nlargest(self):
176191
self.assertEqual(list(self.module.nlargest(n, data, key=f)),
177192
sorted(data, key=f, reverse=True)[:n])
178193

194+
179195
class TestHeapPython(TestHeap):
180196
module = py_heapq
181197

182-
# As an early adopter, we sanity check the
183-
# test.support.import_fresh_module utility function
184-
def test_pure_python(self):
185-
self.assertFalse(sys.modules['heapq'] is self.module)
186-
self.assertTrue(hasattr(self.module.heapify, '__code__'))
187-
188198

199+
@skipUnless(c_heapq, 'requires _heapq')
189200
class TestHeapC(TestHeap):
190201
module = c_heapq
191202

@@ -211,12 +222,6 @@ def __le__(self, other):
211222
self.assertEqual(hsort(data, LT), target)
212223
self.assertRaises(TypeError, data, LE)
213224

214-
# As an early adopter, we sanity check the
215-
# test.support.import_fresh_module utility function
216-
def test_accelerated(self):
217-
self.assertTrue(sys.modules['heapq'] is self.module)
218-
self.assertFalse(hasattr(self.module.heapify, '__code__'))
219-
220225

221226
#==============================================================================
222227

@@ -313,7 +318,9 @@ def L(seqn):
313318
'Test multiple tiers of iterators'
314319
return chain(map(lambda x:x, R(Ig(G(seqn)))))
315320

316-
class TestErrorHandling(unittest.TestCase):
321+
322+
@skipUnless(c_heapq, 'requires _heapq')
323+
class TestErrorHandling(TestCase):
317324
# only for C implementation
318325
module = c_heapq
319326

@@ -372,7 +379,7 @@ def test_iterable_args(self):
372379
def test_main(verbose=None):
373380
from types import BuiltinFunctionType
374381

375-
test_classes = [TestHeapPython, TestHeapC, TestErrorHandling]
382+
test_classes = [TestModules, TestHeapPython, TestHeapC, TestErrorHandling]
376383
support.run_unittest(*test_classes)
377384

378385
# verify reference counting

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,8 @@ Build
353353
Tests
354354
-----
355355

356+
- Issue #11910: Fix test_heapq to skip the C tests when _heapq is missing.
357+
356358
- Fix test_startfile to wait for child process to terminate before finishing.
357359

358360
- Issue #11719: Fix message about unexpected test_msilib skip on non-Windows

0 commit comments

Comments
 (0)