reader accept Path obj

This commit is contained in:
Erik Frojdh
2023-06-02 09:26:12 +02:00
parent 96452488bf
commit 81ccef6e35
3 changed files with 35 additions and 6 deletions

View File

@ -17,22 +17,31 @@ typedef struct {
static int ClusterFileReader_init(ClusterFileReader *self, PyObject *args,
PyObject *Py_UNUSED(kwds)) {
// Parse file name
// Parse file name, accepts string or pathlike objects
const char *fname = NULL;
if (!PyArg_ParseTuple(args, "s", &fname))
PyObject* buf;
Py_ssize_t len;
if (!PyArg_ParseTuple(args, "O&", PyUnicode_FSConverter, &buf))
return -1;
PyBytes_AsStringAndSize(buf, &fname, &len);
self->fp = fopen(fname, "rb");
self->n_left = 0;
//Keep the return code to not return before releasing buffer
int rc = 0;
// Raise python exception using information from errno
if (self->fp == NULL) {
PyErr_SetFromErrnoWithFilename(PyExc_OSError, fname);
return -1;
rc = -1;
}
//Release buffer
Py_DECREF(buf)
// Success
return 0;
// Success or fail
return rc;
}
// Custom destructor to make sure we close the file