Skip to content

Commit

Permalink
GH-53: Export the version of NumPy used to build the project as a con…
Browse files Browse the repository at this point in the history
…stant (via setuptools/cython preprocessor injecting the value during build) (#59)
  • Loading branch information
kinow authored Aug 26, 2024
1 parent ae28b4f commit 70bc5ad
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 3 deletions.
3 changes: 3 additions & 0 deletions crick/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import absolute_import

from ._version import get_versions
from .numpy_version import numpy_version
from .space_saving import SpaceSaving
from .stats import SummaryStats
from .tdigest import TDigest
Expand All @@ -13,3 +14,5 @@
from . import _version

__version__ = _version.get_versions()["version"]

__numpy_version__ = numpy_version()
15 changes: 15 additions & 0 deletions crick/numpy_version.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
cdef extern from "numpy_version_stubs.h":
cdef char* NUMPY_VERSION

def numpy_version():
"""numpy_version()
The NumPy version used to build and compile the Python and Cython code.
Useful for troubleshooting runtime issues.
References
----------
.. https://github.com/dask/crick/issues/53
"""
return NUMPY_VERSION
2 changes: 2 additions & 0 deletions crick/numpy_version_stubs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// empty, NUMPY_VERSION will be inserted by preprocessor
// From: https://stackoverflow.com/a/48411593
2 changes: 1 addition & 1 deletion crick/tdigest.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ cdef class TDigest:
Notes
-----
This implements the "MergingDigest" variant of the T-Digest algorithm
descibed in [1]_. The reference java implementation can be found at [2]_.
described in [1]_. The reference java implementation can be found at [2]_.
References
----------
Expand Down
21 changes: 21 additions & 0 deletions crick/tests/test_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from crick import __numpy_version__, __version__


def test_version():
"""Verify that crick's version is exported correctly."""
assert __version__


def test_numpy_version():
"""Verify that the numpy version exists and is exports correctly.
This value is created with Cython during the build-time, by the
preprocessor that writes the value passed during build-time
(by injecting `np.__version__`). It should match the version
used to build, but not necessarily the version installed/available.
i.e. you can have NumPy 2.0 installed, but the crick developers used
2.1.0 to build and upload the wheel -- then it relies on NumPy's
backward/forward compatibility during run-time.
"""
assert __numpy_version__
9 changes: 7 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
def generate_code(templates):
"""Generate code from template files"""
for template in templates:
# template extention must be .in
# template extension must be .in
assert template.endswith(".in")
outfile = template[:-3]

Expand All @@ -35,7 +35,7 @@ def generate_code(templates):
or os.stat(template).st_mtime < os.stat(outfile).st_mtime
):
# If output is present but template isn't, or if template is not
# updated no need to generate
# updated, then no need to generate
continue

with open(template, "r") as f:
Expand All @@ -54,6 +54,11 @@ def generate_code(templates):
Extension("crick.tdigest", ["crick/tdigest.pyx"], **compile_args),
Extension("crick.space_saving", ["crick/space_saving.pyx"], **compile_args),
Extension("crick.stats", ["crick/stats.pyx"], **compile_args),
Extension(
"crick.numpy_version",
["crick/numpy_version.pyx"],
extra_compile_args=[f'-DNUMPY_VERSION="{np.__version__}"'],
),
]

setup(
Expand Down

0 comments on commit 70bc5ad

Please sign in to comment.