Skip to content

Commit

Permalink
Add int file descriptor overload to open()
Browse files Browse the repository at this point in the history
  • Loading branch information
avitkauskas committed Jan 30, 2025
1 parent 44c59c2 commit 40ca5a6
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions stdlib/internal/file.codon
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ class File:
raise IOError(f"file {path} could not be opened")
self._reset()

def __init__(self, fd: int, mode: str):
self.fp = _C.fdopen(fd, mode.c_str())
if not self.fp:
raise IOError(f"file descriptor {fd} could not be opened")
self._reset()

def _errcheck(self, msg: str):
err = int(_C.ferror(self.fp))
if err:
Expand Down Expand Up @@ -395,6 +401,9 @@ class bzFile:
def open(path: str, mode: str = "r") -> File:
return File(path, mode)

def open(fd: int, mode: str = "r") -> File:
return File(fd, mode)

def gzopen(path: str, mode: str = "r") -> gzFile:
return gzFile(path, mode)

Expand Down

0 comments on commit 40ca5a6

Please sign in to comment.