From 937d021dfd42ba4e77f616663bca0748e21df6ac Mon Sep 17 00:00:00 2001 From: Laurent Perron Date: Wed, 16 Mar 2016 13:34:47 +0100 Subject: [PATCH] polish support for python3 --- makefiles/Makefile.python.mk | 14 +++++++++++++- src/linear_solver/python/linear_solver.swig | 17 +++++++++++++---- src/util/python/proto.swig | 11 +++++++++++ 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/makefiles/Makefile.python.mk b/makefiles/Makefile.python.mk index 708138a8ee..a7dbbdf5cd 100644 --- a/makefiles/Makefile.python.mk +++ b/makefiles/Makefile.python.mk @@ -2,9 +2,21 @@ # Detect python3 +ifeq ("$(PYTHON_VERSION)","3.2") + PYTHON3 = true + SWIG_PYTHON3_FLAG=-py3 -DPY3 +endif ifeq ("$(PYTHON_VERSION)","3.3") PYTHON3 = true - SWIG_PYTHON3_FLAG=-py3 + SWIG_PYTHON3_FLAG=-py3 -DPY3 +endif +ifeq ("$(PYTHON_VERSION)","3.4") + PYTHON3 = true + SWIG_PYTHON3_FLAG=-py3 -DPY3 +endif +ifeq ("$(PYTHON_VERSION)","3.5") + PYTHON3 = true + SWIG_PYTHON3_FLAG=-py3 -DPY3 endif # Main target diff --git a/src/linear_solver/python/linear_solver.swig b/src/linear_solver/python/linear_solver.swig index 19dc641049..fcd6043296 100644 --- a/src/linear_solver/python/linear_solver.swig +++ b/src/linear_solver/python/linear_solver.swig @@ -47,6 +47,7 @@ namespace operations_research { %pythoncode { import numbers +import sys from ortools.linear_solver.linear_solver_natural_api import OFFSET_KEY from ortools.linear_solver.linear_solver_natural_api import inf from ortools.linear_solver.linear_solver_natural_api import LinearExpr @@ -121,8 +122,12 @@ from ortools.linear_solver.linear_solver_natural_api import VariableExpr else: coeffs = expr.GetCoeffs() objective.AddOffset(coeffs.pop(OFFSET_KEY, 0.0)) - for v, c, in coeffs.iteritems(): - objective.SetCoefficient(v, float(c)) + if sys.version_info >= (3, 0): + for v, c, in list(coeffs.items()): + objective.SetCoefficient(v, float(c)) + else: + for v, c, in coeffs.iteritems(): + objective.SetCoefficient(v, float(c)) def Maximize(self, expr): objective = self.Objective() @@ -133,8 +138,12 @@ from ortools.linear_solver.linear_solver_natural_api import VariableExpr else: coeffs = expr.GetCoeffs() objective.AddOffset(coeffs.pop(OFFSET_KEY, 0.0)) - for v, c, in coeffs.iteritems(): - objective.SetCoefficient(v, float(c)) + if sys.version_info >= (3, 0): + for v, c, in list(coeffs.items()): + objective.SetCoefficient(v, float(c)) + else: + for v, c, in coeffs.iteritems(): + objective.SetCoefficient(v, float(c)) } // %pythoncode } diff --git a/src/util/python/proto.swig b/src/util/python/proto.swig index b12fdea7e9..3453d0751a 100644 --- a/src/util/python/proto.swig +++ b/src/util/python/proto.swig @@ -58,9 +58,15 @@ namespace operations_research { %typemap(argout) CppType* const, CppType* { std::string encoded_protobuf; $1->SerializeToString(&encoded_protobuf); +#if defined(PY3) + PyObject* const python_encoded_protobuf = + PyBytes_FromStringAndSize(encoded_protobuf.c_str(), + encoded_protobuf.size()); +#else // PY3 PyObject* const python_encoded_protobuf = PyString_FromStringAndSize(encoded_protobuf.c_str(), encoded_protobuf.size()); +#endif // PY3 if (python_encoded_protobuf != nullptr) { PyObject* const result = PyObject_CallMethod( $input, const_cast("ParseFromString"), @@ -79,8 +85,13 @@ namespace operations_research { if (clss != nullptr) { std::string encoded_protobuf; $1.SerializeToString(&encoded_protobuf); +#if defined(PY3) + PyObject* const python_encoded_protobuf = PyBytes_FromStringAndSize( + encoded_protobuf.c_str(), encoded_protobuf.size()); +#else // PY3 PyObject* const python_encoded_protobuf = PyString_FromStringAndSize( encoded_protobuf.c_str(), encoded_protobuf.size()); +#endif // PY3 PyObject* const result = PyObject_CallMethod( clss, const_cast("FromString"), const_cast("(O)"),