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