Commit 2cf9073f authored by Kirill Smelkov's avatar Kirill Smelkov

bigfile/py: Raise MemoryError for ENOMEM errno

Currently we always raise RuntimeError for problems, which is
more-or-less ok for humans, but soon we'll need to distinguish "no
memory" errors from other error conditions in upper layers in code.

Introduce helper function for choosing appropriate exception type for an
error - MemoryError for when errno=ENOMEM and RuntimeError otherwise,
and use it where appropriate.

( Unfortunately Python does not provide such a helper... )
parent 6fdde936
...@@ -106,6 +106,10 @@ typedef struct PyBigFile PyBigFile; ...@@ -106,6 +106,10 @@ typedef struct PyBigFile PyBigFile;
#define PyType_New(type, typeobj, args) \ #define PyType_New(type, typeobj, args) \
( (type *)PyObject_CallObject((PyObject *)(typeobj), args) ) ( (type *)PyObject_CallObject((PyObject *)(typeobj), args) )
/* like PyErr_SetFromErrno(exc), but chooses exception type automatically */
static void XPyErr_SetFromErrno(void);
/************ /************
* PyVMA * * PyVMA *
************/ ************/
...@@ -257,7 +261,7 @@ PyFunc(pyfileh_mmap, "mmap(pgoffset, pglen) - map fileh part into memory") ...@@ -257,7 +261,7 @@ PyFunc(pyfileh_mmap, "mmap(pgoffset, pglen) - map fileh part into memory")
if (err) { if (err) {
Py_DECREF(pyfileh); Py_DECREF(pyfileh);
Py_DECREF(pyvma); // XXX ok wrt delete pyvma->vma ? Py_DECREF(pyvma); // XXX ok wrt delete pyvma->vma ?
PyErr_SetFromErrno(PyExc_RuntimeError); XPyErr_SetFromErrno();
return NULL; return NULL;
} }
...@@ -656,7 +660,7 @@ pyfileh_open(PyObject *pyfile0, PyObject *args) ...@@ -656,7 +660,7 @@ pyfileh_open(PyObject *pyfile0, PyObject *args)
Py_INCREF(pyfile); Py_INCREF(pyfile);
err = fileh_open(pyfileh, pyfile, ram); err = fileh_open(pyfileh, pyfile, ram);
if (err) { if (err) {
PyErr_SetFromErrno(PyExc_RuntimeError); XPyErr_SetFromErrno();
Py_DECREF(pyfile); Py_DECREF(pyfile);
Py_DECREF(pyfileh); Py_DECREF(pyfileh);
return NULL; return NULL;
...@@ -798,3 +802,17 @@ PyInit__bigfile(void) ...@@ -798,3 +802,17 @@ PyInit__bigfile(void)
#endif #endif
_init_bigfile(); _init_bigfile();
} }
static void
XPyErr_SetFromErrno(void)
{
PyObject *exc;
switch(errno) {
case ENOMEM: exc = PyExc_MemoryError; break;
default: exc = PyExc_RuntimeError;
}
PyErr_SetFromErrno(exc);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment