[Python-checkins] cpython (merge 3.2 -> default): #11910: merge with 3.2.

ezio.melotti python-checkins at python.org
Mon May 9 06:30:44 CEST 2011


http://hg.python.org/cpython/rev/3449406fd04a
changeset: 69962:3449406fd04a
parent: 69957:3e8f0111feed
parent: 69961:4f3f67a595fb
user: Ezio Melotti <ezio.melotti at gmail.com>
date: Mon May 09 07:30:21 2011 +0300
summary:
 #11910: merge with 3.2.
files:
 Lib/test/test_heapq.py | 52 ++++++++++++++++++++---------
 Misc/NEWS | 2 +
 2 files changed, 38 insertions(+), 16 deletions(-)
diff --git a/Lib/test/test_heapq.py b/Lib/test/test_heapq.py
--- 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 support
-import sys
+from unittest import TestCase, skipUnless
 
-# 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 = support.import_fresh_module('heapq', blocked=['_heapq'])
+c_heapq = support.import_fresh_module('heapq', fresh=['_heapq'])
 
-class TestHeap(unittest.TestCase):
+# _heapq.nlargest/nsmallest are saved in heapq._nlargest/_smallest when
+# _heapq is imported, so check them there
+func_names = ['heapify', 'heappop', 'heappush', 'heappushpop',
+ 'heapreplace', '_nlargest', '_nsmallest']
+
+class TestModules(TestCase):
+ def test_py_functions(self):
+ for fname in func_names:
+ self.assertEqual(getattr(py_heapq, fname).__module__, 'heapq')
+
+ @skipUnless(c_heapq, 'requires _heapq')
+ def test_c_functions(self):
+ for fname in func_names:
+ self.assertEqual(getattr(c_heapq, fname).__module__, '_heapq')
+
+
+class TestHeap(TestCase):
 module = None
 
 def test_push_pop(self):
@@ -176,16 +191,12 @@
 self.assertEqual(list(self.module.nlargest(n, data, key=f)),
 sorted(data, key=f, reverse=True)[:n])
 
+
 class TestHeapPython(TestHeap):
 module = py_heapq
 
- # As an early adopter, we sanity check the
- # test.support.import_fresh_module utility function
- def test_pure_python(self):
- self.assertFalse(sys.modules['heapq'] is self.module)
- self.assertTrue(hasattr(self.module.heapify, '__code__'))
 
-
+ at skipUnless(c_heapq, 'requires _heapq')
 class TestHeapC(TestHeap):
 module = c_heapq
 
@@ -307,9 +318,9 @@
 'Test multiple tiers of iterators'
 return chain(map(lambda x:x, R(Ig(G(seqn)))))
 
-class TestErrorHandling(unittest.TestCase):
- # only for C implementation
- module = c_heapq
+
+class TestErrorHandling(TestCase):
+ module = None
 
 def test_non_sequence(self):
 for f in (self.module.heapify, self.module.heappop):
@@ -360,11 +371,20 @@
 self.assertRaises(ZeroDivisionError, f, 2, E(s))
 
 
+class TestErrorHandlingPython(TestErrorHandling):
+ module = py_heapq
+
+ at skipUnless(c_heapq, 'requires _heapq')
+class TestErrorHandlingC(TestErrorHandling):
+ module = c_heapq
+
+
 #==============================================================================
 
 
 def test_main(verbose=None):
- test_classes = [TestHeapPython, TestHeapC, TestErrorHandling]
+ test_classes = [TestModules, TestHeapPython, TestHeapC,
+ TestErrorHandlingPython, TestErrorHandlingC]
 support.run_unittest(*test_classes)
 
 # verify reference counting
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -1132,6 +1132,8 @@
 Tests
 -----
 
+- Issue #11910: Fix test_heapq to skip the C tests when _heapq is missing.
+
 - Fix test_startfile to wait for child process to terminate before finishing.
 
 - Issue #10822: Fix test_posix:test_getgroups failure under Solaris. Patch
-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /