Skip to content

Commit e8013c6

Browse files
committed
Drop portage._unicode_encode/_encodings usage
Python 3's open() accepts str paths directly and always uses UTF-8 as the filesystem and content encoding on modern systems. The _unicode_encode(path, encoding=_encodings["fs"]) pattern was Python 2 compatibility code that converted str paths to bytes before passing them to open(); it is unnecessary in Python 3. Replace open(_unicode_encode(path, encoding=_encodings["fs"]), encoding=_encodings["content"]) with open(path, encoding="utf-8") throughout, and drop the portage imports. Signed-off-by: Matt Turner <mattst88@gentoo.org> Part-of: #62
1 parent 89f068e commit e8013c6

12 files changed

Lines changed: 36 additions & 61 deletions

File tree

pym/gentoolkit/eclean/exclude.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import os
88
import re
99
import portage
10-
from portage import _encodings, _unicode_encode
1110

1211
# Misc. shortcuts to some portage stuff:
1312
listdir = portage.listdir
@@ -78,8 +77,8 @@ def parseExcludeFile(filepath, output):
7877
output("Parsing Exclude file: " + filepath)
7978
try:
8079
file_ = open(
81-
_unicode_encode(filepath, encoding=_encodings["fs"]),
82-
encoding=_encodings["content"],
80+
filepath,
81+
encoding="utf-8",
8382
)
8483
except OSError:
8584
raise ParseExcludeFileException("Could not open exclusion file: " + filepath)

pym/gentoolkit/enalyze/rebuild.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727

2828
import portage
29-
from portage import _encodings, _unicode_encode
3029

3130

3231
def cpv_all_diff_use(
@@ -391,9 +390,9 @@ def save_file(self, filepath, data):
391390
if not self.options["quiet"]:
392391
print(" - Saving file: %s" % filepath)
393392
with open(
394-
_unicode_encode(filepath, encoding=_encodings["fs"]),
393+
filepath,
395394
mode="w",
396-
encoding=_encodings["content"],
395+
encoding="utf-8",
397396
) as output:
398397
output.write("\n".join(data))
399398
output.write("\n")

pym/gentoolkit/equery/uses.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
from glob import glob
1919

2020
from portage import settings
21-
from portage import _encodings, _unicode_encode
2221

2322
import gentoolkit.pprinter as pp
2423
from gentoolkit import errors
@@ -151,8 +150,8 @@ def get_global_useflags():
151150
try:
152151
path = os.path.join(settings["PORTDIR"], "profiles", "use.desc")
153152
with open(
154-
_unicode_encode(path, encoding=_encodings["fs"]),
155-
encoding=_encodings["content"],
153+
path,
154+
encoding="utf-8",
156155
) as open_file:
157156
for line in open_file:
158157
if line.startswith("#"):
@@ -171,8 +170,8 @@ def get_global_useflags():
171170
for path in glob(os.path.join(settings["PORTDIR"], "profiles", "desc", "*.desc")):
172171
try:
173172
with open(
174-
_unicode_encode(path, encoding=_encodings["fs"]),
175-
encoding=_encodings["content"],
173+
path,
174+
encoding="utf-8",
176175
) as open_file:
177176
for line in open_file:
178177
if line.startswith("#"):

pym/gentoolkit/equery/which.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
from gentoolkit.equery import format_options, mod_usage
2323
from gentoolkit.query import Query
2424

25-
from portage import _encodings, _unicode_encode
26-
2725
# =======
2826
# Globals
2927
# =======
@@ -62,8 +60,8 @@ def print_help(with_description=True):
6260
def print_ebuild(ebuild_path):
6361
"""Output the ebuild to std_out"""
6462
with open(
65-
_unicode_encode(ebuild_path, encoding=_encodings["fs"]),
66-
encoding=_encodings["content"],
63+
ebuild_path,
64+
encoding="utf-8",
6765
) as f:
6866
lines = f.readlines()
6967
print("\n\n")

pym/gentoolkit/helpers.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
from itertools import chain
2929

3030
import portage
31-
from portage import _encodings, _unicode_encode
3231

3332
from gentoolkit import pprinter as pp
3433
from gentoolkit import errors
@@ -300,7 +299,7 @@ def get_bintree_cpvs(predicate=None):
300299
def print_file(path):
301300
"""Display the contents of a file."""
302301

303-
with open(_unicode_encode(path, encoding=_encodings["fs"]), mode="rb") as open_file:
302+
with open(path, mode="rb") as open_file:
304303
lines = open_file.read()
305304
pp.uprint(lines.strip())
306305

pym/gentoolkit/package.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151

5252
import portage
5353
from portage.util import LazyItemsDict
54-
from portage import _encodings, _unicode_encode
5554

5655
import gentoolkit.pprinter as pp
5756
from gentoolkit import errors
@@ -397,7 +396,7 @@ def size(self):
397396
size = n_files = n_uncounted = 0
398397
for path in self.parsed_contents(prefix_root=True):
399398
try:
400-
st = os.lstat(_unicode_encode(path, encoding=_encodings["fs"]))
399+
st = os.lstat(path)
401400
except OSError:
402401
continue
403402

pym/gentoolkit/profile.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
import portage
1414
import sys
1515

16-
from portage import _encodings, _unicode_encode
17-
1816

1917
def warning(msg):
2018
"""Write |msg| as a warning to stderr"""
@@ -47,8 +45,8 @@ def load_profile_data(portdir=None, repo=""):
4745
try:
4846
arch_list = os.path.join(portdir, "profiles", "arch.list")
4947
with open(
50-
_unicode_encode(arch_list, encoding=_encodings["fs"]),
51-
encoding=_encodings["content"],
48+
arch_list,
49+
encoding="utf-8",
5250
) as f:
5351
for line in f:
5452
line = line.split("#", 1)[0].strip()
@@ -66,8 +64,8 @@ def load_profile_data(portdir=None, repo=""):
6664
}
6765
profiles_list = os.path.join(portdir, "profiles", "profiles.desc")
6866
with open(
69-
_unicode_encode(profiles_list, encoding=_encodings["fs"]),
70-
encoding=_encodings["content"],
67+
profiles_list,
68+
encoding="utf-8",
7169
) as f:
7270
for line in f:
7371
line = line.split("#", 1)[0].split()
@@ -91,8 +89,8 @@ def load_profile_data(portdir=None, repo=""):
9189
try:
9290
arches_list = os.path.join(portdir, "profiles", "arches.desc")
9391
with open(
94-
_unicode_encode(arches_list, encoding=_encodings["fs"]),
95-
encoding=_encodings["content"],
92+
arches_list,
93+
encoding="utf-8",
9694
) as f:
9795
for line in f:
9896
line = line.split("#", 1)[0].split()

pym/gentoolkit/revdep_rebuild/analyse.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import re
77
import time
88

9-
from portage import _encodings, _unicode_encode
109
from portage.output import bold, blue, yellow, green
1110

1211
from .stuff import scan
@@ -91,8 +90,8 @@ def extract_dependencies_from_la(la, libraries, to_check, logger):
9190
continue
9291

9392
for line in open(
94-
_unicode_encode(_file, encoding=_encodings["fs"]),
95-
encoding=_encodings["content"],
93+
_file,
94+
encoding="utf-8",
9695
).readlines():
9796
line = line.strip()
9897
if line.startswith("dependency_libs="):

pym/gentoolkit/revdep_rebuild/cache.py

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from portage import os
66
import time
77

8-
from portage import _encodings, _unicode_encode
98
from portage.output import red
109
from .settings import DEFAULTS
1110

@@ -28,10 +27,8 @@ def read_cache(temp_path=DEFAULTS["DEFAULT_TMP_DIR"]):
2827
try:
2928
for key, val in ret.items():
3029
_file = open(
31-
_unicode_encode(
32-
os.path.join(temp_path, key), encoding=_encodings["fs"]
33-
),
34-
encoding=_encodings["content"],
30+
os.path.join(temp_path, key),
31+
encoding="utf-8",
3532
)
3633
for line in _file.readlines():
3734
val.add(line.strip())
@@ -60,22 +57,18 @@ def save_cache(logger, to_save={}, temp_path=DEFAULTS["DEFAULT_TMP_DIR"]):
6057

6158
try:
6259
_file = open(
63-
_unicode_encode(
64-
os.path.join(temp_path, "timestamp"), encoding=_encodings["fs"]
65-
),
60+
os.path.join(temp_path, "timestamp"),
6661
mode="w",
67-
encoding=_encodings["content"],
62+
encoding="utf-8",
6863
)
6964
_file.write(str(int(time.time())))
7065
_file.close()
7166

7267
for key, val in to_save.items():
7368
_file = open(
74-
_unicode_encode(
75-
os.path.join(temp_path, key), encoding=_encodings["fs"]
76-
),
69+
os.path.join(temp_path, key),
7770
mode="w",
78-
encoding=_encodings["content"],
71+
encoding="utf-8",
7972
)
8073
for line in val:
8174
_file.write(line + "\n")
@@ -105,8 +98,8 @@ def check_temp_files(
10598

10699
try:
107100
_file = open(
108-
_unicode_encode(timestamp_path, encoding=_encodings["fs"]),
109-
encoding=_encodings["content"],
101+
timestamp_path,
102+
encoding="utf-8",
110103
)
111104
timestamp = int(_file.readline())
112105
_file.close()

pym/gentoolkit/revdep_rebuild/collect.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import stat
99

1010
import portage
11-
from portage import _encodings, _unicode_encode
1211
from portage.output import blue, yellow
1312
from .settings import parse_revdep_config
1413

@@ -27,8 +26,8 @@ def parse_conf(conf_file, visited=None, logger=None):
2726
for conf in conf_file:
2827
try:
2928
with open(
30-
_unicode_encode(conf, encoding=_encodings["fs"]),
31-
encoding=_encodings["content"],
29+
conf,
30+
encoding="utf-8",
3231
) as _file:
3332
for line in _file.readlines():
3433
line = line.strip()
@@ -75,11 +74,8 @@ def prepare_search_dirs(logger, settings):
7574

7675
# try:
7776
with open(
78-
_unicode_encode(
79-
os.path.join(portage.root, settings["DEFAULT_ENV_FILE"]),
80-
encoding=_encodings["fs"],
81-
),
82-
encoding=_encodings["content"],
77+
os.path.join(portage.root, settings["DEFAULT_ENV_FILE"]),
78+
encoding="utf-8",
8379
) as _file:
8480
for line in _file.readlines():
8581
line = line.strip()

0 commit comments

Comments
 (0)