changeset 84:8ee00dc40ae5 numpyconf

Merge with lp:pytave/trunk.
author David <david@stacey>
date Sun, 20 Sep 2009 15:02:30 +0200
parents 8145ecfecfb9 (current diff) e1593574ee97 (diff)
children 891ec70aede9
files pytave.cc
diffstat 5 files changed, 77 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Sat Sep 12 14:52:14 2009 +0200
+++ b/ChangeLog	Sun Sep 20 15:02:30 2009 +0200
@@ -1,3 +1,35 @@
+2009-09-18  Jaroslav Hajek  <highegg@gmail.com>
+
+	* package/pytave.py: Safer check for interactive (mod_wsgi in
+	Apache!)
+	
+2009-09-18  Jaroslav Hajek  <highegg@gmail.com>
+	
+	* python_to_octave.cc (copy_pyarrobj_to_octarray,
+	pyarr_to_octvalue): Support 0D case.
+
+2009-09-18  Jaroslav Hajek  <highegg@gmail.com>
+
+	* package/pytave.py (simplify): Improve NumPy compatibility.
+
+2009-09-17  Jaroslav Hajek  <highegg@gmail.com>
+
+	* pytave.cc: Move #include of arrayobjectdefs after pytavedefs to
+	ensure config.h is included.
+
+2009-09-16  Jaroslav Hajek  <highegg@gmail.com>
+
+	* package/pytave.py (load_package, unload_package): New funcs.
+	* octave_to_python.cc: Workaround for return values bug in 
+	Octave <= 3.2.3.
+
+2009-09-15  Jaroslav Hajek  <highegg@gmail.com>
+
+	* octave_to_python.cc: Move #include of arrayobjectdefs after
+	pytavedefs to ensure config.h is included.
+	* python_to_octave.cc: Ditto.
+	* setup.py.in: Undo leaked local paths.
+
 2009-07-03  David Grundberg  <individ@acc.umu.se>
 
 	* pytave.cc: Sorted includes.
--- a/octave_to_python.cc	Sat Sep 12 14:52:14 2009 +0200
+++ b/octave_to_python.cc	Sun Sep 20 15:02:30 2009 +0200
@@ -18,7 +18,6 @@
  *  along with Pytave.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include "arrayobjectdefs.h"
 #include <boost/python.hpp>
 #include <boost/python/numeric.hpp>
 #include <boost/type_traits/integral_constant.hpp>
@@ -30,6 +29,7 @@
 
 #include <iostream>
 #include "pytavedefs.h"
+#include "arrayobjectdefs.h"
 #include "exceptions.h"
 #include "octave_to_python.h"
 
@@ -306,6 +306,13 @@
                            const octave_value_list &octave_list) {
       boost::python::list seq;
       int length = octave_list.length();
+
+      // FIXME: due to bugs in Octave 3.2.3 and earlier, lists returned from
+      // eval_string and feval may be padded by trailing undefined values.
+      // Fix is already upstream, so this may be eventually removed.
+      while (length > 0 && octave_list(length-1).is_undefined())
+         length--;
+
       for (int i = 0; i < length; i++) {
          boost::python::object py_object;
          octvalue_to_pyobj(py_object, octave_list(i));
--- a/package/pytave.py	Sat Sep 12 14:52:14 2009 +0200
+++ b/package/pytave.py	Sun Sep 20 15:02:30 2009 +0200
@@ -26,7 +26,13 @@
 import sys
 
 arg0 = sys.argv[0]
-interactive = sys.stdin.isatty() and (arg0 == '' or arg0 == '-')
+# Some web application packages, such as mod_wsgi for Apache,
+# completely restrict access to stdin, including an isatty() query.
+# Hence, if an error occurs, we'll stay safe.
+try:
+    interactive = sys.stdin.isatty() and (arg0 == '' or arg0 == '-')
+except IOError:
+    interactive = False
 
 _pytave.init(interactive)
 (OctaveError, ValueConvertError, ObjectConvertError, ParseError, \
@@ -187,13 +193,21 @@
     1xN and 0x0 character arrays to strings, 1xN, Nx1 and 0x0 cell
     arrays to lists, and strip scalar dicts. It will work recursively."""
 
+    def get_typecode(array):
+	"""gets the typecode from both Numeric and NumPy array"""
+	try:
+	    tc = array.typecode()
+	except:
+	    tc = array.dtype.char
+	return tc
+
     def vectordims(dims,column_allowed = True):
 	return (len(dims) == 2 and 
 		((dims[0] == 1 or (column_allowed and dims[1] == 1)) or
 		(dims[0] == 0 and dims[1] == 0)))
 
     if isinstance(obj,Numeric.ArrayType):
-	tc = obj.typecode()
+	tc = get_typecode(obj)
 	if tc == 'O':
 	    if vectordims(Numeric.shape(obj)):
 		return map(simplify,narrowlist(obj))
@@ -203,7 +217,7 @@
 	else:
 	    dims = Numeric.shape(obj)
 	    if dims == (1,1):
-		return obj.toscalar()
+		return obj[0,0]
 	    elif vectordims(dims):
 		return Numeric.ravel(obj)
     elif isinstance(obj,dict):
@@ -232,6 +246,14 @@
 	"""See Octave documentation"""
 	return _pytave.feval(1, "path", paths)[0]
 
+def load_package(pkg_name):
+    """Equivalent to pkg load. See Octave documentation."""
+    return _pytave.feval(0, "pkg", ("load", pkg_name))
+
+def unload_package(pkg_name):
+    """Equivalent to pkg unload. See Octave documentation."""
+    return _pytave.feval(0, "pkg", ("unload", pkg_name))
+
 class _VariablesDict(UserDict.DictMixin):
 	def __init__(self, global_variables, native=False):
 		self.global_variables = global_variables
--- a/pytave.cc	Sat Sep 12 14:52:14 2009 +0200
+++ b/pytave.cc	Sun Sep 20 15:02:30 2009 +0200
@@ -18,8 +18,6 @@
  *  along with Pytave.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#define PYTAVE_DO_DECLARE_SYMBOL
-#include "arrayobjectdefs.h"
 #include <boost/python.hpp>
 #include <boost/python/numeric.hpp>
 
@@ -44,6 +42,8 @@
 
 #include "pytavedefs.h"
 
+#define PYTAVE_DO_DECLARE_SYMBOL
+#include "arrayobjectdefs.h"
 #include "exceptions.h"
 #include "octave_to_python.h"
 #include "python_to_octave.h"
--- a/python_to_octave.cc	Sat Sep 12 14:52:14 2009 +0200
+++ b/python_to_octave.cc	Sun Sep 20 15:02:30 2009 +0200
@@ -21,7 +21,6 @@
 #include <iostream>
 #include <boost/python.hpp>
 #include <boost/python/numeric.hpp>
-#include "arrayobjectdefs.h"
 #include <boost/type_traits/integral_constant.hpp>
 #undef HAVE_STAT /* both boost.python and octave defines HAVE_STAT... */
 #include <octave/oct.h>
@@ -31,6 +30,7 @@
 #include <octave/ov.h>
 
 #include "pytavedefs.h"
+#include "arrayobjectdefs.h"
 #include "exceptions.h"
 
 using namespace std;
@@ -56,6 +56,8 @@
                = *(PythonPrimitive*)
                &ptr[offset + i*pyarr->strides[dimension]];
          }
+      } else if (pyarr->nd == 0) {
+         matrix.elem(0) = *(PythonPrimitive*) ptr;
       } else {
          for (int i = 0; i < pyarr->dimensions[dimension]; i++) {
             copy_pyarrobj_to_octarray<PythonPrimitive, OctaveBase>(
@@ -85,6 +87,10 @@
             pyobj_to_octvalue (matrix.elem(matindex + i*matstride), 
                                object(handle<PyObject> (borrowed (pobj))));
          }
+      } else if (pyarr->nd == 0) {
+            PyObject *pobj = *(PyObject **) ptr;
+            pyobj_to_octvalue (matrix.elem(0), 
+                               object(handle<PyObject> (borrowed (pobj))));
       } else {
          for (int i = 0; i < pyarr->dimensions[dimension]; i++) {
             copy_pyarrobj_to_octarray<PyObject *, Cell>(
@@ -204,10 +210,11 @@
 
    static void pyarr_to_octvalue(octave_value &octvalue,
                                  const PyArrayObject *pyarr) {
-      if (pyarr->nd < 1)
-         throw object_convert_exception("Less than 1 dimensions not supported");
       dim_vector dims;
       switch (pyarr->nd) {
+         case 0:
+            dims = dim_vector (1, 1);
+            break;
          case 1:
             // Always make PyArray vectors row vectors.
             dims = dim_vector(1, pyarr->dimensions[0]);