From 1cabad0efabaee63d83ea044b5c75900e132119b Mon Sep 17 00:00:00 2001 From: pchtsp Date: Fri, 12 Jul 2024 09:32:36 +0200 Subject: [PATCH] workaround to deactivate messages on licenses --- pulp/apis/__init__.py | 2 +- pulp/apis/copt_api.py | 16 ++++++++++++---- pulp/apis/gurobi_api.py | 3 ++- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/pulp/apis/__init__.py b/pulp/apis/__init__.py index 41c3240f..5133a92a 100644 --- a/pulp/apis/__init__.py +++ b/pulp/apis/__init__.py @@ -151,7 +151,7 @@ def listSolvers(onlyAvailable=False): """ result = [] for s in _all_solvers: - solver = s() + solver = s(msg=False) if (not onlyAvailable) or solver.available(): result.append(solver.name) del solver diff --git a/pulp/apis/copt_api.py b/pulp/apis/copt_api.py index f2631197..e2a9ed48 100644 --- a/pulp/apis/copt_api.py +++ b/pulp/apis/copt_api.py @@ -6,7 +6,7 @@ from uuid import uuid4 from .core import sparse, ctypesArrayFill, PulpSolverError -from .core import clock, log +from .core import clock from .core import LpSolver, LpSolver_CMD from ..constants import ( @@ -894,9 +894,17 @@ def __init__( logPath=logPath, warmStart=warmStart, ) - - self.coptenv = coptpy.Envr() - self.coptmdl = self.coptenv.createModel() + # workaround to deactivate logging when msg=False + if not self.msg: + devnull = open("/dev/null", "w") + oldstdout_fno = os.dup(sys.stdout.fileno()) + os.dup2(devnull.fileno(), 1) + self.coptenv = coptpy.Envr() + self.coptmdl = self.coptenv.createModel() + os.dup2(oldstdout_fno, 1) + else: + self.coptenv = coptpy.Envr() + self.coptmdl = self.coptenv.createModel() if not self.msg: self.coptmdl.setParam("Logging", 0) diff --git a/pulp/apis/gurobi_api.py b/pulp/apis/gurobi_api.py index ba8bf66d..514e10ac 100644 --- a/pulp/apis/gurobi_api.py +++ b/pulp/apis/gurobi_api.py @@ -440,7 +440,8 @@ def available(self): # normal execution return True # error: we display the gurobi message - warnings.warn(f"GUROBI error: {out}.") + if self.msg: + warnings.warn(f"GUROBI error: {out}.") return False def actualSolve(self, lp):