Remove encoding warnings. Fixes #3810.
diff --git a/newsfragments/3810.feature.rst b/newsfragments/3810.feature.rst new file mode 100644 index 0000000..e748648 --- /dev/null +++ b/newsfragments/3810.feature.rst
@@ -0,0 +1 @@ +Avoid EncodingWarnings per PEP 597.
diff --git a/pkg_resources/tests/test_pkg_resources.py b/pkg_resources/tests/test_pkg_resources.py index a05aeb2..32cd4b5 100644 --- a/pkg_resources/tests/test_pkg_resources.py +++ b/pkg_resources/tests/test_pkg_resources.py
@@ -120,13 +120,12 @@ filename = zp.get_resource_filename(manager, 'data.dat') actual = datetime.datetime.fromtimestamp(os.stat(filename).st_mtime) assert actual == self.ref_time - f = open(filename, 'w') - f.write('hello, world?') - f.close() + with open(filename, 'w', encoding='utf-8') as f: + f.write('hello, world?') ts = timestamp(self.ref_time) os.utime(filename, (ts, ts)) filename = zp.get_resource_filename(manager, 'data.dat') - with open(filename) as f: + with open(filename, encoding='utf-8') as f: assert f.read() == 'hello, world!' manager.cleanup_resources()
diff --git a/pytest.ini b/pytest.ini index b9f7544..253230a 100644 --- a/pytest.ini +++ b/pytest.ini
@@ -74,10 +74,13 @@ # https://github.com/pypa/setuptools/issues/3655 ignore:The --rsyncdir command line argument and rsyncdirs config variable are deprecated.:DeprecationWarning - # Workarounds for pypa/setuptools#3810 - # Can't use EncodingWarning as it doesn't exist on Python 3.9 - default:'encoding' argument not specified - default:UTF-8 Mode affects locale.getpreferredencoding(). + # workarounds for upstream EncodingWarnings + ignore:'encoding' argument not specified::distutils.text_file + ignore:'encoding' argument not specified::setuptools._distutils.text_file + ignore:'encoding' argument not specified::distutils.dist + ignore:'encoding' argument not specified::setuptools._distutils.dist + ignore:'encoding' argument not specified::_pytest._py.path + ignore:'encoding' argument not specified::_pytest._py.error # Avoid errors when testing pkg_resources.declare_namespace ignore:.*pkg_resources\.declare_namespace.*:DeprecationWarning
diff --git a/setuptools/_imp.py b/setuptools/_imp.py index 9d4ead0..34bb1d6 100644 --- a/setuptools/_imp.py +++ b/setuptools/_imp.py
@@ -65,8 +65,9 @@ elif suffix in importlib.machinery.EXTENSION_SUFFIXES: kind = C_EXTENSION + encoding = dict(encoding='utf-8') if 'b' not in mode else {} if kind in {PY_SOURCE, PY_COMPILED}: - file = open(path, mode) + file = open(path, mode, **encoding) else: path = None suffix = mode = ''
diff --git a/setuptools/command/bdist_egg.py b/setuptools/command/bdist_egg.py index bdece56..2780d35 100644 --- a/setuptools/command/bdist_egg.py +++ b/setuptools/command/bdist_egg.py
@@ -349,9 +349,8 @@ if safe is None or bool(safe) != flag: os.unlink(fn) elif safe is not None and bool(safe) == flag: - f = open(fn, 'wt') - f.write('\n') - f.close() + with open(fn, 'wt', encoding='utf-8') as f: + f.write('\n') safety_flags = {
diff --git a/setuptools/command/develop.py b/setuptools/command/develop.py index 27bcf9e..c833afd 100644 --- a/setuptools/command/develop.py +++ b/setuptools/command/develop.py
@@ -117,7 +117,7 @@ # create an .egg-link in the installation dir, pointing to our egg log.info("Creating %s (link to %s)", self.egg_link, self.egg_base) if not self.dry_run: - with open(self.egg_link, "w") as f: + with open(self.egg_link, "w", encoding='utf-8') as f: f.write(self.egg_path + "\n" + self.setup_path) # postprocess the installed distro, fixing up .pth, installing scripts, # and handling requirements
diff --git a/setuptools/command/easy_install.py b/setuptools/command/easy_install.py index 8ba4f09..c412d78 100644 --- a/setuptools/command/easy_install.py +++ b/setuptools/command/easy_install.py
@@ -496,7 +496,7 @@ try: if test_exists: os.unlink(testfile) - open(testfile, 'w').close() + open(testfile, 'w', encoding='utf-8').close() os.unlink(testfile) except (OSError, IOError): self.cant_write_to_target() @@ -593,7 +593,7 @@ os.unlink(ok_file) dirname = os.path.dirname(ok_file) os.makedirs(dirname, exist_ok=True) - f = open(pth_file, 'w') + f = open(pth_file, 'w', encoding='utf-8') except (OSError, IOError): self.cant_write_to_target() else: @@ -876,7 +876,8 @@ ensure_directory(target) if os.path.exists(target): os.unlink(target) - with open(target, "w" + mode) as f: + encoding = dict(encoding='utf-8') if 'b' not in mode else {} + with open(target, "w" + mode, **encoding) as f: f.write(contents) chmod(target, 0o777 - mask) @@ -1629,7 +1630,7 @@ paths = [] dirty = saw_import = False seen = dict.fromkeys(self.sitedirs) - f = open(self.filename, 'rt') + f = open(self.filename, 'rt', encoding='utf-8') for line in f: path = line.rstrip() # still keep imports and empty/commented lines for formatting @@ -1698,7 +1699,7 @@ data = '\n'.join(lines) + '\n' if os.path.islink(self.filename): os.unlink(self.filename) - with open(self.filename, 'wt') as f: + with open(self.filename, 'wt', encoding='utf-8') as f: f.write(data) elif os.path.exists(self.filename): log.debug("Deleting empty %s", self.filename)
diff --git a/setuptools/command/install_scripts.py b/setuptools/command/install_scripts.py index 72b2e45..935a781 100644 --- a/setuptools/command/install_scripts.py +++ b/setuptools/command/install_scripts.py
@@ -59,8 +59,8 @@ mask = current_umask() if not self.dry_run: + encoding = dict(encoding='utf-8') if 'b' not in mode else {} ensure_directory(target) - f = open(target, "w" + mode) - f.write(contents) - f.close() + with open(target, "w" + mode, **encoding) as f: + f.write(contents) chmod(target, 0o777 - mask)
diff --git a/setuptools/command/setopt.py b/setuptools/command/setopt.py index f9a60751..38c52b0 100644 --- a/setuptools/command/setopt.py +++ b/setuptools/command/setopt.py
@@ -36,7 +36,7 @@ log.debug("Reading configuration from %s", filename) opts = configparser.RawConfigParser() opts.optionxform = lambda x: x - opts.read([filename]) + opts.read([filename], encoding='utf-8') for section, options in settings.items(): if options is None: log.info("Deleting section [%s] from %s", section, filename) @@ -62,7 +62,7 @@ log.info("Writing %s", filename) if not dry_run: - with open(filename, 'w') as f: + with open(filename, 'w', encoding='utf-8') as f: opts.write(f)
diff --git a/setuptools/dist.py b/setuptools/dist.py index 429606f..9084d0d 100644 --- a/setuptools/dist.py +++ b/setuptools/dist.py
@@ -951,7 +951,7 @@ os.mkdir(egg_cache_dir) windows_support.hide_file(egg_cache_dir) readme_txt_filename = os.path.join(egg_cache_dir, 'README.txt') - with open(readme_txt_filename, 'w') as f: + with open(readme_txt_filename, 'w', encoding='utf-8') as f: f.write( 'This directory contains eggs that were downloaded ' 'by setuptools to build, test, and run plug-ins.\n\n'
diff --git a/setuptools/package_index.py b/setuptools/package_index.py index 7095585..ed2e6d5 100644 --- a/setuptools/package_index.py +++ b/setuptools/package_index.py
@@ -1009,7 +1009,7 @@ rc = os.path.join(os.path.expanduser('~'), '.pypirc') if os.path.exists(rc): - self.read(rc) + self.read(rc, encoding='utf-8') @property def creds_by_repository(self): @@ -1111,7 +1111,7 @@ for f in os.listdir(filename): filepath = os.path.join(filename, f) if f == 'index.html': - with open(filepath, 'r') as fp: + with open(filepath, 'r', encoding='utf-8') as fp: body = fp.read() break elif os.path.isdir(filepath):
diff --git a/setuptools/sandbox.py b/setuptools/sandbox.py index 017c897..d516f62 100644 --- a/setuptools/sandbox.py +++ b/setuptools/sandbox.py
@@ -452,6 +452,8 @@ def _open(self, path, mode='r', *args, **kw): if mode not in ('r', 'rt', 'rb', 'rU', 'U') and not self._ok(path): self._violation("open", path, mode, *args, **kw) + if 'encoding' not in kw and 'b' not in mode: + raise ValueError('Need Encoding') return _open(path, mode, *args, **kw) def tmpnam(self):
diff --git a/setuptools/tests/__init__.py b/setuptools/tests/__init__.py index 564adf2..f989e06 100644 --- a/setuptools/tests/__init__.py +++ b/setuptools/tests/__init__.py
@@ -6,5 +6,5 @@ __all__ = ['fail_on_ascii'] -is_ascii = locale.getpreferredencoding() == 'ANSI_X3.4-1968' +is_ascii = locale.getencoding() == 'ANSI_X3.4-1968' fail_on_ascii = pytest.mark.xfail(is_ascii, reason="Test fails in this locale")
diff --git a/setuptools/tests/config/test_apply_pyprojecttoml.py b/setuptools/tests/config/test_apply_pyprojecttoml.py index fd240f8..ace4512 100644 --- a/setuptools/tests/config/test_apply_pyprojecttoml.py +++ b/setuptools/tests/config/test_apply_pyprojecttoml.py
@@ -172,9 +172,9 @@ text = text.replace(orig, subst) pyproject.write_text(text, encoding="utf-8") - (tmp_path / readme).write_text("hello world") - (tmp_path / "LICENSE.txt").write_text("--- LICENSE stub ---") - (tmp_path / "spam.py").write_text(PEP621_EXAMPLE_SCRIPT) + (tmp_path / readme).write_text("hello world", encoding='utf-8') + (tmp_path / "LICENSE.txt").write_text("--- LICENSE stub ---", encoding='utf-8') + (tmp_path / "spam.py").write_text(PEP621_EXAMPLE_SCRIPT, encoding='utf-8') return pyproject
diff --git a/setuptools/tests/config/test_expand.py b/setuptools/tests/config/test_expand.py index cdcbffc..14595e3 100644 --- a/setuptools/tests/config/test_expand.py +++ b/setuptools/tests/config/test_expand.py
@@ -12,7 +12,7 @@ for file, content in files.items(): path = root_dir / file path.parent.mkdir(exist_ok=True, parents=True) - path.write_text(content) + path.write_text(content, encoding='utf-8') def test_glob_relative(tmp_path, monkeypatch):
diff --git a/setuptools/tests/config/test_pyprojecttoml.py b/setuptools/tests/config/test_pyprojecttoml.py index 81ec949..69877109 100644 --- a/setuptools/tests/config/test_pyprojecttoml.py +++ b/setuptools/tests/config/test_pyprojecttoml.py
@@ -97,11 +97,17 @@ (path / file).parent.mkdir(exist_ok=True, parents=True) (path / file).touch() - pyproject.write_text(EXAMPLE) - (path / "README.md").write_text("hello world") - (path / f"{pkg_root}/pkg/mod.py").write_text("class CustomSdist: pass") - (path / f"{pkg_root}/pkg/__version__.py").write_text("VERSION = (3, 10)") - (path / f"{pkg_root}/pkg/__main__.py").write_text("def exec(): print('hello')") + pyproject.write_text(EXAMPLE, encoding='utf-8') + (path / "README.md").write_text("hello world", encoding='utf-8') + (path / f"{pkg_root}/pkg/mod.py").write_text( + "class CustomSdist: pass", encoding='utf-8' + ) + (path / f"{pkg_root}/pkg/__version__.py").write_text( + "VERSION = (3, 10)", encoding='utf-8' + ) + (path / f"{pkg_root}/pkg/__main__.py").write_text( + "def exec(): print('hello')", encoding='utf-8' + ) def verify_example(config, path, pkg_root): @@ -175,7 +181,7 @@ def write_entry_points(self, tmp_path): entry_points = ConfigParser() entry_points.read_dict(ENTRY_POINTS) - with open(tmp_path / "entry-points.txt", "w") as f: + with open(tmp_path / "entry-points.txt", "w", encoding='utf-8') as f: entry_points.write(f) def pyproject(self, dynamic=None): @@ -219,7 +225,9 @@ Framework :: Flask Programming Language :: Haskell """ - (tmp_path / "classifiers.txt").write_text(cleandoc(classifiers)) + (tmp_path / "classifiers.txt").write_text( + cleandoc(classifiers), encoding='utf-8' + ) pyproject = tmp_path / "pyproject.toml" config = read_configuration(pyproject, expand=False) @@ -247,7 +255,7 @@ """ pyproject = tmp_path / "pyproject.toml" - pyproject.write_text(cleandoc(config)) + pyproject.write_text(cleandoc(config), encoding='utf-8') with pytest.raises(OptionError, match="No configuration .* .classifiers."): read_configuration(pyproject) @@ -259,7 +267,7 @@ dynamic = ["readme"] """ pyproject = tmp_path / "pyproject.toml" - pyproject.write_text(cleandoc(config)) + pyproject.write_text(cleandoc(config), encoding='utf-8') dist = Distribution(attrs={"long_description": "42"}) # No error should occur because of missing `readme` dist = apply_configuration(dist, pyproject) @@ -277,7 +285,7 @@ """ pyproject = tmp_path / "pyproject.toml" - pyproject.write_text(cleandoc(config)) + pyproject.write_text(cleandoc(config), encoding='utf-8') with pytest.warns(UserWarning, match="File .*classifiers.txt. cannot be found"): expanded = read_configuration(pyproject) assert "classifiers" not in expanded["project"] @@ -298,7 +306,7 @@ ) def test_ignore_unrelated_config(tmp_path, example): pyproject = tmp_path / "pyproject.toml" - pyproject.write_text(cleandoc(example)) + pyproject.write_text(cleandoc(example), encoding='utf-8') # Make sure no error is raised due to 3rd party configs in pyproject.toml assert read_configuration(pyproject) is not None @@ -320,7 +328,7 @@ ) def test_invalid_example(tmp_path, example, error_msg): pyproject = tmp_path / "pyproject.toml" - pyproject.write_text(cleandoc(example)) + pyproject.write_text(cleandoc(example), encoding='utf-8') pattern = re.compile(f"invalid pyproject.toml.*{error_msg}.*", re.M | re.S) with pytest.raises(ValueError, match=pattern): @@ -330,7 +338,7 @@ @pytest.mark.parametrize("config", ("", "[tool.something]\nvalue = 42")) def test_empty(tmp_path, config): pyproject = tmp_path / "pyproject.toml" - pyproject.write_text(config) + pyproject.write_text(config, encoding='utf-8') # Make sure no error is raised assert read_configuration(pyproject) == {} @@ -342,7 +350,7 @@ default. """ pyproject = tmp_path / "pyproject.toml" - pyproject.write_text(config) + pyproject.write_text(config, encoding='utf-8') config = read_configuration(pyproject) assert config["tool"]["setuptools"]["include-package-data"] is True @@ -355,9 +363,11 @@ See https://github.com/pypa/setuptools/issues/3197#issuecomment-1079023889 """ pyproject = tmp_path / "pyproject.toml" - pyproject.write_text("[project]\nname = 'myproj'\nversion='42'\n") + pyproject.write_text("[project]\nname = 'myproj'\nversion='42'\n", encoding='utf-8') setuppy = tmp_path / "setup.py" - setuppy.write_text("__import__('setuptools').setup(include_package_data=False)") + setuppy.write_text( + "__import__('setuptools').setup(include_package_data=False)", encoding='utf-8' + ) with _Path(tmp_path): dist = distutils.core.run_setup("setup.py", {}, stop_after="config")
diff --git a/setuptools/tests/config/test_pyprojecttoml_dynamic_deps.py b/setuptools/tests/config/test_pyprojecttoml_dynamic_deps.py index b651622..11d5757 100644 --- a/setuptools/tests/config/test_pyprojecttoml_dynamic_deps.py +++ b/setuptools/tests/config/test_pyprojecttoml_dynamic_deps.py
@@ -6,7 +6,7 @@ def test_dynamic_dependencies(tmp_path): - (tmp_path / "requirements.txt").write_text("six\n # comment\n") + (tmp_path / "requirements.txt").write_text("six\n # comment\n", encoding='utf-8') pyproject = tmp_path / "pyproject.toml" pyproject.write_text( DALS( @@ -23,7 +23,8 @@ [tool.setuptools.dynamic.dependencies] file = ["requirements.txt"] """ - ) + ), + encoding='utf-8', ) dist = Distribution() dist = apply_configuration(dist, pyproject) @@ -31,7 +32,9 @@ def test_dynamic_optional_dependencies(tmp_path): - (tmp_path / "requirements-docs.txt").write_text("sphinx\n # comment\n") + (tmp_path / "requirements-docs.txt").write_text( + "sphinx\n # comment\n", encoding='utf-8' + ) pyproject = tmp_path / "pyproject.toml" pyproject.write_text( DALS( @@ -48,7 +51,8 @@ requires = ["setuptools", "wheel"] build-backend = "setuptools.build_meta" """ - ) + ), + encoding='utf-8', ) dist = Distribution() dist = apply_configuration(dist, pyproject) @@ -61,7 +65,9 @@ configurations in the case of fields containing sub-fields (groups), things would work out. """ - (tmp_path / "requirements-images.txt").write_text("pillow~=42.0\n # comment\n") + (tmp_path / "requirements-images.txt").write_text( + "pillow~=42.0\n # comment\n", encoding='utf-8' + ) pyproject = tmp_path / "pyproject.toml" pyproject.write_text( DALS( @@ -81,7 +87,8 @@ requires = ["setuptools", "wheel"] build-backend = "setuptools.build_meta" """ - ) + ), + encoding='utf-8', ) # Test that the mix-and-match doesn't currently validate. with pytest.raises(ValueError, match="project.optional-dependencies"):
diff --git a/setuptools/tests/config/test_setupcfg.py b/setuptools/tests/config/test_setupcfg.py index fa16728..30e343d 100644 --- a/setuptools/tests/config/test_setupcfg.py +++ b/setuptools/tests/config/test_setupcfg.py
@@ -911,7 +911,8 @@ module_path = Path(tmpdir, "src/custom_build.py") # auto discovery for src module_path.parent.mkdir(parents=True, exist_ok=True) module_path.write_text( - "from distutils.core import Command\n" "class CustomCmd(Command): pass\n" + "from distutils.core import Command\n" "class CustomCmd(Command): pass\n", + encoding='utf-8', ) setup_cfg = """
diff --git a/setuptools/tests/integration/helpers.py b/setuptools/tests/integration/helpers.py index d7d43bd..519a4e8 100644 --- a/setuptools/tests/integration/helpers.py +++ b/setuptools/tests/integration/helpers.py
@@ -17,8 +17,9 @@ stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, - env={**os.environ, **(env or {})} + env={**os.environ, **(env or {})}, # ^-- allow overwriting instead of discarding the current env + encoding='utf-8', ) out = r.stdout + "\n" + r.stderr
diff --git a/setuptools/tests/test_build_meta.py b/setuptools/tests/test_build_meta.py index ce58ef2..13d86f3 100644 --- a/setuptools/tests/test_build_meta.py +++ b/setuptools/tests/test_build_meta.py
@@ -296,7 +296,7 @@ first_result = build_method(dist_dir) # Change version. - with open("VERSION", "wt") as version_file: + with open("VERSION", "wt", encoding='utf-8') as version_file: version_file.write("0.0.2") # Build a *second* sdist/wheel. @@ -306,7 +306,7 @@ assert first_result != second_result # And if rebuilding the exact same sdist/wheel? - open(os.path.join(dist_dir, second_result), 'w').close() + open(os.path.join(dist_dir, second_result), 'w', encoding='utf-8').close() third_result = build_method(dist_dir) assert third_result == second_result assert os.path.getsize(os.path.join(dist_dir, third_result)) > 0 @@ -558,9 +558,9 @@ if not os.path.exists(setup_loc): setup_loc = os.path.abspath("setup.cfg") - with open(setup_loc, 'rt') as file_handler: + with open(setup_loc, 'rt', encoding='utf-8') as file_handler: content = file_handler.read() - with open(setup_loc, 'wt') as file_handler: + with open(setup_loc, 'wt', encoding='utf-8') as file_handler: file_handler.write(content.replace("version='0.0.0'", "version='0.0.1'")) shutil.rmtree(sdist_into_directory)
diff --git a/setuptools/tests/test_build_py.py b/setuptools/tests/test_build_py.py index ca50ce6..20f580c 100644 --- a/setuptools/tests/test_build_py.py +++ b/setuptools/tests/test_build_py.py
@@ -49,7 +49,7 @@ ) ) os.makedirs('path/subpath/subsubpath') - open('path/subpath/subsubpath/data', 'w').close() + open('path/subpath/subsubpath/data', 'w', encoding='utf-8').close() dist.parse_command_line() dist.run_commands() @@ -77,8 +77,8 @@ ) ) os.makedirs('pkg') - open('pkg/__init__.py', 'w').close() - open('pkg/data.dat', 'w').close() + open('pkg/__init__.py', 'w', encoding='utf-8').close() + open('pkg/data.dat', 'w', encoding='utf-8').close() os.chmod('pkg/__init__.py', stat.S_IREAD) os.chmod('pkg/data.dat', stat.S_IREAD) dist.parse_command_line() @@ -108,8 +108,8 @@ ) ) os.makedirs('pkg') - open('pkg/__init__.py', 'w').close() - open('pkg/run-me', 'w').close() + open('pkg/__init__.py', 'w', encoding='utf-8').close() + open('pkg/run-me', 'w', encoding='utf-8').close() os.chmod('pkg/run-me', 0o700) dist.parse_command_line()
diff --git a/setuptools/tests/test_config_discovery.py b/setuptools/tests/test_config_discovery.py index 85cb097..c507f2e 100644 --- a/setuptools/tests/test_config_discovery.py +++ b/setuptools/tests/test_config_discovery.py
@@ -176,11 +176,11 @@ else: # Make sure build works with or without setup.cfg pyproject = self.PURPOSEFULLY_EMPY["template-pyproject.toml"] - (tmp_path / "pyproject.toml").write_text(pyproject) + (tmp_path / "pyproject.toml").write_text(pyproject, encoding='utf-8') template_param = param config = self.PURPOSEFULLY_EMPY[config_file].format(param=template_param) - (tmp_path / config_file).write_text(config) + (tmp_path / config_file).write_text(config, encoding='utf-8') dist = _get_dist(tmp_path, {}) # When either parameter package or py_modules is an empty list, @@ -292,10 +292,13 @@ def test_setupcfg_metadata(self, tmp_path, folder, opts): files = [f"{folder}/pkg/__init__.py", "setup.cfg"] _populate_project_dir(tmp_path, files, opts) - (tmp_path / folder / "pkg/__init__.py").write_text("version = 42") + (tmp_path / folder / "pkg/__init__.py").write_text( + "version = 42", encoding='utf-8' + ) (tmp_path / "setup.cfg").write_text( "[metadata]\nversion = attr: pkg.version\n" - + (tmp_path / "setup.cfg").read_text() + + (tmp_path / "setup.cfg").read_text(encoding='utf-8'), + encoding='utf-8', ) dist = _get_dist(tmp_path, {}) @@ -312,10 +315,11 @@ def test_pyproject_metadata(self, tmp_path): _populate_project_dir(tmp_path, ["src/pkg/__init__.py"], {}) - (tmp_path / "src/pkg/__init__.py").write_text("version = 42") + (tmp_path / "src/pkg/__init__.py").write_text("version = 42", encoding='utf-8') (tmp_path / "pyproject.toml").write_text( "[project]\nname = 'pkg'\ndynamic = ['version']\n" - "[tool.setuptools.dynamic]\nversion = {attr = 'pkg.version'}\n" + "[tool.setuptools.dynamic]\nversion = {attr = 'pkg.version'}\n", + encoding='utf-8', ) dist = _get_dist(tmp_path, {}) assert dist.get_version() == "42" @@ -354,7 +358,7 @@ ] setup(ext_modules=ext_modules) """ - (tmp_path / "setup.py").write_text(DALS(setup_script)) + (tmp_path / "setup.py").write_text(DALS(setup_script), encoding='utf-8') def test_skip_discovery_with_setupcfg_metadata(self, tmp_path): """Ensure that auto-discovery is not triggered when the project is based on @@ -367,14 +371,14 @@ requires = [] build-backend = 'setuptools.build_meta' """ - (tmp_path / "pyproject.toml").write_text(DALS(pyproject)) + (tmp_path / "pyproject.toml").write_text(DALS(pyproject), encoding='utf-8') setupcfg = """ [metadata] name = proj version = 42 """ - (tmp_path / "setup.cfg").write_text(DALS(setupcfg)) + (tmp_path / "setup.cfg").write_text(DALS(setupcfg), encoding='utf-8') dist = _get_dist(tmp_path, {}) assert dist.get_name() == "proj" @@ -399,7 +403,7 @@ name = 'proj' version = '42' """ - (tmp_path / "pyproject.toml").write_text(DALS(pyproject)) + (tmp_path / "pyproject.toml").write_text(DALS(pyproject), encoding='utf-8') with pytest.raises(PackageDiscoveryError, match="multiple (packages|modules)"): _get_dist(tmp_path, {}) @@ -416,7 +420,7 @@ manifest = """ global-include *.py *.txt """ - (tmp_path / "MANIFEST.in").write_text(DALS(manifest)) + (tmp_path / "MANIFEST.in").write_text(DALS(manifest), encoding='utf-8') EXAMPLE_SETUPCFG = """ [metadata] @@ -564,9 +568,11 @@ # NOTE: Currently pypa/build will refuse to build the project if no # `pyproject.toml` or `setup.py` is found. So it is impossible to do # completely "config-less" projects. - (root / "setup.py").write_text("import setuptools\nsetuptools.setup()") - (root / "README.md").write_text("# Example Package") - (root / "LICENSE").write_text("Copyright (c) 2018") + (root / "setup.py").write_text( + "import setuptools\nsetuptools.setup()", encoding='utf-8' + ) + (root / "README.md").write_text("# Example Package", encoding='utf-8') + (root / "LICENSE").write_text("Copyright (c) 2018", encoding='utf-8') _write_setupcfg(root, options) paths = (root / f for f in files) for path in paths: @@ -591,10 +597,10 @@ setupcfg["options"][key] = "\n" + str_value else: setupcfg["options"][key] = str(value) - with open(root / "setup.cfg", "w") as f: + with open(root / "setup.cfg", "w", encoding='utf-8') as f: setupcfg.write(f) print("~~~~~ setup.cfg ~~~~~") - print((root / "setup.cfg").read_text()) + print((root / "setup.cfg").read_text(encoding='utf-8')) def _run_build(path, *flags):
diff --git a/setuptools/tests/test_dep_util.py b/setuptools/tests/test_dep_util.py index e5027c1..73c3bb9 100644 --- a/setuptools/tests/test_dep_util.py +++ b/setuptools/tests/test_dep_util.py
@@ -13,7 +13,7 @@ for i in range(len(creation_order)): creation_order[i] = os.path.join(str(tmpdir), creation_order[i]) - with open(creation_order[i], 'w'): + with open(creation_order[i], 'w', encoding='utf-8'): pass # make sure modification times are sequential
diff --git a/setuptools/tests/test_dist_info.py b/setuptools/tests/test_dist_info.py index a76dbeb..b1fce9c 100644 --- a/setuptools/tests/test_dist_info.py +++ b/setuptools/tests/test_dist_info.py
@@ -202,7 +202,7 @@ **kwargs, } cmd = [sys.executable, "-c", "__import__('setuptools').setup()", *map(str, cmd)] - return subprocess.run(cmd, **opts) + return subprocess.run(cmd, encoding='utf-8', **opts) def run_command(*args, **kwargs):
diff --git a/setuptools/tests/test_distutils_adoption.py b/setuptools/tests/test_distutils_adoption.py index b371a5d..a379f39 100644 --- a/setuptools/tests/test_distutils_adoption.py +++ b/setuptools/tests/test_distutils_adoption.py
@@ -15,9 +15,9 @@ Augment the Popen call with the parameters to ensure unicode text. """ return ( - functools.partial(call, universal_newlines=True) + functools.partial(call, universal_newlines=True, encoding='utf-8') if sys.version_info < (3, 7) - else functools.partial(call, text=True) + else functools.partial(call, text=True, encoding='utf-8') )
diff --git a/setuptools/tests/test_easy_install.py b/setuptools/tests/test_easy_install.py index d71e015..c310e1d 100644 --- a/setuptools/tests/test_easy_install.py +++ b/setuptools/tests/test_easy_install.py
@@ -468,7 +468,7 @@ 'from distutils.core import setup', ) with contexts.tempdir(cd=os.chdir): - with open('setup.py', 'w') as f: + with open('setup.py', 'w', encoding='utf-8') as f: f.write(distutils_setup_py) yield @@ -531,6 +531,7 @@ stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, + encoding='utf-8', ) assert cp.returncode != 0 try: @@ -787,7 +788,7 @@ setup_attrs=dict(dependency_links=[]), ) test_setup_cfg = os.path.join(test_pkg, 'setup.cfg') - with open(test_setup_cfg, 'w') as fp: + with open(test_setup_cfg, 'w', encoding='utf-8') as fp: fp.write( DALS( ''' @@ -921,7 +922,7 @@ test_setup_py = os.path.join(test_pkg, 'setup.py') test_setup_cfg = os.path.join(test_pkg, 'setup.cfg') os.mkdir(test_pkg) - with open(test_setup_py, 'w') as fp: + with open(test_setup_py, 'w', encoding='utf-8') as fp: if with_dependency_links_in_setup_py: dependency_links = [os.path.join(temp_dir, 'links')] else: @@ -935,7 +936,7 @@ ''' ).format(dependency_links=dependency_links) ) - with open(test_setup_cfg, 'w') as fp: + with open(test_setup_cfg, 'w', encoding='utf-8') as fp: fp.write( DALS( ''' @@ -987,7 +988,7 @@ test_pkg = os.path.join(temp_dir, 'test_pkg') test_setup_py = os.path.join(test_pkg, 'setup.py') os.mkdir(test_pkg) - with open(test_setup_py, 'w') as fp: + with open(test_setup_py, 'w', encoding='utf-8') as fp: fp.write( DALS( ''' @@ -1071,7 +1072,7 @@ test_pkg = os.path.join(temp_dir, 'test_pkg') test_setup_py = os.path.join(test_pkg, 'setup.py') os.mkdir(test_pkg) - with open(test_setup_py, 'w') as fp: + with open(test_setup_py, 'w', encoding='utf-8') as fp: fp.write( DALS( ''' @@ -1246,7 +1247,7 @@ ) else: test_setup_cfg_contents = '' - with open(os.path.join(test_pkg, 'setup.cfg'), 'w') as f: + with open(os.path.join(test_pkg, 'setup.cfg'), 'w', encoding='utf-8') as f: f.write(test_setup_cfg_contents) # setup.py @@ -1257,7 +1258,7 @@ setuptools.setup(**%r) """ ) - with open(os.path.join(test_pkg, 'setup.py'), 'w') as f: + with open(os.path.join(test_pkg, 'setup.py'), 'w', encoding='utf-8') as f: f.write(setup_py_template % test_setup_attrs) foobar_path = os.path.join(path, '%s-%s.tar.gz' % (distname, version))
diff --git a/setuptools/tests/test_editable_install.py b/setuptools/tests/test_editable_install.py index ca4fe70..e180c86 100644 --- a/setuptools/tests/test_editable_install.py +++ b/setuptools/tests/test_editable_install.py
@@ -146,8 +146,8 @@ cmd = [venv.exe(), "-m", "mypkg"] assert subprocess.check_output(cmd).strip() == b"3.14159.post0 Hello World" - (project / "src/mypkg/data.txt").write_text("foobar") - (project / "src/mypkg/mod.py").write_text("x = 42") + (project / "src/mypkg/data.txt").write_text("foobar", encoding='utf-8') + (project / "src/mypkg/mod.py").write_text("x = 42", encoding='utf-8') assert subprocess.check_output(cmd).strip() == b"3.14159.post0 foobar 42"
diff --git a/setuptools/tests/test_egg_info.py b/setuptools/tests/test_egg_info.py index 614fca7..67a58f3 100644 --- a/setuptools/tests/test_egg_info.py +++ b/setuptools/tests/test_egg_info.py
@@ -97,7 +97,7 @@ ei.initialize_options() ei.save_version_info(setup_cfg) - with open(setup_cfg, 'r') as f: + with open(setup_cfg, 'r', encoding='utf-8') as f: content = f.read() assert '[egg_info]' in content @@ -144,7 +144,7 @@ ei.initialize_options() ei.save_version_info(setup_cfg) - with open(setup_cfg, 'r') as f: + with open(setup_cfg, 'r', encoding='utf-8') as f: content = f.read() assert '[egg_info]' in content @@ -260,7 +260,7 @@ self._run_egg_info_command(tmpdir_cwd, env) egg_info_dir = os.path.join('.', 'foo.egg-info') sources_txt = os.path.join(egg_info_dir, 'SOURCES.txt') - with open(sources_txt) as f: + with open(sources_txt, encoding='utf-8') as f: assert 'docs/usage.rst' in f.read().split('\n') def _setup_script_with_requires(self, requires, use_setup_cfg=False): @@ -506,7 +506,7 @@ egg_info_dir = os.path.join('.', 'foo.egg-info') requires_txt = os.path.join(egg_info_dir, 'requires.txt') if os.path.exists(requires_txt): - with open(requires_txt) as fp: + with open(requires_txt, encoding='utf-8') as fp: install_requires = fp.read() else: install_requires = '' @@ -552,7 +552,9 @@ env=environ, ) egg_info_dir = os.path.join('.', 'foo.egg-info') - with open(os.path.join(egg_info_dir, 'PKG-INFO')) as pkginfo_file: + with open( + os.path.join(egg_info_dir, 'PKG-INFO'), encoding='utf-8' + ) as pkginfo_file: pkg_info_lines = pkginfo_file.read().split('\n') assert 'Provides-Extra: foobar' in pkg_info_lines assert 'Metadata-Version: 2.1' in pkg_info_lines @@ -571,7 +573,9 @@ env=environ, ) egg_info_dir = os.path.join('.', 'foo.egg-info') - with open(os.path.join(egg_info_dir, 'PKG-INFO')) as pkginfo_file: + with open( + os.path.join(egg_info_dir, 'PKG-INFO'), encoding='utf-8' + ) as pkginfo_file: pkg_info_text = pkginfo_file.read() assert 'Provides-Extra:' not in pkg_info_text @@ -650,7 +654,9 @@ ) egg_info_dir = os.path.join('.', 'foo.egg-info') - with open(os.path.join(egg_info_dir, 'SOURCES.txt')) as sources_file: + with open( + os.path.join(egg_info_dir, 'SOURCES.txt'), encoding='utf-8' + ) as sources_file: sources_text = sources_file.read() if license_in_sources: @@ -863,7 +869,9 @@ ) egg_info_dir = os.path.join('.', 'foo.egg-info') - with open(os.path.join(egg_info_dir, 'SOURCES.txt')) as sources_file: + with open( + os.path.join(egg_info_dir, 'SOURCES.txt'), encoding='utf-8' + ) as sources_file: sources_lines = list(line.strip() for line in sources_file) for lf in incl_licenses: @@ -1047,7 +1055,9 @@ ) egg_info_dir = os.path.join('.', 'foo.egg-info') - with open(os.path.join(egg_info_dir, 'SOURCES.txt')) as sources_file: + with open( + os.path.join(egg_info_dir, 'SOURCES.txt'), encoding='utf-8' + ) as sources_file: sources_lines = list(line.strip() for line in sources_file) for lf in incl_licenses: @@ -1081,7 +1091,9 @@ pypath=os.pathsep.join([env.paths['lib'], str(tmpdir_cwd)]), ) egg_info_dir = os.path.join('.', 'foo.egg-info') - with open(os.path.join(egg_info_dir, 'PKG-INFO')) as pkginfo_file: + with open( + os.path.join(egg_info_dir, 'PKG-INFO'), encoding='utf-8' + ) as pkginfo_file: pkg_info_lines = pkginfo_file.read().split('\n') license_file_lines = [ line for line in pkg_info_lines if line.startswith('License-File:') @@ -1102,7 +1114,9 @@ data_stream=1, ) egg_info_dir = os.path.join('.', 'foo.egg-info') - with open(os.path.join(egg_info_dir, 'PKG-INFO')) as pkginfo_file: + with open( + os.path.join(egg_info_dir, 'PKG-INFO'), encoding='utf-8' + ) as pkginfo_file: pkg_info_lines = pkginfo_file.read().split('\n') # Update metadata version if changed assert self._extract_mv_version(pkg_info_lines) == (2, 1) @@ -1128,7 +1142,9 @@ env=environ, ) egg_info_dir = os.path.join('.', 'foo.egg-info') - with open(os.path.join(egg_info_dir, 'PKG-INFO')) as pkginfo_file: + with open( + os.path.join(egg_info_dir, 'PKG-INFO'), encoding='utf-8' + ) as pkginfo_file: pkg_info_lines = pkginfo_file.read().split('\n') expected_line = 'Description-Content-Type: text/markdown' assert expected_line in pkg_info_lines @@ -1149,7 +1165,9 @@ data_stream=1, ) egg_info_dir = os.path.join('.', 'foo.egg-info') - with open(os.path.join(egg_info_dir, 'PKG-INFO')) as pkginfo_file: + with open( + os.path.join(egg_info_dir, 'PKG-INFO'), encoding='utf-8' + ) as pkginfo_file: pkg_info_lines = pkginfo_file.read().split('\n') assert 'Metadata-Version: 2.1' in pkg_info_lines assert '' == pkg_info_lines[-1] # last line should be empty @@ -1181,7 +1199,9 @@ env=environ, ) egg_info_dir = os.path.join('.', 'foo.egg-info') - with open(os.path.join(egg_info_dir, 'PKG-INFO')) as pkginfo_file: + with open( + os.path.join(egg_info_dir, 'PKG-INFO'), encoding='utf-8' + ) as pkginfo_file: pkg_info_lines = pkginfo_file.read().split('\n') expected_line = 'Project-URL: Link One, https://example.com/one/' assert expected_line in pkg_info_lines @@ -1198,7 +1218,9 @@ data_stream=1, ) egg_info_dir = os.path.join('.', 'foo.egg-info') - with open(os.path.join(egg_info_dir, 'PKG-INFO')) as pkginfo_file: + with open( + os.path.join(egg_info_dir, 'PKG-INFO'), encoding='utf-8' + ) as pkginfo_file: pkg_info_lines = pkginfo_file.read().split('\n') assert 'License: MIT' in pkg_info_lines @@ -1213,7 +1235,9 @@ data_stream=1, ) egg_info_dir = os.path.join('.', 'foo.egg-info') - with open(os.path.join(egg_info_dir, 'PKG-INFO')) as pkginfo_file: + with open( + os.path.join(egg_info_dir, 'PKG-INFO'), encoding='utf-8' + ) as pkginfo_file: pkg_info_lines = pkginfo_file.read().split('\n') assert 'License: This is a long license text ' in pkg_info_lines @@ -1232,7 +1256,9 @@ env=environ, ) egg_info_dir = os.path.join('.', 'foo.egg-info') - with open(os.path.join(egg_info_dir, 'PKG-INFO')) as pkginfo_file: + with open( + os.path.join(egg_info_dir, 'PKG-INFO'), encoding='utf-8' + ) as pkginfo_file: pkg_info_lines = pkginfo_file.read().split('\n') assert 'Requires-Python: >=2.7.12' in pkg_info_lines assert self._extract_mv_version(pkg_info_lines) >= (1, 2) @@ -1256,7 +1282,7 @@ assert 'setup.py' in egg_info_instance.filelist.files - with open(egg_info_instance.egg_info + "/SOURCES.txt") as f: + with open(egg_info_instance.egg_info + "/SOURCES.txt", encoding='utf-8') as f: sources = f.read().split('\n') assert 'setup.py' in sources @@ -1295,7 +1321,9 @@ ) self._run_egg_info_command(tmpdir_cwd, env) egg_info_dir = os.path.join('.', 'foo.egg-info') - with open(os.path.join(egg_info_dir, 'PKG-INFO')) as pkginfo_file: + with open( + os.path.join(egg_info_dir, 'PKG-INFO'), encoding='utf-8' + ) as pkginfo_file: pkg_info_lines = pkginfo_file.read().split('\n') assert 'Version: 0.0.0.dev0' in pkg_info_lines
diff --git a/setuptools/tests/test_find_packages.py b/setuptools/tests/test_find_packages.py index 92da882..1150f2b 100644 --- a/setuptools/tests/test_find_packages.py +++ b/setuptools/tests/test_find_packages.py
@@ -82,8 +82,7 @@ def _touch(self, path, dir_=None): if dir_: path = os.path.join(dir_, path) - fp = open(path, 'w') - fp.close() + open(path, 'w', encoding='utf-8').close() return path def test_regular_package(self):
diff --git a/setuptools/tests/test_install_scripts.py b/setuptools/tests/test_install_scripts.py index 4338c79..fdab92b 100644 --- a/setuptools/tests/test_install_scripts.py +++ b/setuptools/tests/test_install_scripts.py
@@ -43,7 +43,7 @@ monkeypatch.setattr('sys.executable', self.unix_exe) with tmpdir.as_cwd(): self._run_install_scripts(str(tmpdir)) - with io.open(str(tmpdir.join('foo')), 'r') as f: + with io.open(str(tmpdir.join('foo')), 'r', encoding='utf-8') as f: actual = f.readline() assert actual == expected @@ -57,7 +57,7 @@ monkeypatch.setattr('sys.executable', self.win32_exe) with tmpdir.as_cwd(): self._run_install_scripts(str(tmpdir)) - with io.open(str(tmpdir.join('foo-script.py')), 'r') as f: + with io.open(str(tmpdir.join('foo-script.py')), 'r', encoding='utf-8') as f: actual = f.readline() assert actual == expected @@ -71,7 +71,7 @@ expected = '#!%s\n' % self.unix_spaces_exe with tmpdir.as_cwd(): self._run_install_scripts(str(tmpdir), self.unix_spaces_exe) - with io.open(str(tmpdir.join('foo')), 'r') as f: + with io.open(str(tmpdir.join('foo')), 'r', encoding='utf-8') as f: actual = f.readline() assert actual == expected
diff --git a/setuptools/tests/test_logging.py b/setuptools/tests/test_logging.py index aa2b502..6dbd2ee 100644 --- a/setuptools/tests/test_logging.py +++ b/setuptools/tests/test_logging.py
@@ -29,7 +29,7 @@ assert logging.getLevelName(unset_log_level) == "NOTSET" setup_script = tmp_path / "setup.py" - setup_script.write_text(setup_py) + setup_script.write_text(setup_py, encoding='utf-8') dist = distutils.core.run_setup(setup_script, stop_after="init") dist.script_args = [flag, "sdist"] dist.parse_command_line() # <- where the log level is set
diff --git a/setuptools/tests/test_manifest.py b/setuptools/tests/test_manifest.py index 33b85d0..7f625ac 100644 --- a/setuptools/tests/test_manifest.py +++ b/setuptools/tests/test_manifest.py
@@ -53,7 +53,7 @@ def touch(filename): - open(filename, 'w').close() + open(filename, 'w', encoding='utf-8').close() # The set of files always in the manifest, including all files in the @@ -173,9 +173,8 @@ def setup_method(self, method): super(TestManifestTest, self).setup_method(method) - f = open(os.path.join(self.temp_dir, 'setup.py'), 'w') - f.write(SETUP_PY) - f.close() + with open(os.path.join(self.temp_dir, 'setup.py'), 'w', encoding='utf-8') as f: + f.write(SETUP_PY) """ Create a file tree like: - LICENSE @@ -211,7 +210,9 @@ def make_manifest(self, contents): """Write a MANIFEST.in.""" - with open(os.path.join(self.temp_dir, 'MANIFEST.in'), 'w') as f: + with open( + os.path.join(self.temp_dir, 'MANIFEST.in'), 'w', encoding='utf-8' + ) as f: f.write(DALS(contents)) def get_files(self): @@ -376,7 +377,7 @@ file = os.path.join(self.temp_dir, file) dirname, basename = os.path.split(file) os.makedirs(dirname, exist_ok=True) - open(file, 'w').close() + open(file, 'w', encoding='utf-8').close() def test_process_template_line(self): # testing all MANIFEST.in template patterns
diff --git a/setuptools/tests/test_sdist.py b/setuptools/tests/test_sdist.py index c297cea..05c48c7 100644 --- a/setuptools/tests/test_sdist.py +++ b/setuptools/tests/test_sdist.py
@@ -224,7 +224,7 @@ assert 'setup.py' not in manifest def test_setup_py_excluded(self): - with open("MANIFEST.in", "w") as manifest_file: + with open("MANIFEST.in", "w", encoding='utf-8') as manifest_file: manifest_file.write("exclude setup.py") dist = Distribution(SETUP_ATTRS) @@ -279,7 +279,7 @@ filename = os.path.join('sdist_test', 'smörbröd.py') # Must create the file or it will get stripped. - open(filename, 'w').close() + open(filename, 'w', encoding='utf-8').close() # Add UTF-8 filename and write manifest with quiet(): @@ -307,7 +307,7 @@ filename = os.path.join(b'sdist_test', Filenames.utf_8) # Must touch the file or risk removal - open(filename, "w").close() + open(filename, "w", encoding='utf-8').close() # Add filename and write manifest with quiet(): @@ -384,7 +384,7 @@ manifest.close() # The file must exist to be included in the filelist - open(filename, 'w').close() + open(filename, 'w', encoding='utf-8').close() # Re-read manifest cmd.filelist.files = [] @@ -436,7 +436,7 @@ cmd.ensure_finalized() filename = os.path.join(b'sdist_test', Filenames.utf_8) - open(filename, 'w').close() + open(filename, 'w', encoding='utf-8').close() with quiet(): cmd.run() @@ -574,7 +574,7 @@ Check that pyproject.toml can excluded even if present """ touch(tmpdir / 'pyproject.toml') - with open('MANIFEST.in', 'w') as mts: + with open('MANIFEST.in', 'w', encoding='utf-8') as mts: print('exclude pyproject.toml', file=mts) dist = Distribution(SETUP_ATTRS) dist.script_name = 'setup.py'
diff --git a/setuptools/wheel.py b/setuptools/wheel.py index c6eabdd..ba0278a 100644 --- a/setuptools/wheel.py +++ b/setuptools/wheel.py
@@ -222,7 +222,7 @@ def _fix_namespace_packages(egg_info, destination_eggdir): namespace_packages = os.path.join(egg_info, 'namespace_packages.txt') if os.path.exists(namespace_packages): - with open(namespace_packages) as fp: + with open(namespace_packages, encoding='utf-8') as fp: namespace_packages = fp.read().split() for mod in namespace_packages: mod_dir = os.path.join(destination_eggdir, *mod.split('.')) @@ -230,5 +230,5 @@ if not os.path.exists(mod_dir): os.mkdir(mod_dir) if not os.path.exists(mod_init): - with open(mod_init, 'w') as fp: + with open(mod_init, 'w', encoding='utf-8') as fp: fp.write(NAMESPACE_PACKAGE_INIT)