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