Skip to content

Commit e9a13f8

Browse files
vlcinskysdispater
authored andcommitted
Review of (explicit) encoding for files being created in text mode (#1088)
* Fix encoding for files created for building package (issue #1027) This is minimal set of modifications, which fixed the problem. There are still more places, where files are open in text mode without explicit encoding, thus possibly failing on systems not using UTF-8 console encoding. * test_api.py use explicit encoding for files. * get-poetry.py: fix explicit (utf-8) encoding for files open in text mode * tests: fix explicit (utf-8) encoding for files open in text mode * poetry init: use explicit utf-8 for created pyproject.toml * installed: use explicit utf-8 for text files * layouts: use explicit utf-8 for open text files * spdx: explicit utf-8 for open text files * pypi_repository: explicit utf-8 for open text files * explicit utf-8 for json * explicit utf-8 for metadata * explicit utf-8 for requires.txt * open files with explicit encoding in py27 * fix creation of poetry.bat on Windows * Make get-poetry.py compatible to py2/3 * Blacked two files to pass linter test * remove extranous encoding related comments * rewert broken move of import related to ansible * removed extraneous comments about opening files in UTF-8 * rewert ansible.release import (keep order of vars unchanged) * Revert to original content (as it comes from external project) * all vendored setup.py files in fixtures reverted back to org
1 parent bd0e327 commit e9a13f8

File tree

17 files changed

+47
-33
lines changed

17 files changed

+47
-33
lines changed

get-poetry.py

+19-10
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
from contextlib import contextmanager
3434
from functools import cmp_to_key
3535
from gzip import GzipFile
36-
from io import UnsupportedOperation
36+
from io import UnsupportedOperation, open
3737

3838
try:
3939
from urllib.error import HTTPError
@@ -58,6 +58,10 @@
5858
except ImportError:
5959
winreg = None
6060

61+
try:
62+
u = unicode
63+
except NameError:
64+
u = str
6165

6266
WINDOWS = sys.platform.startswith("win") or (sys.platform == "cli" and os.name == "nt")
6367

@@ -191,6 +195,7 @@ def expanduser(path):
191195

192196

193197
BIN = """#!/usr/bin/env python
198+
# -*- coding: utf-8 -*-
194199
import glob
195200
import sys
196201
import os
@@ -205,7 +210,7 @@ def expanduser(path):
205210
main()
206211
"""
207212

208-
BAT = '@echo off\r\npython "{poetry_bin}" %*\r\n'
213+
BAT = u('@echo off\r\npython "{poetry_bin}" %*\r\n')
209214

210215

211216
PRE_MESSAGE = """# Welcome to {poetry}!
@@ -387,7 +392,9 @@ def _compare_versions(x, y):
387392

388393
current_version = None
389394
if os.path.exists(POETRY_LIB):
390-
with open(os.path.join(POETRY_LIB, "poetry", "__version__.py")) as f:
395+
with open(
396+
os.path.join(POETRY_LIB, "poetry", "__version__.py"), encoding="utf-8"
397+
) as f:
391398
version_content = f.read()
392399

393400
current_version_re = re.match(
@@ -563,15 +570,17 @@ def make_bin(self):
563570
if WINDOWS:
564571
with open(os.path.join(POETRY_BIN, "poetry.bat"), "w") as f:
565572
f.write(
566-
BAT.format(
567-
poetry_bin=os.path.join(POETRY_BIN, "poetry").replace(
568-
os.environ["USERPROFILE"], "%USERPROFILE%"
573+
u(
574+
BAT.format(
575+
poetry_bin=os.path.join(POETRY_BIN, "poetry").replace(
576+
os.environ["USERPROFILE"], "%USERPROFILE%"
577+
)
569578
)
570579
)
571580
)
572581

573-
with open(os.path.join(POETRY_BIN, "poetry"), "w") as f:
574-
f.write(BIN)
582+
with open(os.path.join(POETRY_BIN, "poetry"), "w", encoding="utf-8") as f:
583+
f.write(u(BIN))
575584

576585
if not WINDOWS:
577586
# Making the file executable
@@ -583,7 +592,7 @@ def make_env(self):
583592
return
584593

585594
with open(os.path.join(POETRY_HOME, "env"), "w") as f:
586-
f.write(self.get_export_string())
595+
f.write(u(self.get_export_string()))
587596

588597
def update_path(self):
589598
"""
@@ -608,7 +617,7 @@ def update_path(self):
608617

609618
if addition not in content:
610619
with open(profile, "a") as f:
611-
f.write(addition)
620+
f.write(u(addition))
612621

613622
updated.append(os.path.relpath(profile, HOME))
614623

poetry/console/commands/init.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def handle(self):
158158

159159
return 1
160160

161-
with (Path.cwd() / "pyproject.toml").open("w") as f:
161+
with (Path.cwd() / "pyproject.toml").open("w", encoding="utf-8") as f:
162162
f.write(content)
163163

164164
def _determine_requirements(

poetry/installation/pip_installer.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ def install_directory(self, package):
193193
# We also need it for non-PEP-517 packages
194194
builder = SdistBuilder(Poetry.create(pyproject.parent), NullEnv(), NullIO())
195195

196-
with open(setup, "w") as f:
196+
with open(setup, "w", encoding="utf-8") as f:
197197
f.write(decode(builder.build_setup()))
198198

199199
if package.develop:

poetry/json/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import jsonschema
55

6+
from io import open
67
from typing import List
78

89
SCHEMA_DIR = os.path.join(os.path.dirname(__file__), "schemas")
@@ -19,7 +20,7 @@ def validate_object(obj, schema_name): # type: (dict, str) -> List[str]
1920
if not os.path.exists(schema):
2021
raise ValueError("Schema {} does not exist.".format(schema_name))
2122

22-
with open(schema) as f:
23+
with open(schema, encoding="utf-8") as f:
2324
schema = json.loads(f.read())
2425

2526
validator = jsonschema.Draft7Validator(schema)

poetry/layouts/layout.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def _create_tests(self, path):
140140
tests.mkdir()
141141
tests_init.touch(exist_ok=False)
142142

143-
with tests_default.open("w") as f:
143+
with tests_default.open("w", encoding="utf-8") as f:
144144
f.write(
145145
TESTS_DEFAULT.format(
146146
package_name=self._package_name, version=self._version
@@ -152,5 +152,5 @@ def _write_poetry(self, path):
152152

153153
poetry = path / "pyproject.toml"
154154

155-
with poetry.open("w") as f:
155+
with poetry.open("w", encoding="utf-8") as f:
156156
f.write(content)

poetry/layouts/src.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ def _create_default(self, path):
1414

1515
package_path.mkdir(parents=True)
1616

17-
with package_init.open("w") as f:
17+
with package_init.open("w", encoding="utf-8") as f:
1818
f.write(DEFAULT.format(version=self._version))

poetry/layouts/standard.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ def _create_default(self, path):
1414

1515
package_path.mkdir()
1616

17-
with package_init.open("w") as f:
17+
with package_init.open("w", encoding="utf-8") as f:
1818
f.write(DEFAULT.format(version=self._version))

poetry/masonry/api.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ def prepare_metadata_for_build_wheel(metadata_directory, config_settings=None):
3939
dist_info.mkdir()
4040

4141
if "scripts" in poetry.local_config or "plugins" in poetry.local_config:
42-
with (dist_info / "entry_points.txt").open("w") as f:
42+
with (dist_info / "entry_points.txt").open("w", encoding="utf-8") as f:
4343
builder._write_entry_points(f)
4444

45-
with (dist_info / "WHEEL").open("w") as f:
45+
with (dist_info / "WHEEL").open("w", encoding="utf-8") as f:
4646
builder._write_wheel_file(f)
4747

48-
with (dist_info / "METADATA").open("w") as f:
48+
with (dist_info / "METADATA").open("w", encoding="utf-8") as f:
4949
builder._write_metadata_file(f)
5050

5151
return dist_info.name

poetry/masonry/metadata.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def from_package(cls, package): # type: (...) -> Metadata
4646
meta.version = normalize_version(package.version.text)
4747
meta.summary = package.description
4848
if package.readme:
49-
with package.readme.open() as f:
49+
with package.readme.open(encoding="utf-8") as f:
5050
meta.description = f.read()
5151

5252
meta.keywords = ",".join(package.keywords)

poetry/puzzle/provider.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ def search_for_directory(
346346
reqs = []
347347
requires = egg_info / "requires.txt"
348348
if requires.exists():
349-
with requires.open() as f:
349+
with requires.open(encoding="utf-8") as f:
350350
reqs = parse_requires(f.read())
351351
finally:
352352
os.chdir(current_dir)

poetry/repositories/pypi_repository.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ def _get_info_from_sdist(
533533

534534
requires = egg_info / "requires.txt"
535535
if requires.exists():
536-
with requires.open() as f:
536+
with requires.open(encoding="utf-8") as f:
537537
info["requires_dist"] = parse_requires(f.read())
538538

539539
return info
@@ -545,7 +545,7 @@ def _get_info_from_sdist(
545545

546546
requires = egg_info / "requires.txt"
547547
if requires.exists():
548-
with requires.open() as f:
548+
with requires.open(encoding="utf-8") as f:
549549
info["requires_dist"] = parse_requires(f.read())
550550

551551
return info

poetry/spdx/__init__.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import json
22
import os
33

4+
from io import open
5+
46
from .license import License
57
from .updater import Updater
68

@@ -26,7 +28,7 @@ def load_licenses():
2628

2729
licenses_file = os.path.join(os.path.dirname(__file__), "data", "licenses.json")
2830

29-
with open(licenses_file) as f:
31+
with open(licenses_file, encoding="utf-8") as f:
3032
data = json.loads(f.read())
3133

3234
for name, license in data.items():

poetry/spdx/updater.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import json
22
import os
33

4+
from io import open
5+
46
try:
57
from urllib.request import urlopen
68
except ImportError:
@@ -20,7 +22,7 @@ def dump(self, file=None):
2022

2123
licenses_url = self._base_url + "licenses.json"
2224

23-
with open(file, "w") as f:
25+
with open(file, "w", encoding="utf-8") as f:
2426
f.write(
2527
json.dumps(self.get_licenses(licenses_url), indent=2, sort_keys=True)
2628
)

tests/console/conftest.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -176,15 +176,15 @@ def repo():
176176
def poetry(repo):
177177
p = Poetry.create(Path(__file__).parent.parent / "fixtures" / "simple_project")
178178

179-
with p.file.path.open() as f:
179+
with p.file.path.open(encoding="utf-8") as f:
180180
content = f.read()
181181

182182
p.pool.remove_repository("pypi")
183183
p.pool.add_repository(repo)
184184

185185
yield p
186186

187-
with p.file.path.open("w") as f:
187+
with p.file.path.open("w", encoding="utf-8") as f:
188188
f.write(content)
189189

190190

tests/masonry/test_api.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,11 @@ def test_prepare_metadata_for_build_wheel():
117117
assert (dist_info / "WHEEL").exists()
118118
assert (dist_info / "METADATA").exists()
119119

120-
with (dist_info / "entry_points.txt").open() as f:
120+
with (dist_info / "entry_points.txt").open(encoding="utf-8") as f:
121121
assert entry_points == decode(f.read())
122122

123-
with (dist_info / "WHEEL").open() as f:
123+
with (dist_info / "WHEEL").open(encoding="utf-8") as f:
124124
assert wheel_data == decode(f.read())
125125

126-
with (dist_info / "METADATA").open() as f:
126+
with (dist_info / "METADATA").open(encoding="utf-8") as f:
127127
assert metadata == decode(f.read())

tests/repositories/test_legacy_repository.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def _get(self, endpoint):
2929

3030
fixture = self.FIXTURES / (name + ".html")
3131

32-
with fixture.open() as f:
32+
with fixture.open(encoding="utf-8") as f:
3333
return Page(self._url + endpoint, f.read(), {})
3434

3535
def _download(self, url, dest):

tests/repositories/test_pypi_repository.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def _get(self, url):
3333
if not fixture.exists():
3434
fixture = self.JSON_FIXTURES / (name + ".json")
3535

36-
with fixture.open() as f:
36+
with fixture.open(encoding="utf-8") as f:
3737
return json.loads(f.read())
3838

3939
def _download(self, url, dest):

0 commit comments

Comments
 (0)