Skip to content

Commit

Permalink
Trac #28756: py3: fix doctests with gurobi
Browse files Browse the repository at this point in the history
With sage 9.0.beta6 and gurobi, we get 17 failing doctests due to str vs
bytes (`TypeError: expected bytes, str found`), char vs str, and the
change of behavior of zip between py2 and py3 (`TypeError: object of
type 'zip' has no len()`). Some problems were fixed in #28206, but it
was apparently not enough.
Try:
{{{
sage -t --long --optional=sage,optional,external
src/sage/numerical/linear_functions.pyx
src/sage/numerical/linear_tensor.py
src/sage/numerical/linear_tensor_constraints.py
src/sage/numerical/linear_tensor_element.pyx  src/sage/numerical/mip.pyx
src/sage/numerical/backends/gurobi_backend.pyx
}}}
which gives
{{{
----------------------------------------------------------------------
sage -t src/sage/numerical/linear_functions.pyx  # 29 doctests failed
sage -t src/sage/numerical/linear_tensor.py  # 20 doctests failed
sage -t src/sage/numerical/linear_tensor_constraints.py  # 28 doctests
failed
sage -t src/sage/numerical/linear_tensor_element.pyx  # 23 doctests
failed
sage -t src/sage/numerical/mip.pyx  # 4 doctests failed
sage -t src/sage/numerical/backends/gurobi_backend.pyx  # 17 doctests
failed
----------------------------------------------------------------------
}}}

We also fix some compilation warnings.

URL: https://trac.sagemath.org/28756
Reported by: dcoudert
Ticket author(s): David Coudert
Reviewer(s): Sébastien Labbé
  • Loading branch information
Release Manager committed Nov 28, 2019
2 parents cdda9f7 + 5c6d5fb commit 307b865
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/sage/numerical/backends/gurobi_backend.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ cdef class GurobiBackend(GenericBackend):

if name is None:
name = b"x_" + bytes(self.ncols())

else:
name = str_to_bytes(name)
c_name = name

if upper_bound is None:
Expand All @@ -182,6 +183,7 @@ cdef class GurobiBackend(GenericBackend):

if coefficients is not None:

coefficients = list(coefficients)
nonzeros = len(coefficients)
c_indices = <int *> sig_malloc(nonzeros * sizeof(int))
c_coeff = <double *> sig_malloc(nonzeros * sizeof(double))
Expand Down Expand Up @@ -518,7 +520,7 @@ cdef class GurobiBackend(GenericBackend):
"""

if lower_bound is None and upper_bound is None:
raise ValueError("At least one of 'upper_bound' or 'lower_bound' must be set.")
raise ValueError("at least one of 'upper_bound' or 'lower_bound' must be set")

coefficients = list(coefficients)
cdef int n = len(coefficients)
Expand Down Expand Up @@ -549,6 +551,10 @@ cdef class GurobiBackend(GenericBackend):
else:
error = GRBaddrangeconstr(self.model, n, row_i, row_values, <double> lower_bound, <double> upper_bound, str_to_bytes(name))

else:
# This case is repeated here to avoid compilation warnings
raise ValueError("at least one of 'upper_bound' or 'lower_bound' must be set")

check(self.env,error)

error = GRBupdatemodel(self.model)
Expand Down Expand Up @@ -850,7 +856,7 @@ cdef class GurobiBackend(GenericBackend):
if name[0] == NULL:
value = ""
else:
value = str(name[0])
value = char_to_str(name[0])
return value

cpdef row_name(self, int index):
Expand All @@ -874,7 +880,7 @@ cdef class GurobiBackend(GenericBackend):
if name[0] == NULL:
value = ""
else:
value = str(name[0])
value = char_to_str(name[0])
return value

cpdef bint is_variable_binary(self, int index):
Expand Down Expand Up @@ -1152,6 +1158,8 @@ cdef class GurobiBackend(GenericBackend):
raise ValueError("This parameter is not available. "+
"Enabling it may not be so hard, though.")

name = str_to_bytes(name)

if t == "int":
if value is None:
check(self.env, GRBgetintparam(self.env, name, tmp_int))
Expand All @@ -1167,9 +1175,9 @@ cdef class GurobiBackend(GenericBackend):
elif t == "string":
if value is None:
check(self.env, GRBgetstrparam(self.env, name, c_name))
return str(c_name)
return char_to_str(c_name)
else:
check(self.env, GRBsetstrparam(self.env, name, value))
check(self.env, GRBsetstrparam(self.env, name, str_to_bytes(value)))
else:
raise RuntimeError("This should not happen.")

Expand Down

0 comments on commit 307b865

Please sign in to comment.