xref: /llvm-project/lldb/unittests/ScriptInterpreter/Python/PythonDataObjectsTests.cpp (revision 7841efbb9255126763167b9d7d59242333c6f1bd)
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 using namespace lldb_private;
20 
21 class PythonDataObjectsTest : public testing::Test
22 {
23   public:
24     void
25     SetUp() override
26     {
27         HostInfoBase::Initialize();
28         // ScriptInterpreterPython::Initialize() depends on HostInfo being
29         // initializedso it can compute the python directory etc.
30         ScriptInterpreterPython::Initialize();
31 
32         // Although we don't care about concurrency for the purposes of running
33         // this test suite, Python requires the GIL to be locked even for
34         // deallocating memory, which can happen when you call Py_DECREF or
35         // Py_INCREF.  So acquire the GIL for the entire duration of this
36         // test suite.
37         m_gil_state = PyGILState_Ensure();
38     }
39 
40     void
41     TearDown() override
42     {
43         PyGILState_Release(m_gil_state);
44 
45         ScriptInterpreterPython::Terminate();
46     }
47 
48   private:
49     PyGILState_STATE m_gil_state;
50 };
51 
52 TEST_F(PythonDataObjectsTest, TestOwnedReferences)
53 {
54     // After creating a new object, the refcount should be >= 1
55     PyObject *obj = PyLong_FromLong(3);
56     Py_ssize_t original_refcnt = obj->ob_refcnt;
57     EXPECT_LE(1, original_refcnt);
58 
59     // If we take an owned reference, the refcount should be the same
60     PythonObject owned_long(PyRefType::Owned, obj);
61     EXPECT_EQ(original_refcnt, owned_long.get()->ob_refcnt);
62 
63     // Take another reference and verify that the refcount increases by 1
64     PythonObject strong_ref(owned_long);
65     EXPECT_EQ(original_refcnt + 1, strong_ref.get()->ob_refcnt);
66 
67     // If we reset the first one, the refcount should be the original value.
68     owned_long.Reset();
69     EXPECT_EQ(original_refcnt, strong_ref.get()->ob_refcnt);
70 }
71 
72 TEST_F(PythonDataObjectsTest, TestResetting)
73 {
74     PythonDictionary dict(PyInitialValue::Empty);
75 
76     PyObject *new_dict = PyDict_New();
77     dict.Reset(PyRefType::Owned, new_dict);
78     EXPECT_EQ(new_dict, dict.get());
79 
80     dict.Reset(PyRefType::Owned, nullptr);
81     EXPECT_EQ(nullptr, dict.get());
82 
83     dict.Reset(PyRefType::Owned, PyDict_New());
84     EXPECT_NE(nullptr, dict.get());
85     dict.Reset();
86     EXPECT_EQ(nullptr, dict.get());
87 }
88 
89 TEST_F(PythonDataObjectsTest, TestBorrowedReferences)
90 {
91     PythonInteger long_value(PyRefType::Owned, PyLong_FromLong(3));
92     Py_ssize_t original_refcnt = long_value.get()->ob_refcnt;
93     EXPECT_LE(1, original_refcnt);
94 
95     PythonInteger borrowed_long(PyRefType::Borrowed, long_value.get());
96     EXPECT_EQ(original_refcnt + 1, borrowed_long.get()->ob_refcnt);
97 }
98 
99 TEST_F(PythonDataObjectsTest, TestGlobalNameResolutionNoDot)
100 {
101     PythonObject sys_module = PythonObject::ResolveNameGlobal("sys");
102     EXPECT_TRUE(sys_module.IsAllocated());
103     EXPECT_TRUE(PythonModule::Check(sys_module.get()));
104 }
105 
106 TEST_F(PythonDataObjectsTest, TestModuleNameResolutionNoDot)
107 {
108     PythonObject sys_module = PythonObject::ResolveNameGlobal("sys");
109     PythonObject sys_path = sys_module.ResolveName("path");
110     PythonObject sys_version_info = sys_module.ResolveName("version_info");
111     EXPECT_TRUE(sys_path.IsAllocated());
112     EXPECT_TRUE(sys_version_info.IsAllocated());
113 
114     EXPECT_TRUE(PythonList::Check(sys_path.get()));
115 }
116 
117 TEST_F(PythonDataObjectsTest, TestTypeNameResolutionNoDot)
118 {
119     PythonObject sys_module = PythonObject::ResolveNameGlobal("sys");
120     PythonObject sys_version_info = 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_module = PythonObject::ResolveNameGlobal("sys");
131     PythonObject sys_version_info = sys_module.ResolveName("version_info");
132     PythonObject major_version_field = sys_version_info.ResolveName("major");
133     PythonObject minor_version_field = sys_version_info.ResolveName("minor");
134 
135     EXPECT_TRUE(major_version_field.IsAllocated());
136     EXPECT_TRUE(minor_version_field.IsAllocated());
137 
138     PythonInteger major_version_value = major_version_field.AsType<PythonInteger>();
139     PythonInteger minor_version_value = minor_version_field.AsType<PythonInteger>();
140 
141     EXPECT_EQ(PY_MAJOR_VERSION, major_version_value.GetInteger());
142     EXPECT_EQ(PY_MINOR_VERSION, minor_version_value.GetInteger());
143 }
144 
145 TEST_F(PythonDataObjectsTest, TestGlobalNameResolutionWithDot)
146 {
147     PythonObject sys_path = PythonObject::ResolveNameGlobal("sys.path");
148     EXPECT_TRUE(sys_path.IsAllocated());
149     EXPECT_TRUE(PythonList::Check(sys_path.get()));
150 
151     PythonInteger version_major = PythonObject::ResolveNameGlobal("sys.version_info.major").AsType<PythonInteger>();
152     PythonInteger version_minor = PythonObject::ResolveNameGlobal("sys.version_info.minor").AsType<PythonInteger>();
153     EXPECT_TRUE(version_major.IsAllocated());
154     EXPECT_TRUE(version_minor.IsAllocated());
155     EXPECT_EQ(PY_MAJOR_VERSION, version_major.GetInteger());
156     EXPECT_EQ(PY_MINOR_VERSION, version_minor.GetInteger());
157 }
158 
159 TEST_F(PythonDataObjectsTest, TestPythonInteger)
160 {
161 // Test that integers behave correctly when wrapped by a PythonInteger.
162 
163 #if PY_MAJOR_VERSION < 3
164     // Verify that `PythonInt` works correctly when given a PyInt object.
165     // Note that PyInt doesn't exist in Python 3.x, so this is only for 2.x
166     PyObject *py_int = PyInt_FromLong(12);
167     EXPECT_TRUE(PythonInteger::Check(py_int));
168     PythonInteger python_int(PyRefType::Owned, py_int);
169 
170     EXPECT_EQ(PyObjectType::Integer, python_int.GetObjectType());
171     EXPECT_EQ(12, python_int.GetInteger());
172 #endif
173 
174     // Verify that `PythonInteger` works correctly when given a PyLong object.
175     PyObject *py_long = PyLong_FromLong(12);
176     EXPECT_TRUE(PythonInteger::Check(py_long));
177     PythonInteger python_long(PyRefType::Owned, py_long);
178     EXPECT_EQ(PyObjectType::Integer, python_long.GetObjectType());
179 
180     // Verify that you can reset the value and that it is reflected properly.
181     python_long.SetInteger(40);
182     EXPECT_EQ(40, python_long.GetInteger());
183 
184     // Test that creating a `PythonInteger` object works correctly with the
185     // int constructor.
186     PythonInteger constructed_int(7);
187     EXPECT_EQ(7, constructed_int.GetInteger());
188 }
189 
190 TEST_F(PythonDataObjectsTest, TestPythonString)
191 {
192     // Test that strings behave correctly when wrapped by a PythonString.
193 
194     static const char *test_string = "PythonDataObjectsTest::TestPythonString1";
195     static const char *test_string2 = "PythonDataObjectsTest::TestPythonString2";
196     static const char *test_string3 = "PythonDataObjectsTest::TestPythonString3";
197 
198 #if PY_MAJOR_VERSION < 3
199     // Verify that `PythonString` works correctly when given a PyString object.
200     // Note that PyString doesn't exist in Python 3.x, so this is only for 2.x
201     PyObject *py_string = PyString_FromString(test_string);
202     EXPECT_TRUE(PythonString::Check(py_string));
203     PythonString python_string(PyRefType::Owned, py_string);
204 
205     EXPECT_EQ(PyObjectType::String, python_string.GetObjectType());
206     EXPECT_STREQ(test_string, python_string.GetString().data());
207 #else
208     // Verify that `PythonString` works correctly when given a PyUnicode object.
209     PyObject *py_unicode = PyUnicode_FromString(test_string);
210     EXPECT_TRUE(PythonString::Check(py_unicode));
211     PythonString python_unicode(PyRefType::Owned, py_unicode);
212     EXPECT_EQ(PyObjectType::String, python_unicode.GetObjectType());
213     EXPECT_STREQ(test_string, python_unicode.GetString().data());
214 #endif
215 
216     // Test that creating a `PythonString` object works correctly with the
217     // string constructor
218     PythonString constructed_string(test_string3);
219     EXPECT_STREQ(test_string3, constructed_string.GetString().str().c_str());
220 }
221 
222 TEST_F(PythonDataObjectsTest, TestPythonStringToStr)
223 {
224     const char *c_str = "PythonDataObjectsTest::TestPythonStringToStr";
225 
226     PythonString str(c_str);
227     EXPECT_STREQ(c_str, str.GetString().str().c_str());
228 
229     PythonString str_str = str.Str();
230     EXPECT_STREQ(c_str, str_str.GetString().str().c_str());
231 }
232 
233 TEST_F(PythonDataObjectsTest, TestPythonIntegerToStr)
234 {
235 }
236 
237 TEST_F(PythonDataObjectsTest, TestPythonIntegerToStructuredInteger)
238 {
239     PythonInteger integer(7);
240     auto int_sp = integer.CreateStructuredInteger();
241     EXPECT_EQ(7, int_sp->GetValue());
242 }
243 
244 TEST_F(PythonDataObjectsTest, TestPythonStringToStructuredString)
245 {
246     static const char *test_string = "PythonDataObjectsTest::TestPythonStringToStructuredString";
247     PythonString constructed_string(test_string);
248     auto string_sp = constructed_string.CreateStructuredString();
249     EXPECT_STREQ(test_string, string_sp->GetStringValue().c_str());
250 }
251 
252 TEST_F(PythonDataObjectsTest, TestPythonListValueEquality)
253 {
254     // Test that a list which is built through the native
255     // Python API behaves correctly when wrapped by a PythonList.
256     static const int list_size = 2;
257     static const long long_value0 = 5;
258     static const char *const string_value1 = "String Index 1";
259 
260     PyObject *py_list = PyList_New(2);
261     EXPECT_TRUE(PythonList::Check(py_list));
262     PythonList list(PyRefType::Owned, py_list);
263 
264     PythonObject list_items[list_size];
265     list_items[0].Reset(PythonInteger(long_value0));
266     list_items[1].Reset(PythonString(string_value1));
267 
268     for (int i = 0; i < list_size; ++i)
269         list.SetItemAtIndex(i, list_items[i]);
270 
271     EXPECT_EQ(list_size, list.GetSize());
272     EXPECT_EQ(PyObjectType::List, list.GetObjectType());
273 
274     // Verify that the values match
275     PythonObject chk_value1 = list.GetItemAtIndex(0);
276     PythonObject chk_value2 = list.GetItemAtIndex(1);
277     EXPECT_TRUE(PythonInteger::Check(chk_value1.get()));
278     EXPECT_TRUE(PythonString::Check(chk_value2.get()));
279 
280     PythonInteger chk_int(PyRefType::Borrowed, chk_value1.get());
281     PythonString chk_str(PyRefType::Borrowed, chk_value2.get());
282 
283     EXPECT_EQ(long_value0, chk_int.GetInteger());
284     EXPECT_STREQ(string_value1, chk_str.GetString().str().c_str());
285 }
286 
287 TEST_F(PythonDataObjectsTest, TestPythonListManipulation)
288 {
289     // Test that manipulation of a PythonList behaves correctly when
290     // wrapped by a PythonDictionary.
291 
292     static const long long_value0 = 5;
293     static const char *const string_value1 = "String Index 1";
294 
295     PythonList list(PyInitialValue::Empty);
296     PythonInteger integer(long_value0);
297     PythonString string(string_value1);
298 
299     list.AppendItem(integer);
300     list.AppendItem(string);
301     EXPECT_EQ(2, list.GetSize());
302 
303     // Verify that the values match
304     PythonObject chk_value1 = list.GetItemAtIndex(0);
305     PythonObject chk_value2 = list.GetItemAtIndex(1);
306     EXPECT_TRUE(PythonInteger::Check(chk_value1.get()));
307     EXPECT_TRUE(PythonString::Check(chk_value2.get()));
308 
309     PythonInteger chk_int(PyRefType::Borrowed, chk_value1.get());
310     PythonString chk_str(PyRefType::Borrowed, chk_value2.get());
311 
312     EXPECT_EQ(long_value0, chk_int.GetInteger());
313     EXPECT_STREQ(string_value1, chk_str.GetString().str().c_str());
314 }
315 
316 TEST_F(PythonDataObjectsTest, TestPythonListToStructuredList)
317 {
318     static const long long_value0 = 5;
319     static const char *const string_value1 = "String Index 1";
320 
321     PythonList list(PyInitialValue::Empty);
322     list.AppendItem(PythonInteger(long_value0));
323     list.AppendItem(PythonString(string_value1));
324 
325     auto array_sp = list.CreateStructuredArray();
326     EXPECT_EQ(StructuredData::Type::eTypeInteger, array_sp->GetItemAtIndex(0)->GetType());
327     EXPECT_EQ(StructuredData::Type::eTypeString, array_sp->GetItemAtIndex(1)->GetType());
328 
329     auto int_sp = array_sp->GetItemAtIndex(0)->GetAsInteger();
330     auto string_sp = array_sp->GetItemAtIndex(1)->GetAsString();
331 
332     EXPECT_EQ(long_value0, int_sp->GetValue());
333     EXPECT_STREQ(string_value1, string_sp->GetValue().c_str());
334 }
335 
336 TEST_F(PythonDataObjectsTest, TestPythonDictionaryValueEquality)
337 {
338     // Test that a dictionary which is built through the native
339     // Python API behaves correctly when wrapped by a PythonDictionary.
340     static const int dict_entries = 2;
341     const char *key_0 = "Key 0";
342     int key_1 = 1;
343     const int value_0 = 0;
344     const char *value_1 = "Value 1";
345 
346     PythonObject py_keys[dict_entries];
347     PythonObject py_values[dict_entries];
348 
349     py_keys[0].Reset(PythonString(key_0));
350     py_keys[1].Reset(PythonInteger(key_1));
351     py_values[0].Reset(PythonInteger(value_0));
352     py_values[1].Reset(PythonString(value_1));
353 
354     PyObject *py_dict = PyDict_New();
355     EXPECT_TRUE(PythonDictionary::Check(py_dict));
356     PythonDictionary dict(PyRefType::Owned, py_dict);
357 
358     for (int i = 0; i < dict_entries; ++i)
359         PyDict_SetItem(py_dict, py_keys[i].get(), py_values[i].get());
360     EXPECT_EQ(dict.GetSize(), dict_entries);
361     EXPECT_EQ(PyObjectType::Dictionary, dict.GetObjectType());
362 
363     // Verify that the values match
364     PythonObject chk_value1 = dict.GetItemForKey(py_keys[0]);
365     PythonObject chk_value2 = dict.GetItemForKey(py_keys[1]);
366     EXPECT_TRUE(PythonInteger::Check(chk_value1.get()));
367     EXPECT_TRUE(PythonString::Check(chk_value2.get()));
368 
369     PythonInteger chk_int(PyRefType::Borrowed, chk_value1.get());
370     PythonString chk_str(PyRefType::Borrowed, chk_value2.get());
371 
372     EXPECT_EQ(value_0, chk_int.GetInteger());
373     EXPECT_STREQ(value_1, chk_str.GetString().str().c_str());
374 }
375 
376 TEST_F(PythonDataObjectsTest, TestPythonDictionaryManipulation)
377 {
378     // Test that manipulation of a dictionary behaves correctly when wrapped
379     // by a PythonDictionary.
380     static const int dict_entries = 2;
381 
382     const char *const key_0 = "Key 0";
383     const char *const key_1 = "Key 1";
384     const long value_0 = 1;
385     const char *const value_1 = "Value 1";
386 
387     PythonString keys[dict_entries];
388     PythonObject values[dict_entries];
389 
390     keys[0].Reset(PythonString(key_0));
391     keys[1].Reset(PythonString(key_1));
392     values[0].Reset(PythonInteger(value_0));
393     values[1].Reset(PythonString(value_1));
394 
395     PythonDictionary dict(PyInitialValue::Empty);
396     for (int i = 0; i < 2; ++i)
397         dict.SetItemForKey(keys[i], values[i]);
398 
399     EXPECT_EQ(dict_entries, dict.GetSize());
400 
401     // Verify that the keys and values match
402     PythonObject chk_value1 = dict.GetItemForKey(keys[0]);
403     PythonObject chk_value2 = dict.GetItemForKey(keys[1]);
404     EXPECT_TRUE(PythonInteger::Check(chk_value1.get()));
405     EXPECT_TRUE(PythonString::Check(chk_value2.get()));
406 
407     PythonInteger chk_int(PyRefType::Borrowed, chk_value1.get());
408     PythonString chk_str(PyRefType::Borrowed, chk_value2.get());
409 
410     EXPECT_EQ(value_0, chk_int.GetInteger());
411     EXPECT_STREQ(value_1, chk_str.GetString().str().c_str());
412 }
413 
414 TEST_F(PythonDataObjectsTest, TestPythonDictionaryToStructuredDictionary)
415 {
416     static const char *const string_key0 = "String Key 0";
417     static const char *const string_key1 = "String Key 1";
418 
419     static const char *const string_value0 = "String Value 0";
420     static const long int_value1 = 7;
421 
422     PythonDictionary dict(PyInitialValue::Empty);
423     dict.SetItemForKey(PythonString(string_key0), PythonString(string_value0));
424     dict.SetItemForKey(PythonString(string_key1), PythonInteger(int_value1));
425 
426     auto dict_sp = dict.CreateStructuredDictionary();
427     EXPECT_EQ(2, dict_sp->GetSize());
428 
429     EXPECT_TRUE(dict_sp->HasKey(string_key0));
430     EXPECT_TRUE(dict_sp->HasKey(string_key1));
431 
432     auto string_sp = dict_sp->GetValueForKey(string_key0)->GetAsString();
433     auto int_sp = dict_sp->GetValueForKey(string_key1)->GetAsInteger();
434 
435     EXPECT_STREQ(string_value0, string_sp->GetValue().c_str());
436     EXPECT_EQ(int_value1, int_sp->GetValue());
437 }
438 
439 TEST_F(PythonDataObjectsTest, TestPythonFile)
440 {
441     File file(FileSystem::DEV_NULL, File::eOpenOptionRead);
442     PythonFile py_file(file, "r");
443     EXPECT_TRUE(PythonFile::Check(py_file.get()));
444 }
445 
446 TEST_F(PythonDataObjectsTest, TestObjectAttributes)
447 {
448     PythonInteger py_int(42);
449     EXPECT_TRUE(py_int.HasAttribute("numerator"));
450     EXPECT_FALSE(py_int.HasAttribute("this_should_not_exist"));
451 
452     PythonInteger numerator_attr = py_int.GetAttributeValue("numerator").AsType<PythonInteger>();
453     EXPECT_TRUE(numerator_attr.IsAllocated());
454     EXPECT_EQ(42, numerator_attr.GetInteger());
455 }