diff options
author | Panu Matilainen <pmatilai@redhat.com> | 2011-07-01 12:08:04 +0300 |
---|---|---|
committer | Panu Matilainen <pmatilai@redhat.com> | 2011-07-01 12:12:41 +0300 |
commit | f4a07884848369513f8e258d32ce2bf4c229206e (patch) | |
tree | f855b3e6d11d76bd402db35b6f590dbb4efbbf32 /python | |
parent | cb84c0db42db105116072cb71d89152e885e2e2c (diff) | |
download | librpm-tizen-f4a07884848369513f8e258d32ce2bf4c229206e.tar.gz librpm-tizen-f4a07884848369513f8e258d32ce2bf4c229206e.tar.bz2 librpm-tizen-f4a07884848369513f8e258d32ce2bf4c229206e.zip |
Fix/sanitize rpm.fd python object creation
- FD's are not really immutable, move the initialization work into
tp_init and use PyType_GenericNew for tp_new since we're not
doing anything special there.
- Remove half a dozen different unnecessary exit points
Diffstat (limited to 'python')
-rw-r--r-- | python/rpmfd-py.c | 36 |
1 files changed, 14 insertions, 22 deletions
diff --git a/python/rpmfd-py.c b/python/rpmfd-py.c index f3536eaf3..df9b6411e 100644 --- a/python/rpmfd-py.c +++ b/python/rpmfd-py.c @@ -54,20 +54,18 @@ static FD_t openPath(const char *path, const char *mode, const char *flags) return fd; } -static PyObject *rpmfd_new(PyTypeObject *subtype, - PyObject *args, PyObject *kwds) +static int rpmfd_init(rpmfdObject *s, PyObject *args, PyObject *kwds) { char *kwlist[] = { "obj", "mode", "flags", NULL }; char *mode = "r"; char *flags = "ufdio"; PyObject *fo = NULL; - rpmfdObject *s = NULL; FD_t fd = NULL; int fdno; if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|ss", kwlist, &fo, &mode, &flags)) - return NULL; + return -1; if (PyBytes_Check(fo)) { fd = openPath(PyBytes_AsString(fo), mode, flags); @@ -79,31 +77,25 @@ static PyObject *rpmfd_new(PyTypeObject *subtype, #else rc = utf8FromPyObject(fo, &enc); #endif - if (rc == 0) - return NULL; - - fd = openPath(PyBytes_AsString(enc), mode, flags); - Py_DECREF(enc); - + if (rc) { + fd = openPath(PyBytes_AsString(enc), mode, flags); + Py_DECREF(enc); + } } else if ((fdno = PyObject_AsFileDescriptor(fo)) >= 0) { fd = fdDup(fdno); } else { PyErr_SetString(PyExc_TypeError, "path or file object expected"); - return NULL; } - if (Ferror(fd)) { + if (fd != NULL) { + /* TODO: remember our filename, mode & flags */ + Fclose(s->fd); /* in case __init__ was called again */ + s->fd = fd; + } else { PyErr_SetString(PyExc_IOError, Fstrerror(fd)); - return NULL; } - if ((s = (rpmfdObject *)subtype->tp_alloc(subtype, 0)) == NULL) { - Fclose(fd); - return NULL; - } - /* TODO: remember our filename, mode & flags */ - s->fd = fd; - return (PyObject*) s; + return (fd == NULL) ? -1 : 0; } static PyObject *do_close(rpmfdObject *s) @@ -346,9 +338,9 @@ PyTypeObject rpmfd_Type = { 0, /* tp_descr_get */ 0, /* tp_descr_set */ 0, /* tp_dictoffset */ - (initproc)0, /* tp_init */ + (initproc)rpmfd_init, /* tp_init */ (allocfunc)0, /* tp_alloc */ - (newfunc) rpmfd_new, /* tp_new */ + PyType_GenericNew, /* tp_new */ (freefunc)0, /* tp_free */ 0, /* tp_is_gc */ }; |