Merged revisions 78689,78696-78697,78708,78711 via svnmerge from

svn+ssh://pythondev@svn.python.org/python/branches/py3k

........
  r78689 | ezio.melotti | 2010-03-05 14:43:17 +0200 (Fri, 05 Mar 2010) | 1 line

  This fixes a missing .lower() on the encoding name, a wrong byte undecodable by UTF-8, a wrong variable name, hopefully some windows buildbot on 3.x and adds a proper skip. It might break other things though.
........
  r78696 | ezio.melotti | 2010-03-05 17:08:19 +0200 (Fri, 05 Mar 2010) | 1 line

  r78689 enabled the test on more platforms but the buildbot did not like it. Using the filesystem encoding might work better. Also see #5604.
........
  r78697 | ezio.melotti | 2010-03-05 17:17:26 +0200 (Fri, 05 Mar 2010) | 1 line

  sys.getdefaultencoding() can return None.
........
  r78708 | ezio.melotti | 2010-03-06 03:20:49 +0200 (Sat, 06 Mar 2010) | 1 line

  Cleanup and minor fixes.
........
  r78711 | ezio.melotti | 2010-03-06 03:50:25 +0200 (Sat, 06 Mar 2010) | 1 line

  The test was failing because the curdir was missing from sys.path. This should fix the problem.
........
This commit is contained in:
Ezio Melotti 2010-03-06 03:09:26 +00:00
parent 7857650833
commit c0ddee54b0
1 changed files with 43 additions and 40 deletions

View File

@ -1,5 +1,4 @@
import imp import imp
import locale
import os import os
import os.path import os.path
import sys import sys
@ -85,13 +84,15 @@ class ImportTests(unittest.TestCase):
# and issue never happens for dynamic modules. # and issue never happens for dynamic modules.
# But sources modified to follow generic way for processing pathes. # But sources modified to follow generic way for processing pathes.
locale_encoding = locale.getpreferredencoding() # the return encoding could be uppercase or None
fs_encoding = sys.getfilesystemencoding()
fs_encoding = fs_encoding.lower() if fs_encoding else 'ascii'
# covers utf-8 and Windows ANSI code pages # covers utf-8 and Windows ANSI code pages
# one non-space symbol from every page # one non-space symbol from every page
# (http://en.wikipedia.org/wiki/Code_page) # (http://en.wikipedia.org/wiki/Code_page)
known_locales = { known_locales = {
'utf-8' : b'\xe4', 'utf-8' : b'\xc3\xa4',
'cp1250' : b'\x8C', 'cp1250' : b'\x8C',
'cp1251' : b'\xc0', 'cp1251' : b'\xc0',
'cp1252' : b'\xc0', 'cp1252' : b'\xc0',
@ -103,47 +104,49 @@ class ImportTests(unittest.TestCase):
'cp1258' : b'\xc0', 'cp1258' : b'\xc0',
} }
special_char = known_locales.get(locale_encoding) special_char = known_locales.get(fs_encoding)
if special_char: if not special_char:
encoded_char = special_char.decode(locale_encoding) self.skipTest("can't run this test with %s as filesystem encoding"
temp_mod_name = 'test_imp_helper_' + encoded_char % fs_encoding)
test_package_name = 'test_imp_helper_package_' + encoded_char decoded_char = special_char.decode(fs_encoding)
init_file_name = os.path.join(test_package_name, '__init__.py') temp_mod_name = 'test_imp_helper_' + decoded_char
try: test_package_name = 'test_imp_helper_package_' + decoded_char
with open(temp_mod_name + '.py', 'w') as file: init_file_name = os.path.join(test_package_name, '__init__.py')
file.write('a = 1\n') try:
file, filename, info = imp.find_module(temp_mod_name) # if the curdir is not in sys.path the test fails when run with
self.assertNotEquals(None, file) # ./python ./Lib/test/regrtest.py test_imp
self.assertTrue(filename[:-3].endswith(temp_mod_name)) sys.path.insert(0, os.curdir)
self.assertEquals('.py', info[0]) with open(temp_mod_name + '.py', 'w') as file:
self.assertEquals('U', info[1]) file.write('a = 1\n')
self.assertEquals(imp.PY_SOURCE, info[2]) file, filename, info = imp.find_module(temp_mod_name)
self.assertIsNotNone(file)
self.assertTrue(filename[:-3].endswith(temp_mod_name))
self.assertEqual(info[0], '.py')
self.assertEqual(info[1], 'U')
self.assertEqual(info[2], imp.PY_SOURCE)
mod = imp.load_module(temp_mod_name, file, filename, info) mod = imp.load_module(temp_mod_name, file, filename, info)
self.assertEquals(1, mod.a) self.assertEqual(mod.a, 1)
file.close() file.close()
mod = imp.load_source(temp_mod_name, temp_mod_name + '.py') mod = imp.load_source(temp_mod_name, temp_mod_name + '.py')
self.assertEquals(1, mod.a) self.assertEqual(mod.a, 1)
mod = imp.load_compiled(temp_mod_name, temp_mod_name + '.pyc') mod = imp.load_compiled(temp_mod_name, temp_mod_name + '.pyc')
self.assertEquals(1, mod.a) self.assertEqual(mod.a, 1)
if not os.path.exists(test_package_name): if not os.path.exists(test_package_name):
os.mkdir(test_package_name) os.mkdir(test_package_name)
with open(init_file_name, 'w') as file: with open(init_file_name, 'w') as file:
file.write('b = 2\n') file.write('b = 2\n')
package = imp.load_package(test_package_name, test_package_name) package = imp.load_package(test_package_name, test_package_name)
self.assertEquals(2, package.b) self.assertEqual(package.b, 2)
finally: finally:
support.unlink(temp_mod_name + '.py') del sys.path[0]
support.unlink(temp_mod_name + '.pyc') for ext in ('.py', '.pyc', '.pyo'):
support.unlink(temp_mod_name + '.pyo') support.unlink(temp_mod_name + ext)
support.unlink(init_file_name + ext)
support.unlink(init_file_name + '.py') support.rmtree(test_package_name)
support.unlink(init_file_name + '.pyc')
support.unlink(init_file_name + '.pyo')
support.rmtree(test_package_name)
def test_reload(self): def test_reload(self):