Skip to content

Commit e7964ea

Browse files
eminyousknmetab0t
andauthored
[KNITRO] Add license validation (metab0t#80)
* Add license validation for KNITRO library and update related tests * fix merge conflicts --------- Co-authored-by: Yue Yang <metab0t@users.noreply.github.com>
1 parent 9d9d437 commit e7964ea

File tree

6 files changed

+29
-3
lines changed

6 files changed

+29
-3
lines changed

include/pyoptinterface/knitro_model.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ APILIST
9393
bool is_library_loaded();
9494

9595
bool load_library(const std::string &path);
96+
97+
bool has_valid_license();
9698
} // namespace knitro
9799

98100
struct KNITROFreeProblemT

lib/knitro_model.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,26 @@ bool load_library(const std::string &path)
4848
return false;
4949
}
5050
}
51+
52+
bool has_valid_license()
53+
{
54+
if (!is_library_loaded())
55+
{
56+
throw std::runtime_error("KNITRO library not loaded");
57+
}
58+
59+
LM_context *lm = nullptr;
60+
int error = KN_checkout_license(&lm);
61+
if (error == 0)
62+
{
63+
KN_release_license(&lm);
64+
return true;
65+
}
66+
else
67+
{
68+
return false;
69+
}
70+
}
5171
} // namespace knitro
5272

5373
void ensure_library_loaded()

lib/knitro_model_ext.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ NB_MODULE(knitro_model_ext, m)
1515

1616
m.def("is_library_loaded", &knitro::is_library_loaded);
1717
m.def("load_library", &knitro::load_library);
18+
m.def("has_valid_license", &knitro::has_valid_license);
1819

1920
bind_knitro_constants(m);
2021

src/pyoptinterface/knitro.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
KN,
44
load_library,
55
is_library_loaded,
6+
has_valid_license,
67
)
78

89
__all__ = [
@@ -12,4 +13,5 @@
1213
"autoload_library",
1314
"load_library",
1415
"is_library_loaded",
16+
"has_valid_license",
1517
]

tests/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def c():
2323
if copt.is_library_loaded():
2424
nlp_model_dict["copt"] = copt.Model
2525

26-
if knitro.is_library_loaded():
26+
if knitro.is_library_loaded() and knitro.has_valid_license():
2727
nlp_model_dict["knitro"] = knitro.Model
2828

2929

@@ -46,7 +46,7 @@ def nlp_model_ctor(request):
4646
model_interface_dict_full["mosek"] = mosek.Model
4747
if highs.is_library_loaded():
4848
model_interface_dict_full["highs"] = highs.Model
49-
if knitro.is_library_loaded():
49+
if knitro.is_library_loaded() and knitro.has_valid_license():
5050
model_interface_dict_full["knitro"] = knitro.Model
5151

5252

tests/test_knitro.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
import pyoptinterface as poi
66

77
pytestmark = pytest.mark.skipif(
8-
not knitro.is_library_loaded(), reason="KNITRO library is not loaded"
8+
not knitro.is_library_loaded() or not knitro.has_valid_license(),
9+
reason="KNITRO library is not loaded or license is not valid",
910
)
1011

1112

0 commit comments

Comments
 (0)