Skip to content

Commit cab5505

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

File tree

2 files changed

+50
-29
lines changed

2 files changed

+50
-29
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: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,25 @@
3434
logger.setLevel(logging.INFO)
3535
logger.addHandler(logging.StreamHandler(stdout))
3636

37+
# Expected return value from native OQS functions
38+
OQS_SUCCESS: Final[int] = 0
39+
OQS_ERROR: Final[int] = -1
40+
41+
42+
def native() -> ct.CDLL:
43+
"""Handle to native liboqs handler."""
44+
return _liboqs
45+
46+
47+
# liboqs initialization
48+
native().OQS_init()
49+
50+
51+
def oqs_version() -> str:
52+
"""`liboqs` version string."""
53+
native().OQS_version.restype = ct.c_char_p
54+
return ct.c_char_p(native().OQS_version()).value.decode("UTF-8") # type: ignore[union-attr]
55+
3756

3857
def oqs_python_version() -> Union[str, None]:
3958
"""liboqs-python version string."""
@@ -45,6 +64,31 @@ def oqs_python_version() -> Union[str, None]:
4564
return result
4665

4766

67+
def version(version_str: str) -> tuple[str, str, str]:
68+
parts = version_str.split(".")
69+
70+
major = parts[0] if len(parts) > 0 else ""
71+
minor = parts[1] if len(parts) > 1 else ""
72+
patch = parts[2] if len(parts) > 2 else ""
73+
74+
return major, minor, patch
75+
76+
77+
oqs_ver = oqs_version()
78+
oqs_ver_major, oqs_ver_minor, oqs_ver_patch = version(oqs_ver)
79+
80+
oqs_python_ver = oqs_python_version()
81+
if oqs_python_ver:
82+
oqs_python_ver_major, oqs_python_ver_minor, oqs_python_ver_patch = version(oqs_python_ver)
83+
# Warn the user if the liboqs version differs from liboqs-python version
84+
if not (oqs_ver_major == oqs_python_ver_major and oqs_ver_minor == oqs_python_ver_minor):
85+
warnings.warn(
86+
f"liboqs version (major, minor) {oqs_version()} differs from liboqs-python version "
87+
f"{oqs_python_version()}",
88+
stacklevel=2,
89+
)
90+
91+
4892
# liboqs-python tries to automatically install and load this liboqs version in
4993
# case no other version is found
5094
OQS_VERSION = oqs_python_version()
@@ -206,35 +250,6 @@ def _load_liboqs() -> ct.CDLL:
206250
_liboqs = _load_liboqs()
207251

208252

209-
# Expected return value from native OQS functions
210-
OQS_SUCCESS: Final[int] = 0
211-
OQS_ERROR: Final[int] = -1
212-
213-
214-
def native() -> ct.CDLL:
215-
"""Handle to native liboqs handler."""
216-
return _liboqs
217-
218-
219-
# liboqs initialization
220-
native().OQS_init()
221-
222-
223-
def oqs_version() -> str:
224-
"""`liboqs` version string."""
225-
native().OQS_version.restype = ct.c_char_p
226-
return ct.c_char_p(native().OQS_version()).value.decode("UTF-8") # type: ignore[union-attr]
227-
228-
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-
)
236-
237-
238253
class MechanismNotSupportedError(Exception):
239254
"""Exception raised when an algorithm is not supported by OQS."""
240255

0 commit comments

Comments
 (0)