From 98e103a0af3b304f062555a4315fb4d9e3df5a45 Mon Sep 17 00:00:00 2001 From: Daniel Rojas Date: Sat, 1 Mar 2025 18:07:43 +0100 Subject: [PATCH] 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 Co-authored-by: JarrettR --- src/wireviz/wireviz.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/wireviz/wireviz.py b/src/wireviz/wireviz.py index 08fbe12..834b8a7 100755 --- a/src/wireviz/wireviz.py +++ b/src/wireviz/wireviz.py @@ -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