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 EXPECT_EQ(PyObjectType::Bytes, python_bytes.GetObjectType()); 213 214 #if PY_MAJOR_VERSION < 3 215 EXPECT_TRUE(PythonString::Check(py_bytes)); 216 EXPECT_EQ(PyObjectType::String, python_bytes.GetObjectType()); 217 #else 218 EXPECT_FALSE(PythonString::Check(py_bytes)); 219 EXPECT_NE(PyObjectType::String, python_bytes.GetObjectType()); 220 #endif 221 222 llvm::ArrayRef<uint8_t> bytes = python_bytes.GetBytes(); 223 EXPECT_EQ(bytes.size(), strlen(test_bytes)); 224 EXPECT_EQ(0, ::memcmp(bytes.data(), test_bytes, bytes.size())); 225 } 226 227 TEST_F(PythonDataObjectsTest, TestPythonString) 228 { 229 // Test that strings behave correctly when wrapped by a PythonString. 230 231 static const char *test_string = "PythonDataObjectsTest::TestPythonString1"; 232 static const char *test_string2 = "PythonDataObjectsTest::TestPythonString2"; 233 static const char *test_string3 = "PythonDataObjectsTest::TestPythonString3"; 234 235 #if PY_MAJOR_VERSION < 3 236 // Verify that `PythonString` works correctly when given a PyString object. 237 // Note that PyString doesn't exist in Python 3.x, so this is only for 2.x 238 PyObject *py_string = PyString_FromString(test_string); 239 EXPECT_TRUE(PythonString::Check(py_string)); 240 PythonString python_string(PyRefType::Owned, py_string); 241 242 EXPECT_EQ(PyObjectType::String, python_string.GetObjectType()); 243 EXPECT_STREQ(test_string, python_string.GetString().data()); 244 #else 245 // Verify that `PythonString` works correctly when given a PyUnicode object. 246 PyObject *py_unicode = PyUnicode_FromString(test_string); 247 EXPECT_TRUE(PythonString::Check(py_unicode)); 248 PythonString python_unicode(PyRefType::Owned, py_unicode); 249 EXPECT_EQ(PyObjectType::String, python_unicode.GetObjectType()); 250 EXPECT_STREQ(test_string, python_unicode.GetString().data()); 251 #endif 252 253 // Test that creating a `PythonString` object works correctly with the 254 // string constructor 255 PythonString constructed_string(test_string3); 256 EXPECT_STREQ(test_string3, constructed_string.GetString().str().c_str()); 257 } 258 259 TEST_F(PythonDataObjectsTest, TestPythonStringToStr) 260 { 261 const char *c_str = "PythonDataObjectsTest::TestPythonStringToStr"; 262 263 PythonString str(c_str); 264 EXPECT_STREQ(c_str, str.GetString().str().c_str()); 265 266 PythonString str_str = str.Str(); 267 EXPECT_STREQ(c_str, str_str.GetString().str().c_str()); 268 } 269 270 TEST_F(PythonDataObjectsTest, TestPythonIntegerToStr) 271 { 272 } 273 274 TEST_F(PythonDataObjectsTest, TestPythonIntegerToStructuredInteger) 275 { 276 PythonInteger integer(7); 277 auto int_sp = integer.CreateStructuredInteger(); 278 EXPECT_EQ(7, int_sp->GetValue()); 279 } 280 281 TEST_F(PythonDataObjectsTest, TestPythonStringToStructuredString) 282 { 283 static const char *test_string = "PythonDataObjectsTest::TestPythonStringToStructuredString"; 284 PythonString constructed_string(test_string); 285 auto string_sp = constructed_string.CreateStructuredString(); 286 EXPECT_STREQ(test_string, string_sp->GetStringValue().c_str()); 287 } 288 289 TEST_F(PythonDataObjectsTest, TestPythonListValueEquality) 290 { 291 // Test that a list which is built through the native 292 // Python API behaves correctly when wrapped by a PythonList. 293 static const int list_size = 2; 294 static const long long_value0 = 5; 295 static const char *const string_value1 = "String Index 1"; 296 297 PyObject *py_list = PyList_New(2); 298 EXPECT_TRUE(PythonList::Check(py_list)); 299 PythonList list(PyRefType::Owned, py_list); 300 301 PythonObject list_items[list_size]; 302 list_items[0].Reset(PythonInteger(long_value0)); 303 list_items[1].Reset(PythonString(string_value1)); 304 305 for (int i = 0; i < list_size; ++i) 306 list.SetItemAtIndex(i, list_items[i]); 307 308 EXPECT_EQ(list_size, list.GetSize()); 309 EXPECT_EQ(PyObjectType::List, list.GetObjectType()); 310 311 // Verify that the values match 312 PythonObject chk_value1 = list.GetItemAtIndex(0); 313 PythonObject chk_value2 = list.GetItemAtIndex(1); 314 EXPECT_TRUE(PythonInteger::Check(chk_value1.get())); 315 EXPECT_TRUE(PythonString::Check(chk_value2.get())); 316 317 PythonInteger chk_int(PyRefType::Borrowed, chk_value1.get()); 318 PythonString chk_str(PyRefType::Borrowed, chk_value2.get()); 319 320 EXPECT_EQ(long_value0, chk_int.GetInteger()); 321 EXPECT_STREQ(string_value1, chk_str.GetString().str().c_str()); 322 } 323 324 TEST_F(PythonDataObjectsTest, TestPythonListManipulation) 325 { 326 // Test that manipulation of a PythonList behaves correctly when 327 // wrapped by a PythonDictionary. 328 329 static const long long_value0 = 5; 330 static const char *const string_value1 = "String Index 1"; 331 332 PythonList list(PyInitialValue::Empty); 333 PythonInteger integer(long_value0); 334 PythonString string(string_value1); 335 336 list.AppendItem(integer); 337 list.AppendItem(string); 338 EXPECT_EQ(2, list.GetSize()); 339 340 // Verify that the values match 341 PythonObject chk_value1 = list.GetItemAtIndex(0); 342 PythonObject chk_value2 = list.GetItemAtIndex(1); 343 EXPECT_TRUE(PythonInteger::Check(chk_value1.get())); 344 EXPECT_TRUE(PythonString::Check(chk_value2.get())); 345 346 PythonInteger chk_int(PyRefType::Borrowed, chk_value1.get()); 347 PythonString chk_str(PyRefType::Borrowed, chk_value2.get()); 348 349 EXPECT_EQ(long_value0, chk_int.GetInteger()); 350 EXPECT_STREQ(string_value1, chk_str.GetString().str().c_str()); 351 } 352 353 TEST_F(PythonDataObjectsTest, TestPythonListToStructuredList) 354 { 355 static const long long_value0 = 5; 356 static const char *const string_value1 = "String Index 1"; 357 358 PythonList list(PyInitialValue::Empty); 359 list.AppendItem(PythonInteger(long_value0)); 360 list.AppendItem(PythonString(string_value1)); 361 362 auto array_sp = list.CreateStructuredArray(); 363 EXPECT_EQ(StructuredData::Type::eTypeInteger, array_sp->GetItemAtIndex(0)->GetType()); 364 EXPECT_EQ(StructuredData::Type::eTypeString, array_sp->GetItemAtIndex(1)->GetType()); 365 366 auto int_sp = array_sp->GetItemAtIndex(0)->GetAsInteger(); 367 auto string_sp = array_sp->GetItemAtIndex(1)->GetAsString(); 368 369 EXPECT_EQ(long_value0, int_sp->GetValue()); 370 EXPECT_STREQ(string_value1, string_sp->GetValue().c_str()); 371 } 372 373 TEST_F(PythonDataObjectsTest, TestPythonTupleSize) 374 { 375 PythonTuple tuple(PyInitialValue::Empty); 376 EXPECT_EQ(0, tuple.GetSize()); 377 378 tuple = PythonTuple(3); 379 EXPECT_EQ(3, tuple.GetSize()); 380 } 381 382 TEST_F(PythonDataObjectsTest, TestPythonTupleValues) 383 { 384 PythonTuple tuple(3); 385 386 PythonInteger int_value(1); 387 PythonString string_value("Test"); 388 PythonObject none_value(PyRefType::Borrowed, Py_None); 389 390 tuple.SetItemAtIndex(0, int_value); 391 tuple.SetItemAtIndex(1, string_value); 392 tuple.SetItemAtIndex(2, none_value); 393 394 EXPECT_EQ(tuple.GetItemAtIndex(0).get(), int_value.get()); 395 EXPECT_EQ(tuple.GetItemAtIndex(1).get(), string_value.get()); 396 EXPECT_EQ(tuple.GetItemAtIndex(2).get(), none_value.get()); 397 } 398 399 TEST_F(PythonDataObjectsTest, TestPythonTupleInitializerList) 400 { 401 PythonInteger int_value(1); 402 PythonString string_value("Test"); 403 PythonObject none_value(PyRefType::Borrowed, Py_None); 404 PythonTuple tuple{ int_value, string_value, none_value }; 405 EXPECT_EQ(3, tuple.GetSize()); 406 407 EXPECT_EQ(tuple.GetItemAtIndex(0).get(), int_value.get()); 408 EXPECT_EQ(tuple.GetItemAtIndex(1).get(), string_value.get()); 409 EXPECT_EQ(tuple.GetItemAtIndex(2).get(), none_value.get()); 410 } 411 412 TEST_F(PythonDataObjectsTest, TestPythonTupleInitializerList2) 413 { 414 PythonInteger int_value(1); 415 PythonString string_value("Test"); 416 PythonObject none_value(PyRefType::Borrowed, Py_None); 417 418 PythonTuple tuple{ int_value.get(), string_value.get(), none_value.get() }; 419 EXPECT_EQ(3, tuple.GetSize()); 420 421 EXPECT_EQ(tuple.GetItemAtIndex(0).get(), int_value.get()); 422 EXPECT_EQ(tuple.GetItemAtIndex(1).get(), string_value.get()); 423 EXPECT_EQ(tuple.GetItemAtIndex(2).get(), none_value.get()); 424 } 425 426 TEST_F(PythonDataObjectsTest, TestPythonTupleToStructuredList) 427 { 428 PythonInteger int_value(1); 429 PythonString string_value("Test"); 430 431 PythonTuple tuple{ int_value.get(), string_value.get() }; 432 433 auto array_sp = tuple.CreateStructuredArray(); 434 EXPECT_EQ(tuple.GetSize(), array_sp->GetSize()); 435 EXPECT_EQ(StructuredData::Type::eTypeInteger, array_sp->GetItemAtIndex(0)->GetType()); 436 EXPECT_EQ(StructuredData::Type::eTypeString, array_sp->GetItemAtIndex(1)->GetType()); 437 } 438 439 TEST_F(PythonDataObjectsTest, TestPythonDictionaryValueEquality) 440 { 441 // Test that a dictionary which is built through the native 442 // Python API behaves correctly when wrapped by a PythonDictionary. 443 static const int dict_entries = 2; 444 const char *key_0 = "Key 0"; 445 int key_1 = 1; 446 const int value_0 = 0; 447 const char *value_1 = "Value 1"; 448 449 PythonObject py_keys[dict_entries]; 450 PythonObject py_values[dict_entries]; 451 452 py_keys[0].Reset(PythonString(key_0)); 453 py_keys[1].Reset(PythonInteger(key_1)); 454 py_values[0].Reset(PythonInteger(value_0)); 455 py_values[1].Reset(PythonString(value_1)); 456 457 PyObject *py_dict = PyDict_New(); 458 EXPECT_TRUE(PythonDictionary::Check(py_dict)); 459 PythonDictionary dict(PyRefType::Owned, py_dict); 460 461 for (int i = 0; i < dict_entries; ++i) 462 PyDict_SetItem(py_dict, py_keys[i].get(), py_values[i].get()); 463 EXPECT_EQ(dict.GetSize(), dict_entries); 464 EXPECT_EQ(PyObjectType::Dictionary, dict.GetObjectType()); 465 466 // Verify that the values match 467 PythonObject chk_value1 = dict.GetItemForKey(py_keys[0]); 468 PythonObject chk_value2 = dict.GetItemForKey(py_keys[1]); 469 EXPECT_TRUE(PythonInteger::Check(chk_value1.get())); 470 EXPECT_TRUE(PythonString::Check(chk_value2.get())); 471 472 PythonInteger chk_int(PyRefType::Borrowed, chk_value1.get()); 473 PythonString chk_str(PyRefType::Borrowed, chk_value2.get()); 474 475 EXPECT_EQ(value_0, chk_int.GetInteger()); 476 EXPECT_STREQ(value_1, chk_str.GetString().str().c_str()); 477 } 478 479 TEST_F(PythonDataObjectsTest, TestPythonDictionaryManipulation) 480 { 481 // Test that manipulation of a dictionary behaves correctly when wrapped 482 // by a PythonDictionary. 483 static const int dict_entries = 2; 484 485 const char *const key_0 = "Key 0"; 486 const char *const key_1 = "Key 1"; 487 const long value_0 = 1; 488 const char *const value_1 = "Value 1"; 489 490 PythonString keys[dict_entries]; 491 PythonObject values[dict_entries]; 492 493 keys[0].Reset(PythonString(key_0)); 494 keys[1].Reset(PythonString(key_1)); 495 values[0].Reset(PythonInteger(value_0)); 496 values[1].Reset(PythonString(value_1)); 497 498 PythonDictionary dict(PyInitialValue::Empty); 499 for (int i = 0; i < 2; ++i) 500 dict.SetItemForKey(keys[i], values[i]); 501 502 EXPECT_EQ(dict_entries, dict.GetSize()); 503 504 // Verify that the keys and values match 505 PythonObject chk_value1 = dict.GetItemForKey(keys[0]); 506 PythonObject chk_value2 = dict.GetItemForKey(keys[1]); 507 EXPECT_TRUE(PythonInteger::Check(chk_value1.get())); 508 EXPECT_TRUE(PythonString::Check(chk_value2.get())); 509 510 PythonInteger chk_int(PyRefType::Borrowed, chk_value1.get()); 511 PythonString chk_str(PyRefType::Borrowed, chk_value2.get()); 512 513 EXPECT_EQ(value_0, chk_int.GetInteger()); 514 EXPECT_STREQ(value_1, chk_str.GetString().str().c_str()); 515 } 516 517 TEST_F(PythonDataObjectsTest, TestPythonDictionaryToStructuredDictionary) 518 { 519 static const char *const string_key0 = "String Key 0"; 520 static const char *const string_key1 = "String Key 1"; 521 522 static const char *const string_value0 = "String Value 0"; 523 static const long int_value1 = 7; 524 525 PythonDictionary dict(PyInitialValue::Empty); 526 dict.SetItemForKey(PythonString(string_key0), PythonString(string_value0)); 527 dict.SetItemForKey(PythonString(string_key1), PythonInteger(int_value1)); 528 529 auto dict_sp = dict.CreateStructuredDictionary(); 530 EXPECT_EQ(2, dict_sp->GetSize()); 531 532 EXPECT_TRUE(dict_sp->HasKey(string_key0)); 533 EXPECT_TRUE(dict_sp->HasKey(string_key1)); 534 535 auto string_sp = dict_sp->GetValueForKey(string_key0)->GetAsString(); 536 auto int_sp = dict_sp->GetValueForKey(string_key1)->GetAsInteger(); 537 538 EXPECT_STREQ(string_value0, string_sp->GetValue().c_str()); 539 EXPECT_EQ(int_value1, int_sp->GetValue()); 540 } 541 542 TEST_F(PythonDataObjectsTest, TestPythonCallableCheck) 543 { 544 PythonObject sys_exc_info = m_sys_module.ResolveName("exc_info"); 545 PythonObject none(PyRefType::Borrowed, Py_None); 546 547 EXPECT_TRUE(PythonCallable::Check(sys_exc_info.get())); 548 EXPECT_FALSE(PythonCallable::Check(none.get())); 549 } 550 551 TEST_F(PythonDataObjectsTest, TestPythonCallableInvoke) 552 { 553 auto list = m_builtins_module.ResolveName("list").AsType<PythonCallable>(); 554 PythonInteger one(1); 555 PythonString two("two"); 556 PythonTuple three = { one, two }; 557 558 PythonTuple tuple_to_convert = { one, two, three }; 559 PythonObject result = list({ tuple_to_convert }); 560 561 EXPECT_TRUE(PythonList::Check(result.get())); 562 auto list_result = result.AsType<PythonList>(); 563 EXPECT_EQ(3, list_result.GetSize()); 564 EXPECT_EQ(one.get(), list_result.GetItemAtIndex(0).get()); 565 EXPECT_EQ(two.get(), list_result.GetItemAtIndex(1).get()); 566 EXPECT_EQ(three.get(), list_result.GetItemAtIndex(2).get()); 567 } 568 569 TEST_F(PythonDataObjectsTest, TestPythonFile) 570 { 571 File file(FileSystem::DEV_NULL, File::eOpenOptionRead); 572 PythonFile py_file(file, "r"); 573 EXPECT_TRUE(PythonFile::Check(py_file.get())); 574 } 575 576 TEST_F(PythonDataObjectsTest, TestObjectAttributes) 577 { 578 PythonInteger py_int(42); 579 EXPECT_TRUE(py_int.HasAttribute("numerator")); 580 EXPECT_FALSE(py_int.HasAttribute("this_should_not_exist")); 581 582 PythonInteger numerator_attr = py_int.GetAttributeValue("numerator").AsType<PythonInteger>(); 583 EXPECT_TRUE(numerator_attr.IsAllocated()); 584 EXPECT_EQ(42, numerator_attr.GetInteger()); 585 }