Skip to content

Commit 70f303e

Browse files
committed
Update imp from CPython 3.10.6
1 parent 582311f commit 70f303e

File tree

5 files changed

+45
-16
lines changed

5 files changed

+45
-16
lines changed

Lib/imp.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
import types
2929
import warnings
3030

31-
warnings.warn("the imp module is deprecated in favour of importlib; "
31+
warnings.warn("the imp module is deprecated in favour of importlib and slated "
32+
"for removal in Python 3.12; "
3233
"see the module's documentation for alternative uses",
3334
DeprecationWarning, stacklevel=2)
3435

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# -*- encoding: utf-8 -*-
2+
3+
# This is a package that contains a number of modules that are used to
4+
# test import from the source files that have different encodings.
5+
# This file (the __init__ module of the package), is encoded in utf-8
6+
# and contains a list of strings from various unicode planes that are
7+
# encoded differently to compare them to the same strings encoded
8+
# differently in submodules. The following list, test_strings,
9+
# contains a list of tuples. The first element of each tuple is the
10+
# suffix that should be prepended with 'module_' to arrive at the
11+
# encoded submodule name, the second item is the encoding and the last
12+
# is the test string. The same string is assigned to the variable
13+
# named 'test' inside the submodule. If the decoding of modules works
14+
# correctly, from module_xyz import test should result in the same
15+
# string as listed below in the 'xyz' entry.
16+
17+
# module, encoding, test string
18+
test_strings = (
19+
('iso_8859_1', 'iso-8859-1', "Les hommes ont oublié cette vérité, "
20+
"dit le renard. Mais tu ne dois pas l'oublier. Tu deviens "
21+
"responsable pour toujours de ce que tu as apprivoisé."),
22+
('koi8_r', 'koi8-r', "Познание бесконечности требует бесконечного времени.")
23+
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# test iso-8859-1 encoding
2+
# -*- encoding: iso-8859-1 -*-
3+
test = ("Les hommes ont oublié cette vérité, "
4+
"dit le renard. Mais tu ne dois pas l'oublier. Tu deviens "
5+
"responsable pour toujours de ce que tu as apprivoisé.")
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# test koi8-r encoding
2+
# -*- encoding: koi8-r -*-
3+
test = "Познание бесконечности требует бесконечного времени."

Lib/test/test_imp.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
import py_compile
66
import sys
77
from test import support
8-
from test.support import script_helper, os_helper, import_helper
8+
from test.support import import_helper
9+
from test.support import os_helper
10+
from test.support import script_helper
911
import unittest
1012
import warnings
1113
with warnings.catch_warnings():
@@ -57,11 +59,10 @@ def testLock(self):
5759
"RuntimeError")
5860

5961
class ImportTests(unittest.TestCase):
60-
# TODO: RustPython
61-
# def setUp(self):
62-
# mod = importlib.import_module('test.encoded_modules')
63-
# self.test_strings = mod.test_strings
64-
# self.test_path = mod.__path__
62+
def setUp(self):
63+
mod = importlib.import_module('test.encoded_modules')
64+
self.test_strings = mod.test_strings
65+
self.test_path = mod.__path__
6566

6667
# TODO: RUSTPYTHON
6768
@unittest.expectedFailure
@@ -71,8 +72,6 @@ def test_import_encoded_module(self):
7172
'module_' + modname)
7273
self.assertEqual(teststr, mod.test)
7374

74-
# TODO: RUSTPYTHON
75-
@unittest.expectedFailure
7675
def test_find_module_encoding(self):
7776
for mod, encoding, _ in self.test_strings:
7877
with imp.find_module('module_' + mod, self.test_path)[0] as fd:
@@ -82,8 +81,6 @@ def test_find_module_encoding(self):
8281
with self.assertRaises(SyntaxError):
8382
imp.find_module('badsyntax_pep3120', path)
8483

85-
# TODO: RUSTPYTHON
86-
@unittest.expectedFailure
8784
def test_issue1267(self):
8885
for mod, encoding, _ in self.test_strings:
8986
fp, filename, info = imp.find_module('module_' + mod,
@@ -107,7 +104,7 @@ def test_issue3594(self):
107104
temp_mod_name = 'test_imp_helper'
108105
sys.path.insert(0, '.')
109106
try:
110-
with open(temp_mod_name + '.py', 'w') as file:
107+
with open(temp_mod_name + '.py', 'w', encoding="latin-1") as file:
111108
file.write("# coding: cp1252\nu = 'test.test_imp'\n")
112109
file, filename, info = imp.find_module(temp_mod_name)
113110
file.close()
@@ -162,7 +159,7 @@ def test_issue5604(self):
162159
# if the curdir is not in sys.path the test fails when run with
163160
# ./python ./Lib/test/regrtest.py test_imp
164161
sys.path.insert(0, os.curdir)
165-
with open(temp_mod_name + '.py', 'w') as file:
162+
with open(temp_mod_name + '.py', 'w', encoding="utf-8") as file:
166163
file.write('a = 1\n')
167164
file, filename, info = imp.find_module(temp_mod_name)
168165
with file:
@@ -190,7 +187,7 @@ def test_issue5604(self):
190187

191188
if not os.path.exists(test_package_name):
192189
os.mkdir(test_package_name)
193-
with open(init_file_name, 'w') as file:
190+
with open(init_file_name, 'w', encoding="utf-8") as file:
194191
file.write('b = 2\n')
195192
with warnings.catch_warnings():
196193
warnings.simplefilter('ignore')
@@ -315,7 +312,7 @@ def test_bug7732(self):
315312
def test_multiple_calls_to_get_data(self):
316313
# Issue #18755: make sure multiple calls to get_data() can succeed.
317314
loader = imp._LoadSourceCompatibility('imp', imp.__file__,
318-
open(imp.__file__))
315+
open(imp.__file__, encoding="utf-8"))
319316
loader.get_data(imp.__file__) # File should be closed
320317
loader.get_data(imp.__file__) # Will need to create a newly opened file
321318

@@ -396,7 +393,7 @@ class ReloadTests(unittest.TestCase):
396393
reload()."""
397394

398395
def test_source(self):
399-
# XXX (ncoghlan): It would be nice to use test.support.CleanImport
396+
# XXX (ncoghlan): It would be nice to use test.import_helper.CleanImport
400397
# here, but that breaks because the os module registers some
401398
# handlers in copy_reg on import. Since CleanImport doesn't
402399
# revert that registration, the module is left in a broken

0 commit comments

Comments
 (0)