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