8000 bpo-43651: PEP 597: Fix EncodingWarning in some tests by methane · Pull Request #25181 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-43651: PEP 597: Fix EncodingWarning in some tests #25181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 5, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix test_import
  • Loading branch information
methane committed Apr 4, 2021
commit fdb60881804a0e13b8519189a3ec0348c8d49465
45 changes: 23 additions & 22 deletions Lib/test/test_import/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def test_from_import_missing_attr_path_is_canonical(self):
def test_from_import_star_invalid_type(self):
import re
with _ready_to_import() as (name, path):
with open(path, 'w') as f:
with open(path, 'w', encoding='utf-8') as f:
f.write("__all__ = [b'invalid_type']")
globals = {}
with self.assertRaisesRegex(
Expand All @@ -126,7 +126,7 @@ def test_from_import_star_invalid_type(self):
exec(f"from {name} import *", globals)
self.assertNotIn(b"invalid_type", globals)
with _ready_to_import() as (name, path):
with open(path, 'w') as f:
with open(path, 'w', encoding='utf-8') as f:
f.write("globals()[b'invalid_type'] = object()")
globals = {}
with self.assertRaisesRegex(
Expand Down Expand Up @@ -155,7 +155,7 @@ def test_with_extension(ext):
else:
pyc = TESTFN + ".pyc"

with open(source, "w") as f:
with open(source, "w", encoding='utf-8') as f:
print("# This tests Python's ability to import a",
ext, "file.", file=f)
a = random.randrange(1000)
Expand Down Expand Up @@ -195,7 +195,7 @@ def test_module_with_large_stack(self, module='longlist'):
filename = module + '.py'

# Create a file with a list of 65000 elements.
with open(filename, 'w') as f:
with open(filename, 'w', encoding='utf-8') as f:
f.write('d = [\n')
for i in range(65000):
f.write('"",\n')
Expand Down Expand Up @@ -232,7 +232,7 @@ def test_module_with_large_stack(self, module='longlist'):

def test_failing_import_sticks(self):
source = TESTFN + ".py"
with open(source, "w") as f:
with open(source, "w", encoding='utf-8') as f:
print("a = 1/0", file=f)

# New in 2.4, we shouldn't be able to import that no matter how often
Expand Down Expand Up @@ -281,7 +281,7 @@ def test_issue31286(self):
def test_failing_reload(self):
# A failing reload should leave the module object in sys.modules.
source = TESTFN + os.extsep + "py"
with open(source, "w") as f:
with open(source, "w", encoding='utf-8') as f:
f.write("a = 1\nb=2\n")

sys.path.insert(0, os.curdir)
Expand All @@ -298,7 +298,7 @@ def test_failing_reload(self):
remove_files(TESTFN)

# Now damage the module.
with open(source, "w") as f:
with open(source, "w", encoding='utf-8') as f:
f.write("a = 10\nb=20//0\n")

self.assertRaises(ZeroDivisionError, importlib.reload, mod)
Expand All @@ -320,7 +320,7 @@ def test_failing_reload(self):
def test_file_to_source(self):
# check if __file__ points to the source file where available
source = TESTFN + ".py"
with open(source, "w") as f:
with open(source, "w", encoding='utf-8') as f:
f.write("test = None\n")

sys.path.insert(0, os.curdir)
Expand Down Expand Up @@ -369,7 +369,7 @@ def test_timestamp_overflow(self):
try:
source = TESTFN + ".py"
compiled = importlib.util.cache_from_source(source)
with open(source, 'w') as f:
with open(source, 'w', encoding='utf-8') as f:
pass
try:
os.utime(source, (2 ** 33 - 5, 2 ** 33 - 5))
Expand Down Expand Up @@ -574,7 +574,7 @@ def test_pyc_always_writable(self):
# with later updates, see issue #6074 for details
with _ready_to_import() as (name, path):
# Write a Python file, make it read-only and import it
with open(path, 'w') as f:
with open(path, 'w', encoding='utf-8') as f:
f.write("x = 'original'\n")
# Tweak the mtime of the source to ensure pyc gets updated later
s = os.stat(path)
Expand All @@ -584,7 +584,7 @@ def test_pyc_always_writable(self):
self.assertEqual(m.x, 'original')
# Change the file and then reimport it
os.chmod(path, 0o600)
with open(path, 'w') as f:
with open(path, 'w', encoding='utf-8') as f:
f.write("x = 'rewritten'\n")
unload(name)
importlib.invalidate_caches()
Expand Down Expand Up @@ -623,7 +623,7 @@ def setUp(self):
self.sys_path = sys.path[:]
self.orig_module = sys.modules.pop(self.module_name, None)
os.mkdir(self.dir_name)
with open(self.file_name, "w") as f:
with open(self.file_name, "w", encoding='utf-8') as f:
f.write(self.module_source)
10000 sys.path.insert(0, self.dir_name)
importlib.invalidate_caches()
Expand Down Expand Up @@ -704,7 +704,8 @@ def tearDown(self):

# Regression test for http://bugs.python.org/issue1293.
def test_trailing_slash(self):
with open(os.path.join(self.path, 'test_trailing_slash.py'), 'w') as f:
with open(os.path.join(self.path, 'test_trailing_slash.py'),
'w', encoding='utf-8') as f:
f.write("testdata = 'test_trailing_slash'")
sys.path.append(self.path+'/')
mod = __import__("test_trailing_slash")
Expand Down Expand Up @@ -842,7 +843,7 @@ def _clean(self):
def setUp(self):
self.source = TESTFN + '.py'
self._clean()
with open(self.source, 'w') as fp:
with open(self.source, 'w', encoding='utf-8') as fp:
print('# This is a test file written by test_import.py', file=fp)
sys.path.insert(0, os.curdir)
importlib.invalidate_caches()
Expand Down Expand Up @@ -941,9 +942,9 @@ def cleanup():
os.mkdir('pep3147')
self.addCleanup(cleanup)
# Touch the __init__.py
with open(os.path.join('pep3147', '__init__.py'), 'w'):
with open(os.path.join('pep3147', '__init__.py'), 'wb'):
pass
with open(os.path.join('pep3147', 'foo.py'), 'w'):
with open(os.path.join('pep3147', 'foo.py'), 'wb'):
pass
importlib.invalidate_caches()
m = __import__('pep3147.foo')
Expand All @@ -964,9 +965,9 @@ def cleanup():
os.mkdir('pep3147')
self.addCleanup(cleanup)
# Touch the __init__.py
with open(os.path.join('pep3147', '__init__.py'), 'w'):
with open(os.path.join('pep3147', '__init__.py'), 'wb'):
pass
with open(os.path.join('pep3147', 'foo.py'), 'w'):
with open(os.path.join('pep3147', 'foo.py'), 'wb'):
pass
importlib.invalidate_caches()
m = __import__('pep3147.foo')
Expand All @@ -986,7 +987,7 @@ def test_recompute_pyc_same_second(self):
# source size is enough to trigger recomputation of the pyc file.
__import__(TESTFN)
unload(TESTFN)
with open(self.source, 'a') as fp:
with open(self.source, 'a', encoding='utf-8') as fp:
print("x = 5", file=fp)
m = __import__(TESTFN)
self.assertEqual(m.x, 5)
Expand Down Expand Up @@ -1118,7 +1119,7 @@ def tearDown(self):

def create_module(self, mod, contents, ext=".py"):
fname = os.path.join(TESTFN, mod + ext)
with open(fname, "w") as f:
with open(fname, "w", encoding='utf-8') as f:
f.write(contents)
self.addCleanup(unload, mod)
importlib.invalidate_caches()
Expand Down Expand Up @@ -1195,10 +1196,10 @@ def _setup_broken_package(self, parent, child):
os.mkdir(pkg_path)
# Touch the __init__.py
init_path = os.path.join(pkg_path, '__init__.py')
with open(init_path, 'w') as f:
with open(init_path, 'w', encoding='utf-8') as f:
f.write(parent)
bar_path = os.path.join(pkg_path, 'bar.py')
with open(bar_path, 'w') as f:
with open(bar_path, 'w', encoding='utf-8') as f:
f.write(child)
importlib.invalidate_caches()
return init_path, bar_path
Expand Down
0