Пример #1
0
static int
AvroDeserializer_init(AvroDeserializer *self, PyObject *args, PyObject *kwds)
{
    int rval;
    PyObject *types = NULL;
    const char *schema_json;
    static char *kwlist[] = {"schema", "types", NULL};

    self->flags = 0;
    self->iface = NULL;

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|O", kwlist,
                                     &schema_json, &types)) {
        return -1;
    }

    rval = avro_schema_from_json(schema_json, 0, &self->schema, NULL);
    if (rval != 0 || self->schema == NULL) {
        PyErr_Format(PyExc_IOError, "Error reading schema: %s",
                     avro_strerror());
        return -1;
    }
    self->flags |= DESERIALIZER_SCHEMA_OK;

    self->iface = avro_generic_class_from_schema(self->schema);
    if (self->iface == NULL) {
        PyErr_SetString(PyExc_IOError,
                        "Error creating generic class interface");
        return -1;
    }

    self->datum_reader = avro_reader_memory(0, 0);
    if (!self->datum_reader) {
        PyErr_NoMemory();
        return -1;
    }
    self->flags |= DESERIALIZER_READER_OK;

    /* copied verbatim from filereader */
    if (types != NULL && PyObject_IsTrue(types)) {
        /* we still haven't incref'ed types here */
        if (Py_TYPE(types) == get_avro_types_type()) {
            Py_INCREF(types);
            self->info.types = types;
        } else {
            self->info.types = PyObject_CallFunctionObjArgs(
                  (PyObject *) get_avro_types_type(), NULL);
            if (self->info.types == NULL) {
                return -1;
            }
            declare_types(&self->info, self->schema);
        }
    } else {
        self->info.types = NULL;
    }

    return 0;
}
Пример #2
0
static PyObject *
create_types_func(PyObject *self, PyObject *args)
{
    int rval;
    avro_schema_t schema;
    PyObject *schema_json;
    ConvertInfo info;
    PyObject *schema_json_bytes;

    if (!PyArg_ParseTuple(args, "O", &schema_json)) {
        return NULL;
    }

    schema_json_bytes = pystring_to_pybytes(schema_json);
    rval = avro_schema_from_json(pybytes_to_chars(schema_json_bytes), 0, &schema, NULL);
    Py_DECREF(schema_json_bytes);

    if (rval != 0 || schema == NULL) {
        PyErr_Format(PyExc_IOError, "Error reading schema: %s", avro_strerror());
        return NULL;
    }

    info.types = PyObject_CallFunctionObjArgs((PyObject *)get_avro_types_type(), NULL);
    if (info.types == NULL) {
        /* XXX: is the exception already set? */
        return NULL;
    }

    declare_types(&info, schema);

    return info.types;
}
Пример #3
0
init_pyavroc(void)
#endif
{
    PyObject* m;

    avroFileReaderType.tp_new = PyType_GenericNew;
    if (PyType_Ready(&avroFileReaderType) < 0) {
        INIT_RETURN(NULL);
    }

    avroFileWriterType.tp_new = PyType_GenericNew;
    if (PyType_Ready(&avroFileWriterType) < 0) {
        INIT_RETURN(NULL);
    }

    avroSerializerType.tp_new = PyType_GenericNew;
    if (PyType_Ready(&avroSerializerType) < 0) {
        INIT_RETURN(NULL);
    }

    avroDeserializerType.tp_new = PyType_GenericNew;
    if (PyType_Ready(&avroDeserializerType) < 0) {
        INIT_RETURN(NULL);
    }

#if PY_MAJOR_VERSION >= 3
    m = PyModule_Create(&moduledef);
#else
    m = Py_InitModule3("_pyavroc", mod_methods,
                       "Python wrapper around Avro-C");
#endif

    Py_INCREF(&avroFileReaderType);
    PyModule_AddObject(m, "AvroFileReader", (PyObject *)&avroFileReaderType);

    Py_INCREF(&avroFileWriterType);
    PyModule_AddObject(m, "AvroFileWriter", (PyObject *)&avroFileWriterType);

    Py_INCREF(&avroSerializerType);
    PyModule_AddObject(m, "AvroSerializer", (PyObject *)&avroSerializerType);

    Py_INCREF(&avroDeserializerType);
    PyModule_AddObject(m, "AvroDeserializer",
                       (PyObject *)&avroDeserializerType);

    PyModule_AddObject(m, "AvroTypes", (PyObject*)get_avro_types_type());

    INIT_RETURN(m);
}
Пример #4
0
static int
AvroFileReader_init(AvroFileReader *self, PyObject *args, PyObject *kwds)
{
    int rval;
    PyObject *pyfile;
    PyObject *types = NULL;
    FILE *file;
    char *schema_json;
    avro_writer_t schema_json_writer;
    size_t len;
    static char *kwlist[] = {"file", "types", NULL};

    self->pyfile = NULL;
    self->flags = 0;
    self->iface = NULL;

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O", kwlist,
                                     &pyfile, &types)) {
        return -1;
    }

    file = PyFile_AsFile(pyfile);

    if (file == NULL) {
        return -1;
    }

    self->pyfile = pyfile;
    Py_INCREF(pyfile);

    if (avro_file_reader_fp(file, "pyfile", 0, &self->reader)) {
        PyErr_Format(PyExc_IOError, "Error opening file: %s", avro_strerror());
        return -1;
    }

    self->flags |= AVROFILE_READER_OK;

    self->schema = avro_file_reader_get_writer_schema(self->reader);

    if (self->schema == NULL) {
        PyErr_Format(PyExc_IOError, "Error reading schema: %s", avro_strerror());
        return -1;
    }

    len = 256;
    do {
        /* XXX horrible loop to get a big enough buffer for schema. */
        len *= 2;
        schema_json = (char *)PyMem_Malloc(len);
        schema_json_writer = avro_writer_memory(schema_json, len);
        rval = avro_schema_to_json(self->schema, schema_json_writer);
        if (!rval) {
            rval = avro_write(schema_json_writer, (void *)"", 1);  /* zero terminate */
            if (!rval) {
                self->schema_json = PyString_FromString(schema_json);
            }
        }
        avro_writer_free(schema_json_writer);
        PyMem_Free(schema_json);
    } while (rval == ENOSPC);

    if (rval) {
        PyErr_Format(PyExc_IOError, "Error saving schema: %s", avro_strerror());
        return -1;
    }

    self->flags |= AVROFILE_SCHEMA_OK;

    self->iface = avro_generic_class_from_schema(self->schema);

    if (self->iface == NULL) {
        PyErr_SetString(PyExc_IOError, "Error creating generic class interface");
        return -1;
    }

    if (types != NULL && PyObject_IsTrue(types)) {
        /* we still haven't incref'ed types here */
        if (Py_TYPE(types) == get_avro_types_type()) {
            Py_INCREF(types);
            self->info.types = types;
        } else {
            self->info.types = PyObject_CallFunctionObjArgs((PyObject *)get_avro_types_type(), NULL);
            if (self->info.types == NULL) {
                return -1;
            }
            declare_types(&self->info, self->schema);
        }
    } else {
        self->info.types = NULL;
    }

    return 0;
}