From b7c72951fe850caac5bd3c82562f331582079b79 Mon Sep 17 00:00:00 2001 From: ari edelkind Date: Tue, 17 Sep 2013 14:20:33 -0400 Subject: [PATCH] Better handling of files without xattrs Return an empty list immediately if a file has no xattrs (as most would not on most systems). The number of system calls for files with no extended attributes is reduced by (at least) half. --- xattr.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/xattr.c b/xattr.c index 57257bb..4ebb5c7 100644 --- a/xattr.c +++ b/xattr.c @@ -467,6 +467,11 @@ get_all(PyObject *self, PyObject *args, PyObject *keywds) goto freetgt; } + if(nalloc == 0) { + res = PyList_New(0); + goto freetgt; + } + /* Try to allocate the memory, using Python's allocator */ if((buf_list = PyMem_Malloc(nalloc)) == NULL) { res = PyErr_NoMemory(); @@ -864,6 +869,11 @@ pylistxattr(PyObject *self, PyObject *args) goto freetgt; } + if(nalloc == 0) { + mylist = PyList_New(0); + goto freetgt; + } + /* Try to allocate the memory, using Python's allocator */ if((buf = PyMem_Malloc(nalloc)) == NULL) { mylist = PyErr_NoMemory(); @@ -964,6 +974,11 @@ xattr_list(PyObject *self, PyObject *args, PyObject *keywds) goto freetgt; } + if(nalloc == 0) { + res = PyList_New(0); + goto freetgt; + } + /* Try to allocate the memory, using Python's allocator */ if((buf = PyMem_Malloc(nalloc)) == NULL) { res = PyErr_NoMemory(); -- 2.39.5