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