diff --git a/docs/development.md b/docs/development.md index ad4cde9..da07331 100644 --- a/docs/development.md +++ b/docs/development.md @@ -6,7 +6,8 @@ with the [CLI](cli.md) or [Python](python.md) guide. ## Build and test The checkout selects the stable Rust toolchain; the minimum supported compiler -is 1.88. Python bindings require Python 3.11 or newer. +is 1.88. Python bindings require Python 3.11 or newer. Source builds require +the tracked `Cargo.lock`; update dependencies explicitly with Cargo. ```sh cargo test --locked @@ -17,14 +18,15 @@ python -I tools/check_installed.py ``` Use `RUSTUP_TOOLCHAIN=1.88.0` to check source builds against the minimum compiler. -The Python package bundles its native library during installation. To test a -newly built library without reinstalling, set `PHEON_LIBRARY` to its absolute +The Python package bundles its native library during installation and reads its +version from installed package metadata. After installation, to test a newly +built library without reinstalling, set `PHEON_LIBRARY` to its absolute path and run `python -m unittest discover -s tests/python` from the checkout. Unset this override when checking installed artifacts with `check_installed.py`. ## Interfaces and reports -The CFFI declarations live in [pheon/_ffi.h](../pheon/_ffi.h). Keep them aligned +The CFFI declarations live in [pheon/_ffi.h](https://mpxd.net/code/jan/pheon/src/branch/master/pheon/_ffi.h). Keep them aligned with the exported native functions. The C API version and [Arrow layout version](arrow-schema.md) are independent of package releases and the [JSON report schema](diff-reports.md). diff --git a/docs/releases.md b/docs/releases.md index 332a447..76e7c7b 100644 --- a/docs/releases.md +++ b/docs/releases.md @@ -20,7 +20,7 @@ provides the CLI. Neither installation discovers or depends on the other. ## Validate artifacts -The [package workflow](../.github/workflows/packages.yml) builds one source archive +The [package workflow](https://mpxd.net/code/jan/pheon/src/branch/master/.github/workflows/packages.yml) builds one source archive and uses cibuildwheel to build and repair wheels for Linux and macOS x86-64/ARM64, and Windows x86-64. Wheels use `py3-none` interpreter/ABI tags plus a platform tag. Linux releases need an appropriate manylinux tag; unrepaired local `linux_*` diff --git a/pheon/__init__.py b/pheon/__init__.py index 09bb1d6..b71eb35 100644 --- a/pheon/__init__.py +++ b/pheon/__init__.py @@ -1,8 +1,10 @@ """Native GDSII helpers. Install pheon[arrow] for the pheon.arrow readers.""" +from importlib.metadata import version as _version + from ._native import PheonError as PheonError from .basic import pack_int2 as pack_int2 from .basic import pack_int4 as pack_int4 -__version__ = "0.3.0" +__version__ = _version("pheon") diff --git a/pheon/arrow.py b/pheon/arrow.py index 5d90631..5aa6ee1 100644 --- a/pheon/arrow.py +++ b/pheon/arrow.py @@ -43,16 +43,13 @@ def _export(call: Callable[[Any, Any], int]) -> pyarrow.StructArray: def read_path(path: str | os.PathLike[str]) -> pyarrow.StructArray: """Read a GDSII file, recognizing gzip by its magic bytes.""" - filename = os.path.abspath(os.path.expanduser(os.fspath(path))) - with open(filename, "rb") as stream: + with open(os.path.expanduser(path), "rb") as stream: compressed = stream.read(2) == b"\x1f\x8b" + stream.seek(0) if compressed: - stream.seek(0) with gzip.GzipFile(fileobj=stream) as source: return read_bytes(source.read()) - # The native Windows path API accepts UTF-8; Unix accepts filesystem bytes. - encoded = filename.encode("utf-8") if os.name == "nt" else os.fsencode(filename) - return _export(lambda array, schema: lib.read_path(encoded, array, schema)) + return read_bytes(stream.read()) def _read_buffer( diff --git a/pheon/basic.py b/pheon/basic.py index 9ca4745..51100aa 100644 --- a/pheon/basic.py +++ b/pheon/basic.py @@ -36,10 +36,8 @@ def _pack_int( arr = numpy.atleast_1d(data) target_dtype = numpy.dtype(f">i{width}") - for dtype, convert in converters.items(): - if arr.dtype != dtype: - continue - + convert = converters.get(arr.dtype.type) if arr.dtype.isnative else None + if convert is not None: arr = numpy.require( arr, requirements=("C_CONTIGUOUS", "ALIGNED", "WRITEABLE", "OWNDATA") ) @@ -60,7 +58,7 @@ def _pack_int( limit = 1 << (width * 8 - 1) if (arr > limit - 1).any() or (arr < -limit).any(): - raise Exception(f"int{width} data out of range: {arr}") + raise ValueError(f"int{width} data out of range: {arr}") return arr.astype(target_dtype).tobytes() diff --git a/setup.py b/setup.py index ca4fd70..17d080a 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,6 @@ import tomllib from setuptools import Distribution, setup from setuptools.command.build_py import build_py -from setuptools.command.sdist import sdist from wheel.bdist_wheel import bdist_wheel @@ -16,14 +15,12 @@ ROOT = Path(__file__).resolve().parent VERSION = tomllib.loads((ROOT / "Cargo.toml").read_text())["package"]["version"] -def ensure_lockfile(): - if not (ROOT / "Cargo.lock").is_file(): - subprocess.run(["cargo", "generate-lockfile"], cwd=ROOT, check=True) +if not (ROOT / "Cargo.lock").is_file(): + raise FileNotFoundError("Source builds require the supplied Cargo.lock") class CargoBuild(build_py): def run(self): - ensure_lockfile() super().run() result = subprocess.run( ["cargo", "build", "--release", "--lib", "--locked", "--message-format=json"], @@ -51,12 +48,6 @@ class CargoBuild(build_py): shutil.copy2(library, Path(self.build_lib) / "pheon" / library.name) -class CargoSdist(sdist): - def run(self): - ensure_lockfile() - super().run() - - class NativeDistribution(Distribution): def has_ext_modules(self): return True @@ -72,5 +63,5 @@ class NativeWheel(bdist_wheel): setup( version=VERSION, distclass=NativeDistribution, - cmdclass={"build_py": CargoBuild, "sdist": CargoSdist, "bdist_wheel": NativeWheel}, + cmdclass={"build_py": CargoBuild, "bdist_wheel": NativeWheel}, ) diff --git a/tests/python/test_arrow.py b/tests/python/test_arrow.py index 30367b4..ed962e2 100644 --- a/tests/python/test_arrow.py +++ b/tests/python/test_arrow.py @@ -2,6 +2,7 @@ import gc import gzip +import io import mmap from pathlib import Path import tempfile @@ -48,6 +49,16 @@ class ArrowTests(unittest.TestCase): path.write_bytes(gzip.compress(layout())) self.assertEqual(arrow.read_path(path).to_pylist(), expected) + def test_path_reader_keeps_using_the_opened_stream(self): + for payload in (layout(), gzip.compress(layout())): + with self.subTest(compressed=payload.startswith(b"\x1f\x8b")): + source = io.BytesIO(payload) + with patch("builtins.open", return_value=source) as opened: + result = arrow.read_path("opened-stream-only.gds") + opened.assert_called_once_with("opened-stream-only.gds", "rb") + self.assertTrue(source.closed) + self.assertEqual(result.field("cell_names")[0].as_py(), ["cell"]) + def test_empty_and_noncontiguous_cell_selections(self): data = layout() empty = arrow.read_cells_bytes(data, []) diff --git a/tests/python/test_packing.py b/tests/python/test_packing.py index 44eb84d..75ce0cd 100644 --- a/tests/python/test_packing.py +++ b/tests/python/test_packing.py @@ -45,13 +45,14 @@ class PackingTests(unittest.TestCase): def test_failed_conversions_preserve_caller_data(self): for pack, width in [(pack_int2, 2), (pack_int4, 4)]: limit = 1 << (8 * width - 1) - for invalid in [limit, -limit - 1]: - data = np.array([[1, 2], [3, invalid]], dtype=np.int64) - before = data.copy() - with self.subTest(width=width, invalid=invalid): - with self.assertRaises(ValueError): - pack(data) - np.testing.assert_array_equal(data, before) + for dtype in [np.dtype("i8"), np.dtype("i8").newbyteorder("S")]: + for invalid in [limit, -limit - 1]: + data = np.array([[1, 2], [3, invalid]], dtype=dtype) + before = data.copy() + with self.subTest(width=width, dtype=dtype, invalid=invalid): + with self.assertRaises(ValueError): + pack(data) + np.testing.assert_array_equal(data, before) for dtype in [np.float32, np.float64]: for invalid in [np.nan, np.inf, -np.inf]: data = np.array([[1, 2], [3, invalid]], dtype=dtype) diff --git a/tools/check_installed.py b/tools/check_installed.py index 707ecbf..970dc90 100644 --- a/tools/check_installed.py +++ b/tools/check_installed.py @@ -10,12 +10,12 @@ ROOT = Path(__file__).resolve().parents[1] # A separate interpreter verifies that base packing does not import PyArrow. subprocess.run( [sys.executable, "-I", "-c", """ -import importlib.util import sys +from importlib.metadata import version import pheon from pheon._native import library +assert pheon.__version__ == version('pheon') assert 'pyarrow' not in sys.modules -assert importlib.util.find_spec('klamath_rs_ext') is None assert library.parent == __import__('pathlib').Path(pheon.__file__).parent assert pheon.pack_int2([1, -2]) == bytes.fromhex('0001fffe') """],