Skip to content
Permalink
Browse files

scripts: dts: Consistently open text files with utf-8.

Zephyr codebase standardizes in UTF-8 as file encoding. To
accommodate this, we explicitly pass encoding="utf-8" to Python's
open() function, to be independent of any locale setting of a
particular system (e.g., CI/build systems oftentimes have "C",
i.e. ASCII-only, locale). In a few places, we lacked this parameter,
so add it consistently.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
  • Loading branch information...
pfalcon authored and galak committed Aug 5, 2019
1 parent 39ada71 commit 04da7eaf07415837c1ba6cfb010d6b1ef7f24eda
Showing with 10 additions and 9 deletions.
  1. +2 −2 scripts/dts/devicetree.py
  2. +7 −6 scripts/dts/dtlib.py
  3. +1 −1 scripts/dts/edtlib.py
@@ -208,7 +208,7 @@ def parse_file(fd, ignore_dts_version=False):

if line.startswith('/include/ '):
_, filename = line.split()
with open(filename.strip()[1:-1], "r") as new_fd:
with open(filename.strip()[1:-1], encoding="utf-8") as new_fd:
nodes.update(parse_file(new_fd, True))
elif line == '/dts-v1/;':
has_v1_tag = True
@@ -300,7 +300,7 @@ def main(args):
else:
formatter = lambda nodes: pprint.pprint(nodes, indent=2)

with open(args[1], "r") as fd:
with open(args[1], encoding="utf-8") as fd:
formatter(parse_file(fd))

return 0
@@ -886,7 +886,7 @@ def _enter_file(self, filename):
except UnicodeDecodeError:
self._parse_error("filename is not valid UTF-8")

with self._open(filename, "r") as f:
with self._open(filename, encoding="utf-8") as f:
try:
self._file_contents = f.read()
except OSError as e:
@@ -1156,9 +1156,10 @@ def sub(match):

return _unescape_re.sub(sub, b)

def _open(self, filename, mode):
# Opens 'filename' in mode 'mode', returning the 'file' object.
# Searches the directory of the current file and the include path.
def _open(self, filename, mode="r", **kwargs):
# Wrapper around standard Python open(), accepting the same params.
# But searches for a 'filename' file in the directory of the current
# file and the include path.

# The C tools support specifying stdin with '-' too
if filename == "-":
@@ -1167,15 +1168,15 @@ def _open(self, filename, mode):
# Try the directory of the current file first
dirname = os.path.dirname(self.filename)
try:
return open(os.path.join(dirname, filename), mode)
return open(os.path.join(dirname, filename), mode, **kwargs)
except OSError as e:
if e.errno != errno.ENOENT:
self._parse_error(e)

# Try each directory from the include path
for path in self._include_path:
try:
return open(os.path.join(path, filename), mode)
return open(os.path.join(path, filename), mode, **kwargs)
except OSError as e:
if e.errno != errno.ENOENT:
self._parse_error(e)
@@ -1029,7 +1029,7 @@ def _binding_compat(binding_path):
# Uses a regex to avoid having to parse the bindings, which is slow when
# done for all bindings.

with open(binding_path) as binding:
with open(binding_path, encoding="utf-8") as binding:
for line in binding:
match = re.match(r'\s+constraint:\s*"([^"]*)"', line)
if match:

0 comments on commit 04da7ea

Please sign in to comment.
You can’t perform that action at this time.