From a936f088884b94d91af88d2554f5dce1b5e37408 Mon Sep 17 00:00:00 2001 From: Shawn Presser Date: Thu, 2 Dec 2021 22:43:22 -0600 Subject: [PATCH] fix: ensure every path is decoded as utf-8 --- protoletariat/fdsetgen.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/protoletariat/fdsetgen.py b/protoletariat/fdsetgen.py index 2e11136d..e7046d11 100644 --- a/protoletariat/fdsetgen.py +++ b/protoletariat/fdsetgen.py @@ -5,6 +5,7 @@ import re import subprocess import tempfile +import os from pathlib import Path from typing import Callable, Iterable, Sequence @@ -14,7 +15,6 @@ _PROTO_SUFFIX_PATTERN = re.compile(r"^(.+)\.proto$") - def _remove_proto_suffix(name: str) -> str: """Remove the `.proto` suffix from `name`.""" return _PROTO_SUFFIX_PATTERN.sub(r"\1", name) @@ -41,10 +41,11 @@ def fix_imports( exclude_imports_glob: Sequence[str], ) -> None: """Fix imports from protoc/buf generated code.""" + python_out = Path(os.fsdecode(python_out)) fdset = FileDescriptorSet.FromString(self.generate_file_descriptor_set_bytes()) for fd in fdset.file: - name = fd.name + name = os.fsdecode(fd.name) if _should_ignore(name, exclude_imports_glob): continue @@ -94,9 +95,9 @@ def __init__( proto_files: Iterable[Path], proto_paths: Iterable[Path], ) -> None: - self.protoc_path = protoc_path - self.proto_files = list(proto_files) - self.proto_paths = list(proto_paths) + self.protoc_path = os.fsdecode(protoc_path) + self.proto_files = [os.fsdecode(file) for file in proto_files] + self.proto_paths = [os.fsdecode(path) for path in proto_paths] def generate_file_descriptor_set_bytes(self) -> bytes: with tempfile.NamedTemporaryFile(delete=False) as f: @@ -105,9 +106,9 @@ def generate_file_descriptor_set_bytes(self) -> bytes: [ self.protoc_path, "--include_imports", - f"--descriptor_set_out={filename}", - *map("--proto_path={}".format, self.proto_paths), - *map(str, self.proto_files), + "--descriptor_set_out=" + os.fsdecode(filename), + *["--proto_path=" + path for path in self.proto_paths], + *self.proto_files, ] )