Fixed bugs found in other projects

When testing amalgamating other projects it was found: invalid Unicode errors were tripping Python's text IO, and the header search order appears differs from the shell version.
dev
Carl Woffenden 2022-01-19 16:56:03 +01:00
parent dd7d29a19c
commit 5fd6ddaf8b
1 changed files with 12 additions and 7 deletions

View File

@ -106,21 +106,21 @@ def text_match_pragma() -> bool:
return True return True
return False return False
# Finds 'file'. First the currently processing file's 'parent' path is looked at # Finds 'file'. First the list of 'root' paths are searched, followed by the
# for a match, followed by the list of 'root' paths, returning a valid Path in # the currently processing file's 'parent' path, returning a valid Path in
# canonical form. If no match is found None is returned. # canonical form. If no match is found None is returned.
# #
def resolve_include(file: str, parent: Optional[Path] = None) -> Optional[Path]: def resolve_include(file: str, parent: Optional[Path] = None) -> Optional[Path]:
for root in roots:
found = root.joinpath(file).resolve()
if (found.is_file()):
return found
if (parent): if (parent):
found = parent.joinpath(file).resolve(); found = parent.joinpath(file).resolve();
else: else:
found = Path(file) found = Path(file)
if (found.is_file()): if (found.is_file()):
return found return found
for root in roots:
found = root.joinpath(file).resolve()
if (found.is_file()):
return found
return None return None
# Helper to resolve lists of files. 'file_list' is passed in from the arguments # Helper to resolve lists of files. 'file_list' is passed in from the arguments
@ -150,12 +150,17 @@ def error_line(line: Any) -> None:
# Inline the contents of 'file' (with any of its includes also inlined, etc.). # Inline the contents of 'file' (with any of its includes also inlined, etc.).
# #
# Note: text encoding errors are ignored and replaced with ? when reading the
# input files. This isn't ideal, but it's more than likely in the comments than
# code and a) the text editor has probably also failed to read the same content,
# and b) the compiler probably did too.
#
def add_file(file: Path, file_name: str = None) -> None: def add_file(file: Path, file_name: str = None) -> None:
if (file.is_file()): if (file.is_file()):
if (not file_name): if (not file_name):
file_name = file.name file_name = file.name
error_line(f'Processing: {file_name}') error_line(f'Processing: {file_name}')
with file.open('r') as opened: with file.open('r', errors='replace') as opened:
for line in opened: for line in opened:
line = line.rstrip('\n') line = line.rstrip('\n')
match_include = include_regex.match(line); match_include = include_regex.match(line);