From bdbc455f9da31b9f426ebe505a78dc1a3f6fc251 Mon Sep 17 00:00:00 2001 From: Lawrence D'Oliveiro Date: Thu, 3 Oct 2019 03:38:08 +0000 Subject: [PATCH 3/3] Context.stat() and File.fstat() now return the same sort of namedtuple as os.stat(). This should be backward-compatible with code that expects a regular tuple. --- smbc/context.c | 12 +---------- smbc/file.c | 12 +---------- smbc/smbcmodule.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++- smbc/smbcmodule.h | 1 + 4 files changed, 53 insertions(+), 23 deletions(-) diff --git a/smbc/context.c b/smbc/context.c index f507952..66a69a1 100644 --- a/smbc/context.c +++ b/smbc/context.c @@ -515,17 +515,7 @@ Context_stat (Context *self, PyObject *args) return NULL; } - return Py_BuildValue ("(IKKKIIKIII)", - st.st_mode, - (unsigned long long)st.st_ino, - (unsigned long long)st.st_dev, - (unsigned long long)st.st_nlink, - st.st_uid, - st.st_gid, - st.st_size, - st.st_atime, - st.st_mtime, - st.st_ctime); + return pysmbc_makestat(&st); } static PyObject * diff --git a/smbc/file.c b/smbc/file.c index 67544b2..ce2727e 100644 --- a/smbc/file.c +++ b/smbc/file.c @@ -236,17 +236,7 @@ File_fstat (File *self, PyObject *args) return NULL; } - return Py_BuildValue ("(IKKKIIKIII)", - st.st_mode, - (unsigned long long)st.st_ino, - (unsigned long long)st.st_dev, - (unsigned long long)st.st_nlink, - st.st_uid, - st.st_gid, - st.st_size, - st.st_atime, - st.st_mtime, - st.st_ctime); + return pysmbc_makestat(&st); } static PyObject * diff --git a/smbc/smbcmodule.c b/smbc/smbcmodule.c index 4cb1973..f9a69f8 100644 --- a/smbc/smbcmodule.c +++ b/smbc/smbcmodule.c @@ -149,6 +149,9 @@ static const struct exception_entry exceptions[] = ARRAY_END }; +static PyObject * + os_stat_result; + PYSMBC_PROTOTYPE_HEADER { PyObject *m = PYSMBC_MODULE_CREATOR; @@ -157,6 +160,7 @@ PYSMBC_PROTOTYPE_HEADER PyObject *result = NULL; #endif PyObject *SmbError = NULL; + PyObject * os_module = NULL; do /*once*/ { @@ -231,15 +235,22 @@ PYSMBC_PROTOTYPE_HEADER } /*for*/ if (PyErr_Occurred()) break; + os_module = PyImport_ImportModule("os"); + if (PyErr_Occurred()) + break; + os_stat_result = PyObject_GetAttrString(os_module, "stat_result"); + if (PyErr_Occurred()) + break; /* all done */ #if PY_MAJOR_VERSION >= 3 result = m; #endif } while (false); + Py_XDECREF(os_module); Py_XDECREF(SmbError); #if PY_MAJOR_VERSION >= 3 - return result; + return result; #endif } @@ -285,6 +296,44 @@ void pysmbc_SetFromErrno() return; } +PyObject *pysmbc_makestat(struct stat * info) + /* turns the contents of info into the same type of namedtuple + as returned by os.stat(). */ + { + PyObject *result = NULL; + PyObject *temptuple = NULL; + PyObject *args = NULL; + do /*once*/ + { + temptuple = Py_BuildValue + ( + "(IKKKIIKIII)", + info->st_mode, + (unsigned long long)info->st_ino, + (unsigned long long)info->st_dev, + (unsigned long long)info->st_nlink, + info->st_uid, + info->st_gid, + info->st_size, + info->st_atime, + info->st_mtime, + info->st_ctime + ); + if (PyErr_Occurred()) + break; + args = Py_BuildValue("(O)", temptuple); + if (PyErr_Occurred()) + break; + result = PyObject_CallObject(os_stat_result, args); + if (PyErr_Occurred()) + break; + } + while (false); + Py_XDECREF(args); + Py_XDECREF(temptuple); + return result; + } /*pysmbc_makestat*/ + /////////////// // Debugging // /////////////// diff --git a/smbc/smbcmodule.h b/smbc/smbcmodule.h index 752a9c0..347045b 100644 --- a/smbc/smbcmodule.h +++ b/smbc/smbcmodule.h @@ -37,6 +37,7 @@ extern void debugprintf (const char *fmt, ...) FORMAT ((__printf__, 1, 2)); extern void pysmbc_SetFromErrno(void); +extern PyObject *pysmbc_makestat(struct stat * info); extern PyObject *NoEntryError; extern PyObject *PermissionError; -- 2.22.0