summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorChristian Heimes <christian@cheimes.de>2008-04-19 00:55:37 (GMT)
committerChristian Heimes <christian@cheimes.de>2008-04-19 00:55:37 (GMT)
commitdae2a8939df14692b59d66b13c587a0a4197700e (patch)
treec6732b7a9193bc6325d9965ba687278a5d4ce4d5 /Lib
parent53876d9cd8a67d9e67772e082deab92a598f74b3 (diff)
downloadcpython-dae2a8939df14692b59d66b13c587a0a4197700e.zip
cpython-dae2a8939df14692b59d66b13c587a0a4197700e.tar.gz
cpython-dae2a8939df14692b59d66b13c587a0a4197700e.tar.bz2
Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r62350 | nick.coghlan | 2008-04-15 12:25:31 +0200 (Tue, 15 Apr 2008) | 1 line Issue 2439: add pkgutils.get_data() as a convenience wrapper for the PEP 302 get_data() API (contributed by Paul Moore) ........ r62351 | nick.coghlan | 2008-04-15 12:28:14 +0200 (Tue, 15 Apr 2008) | 1 line Add test file missing from rev 62350 ........ r62352 | benjamin.peterson | 2008-04-15 13:58:46 +0200 (Tue, 15 Apr 2008) | 2 lines Add myself to Doc/ACKS.txt ........ r62353 | andrew.kuchling | 2008-04-15 15:10:07 +0200 (Tue, 15 Apr 2008) | 6 lines Add *,**,@ to index, as suggested by http://farmdev.com/thoughts/24/what-does-the-def-star-variable-or-def-asterisk-parameter-syntax-do-in-python-/ The right entry type to use isn't clear; operator seems wrong, because *,**,@ aren't being used in expressions here. I put them as 'statement'; 'syntax' might be better. ........ r62354 | andrew.kuchling | 2008-04-15 15:10:41 +0200 (Tue, 15 Apr 2008) | 1 line Typo fix ........ r62355 | mark.dickinson | 2008-04-15 22:51:18 +0200 (Tue, 15 Apr 2008) | 3 lines Fix for possible signed overflow: the behaviour of -LONG_MIN is undefined in ANSI C. ........ r62358 | jeroen.ruigrok | 2008-04-16 14:47:01 +0200 (Wed, 16 Apr 2008) | 2 lines Reformat to 80 columns prior to adding documentation. ........ r62359 | jeroen.ruigrok | 2008-04-16 14:57:43 +0200 (Wed, 16 Apr 2008) | 2 lines Add details about the return value for mmap.flush(). ........ r62364 | raymond.hettinger | 2008-04-17 12:48:31 +0200 (Thu, 17 Apr 2008) | 1 line Issue 2648: Add leading zero to money format recipe in the docs. ........ r62365 | jeroen.ruigrok | 2008-04-17 14:39:45 +0200 (Thu, 17 Apr 2008) | 2 lines Be consistent in the use of read-only. ........ r62370 | andrew.kuchling | 2008-04-17 22:44:06 +0200 (Thu, 17 Apr 2008) | 1 line Typo fixes ........ r62372 | andrew.kuchling | 2008-04-18 04:40:47 +0200 (Fri, 18 Apr 2008) | 1 line Use correct parameter name ........ r62373 | andrew.kuchling | 2008-04-18 18:53:09 +0200 (Fri, 18 Apr 2008) | 1 line #2654: fix typo ........ r62374 | andrew.kuchling | 2008-04-18 20:28:23 +0200 (Fri, 18 Apr 2008) | 4 lines Remove personal note from Jim Roskind; it no longer applies, and the e-mail address is for a previous employer. Can we move the big long copyright statement into a sidebar or something? ........ r62375 | andrew.kuchling | 2008-04-18 20:39:55 +0200 (Fri, 18 Apr 2008) | 1 line Rewrite introductory section, and remove old section. (It was already commented-out, but why keep it?) ........ r62378 | skip.montanaro | 2008-04-18 22:35:46 +0200 (Fri, 18 Apr 2008) | 1 line resolve issue 2014 ........ r62379 | benjamin.peterson | 2008-04-18 22:45:33 +0200 (Fri, 18 Apr 2008) | 2 lines Fix indentation in sysmodule.c ........ r62381 | amaury.forgeotdarc | 2008-04-19 01:31:33 +0200 (Sat, 19 Apr 2008) | 3 lines Some tests did not pass on repeated calls (regrtest -R::) Perform additional cleanup, mostly deleting from sys.modules, or clearing the warnings registry. ........
Diffstat (limited to 'Lib')
-rw-r--r--Lib/pkgutil.py37
-rwxr-xr-xLib/test/regrtest.py5
-rw-r--r--Lib/test/test_frozen.py12
-rw-r--r--Lib/test/test_pkg.py10
-rw-r--r--Lib/test/test_pkgutil.py127
-rwxr-xr-xLib/test/test_profile.py5
-rw-r--r--Lib/test/test_structmembers.py6
-rw-r--r--Lib/test/test_warnings.py2
-rw-r--r--Lib/test/test_xmlrpc.py11
-rw-r--r--Lib/xmlrpclib.py30
10 files changed, 226 insertions, 19 deletions
diff --git a/Lib/pkgutil.py b/Lib/pkgutil.py
index 9c1d07c..ff3467a 100644
--- a/Lib/pkgutil.py
+++ b/Lib/pkgutil.py
@@ -542,3 +542,40 @@ def extend_path(path, name):
f.close()
return path
+
+def get_data(package, resource):
+ """Get a resource from a package.
+
+ This is a wrapper round the PEP 302 loader get_data API. The package
+ argument should be the name of a package, in standard module format
+ (foo.bar). The resource argument should be in the form of a relative
+ filename, using '/' as the path separator. The parent directory name '..'
+ is not allowed, and nor is a rooted name (starting with a '/').
+
+ The function returns a binary string, which is the contents of the
+ specified resource.
+
+ For packages located in the filesystem, which have already been imported,
+ this is the rough equivalent of
+
+ d = os.path.dirname(sys.modules[package].__file__)
+ data = open(os.path.join(d, resource), 'rb').read()
+
+ If the package cannot be located or loaded, or it uses a PEP 302 loader
+ which does not support get_data(), then None is returned.
+ """
+
+ loader = get_loader(package)
+ if loader is None or not hasattr(loader, 'get_data'):
+ return None
+ mod = sys.modules.get(package) or loader.load_module(package)
+ if mod is None or not hasattr(mod, '__file__'):
+ return None
+
+ # Modify the resource name to be compatible with the loader.get_data
+ # signature - an os.path format "filename" starting with the dirname of
+ # the package's __file__
+ parts = resource.split('/')
+ parts.insert(0, os.path.dirname(mod.__file__))
+ resource_name = os.path.join(*parts)
+ return loader.get_data(resource_name)
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())
diff --git a/Lib/xmlrpclib.py b/Lib/xmlrpclib.py
index 2e68a1b..522df4d 100644
--- a/Lib/xmlrpclib.py
+++ b/Lib/xmlrpclib.py
@@ -287,6 +287,20 @@ boolean = Boolean = bool
# @param value The time, given as an ISO 8601 string, a time
# tuple, or a integer time value.
+def _strftime(value):
+ if datetime:
+ if isinstance(value, datetime.datetime):
+ return "%04d%02d%02dT%02d:%02d:%02d" % (
+ value.year, value.month, value.day,
+ value.hour, value.minute, value.second)
+
+ if not isinstance(value, (tuple, time.struct_time)):
+ if value == 0:
+ value = time.time()
+ value = time.localtime(value)
+
+ return "%04d%02d%02dT%02d:%02d:%02d" % value[:6]
+
class DateTime:
"""DateTime wrapper for an ISO 8601 string or time tuple or
localtime integer value to generate 'dateTime.iso8601' XML-RPC
@@ -294,16 +308,10 @@ class DateTime:
"""
def __init__(self, value=0):
- if not isinstance(value, str):
- if datetime and isinstance(value, datetime.datetime):
- self.value = value.strftime("%Y%m%dT%H:%M:%S")
- return
- if not isinstance(value, (tuple, time.struct_time)):
- if value == 0:
- value = time.time()
- value = time.localtime(value)
- value = time.strftime("%Y%m%dT%H:%M:%S", value)
- self.value = value
+ if isinstance(value, str):
+ self.value = value
+ else:
+ self.value = _strftime(value)
def make_comparable(self, other):
if isinstance(other, DateTime):
@@ -700,7 +708,7 @@ class Marshaller:
if datetime:
def dump_datetime(self, value, write):
write("<value><dateTime.iso8601>")
- write(value.strftime("%Y%m%dT%H:%M:%S"))
+ write(_strftime(value))
write("</dateTime.iso8601></value>\n")
dispatch[datetime.datetime] = dump_datetime