Skip to content

Commit

Permalink
typing: snakeoil.fileutils
Browse files Browse the repository at this point in the history
Minor cleanup to use finally syntax since this
is the intent.

Signed-off-by: Brian Harring <[email protected]>
  • Loading branch information
ferringb committed Jan 22, 2024
1 parent 6e7fd0a commit f3b5233
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/snakeoil/fileutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import mmap
import os
from typing import Optional
from functools import partial

from . import _fileutils, data_source
Expand All @@ -12,7 +13,9 @@
from .klass import GetAttrProxy


def touch(fname: str, mode: int = 0o644, dir_fd=None, **kwargs):
def touch(
fname: str, mode: int = 0o644, dir_fd: Optional[int] = None, **kwargs
) -> None:
"""touch(1) equivalent
:param fname: file path
Expand All @@ -34,7 +37,7 @@ def mmap_or_open_for_read(path: str):
size = os.stat(path).st_size
if size == 0:
return (None, data_source.bytes_ro_StringIO(b""))
fd = None
fd: Optional[int] = None
try:
fd = os.open(path, os.O_RDONLY)
return (
Expand All @@ -43,12 +46,12 @@ def mmap_or_open_for_read(path: str):
)
except IGNORED_EXCEPTIONS:
raise
except:
try:
os.close(fd)
except EnvironmentError:
pass
raise
finally:
if fd is not None:
try:
os.close(fd)
except EnvironmentError:
pass


class AtomicWriteFile_mixin:
Expand Down

0 comments on commit f3b5233

Please sign in to comment.