From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Rob Browning Newsgroups: gmane.lisp.guile.devel 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 Message-ID: <20230531004944.1657633-5-rlb@defaultvalue.org> References: <20230531004944.1657633-1-rlb@defaultvalue.org> Mime-Version: 1.0 Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="16016"; mail-complaints-to="usenet@ciao.gmane.io" To: guile-devel@gnu.org Original-X-From: guile-devel-bounces+guile-devel=m.gmane-mx.org@gnu.org Wed May 31 02:50:33 2023 Return-path: Envelope-to: guile-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1q4A2q-00040M-SU for guile-devel@m.gmane-mx.org; Wed, 31 May 2023 02:50:32 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1q4A2D-0006Lp-Nh; Tue, 30 May 2023 20:49:53 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q4A2B-0006Ks-TQ for guile-devel@gnu.org; Tue, 30 May 2023 20:49:51 -0400 Original-Received: from defaultvalue.org ([45.33.119.55]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1q4A27-0008E8-Qk for guile-devel@gnu.org; Tue, 30 May 2023 20:49:50 -0400 Original-Received: from trouble.defaultvalue.org (localhost [127.0.0.1]) (Authenticated sender: rlb@defaultvalue.org) by defaultvalue.org (Postfix) with ESMTPSA id ED5532042A for ; Tue, 30 May 2023 19:49:45 -0500 (CDT) Original-Received: by trouble.defaultvalue.org (Postfix, from userid 1000) id 12BB114E0A2; Tue, 30 May 2023 19:49:45 -0500 (CDT) X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230531004944.1657633-1-rlb@defaultvalue.org> Received-SPF: pass client-ip=45.33.119.55; envelope-from=rlb@defaultvalue.org; helo=defaultvalue.org X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-devel-bounces+guile-devel=m.gmane-mx.org@gnu.org Original-Sender: guile-devel-bounces+guile-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.lisp.guile.devel:21843 Archived-At: Signed-off-by: Rob Browning Tested-by: Rob Browning --- 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