Handle OSError(errno=EINVAL) that might be raised in Windows (#346)

In Windows might OSError(errno=EINVAL) be raised instead of the already
catched exceptions in some cases (depending on the Python version).

Suggested fix posted by JarrettR in
https://github.com/wireviz/WireViz/issues/344#issuecomment-2113476151

Co-authored-by: kvid <kvid@users.noreply.github.com>
Co-authored-by: JarrettR <jrainier@gmail.com>
This commit is contained in:
Daniel Rojas 2025-03-01 18:07:43 +01:00
parent e73e189131
commit 98e103a0af

View File

@ -2,6 +2,7 @@
# -*- coding: utf-8 -*-
import sys
from errno import EINVAL, ENAMETOOLONG
from pathlib import Path
from typing import Any, Dict, List, Tuple, Union
@ -410,12 +411,20 @@ def parse(
def _get_yaml_data_and_path(inp: Union[str, Path, Dict]) -> Tuple[Dict, Path]:
# determine whether inp is a file path, a YAML string, or a Dict
if not isinstance(inp, Dict): # received a str or a Path
if isinstance(inp, Path) or (isinstance(inp, str) and not "\n" in inp):
try:
yaml_path = Path(inp).expanduser().resolve(strict=True)
# if no FileNotFoundError exception happens, get file contents
yaml_str = open_file_read(yaml_path).read()
else:
yaml_path = None
except (FileNotFoundError, OSError) as e:
# if inp is a long YAML string, Pathlib will raise OSError: [Errno 63]
# (in Windows, it seems OSError [errno.EINVAL] might be raised in some cases)
# when trying to expand and resolve it as a path.
# Catch this error, but raise any others
if type(e) is OSError and e.errno not in (EINVAL, ENAMETOOLONG):
raise e
# file does not exist; assume inp is a YAML string
yaml_str = inp
yaml_path = None
yaml_data = yaml.safe_load(yaml_str)
else:
# received a Dict, use as-is