write/read array works

This commit is contained in:
Christian Zimmermann 2024-02-09 00:30:51 +01:00
parent a06bb976a0
commit 5a5e2fe59b
2 changed files with 23 additions and 9 deletions

View file

@ -24,7 +24,7 @@ namespace CNORXZ
virtual void readFile(cer::Format f, const String& fname) const = 0;
#endif
virtual int typenum() const = 0;
virtual const void* data() const = 0;
virtual void* data() const = 0;
virtual SizeT datasize() const = 0;
};
@ -92,8 +92,8 @@ namespace CNORXZ
}
#endif
virtual int typenum() const override final { return Typenum<T>::value; }
virtual const void* data() const override final
{ return reinterpret_cast<const void*>( mArr->data() ); }
virtual void* data() const override final
{ return reinterpret_cast<void*>( mArr->data() ); }
virtual SizeT datasize() const override final { return size() * sizeof(T); }
};
@ -108,6 +108,9 @@ struct PyCxArrayB
int PyCxArrayB_init(PyCxArrayB* self, PyObject* args, PyObject* kwds);
void PyCxArrayB_dealloc(PyCxArrayB* self);
PyObject* PyCxArrayB_size(PyCxArrayB* self);
PyObject* PyCxArrayB_range(PyCxArrayB* self);
PyObject* PyCxArrayB_writeFile(PyCxArrayB* self, PyObject* args, PyObject* kwds);
PyObject* PyCxArrayB_readFile(PyCxArrayB* self, PyObject* args, PyObject* kwds);
PyTypeObject* PyCxArrayBType_init();
extern PyMethodDef PyCxArrayB_methods[];

View file

@ -77,7 +77,9 @@ Format formatFromString(const char* fstr)
return f;
}
void PyCxArrayB_writeFile(PyCxArrayB* self, PyObject* args, PyObject* kwds)
#ifdef HAVE_CEREAL
PyObject* PyCxArrayB_writeFile(PyCxArrayB* self, PyObject* args, PyObject* kwds)
{
if(not self->ptrObj->range()){
PyErr_SetString(PyExc_RuntimeError, "array not initialized");
@ -87,26 +89,29 @@ void PyCxArrayB_writeFile(PyCxArrayB* self, PyObject* args, PyObject* kwds)
char* format = NULL;
if(not PyArg_ParseTupleAndKeywords(args, kwds, "s|s", kwlist, &fname, &format)){
return;
return NULL;
}
const String fn(fname);
const Format f = formatFromString(format);
self->ptrObj->writeFile(f, fn);
Py_RETURN_NONE;
}
void PyCxArrayB_readFile(PyCxArrayB* self, PyObject* args, PyObject* kwds)
PyObject* PyCxArrayB_readFile(PyCxArrayB* self, PyObject* args, PyObject* kwds)
{
static char* kwlist[] = { "format", "filename" , NULL };
char* fname = NULL;
char* format = NULL;
if(not PyArg_ParseTupleAndKeywords(args, kwds, "s|s", kwlist, &fname, &format)){
return;
return NULL;
}
const String fn(fname);
const Format f = formatFromString(format);
self->ptrObj->readFile(f, fn);
Py_RETURN_NONE;
}
#endif
PyObject* PyCxArrayB_npa(PyCxArrayB* self)
{
@ -117,6 +122,10 @@ PyObject* PyCxArrayB_npa(PyCxArrayB* self)
}
const SizeT d = self->ptrObj->range()->dim();
std::vector<npy_intp> dims(d);
for(SizeT i = 0; i != dims.size(); ++i){
dims[i] = self->ptrObj->range()->sub(i)->size();
}
// TODO: Do the following for mutable arrays!!!
/*
Py_INCREF(self); // keep data alive
PyObject* o = PyArray_SimpleNewFromData
@ -129,7 +138,9 @@ PyObject* PyCxArrayB_npa(PyCxArrayB* self)
//const npy_intp s = PyArray_NBYTES(o);
const SizeT s = static_cast<SizeT>( PyArray_NBYTES(o) );
if(s != self->ptrObj->datasize()) {
PyErr_SetString(PyExc_RuntimeError, "lib error");
PyErr_Format(PyExc_RuntimeError, "lib error %d vs %d", s, self->ptrObj->datasize());
//PyErr_SetString(PyExc_RuntimeError, "lib error");
return NULL;
}
memcpy( data, self->ptrObj->data(), self->ptrObj->datasize() );
return o;
@ -156,7 +167,7 @@ PyTypeObject* PyCxArrayBType_init()
PyCxArrayBType.tp_methods = PyCxArrayB_methods;
PyCxArrayBType.tp_init = (initproc) PyCxArrayB_init;
//import_array();
import_array();
if(PyType_Ready(&PyCxArrayBType) < 0) {
return NULL;