1 //===-- PythonDataObjectsTests.cpp ------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "Plugins/ScriptInterpreter/Python/lldb-python.h" 10 #include "gtest/gtest.h" 11 12 #include "Plugins/ScriptInterpreter/Python/PythonDataObjects.h" 13 #include "Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.h" 14 #include "lldb/Host/File.h" 15 #include "lldb/Host/FileSystem.h" 16 #include "lldb/Host/HostInfo.h" 17 #include "lldb/lldb-enumerations.h" 18 #include "llvm/Testing/Support/Error.h" 19 20 #include "PythonTestSuite.h" 21 22 using namespace lldb_private; 23 24 class PythonDataObjectsTest : public PythonTestSuite { 25 public: 26 void SetUp() override { 27 PythonTestSuite::SetUp(); 28 29 PythonString sys_module("sys"); 30 m_sys_module.Reset(PyRefType::Owned, PyImport_Import(sys_module.get())); 31 m_main_module = PythonModule::MainModule(); 32 m_builtins_module = PythonModule::BuiltinsModule(); 33 } 34 35 void TearDown() override { 36 m_sys_module.Reset(); 37 m_main_module.Reset(); 38 m_builtins_module.Reset(); 39 40 PythonTestSuite::TearDown(); 41 } 42 43 protected: 44 PythonModule m_sys_module; 45 PythonModule m_main_module; 46 PythonModule m_builtins_module; 47 }; 48 49 TEST_F(PythonDataObjectsTest, TestOwnedReferences) { 50 // After creating a new object, the refcount should be >= 1 51 PyObject *obj = PyLong_FromLong(3); 52 Py_ssize_t original_refcnt = obj->ob_refcnt; 53 EXPECT_LE(1, original_refcnt); 54 55 // If we take an owned reference, the refcount should be the same 56 PythonObject owned_long(PyRefType::Owned, obj); 57 EXPECT_EQ(original_refcnt, owned_long.get()->ob_refcnt); 58 59 // Take another reference and verify that the refcount increases by 1 60 PythonObject strong_ref(owned_long); 61 EXPECT_EQ(original_refcnt + 1, strong_ref.get()->ob_refcnt); 62 63 // If we reset the first one, the refcount should be the original value. 64 owned_long.Reset(); 65 EXPECT_EQ(original_refcnt, strong_ref.get()->ob_refcnt); 66 } 67 68 TEST_F(PythonDataObjectsTest, TestResetting) { 69 PythonDictionary dict(PyInitialValue::Empty); 70 71 PyObject *new_dict = PyDict_New(); 72 dict.Reset(PyRefType::Owned, new_dict); 73 EXPECT_EQ(new_dict, dict.get()); 74 75 dict.Reset(PyRefType::Owned, nullptr); 76 EXPECT_EQ(nullptr, dict.get()); 77 78 dict.Reset(PyRefType::Owned, PyDict_New()); 79 EXPECT_NE(nullptr, dict.get()); 80 dict.Reset(); 81 EXPECT_EQ(nullptr, dict.get()); 82 } 83 84 TEST_F(PythonDataObjectsTest, TestBorrowedReferences) { 85 PythonInteger long_value(PyRefType::Owned, PyLong_FromLong(3)); 86 Py_ssize_t original_refcnt = long_value.get()->ob_refcnt; 87 EXPECT_LE(1, original_refcnt); 88 89 PythonInteger borrowed_long(PyRefType::Borrowed, long_value.get()); 90 EXPECT_EQ(original_refcnt + 1, borrowed_long.get()->ob_refcnt); 91 } 92 93 TEST_F(PythonDataObjectsTest, TestGlobalNameResolutionNoDot) { 94 PythonObject sys_module = m_main_module.ResolveName("sys"); 95 EXPECT_EQ(m_sys_module.get(), sys_module.get()); 96 EXPECT_TRUE(sys_module.IsAllocated()); 97 EXPECT_TRUE(PythonModule::Check(sys_module.get())); 98 } 99 100 TEST_F(PythonDataObjectsTest, TestModuleNameResolutionNoDot) { 101 PythonObject sys_path = m_sys_module.ResolveName("path"); 102 PythonObject sys_version_info = m_sys_module.ResolveName("version_info"); 103 EXPECT_TRUE(sys_path.IsAllocated()); 104 EXPECT_TRUE(sys_version_info.IsAllocated()); 105 106 EXPECT_TRUE(PythonList::Check(sys_path.get())); 107 } 108 109 TEST_F(PythonDataObjectsTest, TestTypeNameResolutionNoDot) { 110 PythonObject sys_version_info = m_sys_module.ResolveName("version_info"); 111 112 PythonObject version_info_type(PyRefType::Owned, 113 PyObject_Type(sys_version_info.get())); 114 EXPECT_TRUE(version_info_type.IsAllocated()); 115 PythonObject major_version_field = version_info_type.ResolveName("major"); 116 EXPECT_TRUE(major_version_field.IsAllocated()); 117 } 118 119 TEST_F(PythonDataObjectsTest, TestInstanceNameResolutionNoDot) { 120 PythonObject sys_version_info = m_sys_module.ResolveName("version_info"); 121 PythonObject major_version_field = sys_version_info.ResolveName("major"); 122 PythonObject minor_version_field = sys_version_info.ResolveName("minor"); 123 124 EXPECT_TRUE(major_version_field.IsAllocated()); 125 EXPECT_TRUE(minor_version_field.IsAllocated()); 126 127 PythonInteger major_version_value = 128 major_version_field.AsType<PythonInteger>(); 129 PythonInteger minor_version_value = 130 minor_version_field.AsType<PythonInteger>(); 131 132 EXPECT_EQ(PY_MAJOR_VERSION, major_version_value.GetInteger()); 133 EXPECT_EQ(PY_MINOR_VERSION, minor_version_value.GetInteger()); 134 } 135 136 TEST_F(PythonDataObjectsTest, TestGlobalNameResolutionWithDot) { 137 PythonObject sys_path = m_main_module.ResolveName("sys.path"); 138 EXPECT_TRUE(sys_path.IsAllocated()); 139 EXPECT_TRUE(PythonList::Check(sys_path.get())); 140 141 PythonInteger version_major = 142 m_main_module.ResolveName("sys.version_info.major") 143 .AsType<PythonInteger>(); 144 PythonInteger version_minor = 145 m_main_module.ResolveName("sys.version_info.minor") 146 .AsType<PythonInteger>(); 147 EXPECT_TRUE(version_major.IsAllocated()); 148 EXPECT_TRUE(version_minor.IsAllocated()); 149 EXPECT_EQ(PY_MAJOR_VERSION, version_major.GetInteger()); 150 EXPECT_EQ(PY_MINOR_VERSION, version_minor.GetInteger()); 151 } 152 153 TEST_F(PythonDataObjectsTest, TestDictionaryResolutionWithDot) { 154 // Make up a custom dictionary with "sys" pointing to the `sys` module. 155 PythonDictionary dict(PyInitialValue::Empty); 156 dict.SetItemForKey(PythonString("sys"), m_sys_module); 157 158 // Now use that dictionary to resolve `sys.version_info.major` 159 PythonInteger version_major = 160 PythonObject::ResolveNameWithDictionary("sys.version_info.major", dict) 161 .AsType<PythonInteger>(); 162 PythonInteger version_minor = 163 PythonObject::ResolveNameWithDictionary("sys.version_info.minor", dict) 164 .AsType<PythonInteger>(); 165 EXPECT_EQ(PY_MAJOR_VERSION, version_major.GetInteger()); 166 EXPECT_EQ(PY_MINOR_VERSION, version_minor.GetInteger()); 167 } 168 169 TEST_F(PythonDataObjectsTest, TestPythonInteger) { 170 // Test that integers behave correctly when wrapped by a PythonInteger. 171 172 #if PY_MAJOR_VERSION < 3 173 // Verify that `PythonInt` works correctly when given a PyInt object. 174 // Note that PyInt doesn't exist in Python 3.x, so this is only for 2.x 175 PyObject *py_int = PyInt_FromLong(12); 176 EXPECT_TRUE(PythonInteger::Check(py_int)); 177 PythonInteger python_int(PyRefType::Owned, py_int); 178 179 EXPECT_EQ(PyObjectType::Integer, python_int.GetObjectType()); 180 EXPECT_EQ(12, python_int.GetInteger()); 181 #endif 182 183 // Verify that `PythonInteger` works correctly when given a PyLong object. 184 PyObject *py_long = PyLong_FromLong(12); 185 EXPECT_TRUE(PythonInteger::Check(py_long)); 186 PythonInteger python_long(PyRefType::Owned, py_long); 187 EXPECT_EQ(PyObjectType::Integer, python_long.GetObjectType()); 188 189 // Verify that you can reset the value and that it is reflected properly. 190 python_long.SetInteger(40); 191 EXPECT_EQ(40, python_long.GetInteger()); 192 193 // Test that creating a `PythonInteger` object works correctly with the 194 // int constructor. 195 PythonInteger constructed_int(7); 196 EXPECT_EQ(7, constructed_int.GetInteger()); 197 } 198 199 TEST_F(PythonDataObjectsTest, TestPythonBoolean) { 200 // Test PythonBoolean constructed from Py_True 201 EXPECT_TRUE(PythonBoolean::Check(Py_True)); 202 PythonBoolean python_true(PyRefType::Owned, Py_True); 203 EXPECT_EQ(PyObjectType::Boolean, python_true.GetObjectType()); 204 205 // Test PythonBoolean constructed from Py_False 206 EXPECT_TRUE(PythonBoolean::Check(Py_False)); 207 PythonBoolean python_false(PyRefType::Owned, Py_False); 208 EXPECT_EQ(PyObjectType::Boolean, python_false.GetObjectType()); 209 210 auto test_from_long = [](long value) { 211 PyObject *py_bool = PyBool_FromLong(value); 212 EXPECT_TRUE(PythonBoolean::Check(py_bool)); 213 PythonBoolean python_boolean(PyRefType::Owned, py_bool); 214 EXPECT_EQ(PyObjectType::Boolean, python_boolean.GetObjectType()); 215 EXPECT_EQ(bool(value), python_boolean.GetValue()); 216 }; 217 218 // Test PythonBoolean constructed from long integer values. 219 test_from_long(0); // Test 'false' value. 220 test_from_long(1); // Test 'true' value. 221 test_from_long(~0); // Any value != 0 is 'true'. 222 } 223 224 TEST_F(PythonDataObjectsTest, TestPythonBytes) { 225 static const char *test_bytes = "PythonDataObjectsTest::TestPythonBytes"; 226 PyObject *py_bytes = PyBytes_FromString(test_bytes); 227 EXPECT_TRUE(PythonBytes::Check(py_bytes)); 228 PythonBytes python_bytes(PyRefType::Owned, py_bytes); 229 230 #if PY_MAJOR_VERSION < 3 231 EXPECT_TRUE(PythonString::Check(py_bytes)); 232 EXPECT_EQ(PyObjectType::String, python_bytes.GetObjectType()); 233 #else 234 EXPECT_FALSE(PythonString::Check(py_bytes)); 235 EXPECT_EQ(PyObjectType::Bytes, python_bytes.GetObjectType()); 236 #endif 237 238 llvm::ArrayRef<uint8_t> bytes = python_bytes.GetBytes(); 239 EXPECT_EQ(bytes.size(), strlen(test_bytes)); 240 EXPECT_EQ(0, ::memcmp(bytes.data(), test_bytes, bytes.size())); 241 } 242 243 TEST_F(PythonDataObjectsTest, TestPythonByteArray) { 244 static const char *test_bytes = "PythonDataObjectsTest::TestPythonByteArray"; 245 llvm::StringRef orig_bytes(test_bytes); 246 PyObject *py_bytes = 247 PyByteArray_FromStringAndSize(test_bytes, orig_bytes.size()); 248 EXPECT_TRUE(PythonByteArray::Check(py_bytes)); 249 PythonByteArray python_bytes(PyRefType::Owned, py_bytes); 250 EXPECT_EQ(PyObjectType::ByteArray, python_bytes.GetObjectType()); 251 252 llvm::ArrayRef<uint8_t> after_bytes = python_bytes.GetBytes(); 253 EXPECT_EQ(after_bytes.size(), orig_bytes.size()); 254 EXPECT_EQ(0, ::memcmp(orig_bytes.data(), test_bytes, orig_bytes.size())); 255 } 256 257 TEST_F(PythonDataObjectsTest, TestPythonString) { 258 // Test that strings behave correctly when wrapped by a PythonString. 259 260 static const char *test_string = "PythonDataObjectsTest::TestPythonString1"; 261 static const char *test_string2 = "PythonDataObjectsTest::TestPythonString2"; 262 263 #if PY_MAJOR_VERSION < 3 264 // Verify that `PythonString` works correctly when given a PyString object. 265 // Note that PyString doesn't exist in Python 3.x, so this is only for 2.x 266 PyObject *py_string = PyString_FromString(test_string); 267 EXPECT_TRUE(PythonString::Check(py_string)); 268 PythonString python_string(PyRefType::Owned, py_string); 269 270 EXPECT_EQ(PyObjectType::String, python_string.GetObjectType()); 271 EXPECT_STREQ(test_string, python_string.GetString().data()); 272 #else 273 // Verify that `PythonString` works correctly when given a PyUnicode object. 274 PyObject *py_unicode = PyUnicode_FromString(test_string); 275 EXPECT_TRUE(PythonString::Check(py_unicode)); 276 PythonString python_unicode(PyRefType::Owned, py_unicode); 277 EXPECT_EQ(PyObjectType::String, python_unicode.GetObjectType()); 278 EXPECT_STREQ(test_string, python_unicode.GetString().data()); 279 #endif 280 281 // Test that creating a `PythonString` object works correctly with the 282 // string constructor 283 PythonString constructed_string(test_string2); 284 EXPECT_EQ(test_string2, constructed_string.GetString()); 285 } 286 287 TEST_F(PythonDataObjectsTest, TestPythonStringToStr) { 288 const char *GetString = "PythonDataObjectsTest::TestPythonStringToStr"; 289 290 PythonString str(GetString); 291 EXPECT_EQ(GetString, str.GetString()); 292 293 PythonString str_str = str.Str(); 294 EXPECT_EQ(GetString, str_str.GetString()); 295 } 296 297 TEST_F(PythonDataObjectsTest, TestPythonIntegerToStr) {} 298 299 TEST_F(PythonDataObjectsTest, TestPythonIntegerToStructuredInteger) { 300 PythonInteger integer(7); 301 auto int_sp = integer.CreateStructuredInteger(); 302 EXPECT_EQ(7U, int_sp->GetValue()); 303 } 304 305 TEST_F(PythonDataObjectsTest, TestPythonStringToStructuredString) { 306 static const char *test_string = 307 "PythonDataObjectsTest::TestPythonStringToStructuredString"; 308 PythonString constructed_string(test_string); 309 auto string_sp = constructed_string.CreateStructuredString(); 310 EXPECT_EQ(test_string, string_sp->GetStringValue()); 311 } 312 313 TEST_F(PythonDataObjectsTest, TestPythonListValueEquality) { 314 // Test that a list which is built through the native 315 // Python API behaves correctly when wrapped by a PythonList. 316 static const unsigned list_size = 2; 317 static const long long_value0 = 5; 318 static const char *const string_value1 = "String Index 1"; 319 320 PyObject *py_list = PyList_New(2); 321 EXPECT_TRUE(PythonList::Check(py_list)); 322 PythonList list(PyRefType::Owned, py_list); 323 324 PythonObject list_items[list_size]; 325 list_items[0].Reset(PythonInteger(long_value0)); 326 list_items[1].Reset(PythonString(string_value1)); 327 328 for (unsigned i = 0; i < list_size; ++i) 329 list.SetItemAtIndex(i, list_items[i]); 330 331 EXPECT_EQ(list_size, list.GetSize()); 332 EXPECT_EQ(PyObjectType::List, list.GetObjectType()); 333 334 // Verify that the values match 335 PythonObject chk_value1 = list.GetItemAtIndex(0); 336 PythonObject chk_value2 = list.GetItemAtIndex(1); 337 EXPECT_TRUE(PythonInteger::Check(chk_value1.get())); 338 EXPECT_TRUE(PythonString::Check(chk_value2.get())); 339 340 PythonInteger chk_int(PyRefType::Borrowed, chk_value1.get()); 341 PythonString chk_str(PyRefType::Borrowed, chk_value2.get()); 342 343 EXPECT_EQ(long_value0, chk_int.GetInteger()); 344 EXPECT_EQ(string_value1, chk_str.GetString()); 345 } 346 347 TEST_F(PythonDataObjectsTest, TestPythonListManipulation) { 348 // Test that manipulation of a PythonList behaves correctly when 349 // wrapped by a PythonDictionary. 350 351 static const long long_value0 = 5; 352 static const char *const string_value1 = "String Index 1"; 353 354 PythonList list(PyInitialValue::Empty); 355 PythonInteger integer(long_value0); 356 PythonString string(string_value1); 357 358 list.AppendItem(integer); 359 list.AppendItem(string); 360 EXPECT_EQ(2U, list.GetSize()); 361 362 // Verify that the values match 363 PythonObject chk_value1 = list.GetItemAtIndex(0); 364 PythonObject chk_value2 = list.GetItemAtIndex(1); 365 EXPECT_TRUE(PythonInteger::Check(chk_value1.get())); 366 EXPECT_TRUE(PythonString::Check(chk_value2.get())); 367 368 PythonInteger chk_int(PyRefType::Borrowed, chk_value1.get()); 369 PythonString chk_str(PyRefType::Borrowed, chk_value2.get()); 370 371 EXPECT_EQ(long_value0, chk_int.GetInteger()); 372 EXPECT_EQ(string_value1, chk_str.GetString()); 373 } 374 375 TEST_F(PythonDataObjectsTest, TestPythonListToStructuredList) { 376 static const long long_value0 = 5; 377 static const char *const string_value1 = "String Index 1"; 378 379 PythonList list(PyInitialValue::Empty); 380 list.AppendItem(PythonInteger(long_value0)); 381 list.AppendItem(PythonString(string_value1)); 382 383 auto array_sp = list.CreateStructuredArray(); 384 EXPECT_EQ(lldb::eStructuredDataTypeInteger, 385 array_sp->GetItemAtIndex(0)->GetType()); 386 EXPECT_EQ(lldb::eStructuredDataTypeString, 387 array_sp->GetItemAtIndex(1)->GetType()); 388 389 auto int_sp = array_sp->GetItemAtIndex(0)->GetAsInteger(); 390 auto string_sp = array_sp->GetItemAtIndex(1)->GetAsString(); 391 392 EXPECT_EQ(long_value0, long(int_sp->GetValue())); 393 EXPECT_EQ(string_value1, string_sp->GetValue()); 394 } 395 396 TEST_F(PythonDataObjectsTest, TestPythonTupleSize) { 397 PythonTuple tuple(PyInitialValue::Empty); 398 EXPECT_EQ(0U, tuple.GetSize()); 399 400 tuple = PythonTuple(3); 401 EXPECT_EQ(3U, tuple.GetSize()); 402 } 403 404 TEST_F(PythonDataObjectsTest, TestPythonTupleValues) { 405 PythonTuple tuple(3); 406 407 PythonInteger int_value(1); 408 PythonString string_value("Test"); 409 PythonObject none_value(PyRefType::Borrowed, Py_None); 410 411 tuple.SetItemAtIndex(0, int_value); 412 tuple.SetItemAtIndex(1, string_value); 413 tuple.SetItemAtIndex(2, none_value); 414 415 EXPECT_EQ(tuple.GetItemAtIndex(0).get(), int_value.get()); 416 EXPECT_EQ(tuple.GetItemAtIndex(1).get(), string_value.get()); 417 EXPECT_EQ(tuple.GetItemAtIndex(2).get(), none_value.get()); 418 } 419 420 TEST_F(PythonDataObjectsTest, TestPythonTupleInitializerList) { 421 PythonInteger int_value(1); 422 PythonString string_value("Test"); 423 PythonObject none_value(PyRefType::Borrowed, Py_None); 424 PythonTuple tuple{int_value, string_value, none_value}; 425 EXPECT_EQ(3U, tuple.GetSize()); 426 427 EXPECT_EQ(tuple.GetItemAtIndex(0).get(), int_value.get()); 428 EXPECT_EQ(tuple.GetItemAtIndex(1).get(), string_value.get()); 429 EXPECT_EQ(tuple.GetItemAtIndex(2).get(), none_value.get()); 430 } 431 432 TEST_F(PythonDataObjectsTest, TestPythonTupleInitializerList2) { 433 PythonInteger int_value(1); 434 PythonString string_value("Test"); 435 PythonObject none_value(PyRefType::Borrowed, Py_None); 436 437 PythonTuple tuple{int_value.get(), string_value.get(), none_value.get()}; 438 EXPECT_EQ(3U, tuple.GetSize()); 439 440 EXPECT_EQ(tuple.GetItemAtIndex(0).get(), int_value.get()); 441 EXPECT_EQ(tuple.GetItemAtIndex(1).get(), string_value.get()); 442 EXPECT_EQ(tuple.GetItemAtIndex(2).get(), none_value.get()); 443 } 444 445 TEST_F(PythonDataObjectsTest, TestPythonTupleToStructuredList) { 446 PythonInteger int_value(1); 447 PythonString string_value("Test"); 448 449 PythonTuple tuple{int_value.get(), string_value.get()}; 450 451 auto array_sp = tuple.CreateStructuredArray(); 452 EXPECT_EQ(tuple.GetSize(), array_sp->GetSize()); 453 EXPECT_EQ(lldb::eStructuredDataTypeInteger, 454 array_sp->GetItemAtIndex(0)->GetType()); 455 EXPECT_EQ(lldb::eStructuredDataTypeString, 456 array_sp->GetItemAtIndex(1)->GetType()); 457 } 458 459 TEST_F(PythonDataObjectsTest, TestPythonDictionaryValueEquality) { 460 // Test that a dictionary which is built through the native 461 // Python API behaves correctly when wrapped by a PythonDictionary. 462 static const unsigned dict_entries = 2; 463 const char *key_0 = "Key 0"; 464 int key_1 = 1; 465 const int value_0 = 0; 466 const char *value_1 = "Value 1"; 467 468 PythonObject py_keys[dict_entries]; 469 PythonObject py_values[dict_entries]; 470 471 py_keys[0].Reset(PythonString(key_0)); 472 py_keys[1].Reset(PythonInteger(key_1)); 473 py_values[0].Reset(PythonInteger(value_0)); 474 py_values[1].Reset(PythonString(value_1)); 475 476 PyObject *py_dict = PyDict_New(); 477 EXPECT_TRUE(PythonDictionary::Check(py_dict)); 478 PythonDictionary dict(PyRefType::Owned, py_dict); 479 480 for (unsigned i = 0; i < dict_entries; ++i) 481 PyDict_SetItem(py_dict, py_keys[i].get(), py_values[i].get()); 482 EXPECT_EQ(dict.GetSize(), dict_entries); 483 EXPECT_EQ(PyObjectType::Dictionary, dict.GetObjectType()); 484 485 // Verify that the values match 486 PythonObject chk_value1 = dict.GetItemForKey(py_keys[0]); 487 PythonObject chk_value2 = dict.GetItemForKey(py_keys[1]); 488 EXPECT_TRUE(PythonInteger::Check(chk_value1.get())); 489 EXPECT_TRUE(PythonString::Check(chk_value2.get())); 490 491 PythonInteger chk_int(PyRefType::Borrowed, chk_value1.get()); 492 PythonString chk_str(PyRefType::Borrowed, chk_value2.get()); 493 494 EXPECT_EQ(value_0, chk_int.GetInteger()); 495 EXPECT_EQ(value_1, chk_str.GetString()); 496 } 497 498 TEST_F(PythonDataObjectsTest, TestPythonDictionaryManipulation) { 499 // Test that manipulation of a dictionary behaves correctly when wrapped 500 // by a PythonDictionary. 501 static const unsigned dict_entries = 2; 502 503 const char *const key_0 = "Key 0"; 504 const char *const key_1 = "Key 1"; 505 const long value_0 = 1; 506 const char *const value_1 = "Value 1"; 507 508 PythonString keys[dict_entries]; 509 PythonObject values[dict_entries]; 510 511 keys[0].Reset(PythonString(key_0)); 512 keys[1].Reset(PythonString(key_1)); 513 values[0].Reset(PythonInteger(value_0)); 514 values[1].Reset(PythonString(value_1)); 515 516 PythonDictionary dict(PyInitialValue::Empty); 517 for (int i = 0; i < 2; ++i) 518 dict.SetItemForKey(keys[i], values[i]); 519 520 EXPECT_EQ(dict_entries, dict.GetSize()); 521 522 // Verify that the keys and values match 523 PythonObject chk_value1 = dict.GetItemForKey(keys[0]); 524 PythonObject chk_value2 = dict.GetItemForKey(keys[1]); 525 EXPECT_TRUE(PythonInteger::Check(chk_value1.get())); 526 EXPECT_TRUE(PythonString::Check(chk_value2.get())); 527 528 PythonInteger chk_int(PyRefType::Borrowed, chk_value1.get()); 529 PythonString chk_str(PyRefType::Borrowed, chk_value2.get()); 530 531 EXPECT_EQ(value_0, chk_int.GetInteger()); 532 EXPECT_EQ(value_1, chk_str.GetString()); 533 } 534 535 TEST_F(PythonDataObjectsTest, TestPythonDictionaryToStructuredDictionary) { 536 static const char *const string_key0 = "String Key 0"; 537 static const char *const string_key1 = "String Key 1"; 538 539 static const char *const string_value0 = "String Value 0"; 540 static const long int_value1 = 7; 541 542 PythonDictionary dict(PyInitialValue::Empty); 543 dict.SetItemForKey(PythonString(string_key0), PythonString(string_value0)); 544 dict.SetItemForKey(PythonString(string_key1), PythonInteger(int_value1)); 545 546 auto dict_sp = dict.CreateStructuredDictionary(); 547 EXPECT_EQ(2U, dict_sp->GetSize()); 548 549 EXPECT_TRUE(dict_sp->HasKey(string_key0)); 550 EXPECT_TRUE(dict_sp->HasKey(string_key1)); 551 552 auto string_sp = dict_sp->GetValueForKey(string_key0)->GetAsString(); 553 auto int_sp = dict_sp->GetValueForKey(string_key1)->GetAsInteger(); 554 555 EXPECT_EQ(string_value0, string_sp->GetValue()); 556 EXPECT_EQ(int_value1, long(int_sp->GetValue())); 557 } 558 559 TEST_F(PythonDataObjectsTest, TestPythonCallableCheck) { 560 PythonObject sys_exc_info = m_sys_module.ResolveName("exc_info"); 561 PythonObject none(PyRefType::Borrowed, Py_None); 562 563 EXPECT_TRUE(PythonCallable::Check(sys_exc_info.get())); 564 EXPECT_FALSE(PythonCallable::Check(none.get())); 565 } 566 567 TEST_F(PythonDataObjectsTest, TestPythonCallableInvoke) { 568 auto list = m_builtins_module.ResolveName("list").AsType<PythonCallable>(); 569 PythonInteger one(1); 570 PythonString two("two"); 571 PythonTuple three = {one, two}; 572 573 PythonTuple tuple_to_convert = {one, two, three}; 574 PythonObject result = list({tuple_to_convert}); 575 576 EXPECT_TRUE(PythonList::Check(result.get())); 577 auto list_result = result.AsType<PythonList>(); 578 EXPECT_EQ(3U, list_result.GetSize()); 579 EXPECT_EQ(one.get(), list_result.GetItemAtIndex(0).get()); 580 EXPECT_EQ(two.get(), list_result.GetItemAtIndex(1).get()); 581 EXPECT_EQ(three.get(), list_result.GetItemAtIndex(2).get()); 582 } 583 584 TEST_F(PythonDataObjectsTest, TestPythonFile) { 585 auto file = FileSystem::Instance().Open(FileSpec(FileSystem::DEV_NULL), 586 File::eOpenOptionRead); 587 ASSERT_THAT_EXPECTED(file, llvm::Succeeded()); 588 auto py_file = PythonFile::FromFile(*file.get(), "r"); 589 ASSERT_THAT_EXPECTED(py_file, llvm::Succeeded()); 590 EXPECT_TRUE(PythonFile::Check(py_file.get().get())); 591 } 592 593 TEST_F(PythonDataObjectsTest, TestObjectAttributes) { 594 PythonInteger py_int(42); 595 EXPECT_TRUE(py_int.HasAttribute("numerator")); 596 EXPECT_FALSE(py_int.HasAttribute("this_should_not_exist")); 597 598 PythonInteger numerator_attr = 599 py_int.GetAttributeValue("numerator").AsType<PythonInteger>(); 600 EXPECT_TRUE(numerator_attr.IsAllocated()); 601 EXPECT_EQ(42, numerator_attr.GetInteger()); 602 } 603 604 TEST_F(PythonDataObjectsTest, TestExtractingUInt64ThroughStructuredData) { 605 // Make up a custom dictionary with "sys" pointing to the `sys` module. 606 const char *key_name = "addr"; 607 const uint64_t value = 0xf000000000000000ull; 608 PythonDictionary python_dict(PyInitialValue::Empty); 609 PythonInteger python_ull_value(PyRefType::Owned, 610 PyLong_FromUnsignedLongLong(value)); 611 python_dict.SetItemForKey(PythonString(key_name), python_ull_value); 612 StructuredData::ObjectSP structured_data_sp = 613 python_dict.CreateStructuredObject(); 614 EXPECT_TRUE((bool)structured_data_sp); 615 if (structured_data_sp) { 616 StructuredData::Dictionary *structured_dict_ptr = 617 structured_data_sp->GetAsDictionary(); 618 EXPECT_TRUE(structured_dict_ptr != nullptr); 619 if (structured_dict_ptr) { 620 StructuredData::ObjectSP structured_addr_value_sp = 621 structured_dict_ptr->GetValueForKey(key_name); 622 EXPECT_TRUE((bool)structured_addr_value_sp); 623 const uint64_t extracted_value = 624 structured_addr_value_sp->GetIntegerValue(123); 625 EXPECT_TRUE(extracted_value == value); 626 } 627 } 628 } 629