From 26bec6d90793fc5327aeaf71e99a55bde1140845 Mon Sep 17 00:00:00 2001 From: Rohan Santhosh Kumar <181558744+Rohan5commit@users.noreply.github.com> Date: Thu, 21 May 2026 12:17:45 +0800 Subject: [PATCH] fix: make stat() Windows-compatible by guarding os.getegid/geteuid MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #177 — os.getegid() and os.geteuid() are not available on Windows. On Windows, group_access and user_access are omitted from the stat() result rather than crashing with AttributeError. --- src/celpy/__main__.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/celpy/__main__.py b/src/celpy/__main__.py index 3b792db..b7c70aa 100644 --- a/src/celpy/__main__.py +++ b/src/celpy/__main__.py @@ -29,6 +29,7 @@ import logging.config import os from pathlib import Path +import platform import re import stat as os_stat import sys @@ -46,6 +47,8 @@ logger = logging.getLogger("celpy") +_IS_WINDOWS = platform.system() == "Windows" + # For argument parsing purposes. # Note the reliance on `ast.literal_eval` for ListType and MapType conversions. @@ -236,10 +239,14 @@ def get_options(argv: Optional[List[str]] = None) -> argparse.Namespace: def stat(path: Union[Path, str]) -> Optional[celtypes.MapType]: - """This function is added to the CLI to permit file-system interrogation.""" + """This function is added to the CLI to permit file-system interrogation. + + On Windows, ``group_access`` and ``user_access`` are omitted because + ``os.getegid()`` and ``os.geteuid()`` are not available on that platform. + """ try: status = Path(path).stat() - data = { + data: Dict[str, celtypes.Value] = { "st_atime": celtypes.TimestampType( datetime.datetime.fromtimestamp(status.st_atime) ), @@ -253,10 +260,14 @@ def stat(path: Union[Path, str]) -> Optional[celtypes.MapType]: "st_ino": celtypes.IntType(status.st_ino), "st_nlink": celtypes.IntType(status.st_nlink), "st_size": celtypes.IntType(status.st_size), - "group_access": celtypes.BoolType(status.st_gid == os.getegid()), - "user_access": celtypes.BoolType(status.st_uid == os.geteuid()), } + # group_access and user_access rely on os.getegid()/os.geteuid(), + # which are not available on Windows. + if not _IS_WINDOWS: + data["group_access"] = celtypes.BoolType(status.st_gid == os.getegid()) # type: ignore[attr-defined] + data["user_access"] = celtypes.BoolType(status.st_uid == os.geteuid()) # type: ignore[attr-defined] + # From mode File type: # - block, character, directory, regular, symbolic link, named pipe, socket # One predicate should be True; we want the code for that key.