Skip to content

Commit 059a17c

Browse files
committed
Warning only when (major, minor) versions differ
Signed-off-by: Vlad Gheorghiu <[email protected]>
1 parent 3be2109 commit 059a17c

File tree

2 files changed

+44
-22
lines changed

2 files changed

+44
-22
lines changed

CHANGES.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
# Pre-release
22

33
- Added type checking and automatic linting/formatting, https://github.com/open-quantum-safe/liboqs-python/pull/97
4+
- Added a utility function for de-structuring version strings in `oqs.py`
5+
- `version(version_str: str) -> tuple[str, str, str]:` - Returns a tuple
6+
containing the
7+
(major, minor, patch) versions
8+
- A warning is issued only if the liboqs-python version's major and minor
9+
numbers differ from those of liboqs, ignoring the patch version
410

511
# Version 0.12.0 - January 15, 2025
612

oqs/oqs.py

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,6 @@
3535
logger.addHandler(logging.StreamHandler(stdout))
3636

3737

38-
def oqs_python_version() -> Union[str, None]:
39-
"""liboqs-python version string."""
40-
try:
41-
result = importlib.metadata.version("liboqs-python")
42-
except importlib.metadata.PackageNotFoundError:
43-
warnings.warn("Please install liboqs-python using pip install", stacklevel=2)
44-
return None
45-
return result
46-
47-
48-
# liboqs-python tries to automatically install and load this liboqs version in
49-
# case no other version is found
50-
OQS_VERSION = oqs_python_version()
51-
52-
5338
def _countdown(seconds: int) -> None:
5439
while seconds > 0:
5540
logger.info("Installing in %s seconds...", seconds)
@@ -226,13 +211,44 @@ def oqs_version() -> str:
226211
return ct.c_char_p(native().OQS_version()).value.decode("UTF-8") # type: ignore[union-attr]
227212

228213

229-
# Warn the user if the liboqs version differs from liboqs-python version
230-
if oqs_version() != oqs_python_version():
231-
warnings.warn(
232-
f"liboqs version {oqs_version()} differs from liboqs-python version "
233-
f"{oqs_python_version()}",
234-
stacklevel=2,
235-
)
214+
def oqs_python_version() -> Union[str, None]:
215+
"""liboqs-python version string."""
216+
try:
217+
result = importlib.metadata.version("liboqs-python")
218+
except importlib.metadata.PackageNotFoundError:
219+
warnings.warn("Please install liboqs-python using pip install", stacklevel=2)
220+
return None
221+
return result
222+
223+
224+
def version(version_str: str) -> tuple[str, str, str]:
225+
parts = version_str.split(".")
226+
227+
major = parts[0] if len(parts) > 0 else ""
228+
minor = parts[1] if len(parts) > 1 else ""
229+
patch = parts[2] if len(parts) > 2 else ""
230+
231+
return major, minor, patch
232+
233+
234+
oqs_ver = oqs_version()
235+
oqs_ver_major, oqs_ver_minor, oqs_ver_patch = version(oqs_ver)
236+
237+
oqs_python_ver = oqs_python_version()
238+
if oqs_python_ver:
239+
oqs_python_ver_major, oqs_python_ver_minor, oqs_python_ver_patch = version(oqs_python_ver)
240+
# Warn the user if the liboqs version differs from liboqs-python version
241+
if not (oqs_ver_major == oqs_python_ver_major and oqs_ver_minor == oqs_python_ver_minor):
242+
warnings.warn(
243+
f"liboqs version (major, minor) {oqs_version()} differs from liboqs-python version "
244+
f"{oqs_python_version()}",
245+
stacklevel=2,
246+
)
247+
248+
249+
# liboqs-python tries to automatically install and load this liboqs version in
250+
# case no other version is found
251+
OQS_VERSION = oqs_python_version()
236252

237253

238254
class MechanismNotSupportedError(Exception):

0 commit comments

Comments
 (0)