guile-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 5/8] pyutil: add BUP_ASSIGN_PYLONG_TO_INTEGRAL; use EXPR_SIGNED


From: Rob Browning
Subject: [PATCH 5/8] pyutil: add BUP_ASSIGN_PYLONG_TO_INTEGRAL; use EXPR_SIGNED
Date: Tue, 30 May 2023 19:49:41 -0500

Check EXPR_SIGNED to simplify the handling, i.e. we know which path
we're on up front.

Signed-off-by: Rob Browning <rlb@defaultvalue.org>
Tested-by: Rob Browning <rlb@defaultvalue.org>
---
 lib/bup/_helpers.c | 43 ++++---------------------------------------
 src/bup/pyutil.h   | 39 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 43 insertions(+), 39 deletions(-)

diff --git a/lib/bup/_helpers.c b/lib/bup/_helpers.c
index 275ba0171..2ca4ef839 100644
--- a/lib/bup/_helpers.c
+++ b/lib/bup/_helpers.c
@@ -1117,41 +1117,6 @@ static PyObject *bup_set_linux_file_attr(PyObject *self, 
PyObject *args)
 #endif
 #endif // defined BUP_USE_PYTHON_UTIME
 
-#define ASSIGN_PYLONG_TO_INTEGRAL(dest, pylong, overflow) \
-    ({                                                     \
-        int result = 0;                                                 \
-        *(overflow) = 0;                                                \
-        const long long lltmp = PyLong_AsLongLong(pylong);              \
-        if (lltmp == -1 && PyErr_Occurred())                            \
-        {                                                               \
-            if (PyErr_ExceptionMatches(PyExc_OverflowError))            \
-            {                                                           \
-                const unsigned long long ulltmp = 
PyLong_AsUnsignedLongLong(pylong); \
-                if (ulltmp == (unsigned long long) -1 && PyErr_Occurred()) \
-                {                                                       \
-                    if (PyErr_ExceptionMatches(PyExc_OverflowError))    \
-                    {                                                   \
-                        PyErr_Clear();                                  \
-                        *(overflow) = 1;                                \
-                    }                                                   \
-                }                                                       \
-                if (INTEGRAL_ASSIGNMENT_FITS((dest), ulltmp))           \
-                    result = 1;                                         \
-                else                                                    \
-                    *(overflow) = 1;                                    \
-            }                                                           \
-        }                                                               \
-        else                                                            \
-        {                                                               \
-            if (INTEGRAL_ASSIGNMENT_FITS((dest), lltmp))                \
-                result = 1;                                             \
-            else                                                        \
-                *(overflow) = 1;                                        \
-        }                                                               \
-        result;                                                         \
-        })
-
-
 #ifndef BUP_USE_PYTHON_UTIME // just for Python 2 now
 #ifdef HAVE_UTIMENSAT
 
@@ -1172,14 +1137,14 @@ static PyObject *bup_utimensat(PyObject *self, PyObject 
*args)
         return NULL;
 
     int overflow;
-    if (!ASSIGN_PYLONG_TO_INTEGRAL(&(ts[0].tv_sec), access_py, &overflow))
+    if (!BUP_ASSIGN_PYLONG_TO_INTEGRAL(&(ts[0].tv_sec), access_py, &overflow))
     {
         if (overflow)
             PyErr_SetString(PyExc_ValueError,
                             "unable to convert access time seconds for 
utimensat");
         return NULL;
     }
-    if (!ASSIGN_PYLONG_TO_INTEGRAL(&(ts[1].tv_sec), modification_py, 
&overflow))
+    if (!BUP_ASSIGN_PYLONG_TO_INTEGRAL(&(ts[1].tv_sec), modification_py, 
&overflow))
     {
         if (overflow)
             PyErr_SetString(PyExc_ValueError,
@@ -1212,7 +1177,7 @@ static int bup_parse_xutimes_args(char **path,
         return 0;
 
     int overflow;
-    if (!ASSIGN_PYLONG_TO_INTEGRAL(&(tv[0].tv_sec), access_py, &overflow))
+    if (!BUP_ASSIGN_PYLONG_TO_INTEGRAL(&(tv[0].tv_sec), access_py, &overflow))
     {
         if (overflow)
             PyErr_SetString(PyExc_ValueError, "unable to convert access time 
seconds to timeval");
@@ -1223,7 +1188,7 @@ static int bup_parse_xutimes_args(char **path,
         PyErr_SetString(PyExc_ValueError, "unable to convert access time 
nanoseconds to timeval");
         return 0;
     }
-    if (!ASSIGN_PYLONG_TO_INTEGRAL(&(tv[1].tv_sec), modification_py, 
&overflow))
+    if (!BUP_ASSIGN_PYLONG_TO_INTEGRAL(&(tv[1].tv_sec), modification_py, 
&overflow))
     {
         if (overflow)
             PyErr_SetString(PyExc_ValueError, "unable to convert modification 
time seconds to timeval");
diff --git a/src/bup/pyutil.h b/src/bup/pyutil.h
index 4ea99d683..37f4a100b 100644
--- a/src/bup/pyutil.h
+++ b/src/bup/pyutil.h
@@ -13,3 +13,42 @@ void *checked_malloc(size_t n, size_t size);
 int bup_uint_from_py(unsigned int *x, PyObject *py, const char *name);
 int bup_ulong_from_py(unsigned long *x, PyObject *py, const char *name);
 int bup_ullong_from_py(unsigned long long *x, PyObject *py, const char *name);
+
+// Currently only up to signed/unsigned long long given py api.  On
+// success returns non-zero.  On failure returns 0 and overflow will
+// be non-zero if there was an overflow, otherwise a python exception
+// will be pending.
+#define BUP_ASSIGN_PYLONG_TO_INTEGRAL (dest, pylong, overflow)          \
+    ({                                                                  \
+         int result = 0;                                                \
+         int pending_overflow = 0;                                      \
+         if (EXPR_SIGNED(dest)) {                                       \
+             const long long tmp = PyLong_AsLongLong(pylong);           \
+             if (tmp == -1 && PyErr_Occurred()                          \
+                 && PyErr_ExceptionMatches(PyExc_OverflowError))        \
+                 pending_overflow = 2;                                  \
+             else {                                                     \
+                 if (INTEGRAL_ASSIGNMENT_FITS((dest), tmp))             \
+                     result = 1;                                        \
+                 else                                                   \
+                     pending_overflow = 1;                              \
+             }                                                          \
+         } else {                                                       \
+             const unsigned long long tmp =                             \
+                 PyLong_AsUnsignedLongLong(pylong);                     \
+             if (tmp == -1 && PyErr_Occurred()                          \
+                 && PyErr_ExceptionMatches(PyExc_OverflowError))        \
+                 pending_overflow = 2;                                  \
+             else {                                                     \
+                 if (INTEGRAL_ASSIGNMENT_FITS((dest), tmp))             \
+                     result = 1;                                        \
+                 else                                                   \
+                     pending_overflow = 1;                              \
+             }                                                          \
+         }                                                              \
+         if (pending_overflow == 2) {                                   \
+             PyErr_Clear();                                             \
+             *(overflow) = 1;                                           \
+         }                                                              \
+         result;                                                        \
+    })
-- 
2.39.2




reply via email to

[Prev in Thread] Current Thread [Next in Thread]