diff --git a/Lib/imp.py b/Lib/imp.py index 31f8c7663..e02aaef34 100644 --- a/Lib/imp.py +++ b/Lib/imp.py @@ -28,7 +28,8 @@ import tokenize import types import warnings -warnings.warn("the imp module is deprecated in favour of importlib; " +warnings.warn("the imp module is deprecated in favour of importlib and slated " + "for removal in Python 3.12; " "see the module's documentation for alternative uses", DeprecationWarning, stacklevel=2) diff --git a/Lib/test/encoded_modules/__init__.py b/Lib/test/encoded_modules/__init__.py new file mode 100644 index 000000000..ec43252aa --- /dev/null +++ b/Lib/test/encoded_modules/__init__.py @@ -0,0 +1,23 @@ +# -*- encoding: utf-8 -*- + +# This is a package that contains a number of modules that are used to +# test import from the source files that have different encodings. +# This file (the __init__ module of the package), is encoded in utf-8 +# and contains a list of strings from various unicode planes that are +# encoded differently to compare them to the same strings encoded +# differently in submodules. The following list, test_strings, +# contains a list of tuples. The first element of each tuple is the +# suffix that should be prepended with 'module_' to arrive at the +# encoded submodule name, the second item is the encoding and the last +# is the test string. The same string is assigned to the variable +# named 'test' inside the submodule. If the decoding of modules works +# correctly, from module_xyz import test should result in the same +# string as listed below in the 'xyz' entry. + +# module, encoding, test string +test_strings = ( + ('iso_8859_1', 'iso-8859-1', "Les hommes ont oublié cette vérité, " + "dit le renard. Mais tu ne dois pas l'oublier. Tu deviens " + "responsable pour toujours de ce que tu as apprivoisé."), + ('koi8_r', 'koi8-r', "Познание бесконечности требует бесконечного времени.") +) diff --git a/Lib/test/encoded_modules/module_iso_8859_1.py b/Lib/test/encoded_modules/module_iso_8859_1.py new file mode 100644 index 000000000..8f4a15c90 --- /dev/null +++ b/Lib/test/encoded_modules/module_iso_8859_1.py @@ -0,0 +1,5 @@ +# test iso-8859-1 encoding +# -*- encoding: iso-8859-1 -*- +test = ("Les hommes ont oubli cette vrit, " + "dit le renard. Mais tu ne dois pas l'oublier. Tu deviens " + "responsable pour toujours de ce que tu as apprivois.") diff --git a/Lib/test/encoded_modules/module_koi8_r.py b/Lib/test/encoded_modules/module_koi8_r.py new file mode 100644 index 000000000..9b23a5a21 --- /dev/null +++ b/Lib/test/encoded_modules/module_koi8_r.py @@ -0,0 +1,3 @@ +# test koi8-r encoding +# -*- encoding: koi8-r -*- +test = " ." diff --git a/Lib/test/test_imp.py b/Lib/test/test_imp.py index 00734800f..cc4c05e0f 100644 --- a/Lib/test/test_imp.py +++ b/Lib/test/test_imp.py @@ -5,7 +5,9 @@ import os.path import py_compile import sys from test import support -from test.support import script_helper, os_helper, import_helper +from test.support import import_helper +from test.support import os_helper +from test.support import script_helper import unittest import warnings with warnings.catch_warnings(): @@ -57,11 +59,10 @@ class LockTests(unittest.TestCase): "RuntimeError") class ImportTests(unittest.TestCase): - # TODO: RustPython - # def setUp(self): - # mod = importlib.import_module('test.encoded_modules') - # self.test_strings = mod.test_strings - # self.test_path = mod.__path__ + def setUp(self): + mod = importlib.import_module('test.encoded_modules') + self.test_strings = mod.test_strings + self.test_path = mod.__path__ # TODO: RUSTPYTHON @unittest.expectedFailure @@ -71,8 +72,6 @@ class ImportTests(unittest.TestCase): 'module_' + modname) self.assertEqual(teststr, mod.test) - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_find_module_encoding(self): for mod, encoding, _ in self.test_strings: with imp.find_module('module_' + mod, self.test_path)[0] as fd: @@ -82,8 +81,6 @@ class ImportTests(unittest.TestCase): with self.assertRaises(SyntaxError): imp.find_module('badsyntax_pep3120', path) - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_issue1267(self): for mod, encoding, _ in self.test_strings: fp, filename, info = imp.find_module('module_' + mod, @@ -107,7 +104,7 @@ class ImportTests(unittest.TestCase): temp_mod_name = 'test_imp_helper' sys.path.insert(0, '.') try: - with open(temp_mod_name + '.py', 'w') as file: + with open(temp_mod_name + '.py', 'w', encoding="latin-1") as file: file.write("# coding: cp1252\nu = 'test.test_imp'\n") file, filename, info = imp.find_module(temp_mod_name) file.close() @@ -162,7 +159,7 @@ class ImportTests(unittest.TestCase): # if the curdir is not in sys.path the test fails when run with # ./python ./Lib/test/regrtest.py test_imp sys.path.insert(0, os.curdir) - with open(temp_mod_name + '.py', 'w') as file: + with open(temp_mod_name + '.py', 'w', encoding="utf-8") as file: file.write('a = 1\n') file, filename, info = imp.find_module(temp_mod_name) with file: @@ -190,7 +187,7 @@ class ImportTests(unittest.TestCase): if not os.path.exists(test_package_name): os.mkdir(test_package_name) - with open(init_file_name, 'w') as file: + with open(init_file_name, 'w', encoding="utf-8") as file: file.write('b = 2\n') with warnings.catch_warnings(): warnings.simplefilter('ignore') @@ -315,7 +312,7 @@ class ImportTests(unittest.TestCase): def test_multiple_calls_to_get_data(self): # Issue #18755: make sure multiple calls to get_data() can succeed. loader = imp._LoadSourceCompatibility('imp', imp.__file__, - open(imp.__file__)) + open(imp.__file__, encoding="utf-8")) loader.get_data(imp.__file__) # File should be closed loader.get_data(imp.__file__) # Will need to create a newly opened file @@ -396,7 +393,7 @@ class ReloadTests(unittest.TestCase): reload().""" def test_source(self): - # XXX (ncoghlan): It would be nice to use test.support.CleanImport + # XXX (ncoghlan): It would be nice to use test.import_helper.CleanImport # here, but that breaks because the os module registers some # handlers in copy_reg on import. Since CleanImport doesn't # revert that registration, the module is left in a broken