Skip to content

Commit 3f8a139

Browse files
committed
[lldb] Use PyBytes and PyByteArray in Python Data Objects unittest (llvm#82098)
Use a Python Bytes and ByteArray object instead of Integers for TestOwnedReferences and TestBorrowedReferences. These two tests were failing when building against Python 3.12 because these Integer objects had a refcount of 4294967296 (-1). I didn't dig into it, but I suspect the Python runtime has adopted an optimization to decrease refcounting traffic for these simple objects. (cherry picked from commit 27f2908)
1 parent f6f7134 commit 3f8a139

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

lldb/unittests/ScriptInterpreter/Python/PythonDataObjectsTests.cpp

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,24 @@ class PythonDataObjectsTest : public PythonTestSuite {
5252

5353
TEST_F(PythonDataObjectsTest, TestOwnedReferences) {
5454
// After creating a new object, the refcount should be >= 1
55-
PyObject *obj = PyLong_FromLong(3);
56-
Py_ssize_t original_refcnt = obj->ob_refcnt;
55+
PyObject *obj = PyBytes_FromString("foo");
56+
Py_ssize_t original_refcnt = Py_REFCNT(obj);
5757
EXPECT_LE(1, original_refcnt);
5858

5959
// If we take an owned reference, the refcount should be the same
60-
PythonObject owned_long(PyRefType::Owned, obj);
61-
EXPECT_EQ(original_refcnt, owned_long.get()->ob_refcnt);
60+
PythonObject owned(PyRefType::Owned, obj);
61+
Py_ssize_t owned_refcnt = Py_REFCNT(owned.get());
62+
EXPECT_EQ(original_refcnt, owned_refcnt);
6263

6364
// Take another reference and verify that the refcount increases by 1
64-
PythonObject strong_ref(owned_long);
65-
EXPECT_EQ(original_refcnt + 1, strong_ref.get()->ob_refcnt);
65+
PythonObject strong_ref(owned);
66+
Py_ssize_t strong_refcnt = Py_REFCNT(strong_ref.get());
67+
EXPECT_EQ(original_refcnt + 1, strong_refcnt);
6668

6769
// If we reset the first one, the refcount should be the original value.
68-
owned_long.Reset();
69-
EXPECT_EQ(original_refcnt, strong_ref.get()->ob_refcnt);
70+
owned.Reset();
71+
strong_refcnt = Py_REFCNT(strong_ref.get());
72+
EXPECT_EQ(original_refcnt, strong_refcnt);
7073
}
7174

7275
TEST_F(PythonDataObjectsTest, TestResetting) {
@@ -83,12 +86,15 @@ TEST_F(PythonDataObjectsTest, TestResetting) {
8386
}
8487

8588
TEST_F(PythonDataObjectsTest, TestBorrowedReferences) {
86-
PythonInteger long_value(PyRefType::Owned, PyLong_FromLong(3));
87-
Py_ssize_t original_refcnt = long_value.get()->ob_refcnt;
89+
PythonByteArray byte_value(PyRefType::Owned,
90+
PyByteArray_FromStringAndSize("foo", 3));
91+
Py_ssize_t original_refcnt = Py_REFCNT(byte_value.get());
8892
EXPECT_LE(1, original_refcnt);
8993

90-
PythonInteger borrowed_long(PyRefType::Borrowed, long_value.get());
91-
EXPECT_EQ(original_refcnt + 1, borrowed_long.get()->ob_refcnt);
94+
PythonByteArray borrowed_byte(PyRefType::Borrowed, byte_value.get());
95+
Py_ssize_t borrowed_refcnt = Py_REFCNT(borrowed_byte.get());
96+
97+
EXPECT_EQ(original_refcnt + 1, borrowed_refcnt);
9298
}
9399

94100
TEST_F(PythonDataObjectsTest, TestGlobalNameResolutionNoDot) {

0 commit comments

Comments
 (0)