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