From daeeb19282d99866fa07d5b46737021a4ec3b1c2 Mon Sep 17 00:00:00 2001 From: stevenj Date: Sun, 6 Jun 2010 23:30:53 -0400 Subject: [PATCH] python plugin compiles (untested) darcs-hash:20100607033053-c8de0-9dd5b50f08cc6ae4708bf76a1dd75dd42b84aa2b.gz --- configure.ac | 18 ++++++++++++++++++ swig/Makefile.am | 16 ++++++++++++++-- swig/nlopt-guile.i | 2 +- swig/nlopt-python.i | 10 ++++++---- 4 files changed, 39 insertions(+), 7 deletions(-) diff --git a/configure.ac b/configure.ac index 760e941..4782d6c 100644 --- a/configure.ac +++ b/configure.ac @@ -123,6 +123,24 @@ AC_SUBST(GUILE_CPPFLAGS) AC_SUBST(GUILE_LIBS) AM_CONDITIONAL(WITH_GUILE, test x"$GUILE_CONFIG" != "xunknown") +dnl Python: +AM_PATH_PYTHON([],[have_python=yes],[have_python=no]) +if test $have_python = yes; then + pdir=`dirname $pythondir | sed 's,/lib/,/include/,'` + pdir=`eval echo $pdir` + AC_MSG_CHECKING([for Python include directory]) + for PYTHON_INCLUDE_DIR in "$pdir" /usr/include/python$PYTHON_VERSION /usr/local/include/python$PYTHON_VERSION unknown; do + test -d "$PYTHON_INCLUDE_DIR" && break + done + AC_MSG_RESULT([$PYTHON_INCLUDE_DIR]) + if test "x$PYTHON_INCLUDE_DIR" = xunknown; then + AC_MSG_WARN([disabling Python wrappers]) + have_python=no + fi + AC_SUBST(PYTHON_INCLUDE_DIR) +fi +AM_CONDITIONAL(WITH_PYTHON, test x"$have_python" = "xyes") + dnl ----------------------------------------------------------------------- dnl Compiling Octave plug-in diff --git a/swig/Makefile.am b/swig/Makefile.am index 74c0619..663442c 100644 --- a/swig/Makefile.am +++ b/swig/Makefile.am @@ -1,7 +1,7 @@ SWIG_SRC = nlopt.i nlopt-exceptions.i nlopt-enum-renames.i -EXTRA_DIST = $(SWIG_SRC) nlopt-guile.i nlopt-python.i nlopt.scm.in +EXTRA_DIST = $(SWIG_SRC) nlopt-guile.i nlopt-python.i nlopt.scm.in nlopt.py -BUILT_SOURCES = nlopt-guile.cpp nlopt-enum-renames.i nlopt.scm.in +BUILT_SOURCES = nlopt-guile.cpp nlopt-python.cpp nlopt-enum-renames.i nlopt.scm.in HDR = $(top_builddir)/api/nlopt.hpp @@ -19,10 +19,22 @@ guile_DATA = nlopt.scm guilelib = libnlopt@NLOPT_SUFFIX@_guile.la endif +############################################################################## +# Python wrapper + +libnlopt_python_la_SOURCES = nlopt-python.cpp +libnlopt_python_la_LIBADD = ../libnlopt@NLOPT_SUFFIX@.la +libnlopt_python_la_LDFLAGS = -no-undefined -version-info @SHARED_VERSION_INFO@ +libnlopt_python_la_CPPFLAGS = -I$(PYTHON_INCLUDE_DIR) -I$(top_builddir)/api +if WITH_PYTHON +python_PYTHON = nlopt.py +pyexec_LTLIBRARIES = libnlopt_python.la +endif ############################################################################## + lib_LTLIBRARIES = $(guilelib) ############################################################################## diff --git a/swig/nlopt-guile.i b/swig/nlopt-guile.i index 1978a13..f70b6e0 100644 --- a/swig/nlopt-guile.i +++ b/swig/nlopt-guile.i @@ -42,7 +42,7 @@ static double func_guile(unsigned n, const double *x, double *grad, void *f) { $3 = free_guilefunc; $4 = dup_guilefunc; } -%typecheck(SWIG_TYPECHECK_POINTER)(nlopt::vfunc vf, void *f_data) { +%typecheck(SWIG_TYPECHECK_POINTER)(nlopt::vfunc vf, void *f_data, nlopt_munge md, nlopt_munge mc) { $1 = SCM_NFALSEP(scm_procedure_p($input)); } diff --git a/swig/nlopt-python.i b/swig/nlopt-python.i index cd46acd..cbfe99c 100644 --- a/swig/nlopt-python.i +++ b/swig/nlopt-python.i @@ -27,7 +27,7 @@ (PyArrayObject* array=NULL, int is_new_object=0, std::vector arrayv) { npy_intp size[1] = { -1 }; - array = obj_to_array_allow_conversion($input, DATA_TYPECODE, &is_new_object); + array = obj_to_array_allow_conversion($input, NPY_DOUBLE, &is_new_object); if (!array || !require_dimensions(array, 1) || !require_size(array, size, 1)) SWIG_fail; arrayv = std::vector(array_size(array,0)); @@ -64,8 +64,10 @@ static void *dup_pyfunc(void *p) { Py_INCREF((PyObject*) p); return p; } static double func_python(unsigned n, const double *x, double *grad, void *f) { - npy_intp sz = npy_intp(n), sz0 = 0; - PyObject *xpy = PyArray_SimpleNewFromData(1, &sz, NPY_DOUBLE, x); + npy_intp sz = npy_intp(n), sz0 = 0, stride1 = 1; + PyObject *xpy = PyArray_New(&PyArray_Type, 1, &sz, NPY_DOUBLE, &stride1, + const_cast(x), // not NPY_WRITEABLE + 0, NPY_C_CONTIGUOUS | NPY_ALIGNED, NULL); PyObject *gradpy = grad ? PyArray_SimpleNew(1, &sz0, NPY_DOUBLE) : PyArray_SimpleNewFromData(1, &sz, NPY_DOUBLE, grad); @@ -94,6 +96,6 @@ static double func_python(unsigned n, const double *x, double *grad, void *f) $3 = free_pyfunc; $4 = dup_pyfunc; } -%typecheck(SWIG_TYPECHECK_POINTER)(nlopt::func f, void *f_data) { +%typecheck(SWIG_TYPECHECK_POINTER)(nlopt::func f, void *f_data, nlopt_munge md, nlopt_munge mc) { $1 = PyCallable_Check($input); } -- 2.30.2