From e30f7b14bb439ab1b539476c0ae7b8f39df218c1 Mon Sep 17 00:00:00 2001 From: Quantum Date: Sun, 30 Jan 2022 15:55:04 -0500 Subject: [PATCH] Give an actual name to MemoryIO on FreeBSD --- dmoj/cptbox/utils.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/dmoj/cptbox/utils.py b/dmoj/cptbox/utils.py index 323271639..e50bc7172 100644 --- a/dmoj/cptbox/utils.py +++ b/dmoj/cptbox/utils.py @@ -2,14 +2,24 @@ import io import mmap import os +from tempfile import NamedTemporaryFile from typing import Optional from dmoj.cptbox._cptbox import memory_fd_create, memory_fd_seal +from dmoj.cptbox.tracer import FREEBSD class MemoryIO(io.FileIO): + _name: Optional[str] + def __init__(self, prefill: Optional[bytes] = None, seal=False) -> None: - super().__init__(memory_fd_create(), 'r+') + if FREEBSD: + with NamedTemporaryFile(delete=False) as f: + self._name = f.name + super().__init__(os.dup(f.fileno()), 'r+') + else: + super().__init__(memory_fd_create(), 'r+') + if prefill: self.write(prefill) if seal: @@ -32,7 +42,14 @@ def seal(self) -> None: finally: os.close(new_fd) + def close(self) -> None: + super().close() + if self._name: + os.unlink(self._name) + def to_path(self) -> str: + if self._name: + return self._name return f'/proc/{os.getpid()}/fd/{self.fileno()}' def to_bytes(self) -> bytes: