From: Jorge Gorbe Moya Date: Tue, 21 Mar 2023 22:30:32 +0000 (-0700) Subject: [lldb] Update some uses of Python2 API in typemaps. X-Git-Tag: upstream/17.0.6~13972 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=984354fbbe4e207798f6d83c6f46b7603952dd36;p=platform%2Fupstream%2Fllvm.git [lldb] Update some uses of Python2 API in typemaps. Python 3 doesn't have a distinction between PyInt and PyLong, it's all PyLong now. This also fixes a bug in SetNumberFromObject. This used to crash LLDB: ``` lldb -o "script data=lldb.SBData(); data.SetDataFromUInt64Array([2**63])" ``` The problem happened in the PyInt path: ``` if (PyInt_Check(obj)) number = static_cast(PyInt_AsLong(obj)); ``` when obj doesn't fit in a signed long, `PyInt_AsLong` would fail with "OverflowError: Python int too large to convert to C long". The existing long path does the right thing, as it will call `PyLong_AsUnsignedLongLong` for uint64_t. Differential Revision: https://reviews.llvm.org/D146590 --- diff --git a/lldb/bindings/python/python-typemaps.swig b/lldb/bindings/python/python-typemaps.swig index b3e7dd4..3e9675c 100644 --- a/lldb/bindings/python/python-typemaps.swig +++ b/lldb/bindings/python/python-typemaps.swig @@ -103,11 +103,11 @@ // typemap for a char buffer %typemap(in) (char *dst, size_t dst_len) { - if (!PyInt_Check($input)) { + if (!PyLong_Check($input)) { PyErr_SetString(PyExc_ValueError, "Expecting an integer"); SWIG_fail; } - $2 = PyInt_AsLong($input); + $2 = PyLong_AsLong($input); if ($2 <= 0) { PyErr_SetString(PyExc_ValueError, "Positive integer expected"); SWIG_fail; @@ -139,11 +139,11 @@ // typemap for handling an snprintf-like API like SBThread::GetStopDescription. %typemap(in) (char *dst_or_null, size_t dst_len) { - if (!PyInt_Check($input)) { + if (!PyLong_Check($input)) { PyErr_SetString(PyExc_ValueError, "Expecting an integer"); SWIG_fail; } - $2 = PyInt_AsLong($input); + $2 = PyLong_AsLong($input); if ($2 <= 0) { PyErr_SetString(PyExc_ValueError, "Positive integer expected"); SWIG_fail; @@ -205,9 +205,7 @@ // typemap for an incoming buffer // See also SBProcess::ReadMemory. %typemap(in) (void *buf, size_t size) { - if (PyInt_Check($input)) { - $2 = PyInt_AsLong($input); - } else if (PyLong_Check($input)) { + if (PyLong_Check($input)) { $2 = PyLong_AsLong($input); } else { PyErr_SetString(PyExc_ValueError, "Expecting an integer or long object"); @@ -258,9 +256,7 @@ template <> int32_t PyLongAsT(PyObject *obj) { } template bool SetNumberFromPyObject(T &number, PyObject *obj) { - if (PyInt_Check(obj)) - number = static_cast(PyInt_AsLong(obj)); - else if (PyLong_Check(obj)) + if (PyLong_Check(obj)) number = PyLongAsT(obj); else return false; @@ -345,7 +341,7 @@ template <> bool SetNumberFromPyObject(double &number, PyObject *obj) { count = $2; PyObject *list = PyList_New(count); for (uint32_t j = 0; j < count; j++) { - PyObject *item = PyInt_FromLong($1[j]); + PyObject *item = PyLong_FromLong($1[j]); int ok = PyList_SetItem(list, j, item); if (ok != 0) { $result = Py_None; diff --git a/lldb/test/API/python_api/sbdata/TestSBData.py b/lldb/test/API/python_api/sbdata/TestSBData.py index 932781b..ba83959 100644 --- a/lldb/test/API/python_api/sbdata/TestSBData.py +++ b/lldb/test/API/python_api/sbdata/TestSBData.py @@ -387,12 +387,13 @@ class SBDataAPICase(TestBase): self.assert_data(data2.GetUnsignedInt8, 4, 111) self.assert_data(data2.GetUnsignedInt8, 5, 33) - data2.SetDataFromUInt64Array([1, 2, 3, 4, 5]) + data2.SetDataFromUInt64Array([1, 2, 3, 4, 5, 2**63]) self.assert_data(data2.GetUnsignedInt64, 0, 1) self.assert_data(data2.GetUnsignedInt64, 8, 2) self.assert_data(data2.GetUnsignedInt64, 16, 3) self.assert_data(data2.GetUnsignedInt64, 24, 4) self.assert_data(data2.GetUnsignedInt64, 32, 5) + self.assert_data(data2.GetUnsignedInt64, 40, 2**63) self.assertEqual( data2.uint64[0], 1,