Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
test.test_imp.ImportTests Class Reference

List of all members.

Public Member Functions

def setUp
def test_import_encoded_module
def test_find_module_encoding
def test_issue1267
def test_issue3594
def test_issue5604
def test_issue9319

Public Attributes

 test_strings
 test_path

Detailed Description

Definition at line 44 of file test_imp.py.


Member Function Documentation

Definition at line 45 of file test_imp.py.

00045 
00046     def setUp(self):
00047         mod = importlib.import_module('test.encoded_modules')
00048         self.test_strings = mod.test_strings
00049         self.test_path = mod.__path__

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 56 of file test_imp.py.

00056 
00057     def test_find_module_encoding(self):
00058         for mod, encoding, _ in self.test_strings:
00059             with imp.find_module('module_' + mod, self.test_path)[0] as fd:
00060                 self.assertEqual(fd.encoding, encoding)

Here is the call graph for this function:

Definition at line 50 of file test_imp.py.

00050 
00051     def test_import_encoded_module(self):
00052         for modname, encoding, teststr in self.test_strings:
00053             mod = importlib.import_module('test.encoded_modules.'
00054                                           'module_' + modname)
00055             self.assertEqual(teststr, mod.test)

Here is the call graph for this function:

Definition at line 61 of file test_imp.py.

00061 
00062     def test_issue1267(self):
00063         for mod, encoding, _ in self.test_strings:
00064             fp, filename, info  = imp.find_module('module_' + mod,
00065                                                   self.test_path)
00066             with fp:
00067                 self.assertNotEqual(fp, None)
00068                 self.assertEqual(fp.encoding, encoding)
00069                 self.assertEqual(fp.tell(), 0)
00070                 self.assertEqual(fp.readline(), '# test %s encoding\n'
00071                                  % encoding)
00072 
00073         fp, filename, info = imp.find_module("tokenize")
00074         with fp:
00075             self.assertNotEqual(fp, None)
00076             self.assertEqual(fp.encoding, "utf-8")
00077             self.assertEqual(fp.tell(), 0)
00078             self.assertEqual(fp.readline(),
00079                              '"""Tokenization help for Python programs.\n')

Here is the call graph for this function:

Definition at line 80 of file test_imp.py.

00080 
00081     def test_issue3594(self):
00082         temp_mod_name = 'test_imp_helper'
00083         sys.path.insert(0, '.')
00084         try:
00085             with open(temp_mod_name + '.py', 'w') as file:
00086                 file.write("# coding: cp1252\nu = 'test.test_imp'\n")
00087             file, filename, info = imp.find_module(temp_mod_name)
00088             file.close()
00089             self.assertEqual(file.encoding, 'cp1252')
00090         finally:
00091             del sys.path[0]
00092             support.unlink(temp_mod_name + '.py')
00093             support.unlink(temp_mod_name + '.pyc')
00094             support.unlink(temp_mod_name + '.pyo')

Here is the call graph for this function:

Definition at line 95 of file test_imp.py.

00095 
00096     def test_issue5604(self):
00097         # Test cannot cover imp.load_compiled function.
00098         # Martin von Loewis note what shared library cannot have non-ascii
00099         # character because init_xxx function cannot be compiled
00100         # and issue never happens for dynamic modules.
00101         # But sources modified to follow generic way for processing pathes.
00102 
00103         # the return encoding could be uppercase or None
00104         fs_encoding = sys.getfilesystemencoding()
00105 
00106         # covers utf-8 and Windows ANSI code pages
00107         # one non-space symbol from every page
00108         # (http://en.wikipedia.org/wiki/Code_page)
00109         known_locales = {
00110             'utf-8' : b'\xc3\xa4',
00111             'cp1250' : b'\x8C',
00112             'cp1251' : b'\xc0',
00113             'cp1252' : b'\xc0',
00114             'cp1253' : b'\xc1',
00115             'cp1254' : b'\xc0',
00116             'cp1255' : b'\xe0',
00117             'cp1256' : b'\xe0',
00118             'cp1257' : b'\xc0',
00119             'cp1258' : b'\xc0',
00120             }
00121 
00122         if sys.platform == 'darwin':
00123             self.assertEqual(fs_encoding, 'utf-8')
00124             # Mac OS X uses the Normal Form D decomposition
00125             # http://developer.apple.com/mac/library/qa/qa2001/qa1173.html
00126             special_char = b'a\xcc\x88'
00127         else:
00128             special_char = known_locales.get(fs_encoding)
00129 
00130         if not special_char:
00131             self.skipTest("can't run this test with %s as filesystem encoding"
00132                           % fs_encoding)
00133         decoded_char = special_char.decode(fs_encoding)
00134         temp_mod_name = 'test_imp_helper_' + decoded_char
00135         test_package_name = 'test_imp_helper_package_' + decoded_char
00136         init_file_name = os.path.join(test_package_name, '__init__.py')
00137         try:
00138             # if the curdir is not in sys.path the test fails when run with
00139             # ./python ./Lib/test/regrtest.py test_imp
00140             sys.path.insert(0, os.curdir)
00141             with open(temp_mod_name + '.py', 'w') as file:
00142                 file.write('a = 1\n')
00143             file, filename, info = imp.find_module(temp_mod_name)
00144             with file:
00145                 self.assertIsNotNone(file)
00146                 self.assertTrue(filename[:-3].endswith(temp_mod_name))
00147                 self.assertEqual(info[0], '.py')
00148                 self.assertEqual(info[1], 'U')
00149                 self.assertEqual(info[2], imp.PY_SOURCE)
00150 
00151                 mod = imp.load_module(temp_mod_name, file, filename, info)
00152                 self.assertEqual(mod.a, 1)
00153 
00154             mod = imp.load_source(temp_mod_name, temp_mod_name + '.py')
00155             self.assertEqual(mod.a, 1)
00156 
00157             mod = imp.load_compiled(
00158                 temp_mod_name, imp.cache_from_source(temp_mod_name + '.py'))
00159             self.assertEqual(mod.a, 1)
00160 
00161             if not os.path.exists(test_package_name):
00162                 os.mkdir(test_package_name)
00163             with open(init_file_name, 'w') as file:
00164                 file.write('b = 2\n')
00165             package = imp.load_package(test_package_name, test_package_name)
00166             self.assertEqual(package.b, 2)
00167         finally:
00168             del sys.path[0]
00169             for ext in ('.py', '.pyc', '.pyo'):
00170                 support.unlink(temp_mod_name + ext)
00171                 support.unlink(init_file_name + ext)
00172             support.rmtree(test_package_name)

Here is the call graph for this function:

Definition at line 173 of file test_imp.py.

00173 
00174     def test_issue9319(self):
00175         path = os.path.dirname(__file__)
00176         self.assertRaises(SyntaxError,
00177                           imp.find_module, "badsyntax_pep3120", [path])
00178 

Here is the call graph for this function:


Member Data Documentation

Definition at line 48 of file test_imp.py.

Definition at line 47 of file test_imp.py.


The documentation for this class was generated from the following file: