Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion tests/test_general.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
# SPDX-License-Identifier: Apache-2.0

from vaccel import Session
from pathlib import Path

import pytest

from vaccel import Resource, ResourceType, Session


@pytest.fixture
def test_lib(vaccel_paths) -> Path:
return vaccel_paths["lib"] / "libmytestlib.so"


def test_session():
Expand All @@ -9,6 +18,12 @@ def test_session():
assert ses_b.id == ses_a.id + 1


def test_resource(test_lib):
res_a = Resource(test_lib, ResourceType.LIB)
res_b = Resource([test_lib, test_lib], ResourceType.LIB)
assert res_b.id == res_a.id + 1


def test_noop():
session = Session(flags=0)
session.noop()
7 changes: 7 additions & 0 deletions vaccel/_c_types/wrappers/clist.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from vaccel._libvaccel import ffi
from vaccel.error import NullPointerError

from .cstr import CStr


class CList(CType):
"""Wrapper for `list` objects.
Expand Down Expand Up @@ -53,6 +55,8 @@ def __init__(self, items: Sequence[Any]):
def _infer_ctype_str(self, item: CType) -> str:
ptr = item._c_ptr
ctype = ffi.getctype(ffi.typeof(ptr))
if isinstance(self._items[0], CStr):
return ctype.replace("[]", " *")
return ctype.replace(" *", "")

def _init_c_obj(self):
Expand All @@ -61,6 +65,9 @@ def _init_c_obj(self):
for item in self._items
):
self._c_obj = ffi.new(f"{self._ctype_str}[{len(self._items)}]")
elif "*" in self._ctype_str:
ptrs = [item._c_ptr for item in self._items]
self._c_obj = ffi.new(f"{self._ctype_str}[{len(ptrs)}]", ptrs)
else:
values = [item.value for item in self._items]
self._c_obj = ffi.new(f"{self._ctype_str}[{len(values)}]", values)
Expand Down
30 changes: 20 additions & 10 deletions vaccel/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from pathlib import Path
from typing import TYPE_CHECKING

from ._c_types import CType
from ._c_types import CList, CType
from ._c_types.utils import CEnumBuilder
from ._libvaccel import ffi, lib
from .error import FFIError, NullPointerError
Expand All @@ -32,19 +32,26 @@ class Resource(CType):
CType: Abstract base class for defining C data types.

Attributes:
_path (str): The path to the contained file.
_paths (list[Path] | list[str] | Path | str): The path(s) to the
contained file(s).
_type (ResourceType): The type of the resource.
"""

def __init__(self, path: str | Path, type_: ResourceType):
def __init__(
self, paths: list[Path] | list[str] | Path | str, type_: ResourceType
):
"""Initializes a new `Resource` object.

Args:
path: The path to the file that will be represented by the resource.
paths: The path(s) to the file(s) that will be represented by the
resource.
type_: The type of the resource.
"""
# TODO: Allow the use of lists for path # noqa: FIX002
self._path = str(path)
if isinstance(paths, list):
self._paths = [str(path) for path in paths]
else:
self._paths = [str(paths)]
self._c_paths = CList(self._paths)
self._type = type_
self.__sessions = []
super().__init__()
Expand All @@ -56,8 +63,11 @@ def _init_c_obj(self):
FFIError: If resource initialization fails.
"""
self._c_obj_ptr = ffi.new("struct vaccel_resource **")
ret = lib.vaccel_resource_new(
self._c_obj_ptr, self._path.encode(), self._type
ret = lib.vaccel_resource_multi_new(
self._c_obj_ptr,
self._c_paths._c_ptr,
len(self._c_paths),
self._type,
)
if ret != 0:
raise FFIError(ret, "Could not initialize resource")
Expand Down Expand Up @@ -180,14 +190,14 @@ def __repr__(self):
)
resource_id = self.id
remote_id = self.remote_id
path = self._path
paths = self._paths
type_name = getattr(self._type, "name", repr(self._type))
except (AttributeError, TypeError, NullPointerError):
return f"<{self.__class__.__name__} (uninitialized or invalid)>"
return (
f"<{self.__class__.__name__} id={resource_id} "
f"remote_id={remote_id} "
f"path={path!r} "
f"paths={paths!r} "
f"type={type_name} "
f"at {c_ptr}>"
)