2012-07-15 05:09:52 -03:00
|
|
|
from test.support import run_unittest, unload, check_warnings
|
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.
........
2008-04-18 21:55:37 -03:00
|
|
|
import unittest
|
|
|
|
import sys
|
|
|
|
import imp
|
|
|
|
import pkgutil
|
|
|
|
import os
|
|
|
|
import os.path
|
|
|
|
import tempfile
|
|
|
|
import shutil
|
|
|
|
import zipfile
|
|
|
|
|
2012-07-15 08:19:18 -03:00
|
|
|
# Note: pkgutil.walk_packages is currently tested in test_runpy. This is
|
|
|
|
# a hack to get a major issue resolved for 3.3b2. Longer term, it should
|
|
|
|
# be moved back here, perhaps by factoring out the helper code for
|
|
|
|
# creating interesting package layouts to a separate module.
|
|
|
|
# Issue #15348 declares this is indeed a dodgy hack ;)
|
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.
........
2008-04-18 21:55:37 -03:00
|
|
|
|
|
|
|
class PkgutilTests(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.dirname = tempfile.mkdtemp()
|
2011-10-07 16:01:40 -03:00
|
|
|
self.addCleanup(shutil.rmtree, self.dirname)
|
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.
........
2008-04-18 21:55:37 -03:00
|
|
|
sys.path.insert(0, self.dirname)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
del sys.path[0]
|
|
|
|
|
|
|
|
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)
|
2009-07-05 03:42:44 -03:00
|
|
|
|
|
|
|
names = []
|
|
|
|
for loader, name, ispkg in pkgutil.iter_modules([zip_file]):
|
|
|
|
names.append(name)
|
|
|
|
self.assertEqual(names, ['test_getdata_zipfile'])
|
|
|
|
|
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.
........
2008-04-18 21:55:37 -03:00
|
|
|
del sys.path[0]
|
|
|
|
|
|
|
|
del sys.modules[pkg]
|
|
|
|
|
2011-10-06 18:19:06 -03:00
|
|
|
def test_unreadable_dir_on_syspath(self):
|
|
|
|
# issue7367 - walk_packages failed if unreadable dir on sys.path
|
|
|
|
package_name = "unreadable_package"
|
|
|
|
d = os.path.join(self.dirname, package_name)
|
|
|
|
# this does not appear to create an unreadable dir on Windows
|
|
|
|
# but the test should not fail anyway
|
|
|
|
os.mkdir(d, 0)
|
2011-10-07 16:01:40 -03:00
|
|
|
self.addCleanup(os.rmdir, d)
|
2011-10-06 18:19:06 -03:00
|
|
|
for t in pkgutil.walk_packages(path=[self.dirname]):
|
|
|
|
self.fail("unexpected package found")
|
|
|
|
|
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.
........
2008-04-18 21:55:37 -03:00
|
|
|
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']
|
|
|
|
|
2012-05-15 21:44:06 -03:00
|
|
|
|
2012-05-24 21:21:04 -03:00
|
|
|
# These tests, especially the setup and cleanup, are hideous. They
|
|
|
|
# need to be cleaned up once issue 14715 is addressed.
|
2012-05-15 21:44:06 -03:00
|
|
|
class ExtendPathTests(unittest.TestCase):
|
|
|
|
def create_init(self, pkgname):
|
|
|
|
dirname = tempfile.mkdtemp()
|
|
|
|
sys.path.insert(0, dirname)
|
|
|
|
|
|
|
|
pkgdir = os.path.join(dirname, pkgname)
|
|
|
|
os.mkdir(pkgdir)
|
|
|
|
with open(os.path.join(pkgdir, '__init__.py'), 'w') as fl:
|
|
|
|
fl.write('from pkgutil import extend_path\n__path__ = extend_path(__path__, __name__)\n')
|
|
|
|
|
|
|
|
return dirname
|
|
|
|
|
|
|
|
def create_submodule(self, dirname, pkgname, submodule_name, value):
|
|
|
|
module_name = os.path.join(dirname, pkgname, submodule_name + '.py')
|
|
|
|
with open(module_name, 'w') as fl:
|
|
|
|
print('value={}'.format(value), file=fl)
|
|
|
|
|
2012-05-24 21:21:04 -03:00
|
|
|
def test_simple(self):
|
|
|
|
pkgname = 'foo'
|
|
|
|
dirname_0 = self.create_init(pkgname)
|
|
|
|
dirname_1 = self.create_init(pkgname)
|
|
|
|
self.create_submodule(dirname_0, pkgname, 'bar', 0)
|
|
|
|
self.create_submodule(dirname_1, pkgname, 'baz', 1)
|
|
|
|
import foo.bar
|
|
|
|
import foo.baz
|
|
|
|
# Ensure we read the expected values
|
|
|
|
self.assertEqual(foo.bar.value, 0)
|
|
|
|
self.assertEqual(foo.baz.value, 1)
|
2012-05-15 21:44:06 -03:00
|
|
|
|
2012-05-24 21:21:04 -03:00
|
|
|
# Ensure the path is set up correctly
|
|
|
|
self.assertEqual(sorted(foo.__path__),
|
|
|
|
sorted([os.path.join(dirname_0, pkgname),
|
|
|
|
os.path.join(dirname_1, pkgname)]))
|
|
|
|
|
|
|
|
# Cleanup
|
|
|
|
shutil.rmtree(dirname_0)
|
|
|
|
shutil.rmtree(dirname_1)
|
2012-05-15 21:44:06 -03:00
|
|
|
del sys.path[0]
|
|
|
|
del sys.path[0]
|
2012-05-16 05:48:04 -03:00
|
|
|
del sys.modules['foo']
|
|
|
|
del sys.modules['foo.bar']
|
|
|
|
del sys.modules['foo.baz']
|
2012-05-15 21:44:06 -03:00
|
|
|
|
2012-05-24 21:21:04 -03:00
|
|
|
def test_mixed_namespace(self):
|
|
|
|
pkgname = 'foo'
|
|
|
|
dirname_0 = self.create_init(pkgname)
|
|
|
|
dirname_1 = self.create_init(pkgname)
|
|
|
|
self.create_submodule(dirname_0, pkgname, 'bar', 0)
|
|
|
|
# Turn this into a PEP 420 namespace package
|
|
|
|
os.unlink(os.path.join(dirname_0, pkgname, '__init__.py'))
|
|
|
|
self.create_submodule(dirname_1, pkgname, 'baz', 1)
|
2012-05-15 21:44:06 -03:00
|
|
|
import foo.bar
|
|
|
|
import foo.baz
|
|
|
|
# Ensure we read the expected values
|
|
|
|
self.assertEqual(foo.bar.value, 0)
|
|
|
|
self.assertEqual(foo.baz.value, 1)
|
|
|
|
|
|
|
|
# Ensure the path is set up correctly
|
|
|
|
self.assertEqual(sorted(foo.__path__),
|
2012-05-24 21:21:04 -03:00
|
|
|
sorted([os.path.join(dirname_0, pkgname),
|
|
|
|
os.path.join(dirname_1, pkgname)]))
|
|
|
|
|
|
|
|
# Cleanup
|
|
|
|
shutil.rmtree(dirname_0)
|
|
|
|
shutil.rmtree(dirname_1)
|
|
|
|
del sys.path[0]
|
|
|
|
del sys.path[0]
|
|
|
|
del sys.modules['foo']
|
|
|
|
del sys.modules['foo.bar']
|
|
|
|
del sys.modules['foo.baz']
|
2012-05-15 21:44:06 -03:00
|
|
|
|
|
|
|
# XXX: test .pkg files
|
|
|
|
|
|
|
|
|
2012-07-09 16:23:58 -03:00
|
|
|
class NestedNamespacePackageTest(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.basedir = tempfile.mkdtemp()
|
|
|
|
self.old_path = sys.path[:]
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
sys.path[:] = self.old_path
|
|
|
|
shutil.rmtree(self.basedir)
|
|
|
|
|
|
|
|
def create_module(self, name, contents):
|
|
|
|
base, final = name.rsplit('.', 1)
|
|
|
|
base_path = os.path.join(self.basedir, base.replace('.', os.path.sep))
|
|
|
|
os.makedirs(base_path, exist_ok=True)
|
|
|
|
with open(os.path.join(base_path, final + ".py"), 'w') as f:
|
|
|
|
f.write(contents)
|
|
|
|
|
|
|
|
def test_nested(self):
|
|
|
|
pkgutil_boilerplate = (
|
|
|
|
'import pkgutil; '
|
|
|
|
'__path__ = pkgutil.extend_path(__path__, __name__)')
|
|
|
|
self.create_module('a.pkg.__init__', pkgutil_boilerplate)
|
|
|
|
self.create_module('b.pkg.__init__', pkgutil_boilerplate)
|
|
|
|
self.create_module('a.pkg.subpkg.__init__', pkgutil_boilerplate)
|
|
|
|
self.create_module('b.pkg.subpkg.__init__', pkgutil_boilerplate)
|
|
|
|
self.create_module('a.pkg.subpkg.c', 'c = 1')
|
|
|
|
self.create_module('b.pkg.subpkg.d', 'd = 2')
|
|
|
|
sys.path.insert(0, os.path.join(self.basedir, 'a'))
|
|
|
|
sys.path.insert(0, os.path.join(self.basedir, 'b'))
|
|
|
|
import pkg
|
|
|
|
self.addCleanup(unload, 'pkg')
|
|
|
|
self.assertEqual(len(pkg.__path__), 2)
|
|
|
|
import pkg.subpkg
|
|
|
|
self.addCleanup(unload, 'pkg.subpkg')
|
|
|
|
self.assertEqual(len(pkg.subpkg.__path__), 2)
|
|
|
|
from pkg.subpkg.c import c
|
|
|
|
from pkg.subpkg.d import d
|
|
|
|
self.assertEqual(c, 1)
|
|
|
|
self.assertEqual(d, 2)
|
|
|
|
|
|
|
|
|
2012-07-15 05:09:52 -03:00
|
|
|
class ImportlibMigrationTests(unittest.TestCase):
|
|
|
|
# With full PEP 302 support in the standard import machinery, the
|
|
|
|
# PEP 302 emulation in this module is in the process of being
|
|
|
|
# deprecated in favour of importlib proper
|
|
|
|
|
|
|
|
def check_deprecated(self):
|
|
|
|
return check_warnings(
|
|
|
|
("This emulation is deprecated, use 'importlib' instead",
|
|
|
|
DeprecationWarning))
|
|
|
|
|
|
|
|
def test_importer_deprecated(self):
|
|
|
|
with self.check_deprecated():
|
|
|
|
x = pkgutil.ImpImporter("")
|
|
|
|
|
|
|
|
def test_loader_deprecated(self):
|
|
|
|
with self.check_deprecated():
|
|
|
|
x = pkgutil.ImpLoader("", "", "", "")
|
|
|
|
|
|
|
|
def test_get_loader_avoids_emulation(self):
|
|
|
|
with check_warnings() as w:
|
|
|
|
self.assertIsNotNone(pkgutil.get_loader("sys"))
|
|
|
|
self.assertIsNotNone(pkgutil.get_loader("os"))
|
|
|
|
self.assertIsNotNone(pkgutil.get_loader("test.support"))
|
|
|
|
self.assertEqual(len(w.warnings), 0)
|
|
|
|
|
|
|
|
def test_get_importer_avoids_emulation(self):
|
2012-07-17 08:37:58 -03:00
|
|
|
# We use an illegal path so *none* of the path hooks should fire
|
2012-07-15 05:09:52 -03:00
|
|
|
with check_warnings() as w:
|
2012-07-17 08:37:58 -03:00
|
|
|
self.assertIsNone(pkgutil.get_importer("*??"))
|
2012-07-15 05:09:52 -03:00
|
|
|
self.assertEqual(len(w.warnings), 0)
|
|
|
|
|
|
|
|
def test_iter_importers_avoids_emulation(self):
|
|
|
|
with check_warnings() as w:
|
|
|
|
for importer in pkgutil.iter_importers(): pass
|
|
|
|
self.assertEqual(len(w.warnings), 0)
|
|
|
|
|
|
|
|
|
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.
........
2008-04-18 21:55:37 -03:00
|
|
|
def test_main():
|
2012-07-09 16:23:58 -03:00
|
|
|
run_unittest(PkgutilTests, PkgutilPEP302Tests, ExtendPathTests,
|
2012-07-15 05:09:52 -03:00
|
|
|
NestedNamespacePackageTest, ImportlibMigrationTests)
|
2008-04-21 10:08:03 -03:00
|
|
|
# this is necessary if test is run repeated (like when finding leaks)
|
|
|
|
import zipimport
|
2012-07-15 05:09:52 -03:00
|
|
|
import importlib
|
2008-04-21 10:08:03 -03:00
|
|
|
zipimport._zip_directory_cache.clear()
|
2012-07-15 05:09:52 -03:00
|
|
|
importlib.invalidate_caches()
|
|
|
|
|
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.
........
2008-04-18 21:55:37 -03:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
test_main()
|