예제 #1
0
 inline PyObject * operator()(ndarray::EigenView<T,N,C> const & x) const {
     to_python_value< ndarray::Array<T,N,C> const &> array_to_python;
     try {
         numpy::ndarray array(python::detail::new_reference(array_to_python(x.getArray())));
         numpy::matrix matrix(array, array.get_dtype(), false);
         Py_INCREF(matrix.ptr());
         return matrix.ptr();
     } catch (error_already_set & err) {
         handle_exception();
         return NULL;
     }
 }
예제 #2
0
/* returns new reference */
PyObject *
avro_to_python(ConvertInfo *info, avro_value_t *value)
{
    avro_type_t  type = avro_value_get_type(value);
    switch (type) {
    case AVRO_BOOLEAN:
        {
            int  val;
            avro_value_get_boolean(value, &val);
            return PyBool_FromLong(val);
        }

    case AVRO_BYTES:
        {
            const void  *buf;
            size_t  size;
            avro_value_get_bytes(value, &buf, &size);
            /* got pointer into underlying value. no need to free */
            return PyString_FromStringAndSize(buf, size);
        }

    case AVRO_DOUBLE:
        {
            double  val;
            avro_value_get_double(value, &val);
            return PyFloat_FromDouble(val);
        }

    case AVRO_FLOAT:
        {
            float  val;
            avro_value_get_float(value, &val);
            return PyFloat_FromDouble(val);
        }

    case AVRO_INT32:
        {
            int32_t  val;
            avro_value_get_int(value, &val);
            return PyInt_FromLong(val);
        }

    case AVRO_INT64:
        {
            int64_t  val;
            avro_value_get_long(value, &val);
            return PyLong_FromLongLong((PY_LONG_LONG)val);
        }

    case AVRO_NULL:
        {
            avro_value_get_null(value);
            Py_INCREF(Py_None);
            return Py_None;
        }

    case AVRO_STRING:
        {
            /* TODO: Convert the UTF-8 to the current
             * locale's character set */
            const char  *buf;
            size_t  size;
            avro_value_get_string(value, &buf, &size);
            /* For strings, size includes the NUL terminator. */
            return PyString_FromStringAndSize(buf, size - 1);
        }

    case AVRO_ARRAY:
        return array_to_python(info, value);

    case AVRO_ENUM:
        if (info->types == NULL) {
            /* just an int */
            return enum_to_python(info, value);
        } else {
            /* a fancy object */
            return enum_to_python_object(info, value);
        }

    case AVRO_FIXED:
        {
            const void  *buf;
            size_t  size;
            avro_value_get_fixed(value, &buf, &size);
            return PyString_FromStringAndSize((const char *)buf, size);
        }

    case AVRO_MAP:
        return map_to_python(info, value);

    case AVRO_RECORD:
        if (info->types == NULL) {
            /* just a dictionary */
            return record_to_python(info, value);
        } else {
            /* a fancy object */
            return record_to_python_object(info, value);
        }

    case AVRO_UNION:
        return union_to_python(info, value);

    default:
        return NULL;
    }
}