diff options
Diffstat (limited to 'Lib/test')
-rwxr-xr-x | Lib/test/regrtest.py | 5 | ||||
-rw-r--r-- | Lib/test/test_frozen.py | 12 | ||||
-rw-r--r-- | Lib/test/test_pkg.py | 10 | ||||
-rw-r--r-- | Lib/test/test_pkgutil.py | 127 | ||||
-rwxr-xr-x | Lib/test/test_profile.py | 5 | ||||
-rw-r--r-- | Lib/test/test_structmembers.py | 6 | ||||
-rw-r--r-- | Lib/test/test_warnings.py | 2 | ||||
-rw-r--r-- | Lib/test/test_xmlrpc.py | 11 |
8 files changed, 170 insertions, 8 deletions
diff --git a/Lib/test/regrtest.py b/Lib/test/regrtest.py index 000f241..675cea3 100755 --- a/Lib/test/regrtest.py +++ b/Lib/test/regrtest.py @@ -731,6 +731,11 @@ def dash_R_cleanup(fs, ps, pic, abcs): from distutils.dir_util import _path_created from weakref import WeakSet + # Clear the warnings registry, so they can be displayed again + for mod in sys.modules.values(): + if hasattr(mod, '__warningregistry__'): + del mod.__warningregistry__ + # Restore some original values. warnings.filters[:] = fs copy_reg.dispatch_table.clear() diff --git a/Lib/test/test_frozen.py b/Lib/test/test_frozen.py index 99f499d..c86a24d 100644 --- a/Lib/test/test_frozen.py +++ b/Lib/test/test_frozen.py @@ -39,6 +39,18 @@ class FrozenTests(unittest.TestCase): else: self.fail("import __phello__.foo should have failed") + if sys.platform != "mac": # On the Mac this import does succeed. + try: + import __phello__.foo + except ImportError: + pass + else: + self.fail("import __phello__.foo should have failed") + + del sys.modules['__hello__'] + del sys.modules['__phello__'] + del sys.modules['__phello__.spam'] + def test_main(): run_unittest(FrozenTests) diff --git a/Lib/test/test_pkg.py b/Lib/test/test_pkg.py index dcc7433..db8e844 100644 --- a/Lib/test/test_pkg.py +++ b/Lib/test/test_pkg.py @@ -46,6 +46,7 @@ class TestPkg(unittest.TestCase): def setUp(self): self.root = None + self.pkgname = None self.syspath = list(sys.path) self.sysmodules = sys.modules.copy() @@ -56,6 +57,13 @@ class TestPkg(unittest.TestCase): del self.sysmodules cleanout(self.root) + # delete all modules concerning the tested hiearchy + if self.pkgname: + modules = [name for name in sys.modules + if self.pkgname in name.split('.')] + for name in modules: + del sys.modules[name] + def run_code(self, code): exec(textwrap.dedent(code), globals(), {"self": self}) @@ -78,6 +86,8 @@ class TestPkg(unittest.TestCase): f.write('\n') f.close() self.root = root + # package name is the name of the first item + self.pkgname = descr[0][0] def test_1(self): hier = [("t1", None), ("t1 __init__.py", "")] diff --git a/Lib/test/test_pkgutil.py b/Lib/test/test_pkgutil.py new file mode 100644 index 0000000..c9ec060 --- /dev/null +++ b/Lib/test/test_pkgutil.py @@ -0,0 +1,127 @@ +from test.test_support import run_unittest +import unittest +import sys +import imp +import pkgutil +import os +import os.path +import tempfile +import shutil +import zipfile + + + +class PkgutilTests(unittest.TestCase): + + def setUp(self): + self.dirname = tempfile.mkdtemp() + sys.path.insert(0, self.dirname) + + def tearDown(self): + del sys.path[0] + shutil.rmtree(self.dirname) + + def test_getdata_filesys(self): + pkg = 'test_getdata_filesys' + + # Include a LF and a CRLF, to test that binary data is read back + RESOURCE_DATA = b'Hello, world!\nSecond line\r\nThird line' + + # Make a package with some resources + package_dir = os.path.join(self.dirname, pkg) + os.mkdir(package_dir) + # Empty init.py + f = open(os.path.join(package_dir, '__init__.py'), "wb") + f.close() + # Resource files, res.txt, sub/res.txt + f = open(os.path.join(package_dir, 'res.txt'), "wb") + f.write(RESOURCE_DATA) + f.close() + os.mkdir(os.path.join(package_dir, 'sub')) + f = open(os.path.join(package_dir, 'sub', 'res.txt'), "wb") + f.write(RESOURCE_DATA) + f.close() + + # Check we can read the resources + res1 = pkgutil.get_data(pkg, 'res.txt') + self.assertEqual(res1, RESOURCE_DATA) + res2 = pkgutil.get_data(pkg, 'sub/res.txt') + self.assertEqual(res2, RESOURCE_DATA) + + del sys.modules[pkg] + + def test_getdata_zipfile(self): + zip = 'test_getdata_zipfile.zip' + pkg = 'test_getdata_zipfile' + + # Include a LF and a CRLF, to test that binary data is read back + RESOURCE_DATA = b'Hello, world!\nSecond line\r\nThird line' + + # Make a package with some resources + zip_file = os.path.join(self.dirname, zip) + z = zipfile.ZipFile(zip_file, 'w') + + # Empty init.py + z.writestr(pkg + '/__init__.py', "") + # Resource files, res.txt, sub/res.txt + z.writestr(pkg + '/res.txt', RESOURCE_DATA) + z.writestr(pkg + '/sub/res.txt', RESOURCE_DATA) + z.close() + + # Check we can read the resources + sys.path.insert(0, zip_file) + res1 = pkgutil.get_data(pkg, 'res.txt') + self.assertEqual(res1, RESOURCE_DATA) + res2 = pkgutil.get_data(pkg, 'sub/res.txt') + self.assertEqual(res2, RESOURCE_DATA) + del sys.path[0] + + del sys.modules[pkg] + +class PkgutilPEP302Tests(unittest.TestCase): + + class MyTestLoader(object): + def load_module(self, fullname): + # Create an empty module + mod = sys.modules.setdefault(fullname, imp.new_module(fullname)) + mod.__file__ = "<%s>" % self.__class__.__name__ + mod.__loader__ = self + # Make it a package + mod.__path__ = [] + # Count how many times the module is reloaded + mod.__dict__['loads'] = mod.__dict__.get('loads',0) + 1 + return mod + + def get_data(self, path): + return "Hello, world!" + + class MyTestImporter(object): + def find_module(self, fullname, path=None): + return PkgutilPEP302Tests.MyTestLoader() + + def setUp(self): + sys.meta_path.insert(0, self.MyTestImporter()) + + def tearDown(self): + del sys.meta_path[0] + + def test_getdata_pep302(self): + # Use a dummy importer/loader + self.assertEqual(pkgutil.get_data('foo', 'dummy'), "Hello, world!") + del sys.modules['foo'] + + def test_alreadyloaded(self): + # Ensure that get_data works without reloading - the "loads" module + # variable in the example loader should count how many times a reload + # occurs. + import foo + self.assertEqual(foo.loads, 1) + self.assertEqual(pkgutil.get_data('foo', 'dummy'), "Hello, world!") + self.assertEqual(foo.loads, 1) + del sys.modules['foo'] + +def test_main(): + run_unittest(PkgutilTests, PkgutilPEP302Tests) + +if __name__ == '__main__': + test_main() diff --git a/Lib/test/test_profile.py b/Lib/test/test_profile.py index 398ab86..9fea434 100755 --- a/Lib/test/test_profile.py +++ b/Lib/test/test_profile.py @@ -22,8 +22,9 @@ class ProfileTest(unittest.TestCase): def do_profiling(cls): results = [] prof = cls.profilerclass(timer, 0.001) + start_timer = timer() prof.runctx("testfunc()", globals(), locals()) - results.append(timer()) + results.append(timer() - start_timer) for methodname in cls.methodnames: s = StringIO() stats = pstats.Stats(prof, stream=s) @@ -40,7 +41,7 @@ class ProfileTest(unittest.TestCase): def test_cprofile(self): results = self.do_profiling() - self.assertEqual(results[0], 43000) + self.assertEqual(results[0], 1000) for i, method in enumerate(self.methodnames): if results[i+1] != self.expected_output[method]: print("Stats.%s output for %s doesn't fit expectation!" % diff --git a/Lib/test/test_structmembers.py b/Lib/test/test_structmembers.py index 27b4b70..97d7b23 100644 --- a/Lib/test/test_structmembers.py +++ b/Lib/test/test_structmembers.py @@ -101,12 +101,6 @@ class TestWarnings(unittest.TestCase): def test_main(verbose=None): - # Obscure hack so that this test passes after reloads or repeated calls - # to test_main (regrtest -R). - if '__warningregistry__' in globals(): - del globals()['__warningregistry__'] - if hasattr(sys, '__warningregistry__'): - del sys.__warningregistry__ test_support.run_unittest(__name__) if __name__ == "__main__": diff --git a/Lib/test/test_warnings.py b/Lib/test/test_warnings.py index d8aee79..a6cbddf 100644 --- a/Lib/test/test_warnings.py +++ b/Lib/test/test_warnings.py @@ -391,6 +391,8 @@ class PyWarningsDisplayTests(BaseTest, WarningsDisplayTests): def test_main(): + py_warnings.onceregistry.clear() + c_warnings.onceregistry.clear() test_support.run_unittest(CFilterTests, PyFilterTests, CWarnTests, diff --git a/Lib/test/test_xmlrpc.py b/Lib/test/test_xmlrpc.py index e708927..a254eee 100644 --- a/Lib/test/test_xmlrpc.py +++ b/Lib/test/test_xmlrpc.py @@ -49,6 +49,17 @@ class XMLRPCTestCase(unittest.TestCase): (newdt,), m = xmlrpclib.loads(s, use_datetime=0) self.assertEquals(newdt, xmlrpclib.DateTime('20050210T11:41:23')) + def test_datetime_before_1900(self): + # same as before but with an date before 1900 + dt = datetime.datetime(1, 2, 10, 11, 41, 23) + s = xmlrpclib.dumps((dt,)) + (newdt,), m = xmlrpclib.loads(s, use_datetime=1) + self.assertEquals(newdt, dt) + self.assertEquals(m, None) + + (newdt,), m = xmlrpclib.loads(s, use_datetime=0) + self.assertEquals(newdt, xmlrpclib.DateTime('00010210T11:41:23')) + def test_cmp_datetime_DateTime(self): now = datetime.datetime.now() dt = xmlrpclib.DateTime(now.timetuple()) |