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
Show file tree
Hide file tree
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
Fix test_importlib
  • Loading branch information
methane committed Apr 4, 2021
commit 7b4b9a2d70dad8aa94ca1a61e6fc6c5db1bb3f59
2 changes: 1 addition & 1 deletion Lib/test/test_importlib/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ def build_files(file_defs, prefix=pathlib.Path()):
with full_name.open('wb') as f:
f.write(contents)
else:
with full_name.open('w') as f:
with full_name.open('w', encoding='utf-8') as f:
f.write(DALS(contents))


Expand Down
10 changes: 5 additions & 5 deletions Lib/test/test_importlib/source/test_file_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def test_module_reuse(self):
module = loader.load_module('_temp')
module_id = id(module)
module_dict_id = id(module.__dict__)
with open(mapping['_temp'], 'w') as file:
with open(mapping['_temp'], 'w', encoding='utf-8') as file:
file.write("testing_var = 42\n")
with warnings.catch_warnings():
warnings.simplefilter('ignore', DeprecationWarning)
Expand All @@ -145,7 +145,7 @@ def test_state_after_failure(self):
orig_module = types.ModuleType(name)
for attr in attributes:
setattr( 10000 orig_module, attr, value)
with open(mapping[name], 'w') as file:
with open(mapping[name], 'w', encoding='utf-8') as file:
file.write('+++ bad syntax +++')
loader = self.machinery.SourceFileLoader('_temp', mapping['_temp'])
with self.assertRaises(SyntaxError):
Expand All @@ -162,7 +162,7 @@ def test_state_after_failure(self):
# [syntax error]
def test_bad_syntax(self):
with util.create_modules('_temp') as mapping:
with open(mapping['_temp'], 'w') as file:
with open(mapping['_temp'], 'w', encoding='utf-8') as file:
file.write('=')
loader = self.machinery.SourceFileLoader('_temp', mapping['_temp'])
with self.assertRaises(SyntaxError):
Expand All @@ -175,7 +175,7 @@ def test_file_from_empty_string_dir(self):
# Loading a module found from an empty string entry on sys.path should
# not only work, but keep all attributes relative.
file_path = '_temp.py'
with open(file_path, 'w') as file:
with open(file_path, 'w', encoding='utf-8') as file:
file.write("# test file for importlib")
try:
with util.uncache('_temp'):
Expand All @@ -199,7 +199,7 @@ def test_timestamp_overflow(self):
with util.create_modules('_temp') as mapping:
source = mapping['_temp']
compiled = self.util.cache_from_source(source)
with open(source, 'w') as f:
with open(source, 'w', encoding='utf-8') as f:
f.write("x = 5")
try:
os.utime(source, (2 ** 33 - 5, 2 ** 33 - 5))
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_importlib/source/test_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def test_empty_string_for_dir(self):
# The empty string from sys.path means to search in the cwd.
finder = self.machinery.FileFinder('', (self.machinery.SourceFileLoader,
self.machinery.SOURCE_SUFFIXES))
with open('mod.py', 'w') as file:
with open('mod.py', 'w', encoding='utf-8') as file:
file.write("# test file for importlib")
try:
loader = self._find(finder, 'mod', loader_only=True)
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_importlib/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ def test_reload_namespace_changed(self):
'__file__': None,
}
os.mkdir(name)
with open(bad_path, 'w') as init_file:
with open(bad_path, 'w', encoding='utf-8') as init_file:
init_file.write('eggs = None')
module = self.init.import_module(name)
ns = vars(module).copy()
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_importlib/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def test_read_bytes(self):

def test_read_text(self):
files = resources.files(self.data)
actual = files.joinpath('utf-8.file').read_text()
actual = files.joinpath('utf-8.file').read_text(encoding='utf-8')
assert actual == 'Hello, UTF-8 world!\n'

@unittest.skipUnless(
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_importlib/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def pkg_with_dashes(site_dir):
metadata_dir = site_dir / 'my_pkg.dist-info'
metadata_dir.mkdir()
metadata = metadata_dir / 'METADATA'
with metadata.open('w') as strm:
with metadata.open('w', encoding='utf-8') as strm:
strm.write('Version: 1.0\n')
return 'my-pkg'

Expand All @@ -102,7 +102,7 @@ def pkg_with_mixed_case(site_dir):
metadata_dir = site_dir / 'CherryPy.dist-info'
metadata_dir.mkdir()
metadata = metadata_dir / 'METADATA'
with metadata.open('w') as strm:
with metadata.open('w', encoding='utf-8') as strm:
strm.write('Version: 1.0\n')
return 'CherryPy'

Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_importlib/test_pkg_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def rewrite_file(self, contents):
compiled_path = cache_from_source(self.module_path)
if os.path.exists(compiled_path):
os.remove(compiled_path)
with open(self.module_path, 'w') as f:
with open(self.module_path, 'w', encoding='utf-8') as f:
f.write(contents)

def test_package_import__semantics(self):
Expand Down
6 changes: 3 additions & 3 deletions Lib/test/test_importlib/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def case_insensitive_tests(test):

def submodule(parent, name, pkg_dir, content=''):
path = os.path.join(pkg_dir, name + '.py')
with open(path, 'w') as subfile:
with open(path, 'w', encoding='utf-8') as subfile:
subfile.write(content)
return '{}.{}'.format(parent, name), path

Expand Down Expand Up @@ -176,7 +176,7 @@ def temp_module(name, content='', *, pkg=False):
content = ''
if content is not None:
# not a namespace package
with open(modpath, 'w') as modfile:
with open(modpath, 'w', encoding='utf-8') as modfile:
modfile.write(content)
yield location

Expand Down Expand Up @@ -384,7 +384,7 @@ def create_modules(*names):
os.mkdir(file_path)
created_paths.append(file_path)
file_path = os.path.join(file_path, name_parts[-1] + '.py')
with open(file_path, 'w') as file:
with open(file_path, 'w', encoding='utf-8') as file:
file.write(source.format(name))
created_paths.append(file_path)
mapping[name] = file_path
Expand Down
0