Skip to content

Commit

Permalink
libkd: raise a proper exception when tree_open() fails
Browse files Browse the repository at this point in the history
  • Loading branch information
dstndstn committed Apr 15, 2022
1 parent 9574230 commit 2638e2a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion __init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.89-5-ga3fbf4eb'
__version__ = '0.89-11-g95742305'
23 changes: 19 additions & 4 deletions libkd/pyspherematch.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "dualtree_nearestneighbour.h"
#include "bl.h"
#include "mathutil.h"
#include "errors.h"

typedef struct {
PyObject_HEAD
Expand Down Expand Up @@ -167,8 +168,6 @@ static int KdTree_init(KdObject *self, PyObject *args, PyObject *keywords) {
if (fnbytes == NULL)
return -1;
filename = PyBytes_AsString(fnbytes);
self->kd = kdtree_fits_read(filename, treename, NULL);
Py_DECREF(fnbytes);
#else
if (n == 1) {
if (!PyArg_ParseTuple(args, "s", &filename))
Expand All @@ -177,10 +176,26 @@ static int KdTree_init(KdObject *self, PyObject *args, PyObject *keywords) {
if (!PyArg_ParseTuple(args, "ss", &filename, &treename))
return -1;
}
self->kd = kdtree_fits_read(filename, treename, NULL);
#endif
if (!self->kd)

char* errstr = NULL;
errors_start_logging_to_string();
self->kd = kdtree_fits_read(filename, treename, NULL);
errstr = errors_stop_logging_to_string("\n");

if (!self->kd) {
if (fnbytes && ((errno == ENOENT) || (errno == EACCES) || (errno == EEXIST))) {
PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, fnbytes);
Py_DECREF(fnbytes);
return -1;
}
PyErr_SetString(PyExc_ValueError, errstr);
if (fnbytes)
Py_DECREF(fnbytes);
return -1;
}
if (fnbytes)
Py_DECREF(fnbytes);
return 0;
}

Expand Down

0 comments on commit 2638e2a

Please sign in to comment.