summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test')
-rwxr-xr-xLib/test/regrtest.py8
-rw-r--r--Lib/test/test_import.py14
-rw-r--r--Lib/test/test_int_literal.py4
-rw-r--r--Lib/test/test_nis.py10
-rw-r--r--Lib/test/test_posix.py28
-rw-r--r--Lib/test/test_print.py14
-rw-r--r--Lib/test/test_winsound.py36
-rw-r--r--Lib/test/test_zlib.py7
8 files changed, 84 insertions, 37 deletions
diff --git a/Lib/test/regrtest.py b/Lib/test/regrtest.py
index 2212af5..100b26f 100755
--- a/Lib/test/regrtest.py
+++ b/Lib/test/regrtest.py
@@ -1157,6 +1157,14 @@ class _ExpectedSkips:
if sys.platform != 'sunos5':
self.expected.add('test_nis')
+ # TODO: This is a hack to raise TestSkipped if -3 is not enabled.
+ # Instead of relying on callable to have a warning, we should expose
+ # the -3 flag to Python code somehow
+ with test_support.catch_warning() as w:
+ callable(int)
+ if w.message is None:
+ self.expected.add('test_py3kwarn')
+
self.valid = True
def isvalid(self):
diff --git a/Lib/test/test_import.py b/Lib/test/test_import.py
index 6eac67e..fe1da52 100644
--- a/Lib/test/test_import.py
+++ b/Lib/test/test_import.py
@@ -255,8 +255,20 @@ class PathsTests(unittest.TestCase):
self.assertEqual(mod.testdata, 'test_trailing_slash')
unload("test_trailing_slash")
+class RelativeImport(unittest.TestCase):
+ def tearDown(self):
+ try:
+ del sys.modules["test.relimport"]
+ except:
+ pass
+
+ def test_relimport_star(self):
+ # This will import * from .test_import.
+ from . import relimport
+ self.assertTrue(hasattr(relimport, "RelativeImport"))
+
def test_main(verbose=None):
- run_unittest(ImportTest, PathsTests)
+ run_unittest(ImportTest, PathsTests, RelativeImport)
if __name__ == '__main__':
test_main()
diff --git a/Lib/test/test_int_literal.py b/Lib/test/test_int_literal.py
index c9ad2a3..9bd0c41 100644
--- a/Lib/test/test_int_literal.py
+++ b/Lib/test/test_int_literal.py
@@ -6,7 +6,7 @@ This is complex because of changes due to PEP 237.
import unittest
from test import test_support
-class TextHexOctBin(unittest.TestCase):
+class TestHexOctBin(unittest.TestCase):
def test_hex_baseline(self):
# A few upper/lowercase tests
@@ -141,7 +141,7 @@ class TextHexOctBin(unittest.TestCase):
self.assertEqual(-0b1111111111111111111111111111111111111111111111111111111111111111, -18446744073709551615)
def test_main():
- test_support.run_unittest(TextHexOctBin)
+ test_support.run_unittest(TestHexOctBin)
if __name__ == "__main__":
test_main()
diff --git a/Lib/test/test_nis.py b/Lib/test/test_nis.py
index 55dd32c..757edd6 100644
--- a/Lib/test/test_nis.py
+++ b/Lib/test/test_nis.py
@@ -1,4 +1,4 @@
-from test.test_support import verbose, run_unittest
+from test import test_support
import unittest
import nis
@@ -8,8 +8,10 @@ class NisTests(unittest.TestCase):
maps = nis.maps()
except nis.error as msg:
# NIS is probably not active, so this test isn't useful
- if verbose:
- self.fail("(failing because of verbose mode) %s" % msg)
+ if test_support.verbose:
+ print("Test Skipped:", msg)
+ # Can't raise TestSkipped as regrtest only recognizes the exception
+ # import time.
return
try:
# On some systems, this map is only accessible to the
@@ -35,7 +37,7 @@ class NisTests(unittest.TestCase):
break
def test_main():
- run_unittest(NisTests)
+ test_support.run_unittest(NisTests)
if __name__ == '__main__':
test_main()
diff --git a/Lib/test/test_posix.py b/Lib/test/test_posix.py
index 13f8f9e..57e9d15 100644
--- a/Lib/test/test_posix.py
+++ b/Lib/test/test_posix.py
@@ -9,6 +9,7 @@ except ImportError:
import time
import os
+import pwd
import unittest
import warnings
warnings.filterwarnings('ignore', '.* potential security risk .*',
@@ -141,6 +142,33 @@ class PosixTester(unittest.TestCase):
if hasattr(posix, 'stat'):
self.assert_(posix.stat(test_support.TESTFN))
+ if hasattr(posix, 'chown'):
+ def test_chown(self):
+ # raise an OSError if the file does not exist
+ os.unlink(test_support.TESTFN)
+ self.assertRaises(OSError, posix.chown, test_support.TESTFN, -1, -1)
+
+ # re-create the file
+ open(test_support.TESTFN, 'w').close()
+ if os.getuid() == 0:
+ try:
+ # Many linux distros have a nfsnobody user as MAX_UID-2
+ # that makes a good test case for signedness issues.
+ # http://bugs.python.org/issue1747858
+ # This part of the test only runs when run as root.
+ # Only scary people run their tests as root.
+ ent = pwd.getpwnam('nfsnobody')
+ posix.chown(test_support.TESTFN, ent.pw_uid, ent.pw_gid)
+ except KeyError:
+ pass
+ else:
+ # non-root cannot chown to root, raises OSError
+ self.assertRaises(OSError, posix.chown,
+ test_support.TESTFN, 0, 0)
+
+ # test a successful chown call
+ posix.chown(test_support.TESTFN, os.getuid(), os.getgid())
+
def test_chdir(self):
if hasattr(posix, 'chdir'):
posix.chdir(os.curdir)
diff --git a/Lib/test/test_print.py b/Lib/test/test_print.py
index baeab3d..10ef60a 100644
--- a/Lib/test/test_print.py
+++ b/Lib/test/test_print.py
@@ -12,8 +12,6 @@ except ImportError:
# 2.x
from StringIO import StringIO
-from contextlib import contextmanager
-
NotDefined = object()
# A dispatch table all 8 combinations of providing
@@ -40,15 +38,6 @@ dispatch = {
lambda args, sep, end, file: print(sep=sep, end=end, file=file, *args),
}
-@contextmanager
-def stdout_redirected(new_stdout):
- save_stdout = sys.stdout
- sys.stdout = new_stdout
- try:
- yield None
- finally:
- sys.stdout = save_stdout
-
# Class used to test __str__ and print
class ClassWith__str__:
def __init__(self, x):
@@ -69,8 +58,7 @@ class TestPrint(unittest.TestCase):
end is not NotDefined,
file is not NotDefined)]
- t = StringIO()
- with stdout_redirected(t):
+ with test_support.captured_stdout() as t:
fn(args, sep, end, file)
self.assertEqual(t.getvalue(), expected)
diff --git a/Lib/test/test_winsound.py b/Lib/test/test_winsound.py
index 7cdc275..dac569c 100644
--- a/Lib/test/test_winsound.py
+++ b/Lib/test/test_winsound.py
@@ -23,25 +23,27 @@ class BeepTest(unittest.TestCase):
self.assertRaises(ValueError, winsound.Beep, 32768, 75)
def test_extremes(self):
- if _have_soundcard():
- winsound.Beep(37, 75)
- winsound.Beep(32767, 75)
- else:
- # The behaviour of winsound.Beep() seems to differ between
- # different versions of Windows when there's either a) no
- # sound card entirely, b) legacy beep driver has been disabled,
- # or c) the legacy beep driver has been uninstalled. Sometimes
- # RuntimeErrors are raised, sometimes they're not. Meh.
- try:
- winsound.Beep(37, 75)
- winsound.Beep(32767, 75)
- except RuntimeError:
- pass
+ self._beep(37, 75)
+ self._beep(32767, 75)
def test_increasingfrequency(self):
- if _have_soundcard():
- for i in range(100, 2000, 100):
- winsound.Beep(i, 75)
+ for i in xrange(100, 2000, 100):
+ self._beep(i, 75)
+
+ def _beep(self, *args):
+ # these tests used to use _have_soundcard(), but it's quite
+ # possible to have a soundcard, and yet have the beep driver
+ # disabled. So basically, we have no way of knowing whether
+ # a beep should be produced or not, so currently if these
+ # tests fail we're ignoring them
+ #
+ # XXX the right fix for this is to define something like
+ # _have_enabled_beep_driver() and use that instead of the
+ # try/except below
+ try:
+ winsound.Beep(*args)
+ except RuntimeError:
+ pass
class MessageBeepTest(unittest.TestCase):
diff --git a/Lib/test/test_zlib.py b/Lib/test/test_zlib.py
index 0adf507..d2fd504 100644
--- a/Lib/test/test_zlib.py
+++ b/Lib/test/test_zlib.py
@@ -1,6 +1,7 @@
import unittest
from test import test_support
import zlib
+import binascii
import random
@@ -46,6 +47,12 @@ class ChecksumTestCase(unittest.TestCase):
self.assertEqual(zlib.adler32(foo+foo), 3573550353)
self.assertEqual(zlib.adler32('spam'), 72286642)
+ def test_same_as_binascii_crc32(self):
+ foo = 'abcdefghijklmnop'
+ crc = -1808088941
+ self.assertEqual(binascii.crc32(foo), crc)
+ self.assertEqual(zlib.crc32(foo), crc)
+ self.assertEqual(binascii.crc32('spam'), zlib.crc32('spam'))