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 5/8] pyutil: add BUP_ASSIGN_PYLONG_TO_INTEGRAL; use EXPR_SIGNED Date: Tue, 30 May 2023 19:49:41 -0500 Message-ID: <20230531004944.1657633-6-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="19144"; 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:51:08 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 1q4A3P-0004iT-GT for guile-devel@m.gmane-mx.org; Wed, 31 May 2023 02:51:07 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1q4A2G-0006Mj-0Y; Tue, 30 May 2023 20:49:56 -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 1q4A2C-0006LM-JZ for guile-devel@gnu.org; Tue, 30 May 2023 20:49:52 -0400 Original-Received: from defaultvalue.org ([45.33.119.55]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1q4A29-0008Ed-Ne for guile-devel@gnu.org; Tue, 30 May 2023 20:49:52 -0400 Original-Received: from trouble.defaultvalue.org (localhost [127.0.0.1]) (Authenticated sender: rlb@defaultvalue.org) by defaultvalue.org (Postfix) with ESMTPSA id 1F97220696 for ; Tue, 30 May 2023 19:49:46 -0500 (CDT) Original-Received: by trouble.defaultvalue.org (Postfix, from userid 1000) id 16A2314E0A7; 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:21847 Archived-At: Check EXPR_SIGNED to simplify the handling, i.e. we know which path we're on up front. Signed-off-by: Rob Browning Tested-by: Rob Browning --- 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