guile-devel
[Top][All Lists]
Advanced

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

[PATCH 4/8] pyutil: add bup_uint_from_py bup_ulong_from_py bup_ullong_fr


From: Rob Browning
Subject: [PATCH 4/8] pyutil: add bup_uint_from_py bup_ulong_from_py bup_ullong_from_py
Date: Tue, 30 May 2023 19:49:40 -0500

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

diff --git a/lib/bup/_helpers.c b/lib/bup/_helpers.c
index d58cf9954..275ba0171 100644
--- a/lib/bup/_helpers.c
+++ b/lib/bup/_helpers.c
@@ -126,64 +126,6 @@ static uint64_t htonll(uint64_t value)
 #define INTEGRAL_ASSIGNMENT_FITS(dest, src) INT_ADD_OK(src, 0, dest)
 
 
-static int bup_ulong_from_py(unsigned long *x, PyObject *py, const char *name)
-{
-    if (!PyLong_Check(py))
-    {
-        PyErr_Format(PyExc_TypeError, "expected integer %s", name);
-        return 0;
-    }
-
-    const unsigned long tmp = PyLong_AsUnsignedLong(py);
-    if (PyErr_Occurred())
-    {
-        if (PyErr_ExceptionMatches(PyExc_OverflowError))
-            PyErr_Format(PyExc_OverflowError, "%s too big for unsigned long",
-                         name);
-        return 0;
-    }
-    *x = tmp;
-    return 1;
-}
-
-
-static int bup_uint_from_py(unsigned int *x, PyObject *py, const char *name)
-{
-    unsigned long tmp;
-    if (!bup_ulong_from_py(&tmp, py, name))
-        return 0;
-
-    if (tmp > UINT_MAX)
-    {
-        PyErr_Format(PyExc_OverflowError, "%s too big for unsigned int", name);
-        return 0;
-    }
-    *x = (unsigned int) tmp;
-    return 1;
-}
-
-static int bup_ullong_from_py(unsigned PY_LONG_LONG *x, PyObject *py,
-                              const char *name)
-{
-    if (!PyLong_Check(py))
-    {
-        PyErr_Format(PyExc_TypeError, "integer argument expected for %s", 
name);
-        return 0;
-    }
-
-    const unsigned PY_LONG_LONG tmp = PyLong_AsUnsignedLongLong(py);
-    if (tmp == (unsigned long long) -1 && PyErr_Occurred())
-    {
-        if (PyErr_ExceptionMatches(PyExc_OverflowError))
-            PyErr_Format(PyExc_OverflowError,
-                         "%s too big for unsigned long long", name);
-        return 0;
-    }
-    *x = tmp;
-    return 1;
-}
-
-
 static PyObject *bup_bytescmp(PyObject *self, PyObject *args)
 {
     PyObject *py_s1, *py_s2;  // This is really a PyBytes/PyString
diff --git a/src/bup/pyutil.c b/src/bup/pyutil.c
index 53ca39c1d..5c480a4d3 100644
--- a/src/bup/pyutil.c
+++ b/src/bup/pyutil.c
@@ -35,3 +35,59 @@ void *checked_malloc(size_t n, size_t size)
         return PyErr_NoMemory();
     return result;
 }
+
+int bup_ulong_from_py(unsigned long *x, PyObject *py, const char *name)
+{
+    if (!PyLong_Check(py))
+    {
+        PyErr_Format(PyExc_TypeError, "%s expected integer, not %R", name, py);
+        return 0;
+    }
+
+    const unsigned long tmp = PyLong_AsUnsignedLong(py);
+    if (PyErr_Occurred())
+    {
+        if (PyErr_ExceptionMatches(PyExc_OverflowError))
+            PyErr_Format(PyExc_OverflowError, "%s overflows unsigned long: %R",
+                         name, py);
+        return 0;
+    }
+    *x = tmp;
+    return 1;
+}
+
+int bup_uint_from_py(unsigned int *x, PyObject *py, const char *name)
+{
+    unsigned long tmp;
+    if (!bup_ulong_from_py(&tmp, py, name))
+        return 0;
+
+    if (tmp > UINT_MAX)
+    {
+        PyErr_Format(PyExc_OverflowError, "%s overflows unsigned int: %R",
+                     name, py);
+        return 0;
+    }
+    *x = (unsigned int) tmp;
+    return 1;
+}
+
+int bup_ullong_from_py(unsigned PY_LONG_LONG *x, PyObject *py, const char 
*name)
+{
+    if (!PyLong_Check(py))
+    {
+        PyErr_Format(PyExc_TypeError, "%s expected integer, not %R", name, py);
+        return 0;
+    }
+
+    const unsigned PY_LONG_LONG tmp = PyLong_AsUnsignedLongLong(py);
+    if (tmp == (unsigned long long) -1 && PyErr_Occurred())
+    {
+        if (PyErr_ExceptionMatches(PyExc_OverflowError))
+            PyErr_Format(PyExc_OverflowError,
+                         "%s overflows unsigned long long: %R", name, py);
+        return 0;
+    }
+    *x = tmp;
+    return 1;
+}
diff --git a/src/bup/pyutil.h b/src/bup/pyutil.h
index d49e6f001..4ea99d683 100644
--- a/src/bup/pyutil.h
+++ b/src/bup/pyutil.h
@@ -9,3 +9,7 @@
 
 void *checked_calloc(size_t n, size_t size);
 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);
-- 
2.39.2




reply via email to

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