Line numbers for OOM exceptions in the unrar module

This commit is contained in:
Kovid Goyal 2013-09-03 13:21:47 +05:30
parent a4f2d0d664
commit cca7c89f24

View File

@ -30,6 +30,10 @@ static int wcscpy_s(wchar_t *dest, size_t sz, const wchar_t *src) {
} }
#endif #endif
#define STRFY(x) #x
#define STRFY2(x) STRFY(x)
#define NOMEM PyErr_SetString(PyExc_MemoryError, "Out of memory at line number: " STRFY2(__LINE__))
static wchar_t *unicode_to_wchar(PyObject *o) { static wchar_t *unicode_to_wchar(PyObject *o) {
wchar_t *buf; wchar_t *buf;
Py_ssize_t len; Py_ssize_t len;
@ -37,7 +41,7 @@ static wchar_t *unicode_to_wchar(PyObject *o) {
if (!PyUnicode_Check(o)) {PyErr_Format(PyExc_TypeError, "The python object must be a unicode object"); return NULL;} if (!PyUnicode_Check(o)) {PyErr_Format(PyExc_TypeError, "The python object must be a unicode object"); return NULL;}
len = PyUnicode_GET_SIZE(o); len = PyUnicode_GET_SIZE(o);
buf = (wchar_t *)calloc(len+2, sizeof(wchar_t)); buf = (wchar_t *)calloc(len+2, sizeof(wchar_t));
if (buf == NULL) { PyErr_NoMemory(); return NULL; } if (buf == NULL) { NOMEM; return NULL; }
len = PyUnicode_AsWideChar((PyUnicodeObject*)o, buf, len); len = PyUnicode_AsWideChar((PyUnicodeObject*)o, buf, len);
if (len == -1) { free(buf); PyErr_Format(PyExc_TypeError, "Invalid python unicode object."); return NULL; } if (len == -1) { free(buf); PyErr_Format(PyExc_TypeError, "Invalid python unicode object."); return NULL; }
return buf; return buf;
@ -47,7 +51,7 @@ static PyObject *wchar_to_unicode(const wchar_t *o) {
PyObject *ans; PyObject *ans;
if (o == NULL) return NULL; if (o == NULL) return NULL;
ans = PyUnicode_FromWideChar(o, wcslen(o)); ans = PyUnicode_FromWideChar(o, wcslen(o));
if (ans == NULL) PyErr_NoMemory(); if (ans == NULL) NOMEM;
return ans; return ans;
} }
@ -237,14 +241,14 @@ RAR_init(RARArchive *self, PyObject *args, PyObject *kwds) {
self->Cmd.UserData = (LPARAM)pycallback; self->Cmd.UserData = (LPARAM)pycallback;
self->archive = new (std::nothrow) PyArchive(file, cname, &self->Cmd); self->archive = new (std::nothrow) PyArchive(file, cname, &self->Cmd);
if (self->archive == NULL) { PyErr_NoMemory(); return -1; } if (self->archive == NULL) { NOMEM; return -1; }
free(cname); free(cname);
self->DataIO.UnpArcSize=self->archive->FileLength(); self->DataIO.UnpArcSize=self->archive->FileLength();
self->DataIO.UnpVolume=false; self->DataIO.UnpVolume=false;
self->Unp = new (std::nothrow) Unpack(&self->DataIO); self->Unp = new (std::nothrow) Unpack(&self->DataIO);
if (self->Unp == NULL) { PyErr_NoMemory(); return -1; } if (self->Unp == NULL) { NOMEM; return -1; }
self->file_count = 0; self->file_count = 0;
try { try {
@ -259,7 +263,7 @@ RAR_init(RARArchive *self, PyObject *args, PyObject *kwds) {
Array<byte> cdata; Array<byte> cdata;
if (self->archive->GetComment(&cdata, NULL)) { if (self->archive->GetComment(&cdata, NULL)) {
self->comment = PyBytes_FromStringAndSize((const char*)&cdata[0], cdata.Size()); self->comment = PyBytes_FromStringAndSize((const char*)&cdata[0], cdata.Size());
if (self->comment == NULL) { PyErr_NoMemory(); return -1; } if (self->comment == NULL) { NOMEM; return -1; }
} else { } else {
self->comment = Py_None; self->comment = Py_None;
Py_INCREF(self->comment); Py_INCREF(self->comment);
@ -275,7 +279,7 @@ RAR_init(RARArchive *self, PyObject *args, PyObject *kwds) {
return -1; return -1;
} catch (std::bad_alloc) { } catch (std::bad_alloc) {
if (!PyErr_Occurred()) if (!PyErr_Occurred())
PyErr_NoMemory(); NOMEM;
return -1; return -1;
} }
@ -328,7 +332,7 @@ RAR_current_item(RARArchive *self, PyObject *args) {
return NULL; return NULL;
} catch (std::bad_alloc) { } catch (std::bad_alloc) {
if (!PyErr_Occurred()) if (!PyErr_Occurred())
PyErr_NoMemory(); NOMEM;
return NULL; return NULL;
} }
@ -451,7 +455,7 @@ RAR_process_item(RARArchive *self, PyObject *args) {
return NULL; return NULL;
} catch (std::bad_alloc) { } catch (std::bad_alloc) {
if (!PyErr_Occurred()) if (!PyErr_Occurred())
PyErr_NoMemory(); NOMEM;
return NULL; return NULL;
} }
Py_RETURN_NONE; Py_RETURN_NONE;