private final static int[] _shapeOf(final PyObject seq, final int depth) {
        int items;
        // Take care of special cases (strings, nonsequence, and empty
        // sequence).
        if (seq instanceof PyString) { return new int[depth]; }
        try {
            items = seq.__len__();
        } catch (final Throwable e) {
            return new int[depth];
        }
        if (items == 0) { return new int[depth + 1]; }
        // Loop over sequence elements and determine shape.
        final int[] shape = PyMultiarray
                ._shapeOf(seq.__getitem__(0), depth + 1);
        shape[depth] = items;
        for (int i = 1; i < items; i++) {
            final int[] shape2 = PyMultiarray._shapeOf(
                    seq.__getitem__(i),
                    depth + 1);
            if (shape.length != shape2.length) { throw Py
                    .ValueError("malformed array"); }
            for (int j = depth + 1; j < shape.length; j++) {
                if (shape[j] != shape2[j]) { throw Py
                        .ValueError("malformed array"); }
            }
        }
        return shape;
    }