Skip to content

Commit 27f2908

Browse files
authored
[lldb] Use PyBytes and PyByteArray in Python Data Objects unittest (#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.
1 parent d5167c8 commit 27f2908

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
@@ -51,21 +51,24 @@ class PythonDataObjectsTest : public PythonTestSuite {
5151

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

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

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

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

7174
TEST_F(PythonDataObjectsTest, TestResetting) {
@@ -82,12 +85,15 @@ TEST_F(PythonDataObjectsTest, TestResetting) {
8285
}
8386

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

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

9399
TEST_F(PythonDataObjectsTest, TestGlobalNameResolutionNoDot) {

0 commit comments

Comments
 (0)