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