1 //===-- PythonDataObjectsTests.cpp ------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "gtest/gtest.h" 11 12 #include "lldb/Host/HostInfo.h" 13 #include "Plugins/ScriptInterpreter/Python/lldb-python.h" 14 #include "Plugins/ScriptInterpreter/Python/PythonDataObjects.h" 15 #include "Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.h" 16 17 using namespace lldb_private; 18 19 class PythonDataObjectsTest : public testing::Test 20 { 21 public: 22 void 23 SetUp() override 24 { 25 HostInfoBase::Initialize(); 26 // ScriptInterpreterPython::Initialize() depends on HostInfo being 27 // initializedso it can compute the python directory etc. 28 ScriptInterpreterPython::Initialize(); 29 30 // Although we don't care about concurrency for the purposes of running 31 // this test suite, Python requires the GIL to be locked even for 32 // deallocating memory, which can happen when you call Py_DECREF or 33 // Py_INCREF. So acquire the GIL for the entire duration of this 34 // test suite. 35 m_gil_state = PyGILState_Ensure(); 36 } 37 38 void 39 TearDown() override 40 { 41 PyGILState_Release(m_gil_state); 42 43 ScriptInterpreterPython::Terminate(); 44 } 45 46 private: 47 PyGILState_STATE m_gil_state; 48 }; 49 50 TEST_F(PythonDataObjectsTest, TestOwnedReferences) 51 { 52 // After creating a new object, the refcount should be >= 1 53 PyObject *obj = PyLong_FromLong(3); 54 Py_ssize_t original_refcnt = obj->ob_refcnt; 55 EXPECT_LE(1, original_refcnt); 56 57 // If we take an owned reference, the refcount should be the same 58 PythonObject owned_long(PyRefType::Owned, obj); 59 EXPECT_EQ(original_refcnt, owned_long.get()->ob_refcnt); 60 61 // Take another reference and verify that the refcount increases by 1 62 PythonObject strong_ref(owned_long); 63 EXPECT_EQ(original_refcnt + 1, strong_ref.get()->ob_refcnt); 64 65 // If we reset the first one, the refcount should be the original value. 66 owned_long.Reset(); 67 EXPECT_EQ(original_refcnt, strong_ref.get()->ob_refcnt); 68 } 69 70 TEST_F(PythonDataObjectsTest, TestResetting) 71 { 72 PythonDictionary dict(PyInitialValue::Empty); 73 74 PyObject *new_dict = PyDict_New(); 75 dict.Reset(PyRefType::Owned, new_dict); 76 EXPECT_EQ(new_dict, dict.get()); 77 78 dict.Reset(PyRefType::Owned, nullptr); 79 EXPECT_EQ(nullptr, dict.get()); 80 81 dict.Reset(PyRefType::Owned, PyDict_New()); 82 EXPECT_NE(nullptr, dict.get()); 83 dict.Reset(); 84 EXPECT_EQ(nullptr, dict.get()); 85 } 86 87 TEST_F(PythonDataObjectsTest, TestBorrowedReferences) 88 { 89 PythonInteger long_value(PyRefType::Owned, PyLong_FromLong(3)); 90 Py_ssize_t original_refcnt = long_value.get()->ob_refcnt; 91 EXPECT_LE(1, original_refcnt); 92 93 PythonInteger borrowed_long(PyRefType::Borrowed, long_value.get()); 94 EXPECT_EQ(original_refcnt + 1, borrowed_long.get()->ob_refcnt); 95 } 96 97 TEST_F(PythonDataObjectsTest, TestPythonInteger) 98 { 99 // Test that integers behave correctly when wrapped by a PythonInteger. 100 101 #if PY_MAJOR_VERSION < 3 102 // Verify that `PythonInt` works correctly when given a PyInt object. 103 // Note that PyInt doesn't exist in Python 3.x, so this is only for 2.x 104 PyObject *py_int = PyInt_FromLong(12); 105 EXPECT_TRUE(PythonInteger::Check(py_int)); 106 PythonInteger python_int(PyRefType::Owned, py_int); 107 108 EXPECT_EQ(PyObjectType::Integer, python_int.GetObjectType()); 109 EXPECT_EQ(12, python_int.GetInteger()); 110 #endif 111 112 // Verify that `PythonInteger` works correctly when given a PyLong object. 113 PyObject *py_long = PyLong_FromLong(12); 114 EXPECT_TRUE(PythonInteger::Check(py_long)); 115 PythonInteger python_long(PyRefType::Owned, py_long); 116 EXPECT_EQ(PyObjectType::Integer, python_long.GetObjectType()); 117 118 // Verify that you can reset the value and that it is reflected properly. 119 python_long.SetInteger(40); 120 EXPECT_EQ(40, python_long.GetInteger()); 121 122 // Test that creating a `PythonInteger` object works correctly with the 123 // int constructor. 124 PythonInteger constructed_int(7); 125 EXPECT_EQ(7, constructed_int.GetInteger()); 126 } 127 128 TEST_F(PythonDataObjectsTest, TestPythonString) 129 { 130 // Test that strings behave correctly when wrapped by a PythonString. 131 132 static const char *test_string = "PythonDataObjectsTest::TestPythonString1"; 133 static const char *test_string2 = "PythonDataObjectsTest::TestPythonString2"; 134 static const char *test_string3 = "PythonDataObjectsTest::TestPythonString3"; 135 136 #if PY_MAJOR_VERSION < 3 137 // Verify that `PythonString` works correctly when given a PyString object. 138 // Note that PyString doesn't exist in Python 3.x, so this is only for 2.x 139 PyObject *py_string = PyString_FromString(test_string); 140 EXPECT_TRUE(PythonString::Check(py_string)); 141 PythonString python_string(PyRefType::Owned, py_string); 142 143 EXPECT_EQ(PyObjectType::String, python_string.GetObjectType()); 144 EXPECT_STREQ(test_string, python_string.GetString().data()); 145 #else 146 // Verify that `PythonString` works correctly when given a PyUnicode object. 147 PyObject *py_unicode = PyUnicode_FromString(test_string); 148 EXPECT_TRUE(PythonString::Check(py_unicode)); 149 PythonString python_unicode(PyRefType::Owned, py_unicode); 150 EXPECT_EQ(PyObjectType::String, python_unicode.GetObjectType()); 151 EXPECT_STREQ(test_string, python_unicode.GetString().data()); 152 #endif 153 154 // Test that creating a `PythonString` object works correctly with the 155 // string constructor 156 PythonString constructed_string(test_string3); 157 EXPECT_STREQ(test_string3, constructed_string.GetString().str().c_str()); 158 } 159 160 TEST_F(PythonDataObjectsTest, TestPythonStringToStr) 161 { 162 const char *c_str = "PythonDataObjectsTest::TestPythonStringToStr"; 163 164 PythonString str(c_str); 165 EXPECT_STREQ(c_str, str.GetString().str().c_str()); 166 167 PythonString str_str = str.Str(); 168 EXPECT_STREQ(c_str, str_str.GetString().str().c_str()); 169 } 170 171 TEST_F(PythonDataObjectsTest, TestPythonIntegerToStr) 172 { 173 } 174 175 TEST_F(PythonDataObjectsTest, TestPythonIntegerToStructuredInteger) 176 { 177 PythonInteger integer(7); 178 auto int_sp = integer.CreateStructuredInteger(); 179 EXPECT_EQ(7, int_sp->GetValue()); 180 } 181 182 TEST_F(PythonDataObjectsTest, TestPythonStringToStructuredString) 183 { 184 static const char *test_string = "PythonDataObjectsTest::TestPythonStringToStructuredString"; 185 PythonString constructed_string(test_string); 186 auto string_sp = constructed_string.CreateStructuredString(); 187 EXPECT_STREQ(test_string, string_sp->GetStringValue().c_str()); 188 } 189 190 TEST_F(PythonDataObjectsTest, TestPythonListValueEquality) 191 { 192 // Test that a list which is built through the native 193 // Python API behaves correctly when wrapped by a PythonList. 194 static const int list_size = 2; 195 static const long long_value0 = 5; 196 static const char *const string_value1 = "String Index 1"; 197 198 PyObject *py_list = PyList_New(2); 199 EXPECT_TRUE(PythonList::Check(py_list)); 200 PythonList list(PyRefType::Owned, py_list); 201 202 PythonObject list_items[list_size]; 203 list_items[0].Reset(PythonInteger(long_value0)); 204 list_items[1].Reset(PythonString(string_value1)); 205 206 for (int i = 0; i < list_size; ++i) 207 list.SetItemAtIndex(i, list_items[i]); 208 209 EXPECT_EQ(list_size, list.GetSize()); 210 EXPECT_EQ(PyObjectType::List, list.GetObjectType()); 211 212 // Verify that the values match 213 PythonObject chk_value1 = list.GetItemAtIndex(0); 214 PythonObject chk_value2 = list.GetItemAtIndex(1); 215 EXPECT_TRUE(PythonInteger::Check(chk_value1.get())); 216 EXPECT_TRUE(PythonString::Check(chk_value2.get())); 217 218 PythonInteger chk_int(PyRefType::Borrowed, chk_value1.get()); 219 PythonString chk_str(PyRefType::Borrowed, chk_value2.get()); 220 221 EXPECT_EQ(long_value0, chk_int.GetInteger()); 222 EXPECT_STREQ(string_value1, chk_str.GetString().str().c_str()); 223 } 224 225 TEST_F(PythonDataObjectsTest, TestPythonListManipulation) 226 { 227 // Test that manipulation of a PythonList behaves correctly when 228 // wrapped by a PythonDictionary. 229 230 static const long long_value0 = 5; 231 static const char *const string_value1 = "String Index 1"; 232 233 PythonList list(PyInitialValue::Empty); 234 PythonInteger integer(long_value0); 235 PythonString string(string_value1); 236 237 list.AppendItem(integer); 238 list.AppendItem(string); 239 EXPECT_EQ(2, list.GetSize()); 240 241 // Verify that the values match 242 PythonObject chk_value1 = list.GetItemAtIndex(0); 243 PythonObject chk_value2 = list.GetItemAtIndex(1); 244 EXPECT_TRUE(PythonInteger::Check(chk_value1.get())); 245 EXPECT_TRUE(PythonString::Check(chk_value2.get())); 246 247 PythonInteger chk_int(PyRefType::Borrowed, chk_value1.get()); 248 PythonString chk_str(PyRefType::Borrowed, chk_value2.get()); 249 250 EXPECT_EQ(long_value0, chk_int.GetInteger()); 251 EXPECT_STREQ(string_value1, chk_str.GetString().str().c_str()); 252 } 253 254 TEST_F(PythonDataObjectsTest, TestPythonListToStructuredList) 255 { 256 static const long long_value0 = 5; 257 static const char *const string_value1 = "String Index 1"; 258 259 PythonList list(PyInitialValue::Empty); 260 list.AppendItem(PythonInteger(long_value0)); 261 list.AppendItem(PythonString(string_value1)); 262 263 auto array_sp = list.CreateStructuredArray(); 264 EXPECT_EQ(StructuredData::Type::eTypeInteger, array_sp->GetItemAtIndex(0)->GetType()); 265 EXPECT_EQ(StructuredData::Type::eTypeString, array_sp->GetItemAtIndex(1)->GetType()); 266 267 auto int_sp = array_sp->GetItemAtIndex(0)->GetAsInteger(); 268 auto string_sp = array_sp->GetItemAtIndex(1)->GetAsString(); 269 270 EXPECT_EQ(long_value0, int_sp->GetValue()); 271 EXPECT_STREQ(string_value1, string_sp->GetValue().c_str()); 272 } 273 274 TEST_F(PythonDataObjectsTest, TestPythonDictionaryValueEquality) 275 { 276 // Test that a dictionary which is built through the native 277 // Python API behaves correctly when wrapped by a PythonDictionary. 278 static const int dict_entries = 2; 279 const char *key_0 = "Key 0"; 280 int key_1 = 1; 281 const int value_0 = 0; 282 const char *value_1 = "Value 1"; 283 284 PythonObject py_keys[dict_entries]; 285 PythonObject py_values[dict_entries]; 286 287 py_keys[0].Reset(PythonString(key_0)); 288 py_keys[1].Reset(PythonInteger(key_1)); 289 py_values[0].Reset(PythonInteger(value_0)); 290 py_values[1].Reset(PythonString(value_1)); 291 292 PyObject *py_dict = PyDict_New(); 293 EXPECT_TRUE(PythonDictionary::Check(py_dict)); 294 PythonDictionary dict(PyRefType::Owned, py_dict); 295 296 for (int i = 0; i < dict_entries; ++i) 297 PyDict_SetItem(py_dict, py_keys[i].get(), py_values[i].get()); 298 EXPECT_EQ(dict.GetSize(), dict_entries); 299 EXPECT_EQ(PyObjectType::Dictionary, dict.GetObjectType()); 300 301 // Verify that the values match 302 PythonObject chk_value1 = dict.GetItemForKey(py_keys[0]); 303 PythonObject chk_value2 = dict.GetItemForKey(py_keys[1]); 304 EXPECT_TRUE(PythonInteger::Check(chk_value1.get())); 305 EXPECT_TRUE(PythonString::Check(chk_value2.get())); 306 307 PythonInteger chk_int(PyRefType::Borrowed, chk_value1.get()); 308 PythonString chk_str(PyRefType::Borrowed, chk_value2.get()); 309 310 EXPECT_EQ(value_0, chk_int.GetInteger()); 311 EXPECT_STREQ(value_1, chk_str.GetString().str().c_str()); 312 } 313 314 TEST_F(PythonDataObjectsTest, TestPythonDictionaryManipulation) 315 { 316 // Test that manipulation of a dictionary behaves correctly when wrapped 317 // by a PythonDictionary. 318 static const int dict_entries = 2; 319 320 const char *const key_0 = "Key 0"; 321 const char *const key_1 = "Key 1"; 322 const long value_0 = 1; 323 const char *const value_1 = "Value 1"; 324 325 PythonString keys[dict_entries]; 326 PythonObject values[dict_entries]; 327 328 keys[0].Reset(PythonString(key_0)); 329 keys[1].Reset(PythonString(key_1)); 330 values[0].Reset(PythonInteger(value_0)); 331 values[1].Reset(PythonString(value_1)); 332 333 PythonDictionary dict(PyInitialValue::Empty); 334 for (int i = 0; i < 2; ++i) 335 dict.SetItemForKey(keys[i], values[i]); 336 337 EXPECT_EQ(dict_entries, dict.GetSize()); 338 339 // Verify that the keys and values match 340 PythonObject chk_value1 = dict.GetItemForKey(keys[0]); 341 PythonObject chk_value2 = dict.GetItemForKey(keys[1]); 342 EXPECT_TRUE(PythonInteger::Check(chk_value1.get())); 343 EXPECT_TRUE(PythonString::Check(chk_value2.get())); 344 345 PythonInteger chk_int(PyRefType::Borrowed, chk_value1.get()); 346 PythonString chk_str(PyRefType::Borrowed, chk_value2.get()); 347 348 EXPECT_EQ(value_0, chk_int.GetInteger()); 349 EXPECT_STREQ(value_1, chk_str.GetString().str().c_str()); 350 } 351 352 TEST_F(PythonDataObjectsTest, TestPythonDictionaryToStructuredDictionary) 353 { 354 static const char *const string_key0 = "String Key 0"; 355 static const char *const string_key1 = "String Key 1"; 356 357 static const char *const string_value0 = "String Value 0"; 358 static const long int_value1 = 7; 359 360 PythonDictionary dict(PyInitialValue::Empty); 361 dict.SetItemForKey(PythonString(string_key0), PythonString(string_value0)); 362 dict.SetItemForKey(PythonString(string_key1), PythonInteger(int_value1)); 363 364 auto dict_sp = dict.CreateStructuredDictionary(); 365 EXPECT_EQ(2, dict_sp->GetSize()); 366 367 EXPECT_TRUE(dict_sp->HasKey(string_key0)); 368 EXPECT_TRUE(dict_sp->HasKey(string_key1)); 369 370 auto string_sp = dict_sp->GetValueForKey(string_key0)->GetAsString(); 371 auto int_sp = dict_sp->GetValueForKey(string_key1)->GetAsInteger(); 372 373 EXPECT_STREQ(string_value0, string_sp->GetValue().c_str()); 374 EXPECT_EQ(int_value1, int_sp->GetValue()); 375 } 376