util: use Libc.memfd_create() when os.memfd_create() is missing

This provide compat for pyton versions below 3.8. This can be
removed (together with the previous commit) once we are at
python3.8+.
This commit is contained in:
Michael Vogt 2024-08-12 16:48:31 +02:00 committed by Achilleas Koutsou
parent 09e78c52d9
commit 478fee2876

View file

@ -13,12 +13,20 @@ import os
import socket
from typing import Any, List, Optional
from .linux import Libc
from .types import PathLike
@contextlib.contextmanager
def memfd(name):
fd = os.memfd_create(name, 0)
if hasattr(os, "memfd_create"):
fd = os.memfd_create(name)
else:
# we can remove this "if/else" once we are at python3.8+
# and just use "os.memfd_create()"
libc = Libc.default()
fd = libc.memfd_create(name)
try:
yield fd
finally: